A Minecraft style clone in Godot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

77 lines
2.6 KiB

class_name World
extends Node3D
@export var default_spawn_position: Vector3 = Vector3.ZERO
@export_group("Nodes and Scenes")
@export var blocks_container: Node3D ## Container where spawned blocks are added
@export var dropped_items_container: Node3D ## Container where spawned dropped items are added
@export var player_scene: PackedScene
func _ready() -> void:
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
EntityManager.create_block.connect(_create_block.bind())
EntityManager.drop_block.connect(_create_dropped_block.bind())
EntityManager.reset_world.connect(_on_reset_world.bind())
EntityManager.spawn_player.connect(_spawn_player.bind())
_initialize_ground()
_create_test_blocks()
var default_transform: Transform3D = Transform3D()
default_transform.origin = default_spawn_position
_spawn_player(default_transform)
func _create_block(id: String, block_position: Vector3) -> void:
var block: Block = Globals.BLOCK_PREFAB.instantiate()
block.position = block_position
block.set_id(id)
blocks_container.add_child(block)
func _create_dropped_block(id: String, block_position: Vector3) -> void:
var block: DroppedBlock = Globals.DROPPED_BLOCK_PREFAB.instantiate()
dropped_items_container.add_child(block)
block.initialize(id, block_position)
func _create_test_blocks() -> void:
var test_blocks: Array = ["001", "002", "003", "004", "005", "006"]
for index: int in range(1, test_blocks.size() + 1):
_create_block("00" + str(index), Vector3(index, 1, -3))
_create_block("00" + str(index), Vector3(index, 2, -4))
_create_block("00" + str(index), Vector3(index, 3, -5))
_create_dropped_block("00" + str(index), Vector3(index, 2, -3))
func _spawn_player(player_position: Transform3D) -> void:
if has_node("Player"):
$Player.queue_free()
await $Player.tree_exited
var player: Player = player_scene.instantiate()
player.transform = player_position
player.name = "Player"
add_child(player)
func _initialize_ground() -> void:
for x: int in range(-10, 11):
for z: int in range(-10, 11):
var ground_position: Vector3 = Vector3(x, 0, z)
var random: int = randi_range(0, 1)
if random:
# Just for testing.. Would make more sense to just call _create_block() directly
EntityManager.create_block.emit("001", ground_position)
else:
EntityManager.create_block.emit("002", ground_position)
## Generally for resetting/emptying the world to allow for load_game
func _on_reset_world() -> void:
for block: Block in blocks_container.get_children():
block.queue_free()
for block: DroppedBlock in dropped_items_container.get_children():
block.queue_free()