The_Dark_Side_of_Earth/buildings/building.gd

43 lines
1.5 KiB
GDScript

class_name Building extends Node2D
@export var location : Vector2i # x is the angle, y is the height in the grid
@export var dimension : Vector2i = Vector2(2, 1) # same as above
@export var blocks_area = true
var objects = []
var destroyed = false
# make sure location is set before adding a building to the scene tree
# also make sure that the buildings are instantiated as children of the grid
func _ready() -> void:
global_position = Grid.get_world_position(location)
if blocks_area:
Grid.buildings.append(self)
if get_node_or_null("ObjectList") != null:
var obj_list = $ObjectList
Grid.call_deferred("place_object_list", obj_list, self)
func overlaps(other : Building):
# heights don't overlap
if location.y >= other.location.y + other.dimension.y: return false # other is below
if location.y + dimension.y <= other.location.y: return false # other is above
# angles overlap. We can now assume heights overlap
var relative_other_loc = (other.location.x - location.x + Grid.num_collumns) % Grid.num_collumns
if dimension.x > relative_other_loc: return true
if relative_other_loc + other.dimension.x > Grid.num_collumns: return true
# If we get here, angles do not overlap
return false
func destroy():
if not destroyed:
# On destruction, the building is removed from the list and destroyed together with all its objects.
Grid.buildings.remove_at(Grid.buildings.find(self))
for object in objects:
if object != null and not ("collected" in object and object.collected):
object.queue_free()
destroyed = true
queue_free()