The_Dark_Side_of_Earth/world/grid.gd

56 lines
1.6 KiB
GDScript

class_name Grid extends Node2D
@export var ground_radius : float
@export var cell_height : float
@export var num_collumns : int
@export var debug : bool
@export var packed_buildings : Array[PackedScene]
@export var max_build_height = 6
var buildings : Array[Building] = []
func _draw() -> void:
if !debug:
return
for i in range(10):
draw_arc(Vector2.ZERO, ground_radius + i * cell_height, 0, TAU, 250, Color.SKY_BLUE, 1.0, true);
for i in range(num_collumns):
var angle = i * TAU / num_collumns;
draw_line(Vector2.ZERO, 10000 * Vector2.from_angle(angle), Color.SKY_BLUE);
func add_building_to_collumn(building : Building, collumn : int):
# find the height of the top building in the buildings list:
building.location = Vector2(collumn, -1)
var spot_clear : bool = false
while(!spot_clear):
building.location.y += 1
spot_clear = true
for other in buildings:
if other.overlaps(building):
spot_clear = false
if building.location.y + building.dimension.y > max_build_height:
building.free()
return false
add_child(building)
return true
func get_world_position (location: Vector2, offset: Vector2 = Vector2.ZERO) -> Vector2:
var height = ground_radius + location.y * cell_height - offset.y # currently assumes anchor is bottom left
var angle = (location.x + offset.x / cell_height) * TAU / num_collumns
return height * Vector2.from_angle(angle)
# for testing
#func _ready() -> void:
#
#
#
#for i in range(100):
#var test_building = packed_buildings[0].instantiate()
#var collumn = randi() % 60
#add_building_to_collumn(test_building, collumn)