Made buildings spawn randomly

This commit is contained in:
Florian 2025-09-16 11:44:16 +02:00
parent 11bd9d46fd
commit 0dbdca8efb
6 changed files with 25 additions and 21 deletions

View file

@ -1,16 +1,17 @@
class_name Building extends Node2D
var location : Vector2i # x is the angle, y is the height in the grid
var dimension : Vector2i = Vector2.ONE # same as above
var dimension : Vector2i = Vector2(2, 1) # same as above
@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:
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)
print(angle, " ", height, " ", position)
grid.buildings.append(self)
func overlaps(other : Building):
# heights don't overlap
@ -20,7 +21,7 @@ func overlaps(other : Building):
# 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 > grid.num_collumns: return true
if relative_other_loc + other.dimension.x > grid.num_collumns: return true
# If we get here, angles do not overlap
return false

View file

@ -18,6 +18,6 @@ script = ExtResource("1_5j34s")
[node name="Sprite2D" type="Sprite2D" parent="."]
self_modulate = Color(0.1764706, 0, 0.003921569, 0.003921569)
material = SubResource("ShaderMaterial_qnfc1")
scale = Vector2(7, 7)
scale = Vector2(25, 25)
texture = ExtResource("2_2yopf")
script = ExtResource("4_qnfc1")

1
building_generator.gd Normal file
View file

@ -0,0 +1 @@
extends Node

View file

@ -0,0 +1 @@
uid://colvx6wq0e8n7

29
grid.gd
View file

@ -20,27 +20,24 @@ func _draw() -> void:
func add_building_to_collumn(building : Building, collumn : int):
# find the height of the top building in the buildings list:
building.location = Vector2(collumn, 0)
var height = 0
# TODO: support other dimensions for buildings
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
# add the new building one higher than the previous
height += 1
building.location = Vector2i(collumn, height);
add_child(building)
# for testing
func _ready() -> void:
var packed : PackedScene = preload("res://building.tscn")
var test_building = packed.instantiate()
test_building.location = Vector2(45, 0)
add_child(test_building)
test_building = packed.instantiate()
test_building.location = Vector2(44, 0)
add_child(test_building)
test_building = packed.instantiate()
test_building.location = Vector2(45, 1)
add_child(test_building)
for i in range(100):
var test_building = packed.instantiate()
var collumn = randi() % 60
add_building_to_collumn(test_building, collumn)

View file

@ -1,8 +1,9 @@
[gd_scene load_steps=4 format=3 uid="uid://cxo6bq26huau7"]
[gd_scene load_steps=5 format=3 uid="uid://cxo6bq26huau7"]
[ext_resource type="PackedScene" uid="uid://cmaovvr15b3qk" path="res://player.tscn" id="2_1bvp3"]
[ext_resource type="PackedScene" uid="uid://chu67ci7sl488" path="res://ghost.tscn" id="3_h2yge"]
[ext_resource type="PackedScene" uid="uid://jjoyj1ldafkf" path="res://earth.tscn" id="3_lquwl"]
[ext_resource type="Script" uid="uid://colvx6wq0e8n7" path="res://building_generator.gd" id="4_1bvp3"]
[node name="main" type="Node2D"]
@ -14,3 +15,6 @@ scale = Vector2(3, 3)
[node name="Ghost" parent="." instance=ExtResource("3_h2yge")]
position = Vector2(0, -3200)
[node name="Building Generator" type="Node" parent="."]
script = ExtResource("4_1bvp3")