From b75e6be470e37b5ea928929ca064f7dd3835d37d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Melvin=20Wei=C3=9F?= Date: Wed, 22 Oct 2025 03:29:21 +0200 Subject: [PATCH] Documented and refactored world scripts. --- bg_image.gd => background/bg_image.gd | 2 + bg_image.gd.uid => background/bg_image.gd.uid | 0 main.tscn | 2 +- world/building_generator.gd | 39 ++++++++++--------- world/draw_circle.gd | 5 --- world/draw_circle.gd.uid | 1 - world/earth.gd | 2 +- world/grid.gd | 19 +++++---- 8 files changed, 34 insertions(+), 36 deletions(-) rename bg_image.gd => background/bg_image.gd (80%) rename bg_image.gd.uid => background/bg_image.gd.uid (100%) delete mode 100644 world/draw_circle.gd delete mode 100644 world/draw_circle.gd.uid diff --git a/bg_image.gd b/background/bg_image.gd similarity index 80% rename from bg_image.gd rename to background/bg_image.gd index b30dc26..945ad6d 100644 --- a/bg_image.gd +++ b/background/bg_image.gd @@ -2,6 +2,8 @@ extends TextureRect @export var colors : Array[Color] = [Color(0.3, 1, 1) * 0.7, Color(1, 0.6, 0.6) * 0.7, Color(1, 1, 1) * 0.7] +# Modulate the background with an interpolation of the colors in the list, +# depending on the players position on the earth. func _process(_delta: float) -> void: var index : int = floor((%Player.position.angle() + PI)/ TAU * colors.size()) var diff = (%Player.position.angle() + PI)/ TAU * colors.size() - index diff --git a/bg_image.gd.uid b/background/bg_image.gd.uid similarity index 100% rename from bg_image.gd.uid rename to background/bg_image.gd.uid diff --git a/main.tscn b/main.tscn index 18115bd..34436e3 100644 --- a/main.tscn +++ b/main.tscn @@ -14,7 +14,7 @@ [ext_resource type="Script" uid="uid://cpaskpj67pnaj" path="res://enemies/boss/boss_spawner.gd" id="10_efxa6"] [ext_resource type="PackedScene" uid="uid://cqn67nwyrtq3k" path="res://ui/journal/journal.tscn" id="10_w48qg"] [ext_resource type="PackedScene" uid="uid://cpe4s6vsn0ujd" path="res://enemies/boss/boss.tscn" id="11_efxa6"] -[ext_resource type="Script" uid="uid://gul4u5tw1vxk" path="res://bg_image.gd" id="13_vivmo"] +[ext_resource type="Script" uid="uid://gul4u5tw1vxk" path="res://background/bg_image.gd" id="13_vivmo"] [node name="main" type="Node2D"] diff --git a/world/building_generator.gd b/world/building_generator.gd index 8bcea99..8f6e861 100644 --- a/world/building_generator.gd +++ b/world/building_generator.gd @@ -6,40 +6,43 @@ class_name BuildingGenerator extends Node @export var only_on_first_load = false static var first_load = true -func random_oppostite_collumn() -> int: - var playerpos = %Player.position - var player_angle = atan2(playerpos.y, playerpos.x) - - var offset = randf_range(TAU/3, 2*TAU/3) - var spawn_angle = player_angle + offset - var collumn = int(spawn_angle / TAU * Grid.num_collumns + Grid.num_collumns) % Grid.num_collumns - +# Determine the player's column, then choose one in the opposite third of the circle +func random_opposite_collumn() -> int: + var player_location = Grid.get_location_from_world_pos(%Player.position) + var offset = randi_range(int(Grid.num_collumns / 3.0), int(2 * Grid.num_collumns / 3.0)) + var collumn = player_location.x + offset return collumn - + func random_collumn() -> int: return randi_range(0, Grid.num_collumns - 1) - + func _ready(): + # The initial buildings in the main menu are only to be spawned upon first loading if not (only_on_first_load and not first_load): first_load = false for i in range(initial_buildings): + # For each building, attempt spawns a few times. + # Spawns are not viable if they are within the player spawn protection + # or overlap with existing buildings for j in range(spawn_attempts): var collumn = random_collumn() if initial_spawn_protection and 43 <= collumn and collumn <= 49: continue - var building = randomize_building() - building.z_index = -2 + var building = random_building() if Grid.add_building_to_collumn(building, collumn): break +# Each time the building generator's timer runs out, generate a new building func _on_timer_timeout() -> void: for i in range(spawn_attempts): - var collumn = random_oppostite_collumn() - var building : Building = randomize_building() - building.z_index = -2 + var collumn = random_opposite_collumn() + var building : Building = random_building() if Grid.add_building_to_collumn(building, collumn): break - -func randomize_building() -> Building: + +# Picks a random building from the list. Sets the z_index. +func random_building() -> Building: var index = randi() % Grid.packed_buildings.size() - return Grid.packed_buildings[index].instantiate() + var ret = Grid.packed_buildings[index].instantiate() + ret.z_index = -2 + return ret diff --git a/world/draw_circle.gd b/world/draw_circle.gd deleted file mode 100644 index d9ac2d5..0000000 --- a/world/draw_circle.gd +++ /dev/null @@ -1,5 +0,0 @@ -extends Node2D -@export var radius : float; - -func _draw(): - draw_circle(Vector2.ZERO, radius, Color.BLACK, true, -1.0, true) diff --git a/world/draw_circle.gd.uid b/world/draw_circle.gd.uid deleted file mode 100644 index 1f12484..0000000 --- a/world/draw_circle.gd.uid +++ /dev/null @@ -1 +0,0 @@ -uid://b5fhsy1xlreco diff --git a/world/earth.gd b/world/earth.gd index f65695c..bcc587f 100644 --- a/world/earth.gd +++ b/world/earth.gd @@ -3,7 +3,7 @@ extends Node2D @export var grass : PackedScene func _ready() -> void: - ItemSpawn.item_pool = ResourceLoader.load("res://items/generic/item_pool.tres","",ResourceLoader.CACHE_MODE_IGNORE) + # Place grass for column in range(Grid.num_collumns): var grass_placed : Building = grass.instantiate() grass_placed.location = Vector2(column, 0) diff --git a/world/grid.gd b/world/grid.gd index 6147f41..471c675 100644 --- a/world/grid.gd +++ b/world/grid.gd @@ -62,11 +62,13 @@ func place_object_list(obj_list : Node2D, building): object.init_at_horizontal_distortion(object.position.length() / Grid.ground_radius) grid_entrance_callback(object) +# Returns the global position for a given grid location and offset 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) +# Returns the grid location for a given global position func get_location_from_world_pos(pos : Vector2): var angle = fposmod(pos.angle(), TAU) var x = floor(num_collumns * angle / TAU) @@ -74,6 +76,7 @@ func get_location_from_world_pos(pos : Vector2): var y = ceil((height - ground_radius)/cell_height) return Vector2(x, y) +# Returns the offset with respect to the grid location for a given global position func get_offset_from_world_pos(pos : Vector2): var angle = pos.angle() var x = fposmod(num_collumns * angle / TAU, 1) * cell_height @@ -81,7 +84,10 @@ func get_offset_from_world_pos(pos : Vector2): var y = fposmod(-(height - ground_radius)/cell_height, 1) * cell_height return Vector2(x, y) +# Resets objects in the grids and the item pool. +# Also initializes vines_per_node. func reset(): + ItemSpawn.item_pool = ResourceLoader.load("res://items/generic/item_pool.tres","",ResourceLoader.CACHE_MODE_IGNORE) for obj in get_children(): obj.free() buildings = [] @@ -92,29 +98,22 @@ func reset(): arr.append([]) vines_per_node.append(arr) +# Returns all vines present at given grid location. func get_vines_at(location) -> Array: location = Global.vec_mod(location, num_collumns, true) return vines_per_node[location.x][location.y] +# Registers a vine at a given grid location. func add_vine_to(vine, location) -> void: if location.y > max_bud_height + 1 or location.y < 0: return location = Global.vec_mod(location, num_collumns, true) vines_per_node[location.x][location.y].append(vine) +# Calls _enter_grid on all children. 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") - -# 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)