feat: Open all bottom exits & organize level.gd

This commit is contained in:
Dendy 2026-03-04 21:08:03 +01:00
parent c5a557c830
commit 6704b3e4f0
1 changed files with 27 additions and 14 deletions

View File

@ -32,15 +32,17 @@ func vector_to_dir(vec: Vector2i) -> int:
return -1
func _ready() -> void:
# Decide position of station & spawn
# ---- Decide position of Spawn & Station ----
var station = Vector2i(randi() % dimension, 0)
var spawn = Vector2i(dimension - station.x - 1, dimension - 1)
$Player.position.x = spawn.x * Global.chunk_size
$Player.position.z = spawn.y * Global.chunk_size
# ---- Generate the paths ----
var paths: Array = []
for path_idx in range(path_count):
# Station's and the chunk below are always the same.
var path: Array[Vector2i] = [
station,
Vector2i(station.x, station.y + 1),
@ -51,21 +53,18 @@ func _ready() -> void:
var next_dir = dir_to_vector(randi() % 3 + 1) # cannot go up
var next_pos = (last_pos + next_dir).clampi(0,dimension-1)
if next_pos in path:
# Invalid path, try again
if next_pos in path: # Invalid path, try again
continue
path.append(next_pos)
if next_pos == spawn or next_pos.y == dimension-1:
# End of path
if next_pos == spawn or next_pos.y == dimension-1: # End of path
break
paths.append(path)
var chunks: Array
# Populate the array with [dimension(x)][dimension(y)]instance
# ---- Instantiate chunks, station & spawn ----
var chunks: Array # Array[int(x)][int(y)][station_scn|chunk_scn|spawn_scn]
for x in range(dimension):
var row: Array
for y in range(dimension):
@ -79,25 +78,39 @@ func _ready() -> void:
add_child(new_chunk)
chunks.append(row)
# ---- Set exits based on paths ----
for path in paths:
for i in range(path.size()):
var curr = path[i]
var next = path[i+1] if i+1 < path.size() else null
var curr_chunk = chunks[curr.x][curr.y]
var next_chunk = chunks[next.x][next.y] if next else null
if curr_chunk.scene_file_path != "res://chunk.tscn":
if not next_chunk:
continue
if next_chunk:
var revdir = vector_to_dir(curr - next)
next_chunk.exits[revdir] = true
next_chunk.update()
if curr_chunk.scene_file_path == "res://chunk.tscn":
var dir = vector_to_dir(next - curr)
curr_chunk.exits[dir] = true
curr_chunk.update()
# ---- Open all bottom exits ----
for x in range(dimension):
var chunk = chunks[x][dimension-1]
if chunk.scene_file_path != "res://chunk.tscn":
continue
if x > 0:
chunk.exits[2] = true
if x < dimension-1:
chunk.exits[3] = true
chunk.update()
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
var spawn_radius = $Player/SpawnArea/Collision.shape.radius + randf_range(-5, 5)