delurbelako/scripts/placement_manager.gd
2025-10-15 00:10:51 +05:00

50 lines
1.5 KiB
GDScript

extends Node2D
## Currently held structure
var held_construction : Structure
var selected_prototype : Prototype
func _ready() -> void:
GuiEventBus.construction_selected.connect(on_construction_selected)
func _input(event: InputEvent) -> void:
if held_construction == null:
return
if event.is_action_pressed("plc_place"):
var zone = try_get_zone(held_construction.global_position)
if zone != null and held_construction.try_place(zone):
var facing = held_construction.facing
held_construction = selected_prototype.scene.instantiate()
add_child(held_construction)
held_construction.set_facing(facing)
if event.is_action_pressed("plc_rotate_up"):
if held_construction != null:
held_construction.cycle_up_facing()
if event.is_action_pressed("plc_cancel"):
held_construction.queue_free()
selected_prototype = null
func on_construction_selected(constructible : Prototype):
if held_construction:
held_construction.queue_free()
held_construction = constructible.scene.instantiate()
add_child(held_construction)
selected_prototype = constructible
func _process(_delta: float) -> void:
if held_construction == null:
return
var mouse_pos = get_global_mouse_position()
var zone = try_get_zone(mouse_pos)
if zone and held_construction.can_be_placed(zone):
global_position = zone.get_placement_position(mouse_pos)
else:
global_position = mouse_pos
func try_get_zone(point : Vector2) -> PlacementZone:
for zone in RuntimePlayerData.build_zones:
if zone.is_global_point_in_zone(point):
return zone
return null