feat: Integrate station into level, move player to spawn

This commit is contained in:
Dendy 2026-03-04 20:42:36 +01:00
parent d18f21c90d
commit 13b08b660e
1 changed files with 25 additions and 13 deletions

View File

@ -5,7 +5,9 @@ extends Node3D
@export var ped_spawn_rate: int = 20
var pedestrian_scene = preload("res://pedestrian.tscn")
var pedestrian_scn = preload("res://pedestrian.tscn")
var chunk_scn = preload("res://chunk.tscn")
var station_scn = preload("res://station.tscn")
func dir_to_vector(dir: int) -> Vector2i:
assert(dir >= 0 and dir <= 3)
@ -34,11 +36,11 @@ func _ready() -> void:
var station = Vector2i(randi() % dimension, 0)
var spawn = Vector2i(dimension - station.x - 1, dimension - 1)
$Player.position.x = station.x * Global.chunk_size
$Player.position.z = station.y * Global.chunk_size
$Player.position.x = spawn.x * Global.chunk_size
$Player.position.z = spawn.y * Global.chunk_size
$Pedestrian.position.x = station.x * Global.chunk_size
$Pedestrian.position.z = station.y * Global.chunk_size
$Pedestrian.position.x = spawn.x * Global.chunk_size
$Pedestrian.position.z = spawn.y * Global.chunk_size
var paths: Array = []
for path_idx in range(path_count):
@ -64,13 +66,17 @@ func _ready() -> void:
paths.append(path)
var chunk_scn = preload("res://chunk.tscn")
var chunks: Array
# Populate the array with [dimension(x)][dimension(y)]instance
for x in range(dimension):
var row: Array
for y in range(dimension):
var new_chunk = chunk_scn.instantiate()
var new_chunk = null
if Vector2i(x,y) == station:
new_chunk = station_scn.instantiate()
else:
new_chunk = chunk_scn.instantiate()
new_chunk.position = Vector3(x * Global.chunk_size+1, 0, y * Global.chunk_size+1)
row.append(new_chunk)
add_child(new_chunk)
@ -81,13 +87,19 @@ func _ready() -> void:
var curr = path[i]
var next = path[i+1] if i+1 < path.size() else null
if next:
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":
continue
if next_chunk:
var revdir = vector_to_dir(curr - next)
chunks[next.x][next.y].exits[revdir] = true
chunks[next.x][next.y].update()
next_chunk.exits[revdir] = true
next_chunk.update()
var dir = vector_to_dir(next - curr)
chunks[curr.x][curr.y].exits[dir] = true
chunks[curr.x][curr.y].update()
curr_chunk.exits[dir] = true
curr_chunk.update()
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
@ -96,6 +108,6 @@ func _process(delta: float) -> void:
var ped_pos = $Player.position + rand_unit_vec * spawn_radius
if (randi() % ped_spawn_rate == 0):
print("Pedestrian at ", ped_pos)
var new_ped = pedestrian_scene.instantiate()
var new_ped = pedestrian_scn.instantiate()
add_child(new_ped)
new_ped.position = ped_pos