From 8e4bfe6f74bfb2d73a62e62b13584c6f17f26bb4 Mon Sep 17 00:00:00 2001
From: Ryan Reed <git@ryanreed.net>
Date: Wed, 5 Mar 2025 20:32:42 -0500
Subject: [PATCH] Adding ability to create blocks

---
 README.md                    | 13 +++++++++++++
 project.godot                | 10 ++++++++++
 scenes/blocks/block.gd       |  5 +++++
 scenes/player/ray_cast_3d.gd |  4 +++-
 scenes/world/world.gd        | 14 +++++++++++---
 5 files changed, 42 insertions(+), 4 deletions(-)

diff --git a/README.md b/README.md
index 28b31cb..1743a1d 100644
--- a/README.md
+++ b/README.md
@@ -5,3 +5,16 @@
 Building something similar to Minecraft
 
 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.
diff --git a/project.godot b/project.godot
index 807f921..39f0a9d 100644
--- a/project.godot
+++ b/project.godot
@@ -15,6 +15,10 @@ run/main_scene="uid://cgx0nawwjjj7g"
 config/features=PackedStringArray("4.4", "Forward Plus")
 config/icon="res://icon.svg"
 
+[autoload]
+
+EntityManager="*res://autoloads/entity_manager.gd"
+
 [debug]
 
 gdscript/warnings/untyped_declaration=1
@@ -26,6 +30,7 @@ gdscript/warnings/unsafe_method_access=1
 folder_colors={
 "res://assets/": "red",
 "res://assets/ui/": "green",
+"res://autoloads/": "orange",
 "res://data_structure/": "blue",
 "res://resources/": "blue",
 "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)
 ]
 }
+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]
 
diff --git a/scenes/blocks/block.gd b/scenes/blocks/block.gd
index fda7c7a..7d9c77e 100644
--- a/scenes/blocks/block.gd
+++ b/scenes/blocks/block.gd
@@ -30,9 +30,14 @@ func hook() -> void:
 	highlight_mesh.visible = true
 
 # TODO: Rename to something else (maybe remove())
+# Move to signal?
 func interact_left_click() -> void:
 	queue_free()
 
+# TODO: Rename to something else
+func interact_right_click() -> void:
+	EntityManager.create_block.emit("001", Vector3(5,5,5))
+
 func release() -> void:
 	highlight_mesh.visible = false
 
diff --git a/scenes/player/ray_cast_3d.gd b/scenes/player/ray_cast_3d.gd
index e1cc76b..d310ded 100644
--- a/scenes/player/ray_cast_3d.gd
+++ b/scenes/player/ray_cast_3d.gd
@@ -7,7 +7,9 @@ func _process(_delta: float) -> void:
 
 		if collider is Block:
 			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:
 				return
diff --git a/scenes/world/world.gd b/scenes/world/world.gd
index 42d6173..6cffb14 100644
--- a/scenes/world/world.gd
+++ b/scenes/world/world.gd
@@ -7,13 +7,20 @@ const BLOCK_PREFAB: PackedScene = preload("res://scenes/blocks/block.tscn")
 
 func _ready() -> void:
 	Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
+	EntityManager.create_block.connect(create_block.bind())
+
 	_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:
 	var block: Block = BLOCK_PREFAB.instantiate()
 	block.position = block_position
-	block.name = "%s" % [block_position]
 	block.set_id(id)
 
 	add_child(block)
@@ -26,6 +33,7 @@ func _initialize_ground() -> void:
 
 			var random: int = randi_range(0, 1)
 			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:
-				create_block("002", ground_position)
+				EntityManager.create_block.emit("002", ground_position)