62 lines
2.4 KiB
GDScript
62 lines
2.4 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
|
|
|
|
@onready var grid : Grid = get_parent()
|
|
|
|
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:
|
|
assert(grid != null)
|
|
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)
|
|
if blocks_area:
|
|
grid.buildings.append(self)
|
|
|
|
await get_tree().create_timer(0.2).timeout
|
|
if get_node_or_null("EnemyList") != null:
|
|
var enemies = $EnemyList.get_children()
|
|
|
|
$EnemyList.reparent(get_tree().get_root().get_node("main"), false)
|
|
|
|
for enemy in enemies:
|
|
var oldpos = enemy.position;
|
|
enemy.position = get_world_position(oldpos)
|
|
if enemy is Platform or enemy is Trap or enemy is Item:
|
|
objects.append(enemy)
|
|
if "building" in enemy: enemy.building = self
|
|
if(enemy.has_method("init_at_horizontal_distortion")):
|
|
enemy.init_at_horizontal_distortion(enemy.position.length() / grid.ground_radius)
|
|
|
|
func get_world_position (local_position: Vector2) -> Vector2:
|
|
var height = grid.ground_radius + location.y * grid.cell_height - local_position.y
|
|
var angle = (location.x + local_position.x / grid.cell_height) * TAU / grid.num_collumns
|
|
return height * Vector2.from_angle(angle)
|
|
|
|
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:
|
|
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()
|