delurbelako/scripts/placement_manager.gd

75 lines
2.3 KiB
GDScript

extends Node2D
## Currently held structure
var held_construction : Structure
var selected_prototype : Prototype
var delete_mode : bool = false
func _ready() -> void:
GuiEventBus.construction_selected.connect(on_construction_selected)
func _input(event: InputEvent) -> void:
if event.is_action_pressed("plc_remove_toggle"):
delete_mode = not delete_mode
if held_construction:
held_construction.queue_free()
selected_prototype = null
if event.is_action_pressed("plc_place") and delete_mode:
var zone : PlacementZone = try_get_zone(global_position)
if zone != null and zone.grid_controller.is_point_occupied(global_position):
zone.grid_controller.destroy_at(global_position)
if event.is_action_pressed("plc_cancel") and delete_mode:
delete_mode = false
if held_construction == null or delete_mode:
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 direction = held_construction.direction
held_construction = selected_prototype.scene.instantiate()
add_child(held_construction)
held_construction.set_direction(direction)
if event.is_action_pressed("plc_rotate_up"):
if held_construction != null:
held_construction.increment_direction()
if event.is_action_pressed("plc_rotate_down"):
if held_construction != null:
held_construction.decrement_direction()
if event.is_action_pressed("plc_cancel"):
held_construction.queue_free()
selected_prototype = null
func on_construction_selected(constructible : Prototype):
if delete_mode:
return
if held_construction:
held_construction.queue_free()
held_construction = constructible.scene.instantiate()
add_child(held_construction)
selected_prototype = constructible
func _process(_delta: float) -> void:
var mouse_pos = get_global_mouse_position()
if held_construction == null:
global_position = mouse_pos
return
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