Documented and refactored world scripts.

This commit is contained in:
Melvin Weiß 2025-10-22 03:29:21 +02:00
parent 8ca085192d
commit b75e6be470
8 changed files with 34 additions and 36 deletions

View file

@ -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

View file

@ -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"]

View file

@ -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

View file

@ -1,5 +0,0 @@
extends Node2D
@export var radius : float;
func _draw():
draw_circle(Vector2.ZERO, radius, Color.BLACK, true, -1.0, true)

View file

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

View file

@ -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)

View file

@ -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)