The_Dark_Side_of_Earth/building.gd

29 lines
1.2 KiB
GDScript3
Raw Normal View History

2025-09-16 00:19:40 +02:00
class_name Building extends Node2D
var location : Vector2i # x is the angle, y is the height in the grid
2025-09-16 11:44:16 +02:00
var dimension : Vector2i = Vector2(2, 1) # same as above
2025-09-16 00:19:40 +02:00
@onready var grid : Grid = get_parent()
# 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-09-16 11:44:16 +02:00
assert(grid != null)
2025-09-16 00:19:40 +02:00
var angle = location.x * TAU / grid.num_collumns; # currently assumes anchor is bottom left
var height = grid.ground_radius + location.y * grid.cell_height;
position = height * Vector2.from_angle(angle)
2025-09-16 11:44:16 +02:00
grid.buildings.append(self)
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
var relative_other_loc = (other.location.x - location.x + grid.num_collumns) % grid.num_collumns
if dimension.x > relative_other_loc: return true
2025-09-16 11:44:16 +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