TinyAdventure/systems/BuildSystem.gd

38 lines
1.3 KiB
GDScript

class_name BuildSystem
extends Node
@onready var build_preview = %BuildPreview
@onready var player = %Player
@onready var built_structures = %BuiltStructures
@export var build_item:Item = null
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
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())
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(_delta):
update_build_preview_item()
var build_location:Vector3 = player.get_actionable_global_transform()
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