Browse Source

Adding ability to create blocks

pull/1/head
Ryan Reed 1 month ago
parent
commit
8e4bfe6f74
5 changed files with 42 additions and 4 deletions
  1. +13
    -0
      README.md
  2. +10
    -0
      project.godot
  3. +5
    -0
      scenes/blocks/block.gd
  4. +3
    -1
      scenes/player/ray_cast_3d.gd
  5. +11
    -3
      scenes/world/world.gd

+ 13
- 0
README.md View File

@ -5,3 +5,16 @@
Building something similar to Minecraft Building something similar to Minecraft
Based on Sable Spirit Studio's YT series Based on Sable Spirit Studio's YT series
## Notes
My code tends to diverge quite heavily at times as I worked out better methods of performing certain actions.
For instance, he went through a lot of work creating collision shapes for each block face and then using that to determine what face the player was looking at. However, I found the following basically removed any need for such complicated collision shapes and code:
```
EntityManager.create_block.emit("001", collider.position + get_collision_normal())
```
1 line of code replaced a very large amount of code and changes that were implemented in his `Create Blocks` video.

+ 10
- 0
project.godot View File

@ -15,6 +15,10 @@ run/main_scene="uid://cgx0nawwjjj7g"
config/features=PackedStringArray("4.4", "Forward Plus") config/features=PackedStringArray("4.4", "Forward Plus")
config/icon="res://icon.svg" config/icon="res://icon.svg"
[autoload]
EntityManager="*res://autoloads/entity_manager.gd"
[debug] [debug]
gdscript/warnings/untyped_declaration=1 gdscript/warnings/untyped_declaration=1
@ -26,6 +30,7 @@ gdscript/warnings/unsafe_method_access=1
folder_colors={ folder_colors={
"res://assets/": "red", "res://assets/": "red",
"res://assets/ui/": "green", "res://assets/ui/": "green",
"res://autoloads/": "orange",
"res://data_structure/": "blue", "res://data_structure/": "blue",
"res://resources/": "blue", "res://resources/": "blue",
"res://scenes/": "yellow", "res://scenes/": "yellow",
@ -81,6 +86,11 @@ left_click_interact={
"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":1,"position":Vector2(67, 28),"global_position":Vector2(76, 74),"factor":1.0,"button_index":1,"canceled":false,"pressed":true,"double_click":false,"script":null) "events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":1,"position":Vector2(67, 28),"global_position":Vector2(76, 74),"factor":1.0,"button_index":1,"canceled":false,"pressed":true,"double_click":false,"script":null)
] ]
} }
right_click_interact={
"deadzone": 0.2,
"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":2,"position":Vector2(71, 28),"global_position":Vector2(80, 74),"factor":1.0,"button_index":2,"canceled":false,"pressed":true,"double_click":false,"script":null)
]
}
[layer_names] [layer_names]


+ 5
- 0
scenes/blocks/block.gd View File

@ -30,9 +30,14 @@ func hook() -> void:
highlight_mesh.visible = true highlight_mesh.visible = true
# TODO: Rename to something else (maybe remove()) # TODO: Rename to something else (maybe remove())
# Move to signal?
func interact_left_click() -> void: func interact_left_click() -> void:
queue_free() queue_free()
# TODO: Rename to something else
func interact_right_click() -> void:
EntityManager.create_block.emit("001", Vector3(5,5,5))
func release() -> void: func release() -> void:
highlight_mesh.visible = false highlight_mesh.visible = false


+ 3
- 1
scenes/player/ray_cast_3d.gd View File

@ -7,7 +7,9 @@ func _process(_delta: float) -> void:
if collider is Block: if collider is Block:
if Input.is_action_just_pressed("left_click_interact"): if Input.is_action_just_pressed("left_click_interact"):
collider.interact_left_click()
(collider as Block).interact_left_click()
if Input.is_action_just_pressed("right_click_interact"):
EntityManager.create_block.emit("001", collider.position + get_collision_normal())
if Waila.ref.get_target() == collider: if Waila.ref.get_target() == collider:
return return


+ 11
- 3
scenes/world/world.gd View File

@ -7,13 +7,20 @@ const BLOCK_PREFAB: PackedScene = preload("res://scenes/blocks/block.tscn")
func _ready() -> void: func _ready() -> void:
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
EntityManager.create_block.connect(create_block.bind())
_initialize_ground() _initialize_ground()
create_block("001", Vector3(2,1, 2))
create_block("002", Vector3(2,1, 1))
create_block("003", Vector3(1,1, 2))
create_block("004", Vector3(1,2, 2))
create_block("003", Vector3(-2,3, 2))
func create_block(id: String, block_position: Vector3) -> void: func create_block(id: String, block_position: Vector3) -> void:
var block: Block = BLOCK_PREFAB.instantiate() var block: Block = BLOCK_PREFAB.instantiate()
block.position = block_position block.position = block_position
block.name = "%s" % [block_position]
block.set_id(id) block.set_id(id)
add_child(block) add_child(block)
@ -26,6 +33,7 @@ func _initialize_ground() -> void:
var random: int = randi_range(0, 1) var random: int = randi_range(0, 1)
if random: if random:
create_block("001", ground_position)
# Just for testing.. Would probably make mmore sense to just call create_block() directly if still in World
EntityManager.create_block.emit("001", ground_position)
else: else:
create_block("002", ground_position)
EntityManager.create_block.emit("002", ground_position)

Loading…
Cancel
Save