delurbelako/scripts/grid_controller.gd

54 lines
1.6 KiB
GDScript

extends Node2D
# TODO: Make able to hold multiple placement zones, supporting uniform forms
## Class that contains all structures of platform
class_name GridController
## Building zone controller uses for checks and capacity
@export var building_zone : PlacementZone
## Internal structure storage
var structures : Array[Structure]
func _ready() -> void:
structures.resize(building_zone.get_capacity())
## Tries to add structure to its internal storage [br]
## Returns false if has colliding structures
func add_structure(structure : Structure) -> bool:
var structure_dp = structure.get_dimension_points()
for point in structure_dp:
if structures[building_zone.indexify_global_point(structure.global_position + point)]:
return false
structure.reparent(self)
structure.global_position = building_zone.get_placement_position(structure.global_position)
for point in structure_dp:
structures[building_zone.indexify_global_point(structure.global_position + point)] = structure
return true
## Returns structure at point. [br]
## Returns null if no structure is at point
func get_at(point : Vector2) -> Structure:
var index = building_zone.indexify_global_point(point)
if index == -1:
return null
return structures[index]
func destroy_at(point : Vector2) -> void:
var index = building_zone.indexify_global_point(point)
if index == -1:
return
var found: Structure = structures[index]
for dim_point in found.get_dimension_points():
structures[building_zone.indexify_global_point(found.global_position+dim_point)] = null
found.queue_free()
func is_point_occupied(point : Vector2) -> bool:
return get_at(point) != null