2024-09-08 22:06:34 +02:00
|
|
|
class_name BuildSystem
|
|
|
|
extends Node
|
|
|
|
|
2024-09-10 22:26:26 +02:00
|
|
|
@onready var build_preview = %BuildPreview
|
2024-09-08 22:06:34 +02:00
|
|
|
@onready var player = %Player
|
2024-09-10 22:26:26 +02:00
|
|
|
@onready var built_structures = %BuiltStructures
|
|
|
|
|
|
|
|
@export var build_item:Item = null
|
2024-09-08 22:06:34 +02:00
|
|
|
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
|
|
func _ready():
|
|
|
|
pass # Replace with function body.
|
|
|
|
|
|
|
|
|
2024-09-10 22:26:26 +02:00
|
|
|
func update_build_preview_item() -> void:
|
|
|
|
for child in build_preview.get_children():
|
|
|
|
if build_item == null or child.scene_file_path != build_item.scene.resource_path:
|
|
|
|
child.get_parent().remove_child(child)
|
|
|
|
child.queue_free()
|
|
|
|
|
|
|
|
if build_preview.get_child_count() == 0 and build_item != null:
|
|
|
|
build_preview.add_child(build_item.scene.instantiate())
|
|
|
|
|
2024-09-08 22:06:34 +02:00
|
|
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
2024-09-10 22:26:26 +02:00
|
|
|
func _physics_process(_delta):
|
|
|
|
update_build_preview_item()
|
|
|
|
|
2024-09-08 22:06:34 +02:00
|
|
|
var build_location:Vector3 = player.get_actionable_global_transform()
|
2024-09-10 22:26:26 +02:00
|
|
|
build_location = Vector3(roundf(build_location.x * 2), build_location.y, roundf(build_location.z * 2)) * 0.5
|
|
|
|
build_preview.global_position = build_location
|
|
|
|
|
|
|
|
if build_item != null and Input.is_action_just_pressed("interaction"):
|
|
|
|
var new_structure:Node3D = build_item.scene.instantiate()
|
|
|
|
new_structure.transform = build_preview.transform
|
|
|
|
built_structures.add_child(new_structure)
|
|
|
|
get_viewport().set_input_as_handled()
|
|
|
|
return
|