2025-09-16 00:19:40 +02:00
|
|
|
class_name Building extends Node2D
|
|
|
|
|
|
2025-09-16 23:42:09 +02:00
|
|
|
@export var location : Vector2i # x is the angle, y is the height in the grid
|
2025-09-16 16:24:53 +02:00
|
|
|
@export var dimension : Vector2i = Vector2(2, 1) # same as above
|
2025-09-24 16:37:34 +02:00
|
|
|
@export var blocks_area = true
|
|
|
|
|
|
2025-09-18 18:10:44 +02:00
|
|
|
var objects = []
|
|
|
|
|
var destroyed = false
|
|
|
|
|
|
2025-09-16 00:19:40 +02:00
|
|
|
# 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:
|
2025-10-11 19:30:55 +02:00
|
|
|
global_position = Grid.get_world_position(location)
|
|
|
|
|
|
2025-09-24 16:37:34 +02:00
|
|
|
if blocks_area:
|
2025-10-11 17:36:43 +02:00
|
|
|
Grid.buildings.append(self)
|
2025-09-16 16:13:14 +02:00
|
|
|
|
2025-10-21 17:54:43 +02:00
|
|
|
#await get_tree().create_timer(0.2).timeout
|
2025-10-11 19:30:55 +02:00
|
|
|
if get_node_or_null("ObjectList") != null:
|
2025-10-21 16:06:16 +02:00
|
|
|
var obj_list = $ObjectList
|
2025-10-21 17:54:43 +02:00
|
|
|
obj_list.call_deferred("reparent", get_tree().get_root().get_node("main"), false)
|
2025-10-21 16:06:16 +02:00
|
|
|
await get_tree().create_timer(0.2).timeout
|
|
|
|
|
var objects_to_be_placed = obj_list.get_children()
|
2025-10-11 19:30:55 +02:00
|
|
|
for object in objects_to_be_placed:
|
|
|
|
|
var offset = object.global_position;
|
|
|
|
|
object.global_position = Grid.get_world_position(location, offset)
|
2025-10-21 16:06:16 +02:00
|
|
|
|
2025-10-11 19:30:55 +02:00
|
|
|
# The building remembers these objects: If it is destroyed, so are they.
|
|
|
|
|
if object is Platform or object is Trap or object is Item:
|
|
|
|
|
objects.append(object)
|
|
|
|
|
if "building" in object: object.building = self
|
|
|
|
|
# This scales platforms hoizontally to make sure they still form a floor without gaps.
|
|
|
|
|
if(object.has_method("init_at_horizontal_distortion")):
|
|
|
|
|
object.init_at_horizontal_distortion(object.position.length() / Grid.ground_radius)
|
2025-10-21 16:06:16 +02:00
|
|
|
grid_entrance_callback(object)
|
|
|
|
|
|
|
|
|
|
func grid_entrance_callback(object : Node):
|
|
|
|
|
# Objects receive a callback when placed, starting from the deepest children
|
|
|
|
|
for child in object.get_children():
|
|
|
|
|
grid_entrance_callback(child)
|
|
|
|
|
if object.has_method("_enter_grid"):
|
|
|
|
|
object.call_deferred("_enter_grid")
|
2025-09-16 00:19:40 +02:00
|
|
|
|
|
|
|
|
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
|
2025-10-11 17:36:43 +02:00
|
|
|
var relative_other_loc = (other.location.x - location.x + Grid.num_collumns) % Grid.num_collumns
|
2025-09-16 00:19:40 +02:00
|
|
|
if dimension.x > relative_other_loc: return true
|
2025-10-11 17:36:43 +02:00
|
|
|
if relative_other_loc + other.dimension.x > Grid.num_collumns: return true
|
2025-09-16 00:19:40 +02:00
|
|
|
|
|
|
|
|
# If we get here, angles do not overlap
|
|
|
|
|
return false
|
2025-09-18 18:10:44 +02:00
|
|
|
|
|
|
|
|
func destroy():
|
|
|
|
|
if not destroyed:
|
2025-10-11 19:30:55 +02:00
|
|
|
# On destruction, the building is removed from the list and destroyed together with all its objects.
|
2025-10-11 17:36:43 +02:00
|
|
|
Grid.buildings.remove_at(Grid.buildings.find(self))
|
2025-09-18 18:10:44 +02:00
|
|
|
for object in objects:
|
|
|
|
|
if object != null and not ("collected" in object and object.collected):
|
|
|
|
|
object.queue_free()
|
|
|
|
|
destroyed = true
|
|
|
|
|
queue_free()
|