extends Node3D @export var dimension: int = 5 @export var path_count: int = 5 func dir_to_vector(dir: int) -> Vector2i: assert(dir >= 0 and dir <= 3) var polarity = dir % 2 # 0: negative, 1: positive var axis = floor(dir / 2) # 0: vertical, 1: horizontal return Vector2i( axis * (polarity * 2 - 1), (1 - axis) * (polarity * 2 -1), ) func vector_to_dir(vec: Vector2i) -> int: assert(vec.length() == 1) if vec == Vector2i(0, -1): return 0 elif vec == Vector2i(0, 1): return 1 elif vec == Vector2i(-1, 0): return 2 elif vec == Vector2i(1, 0): return 3 assert(false) return -1 func _ready() -> void: # Decide position of station & spawn var station = Vector2i(randi() % dimension, 0) var spawn = Vector2i(dimension - station.x - 1, dimension - 1) $Player.position.x = station.x * 100 $Player.position.z = station.y * 100 $Pedestrian.position.x = station.x * 100 $Pedestrian.position.z = station.y * 100 var paths: Array = [] for path_idx in range(path_count): var path: Array[Vector2i] = [ station, Vector2i(station.x, station.y + 1), ] while true: # Decide each of the steps var last_pos = path.slice(-1)[0] 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 continue path.append(next_pos) if next_pos == spawn or next_pos.y == dimension-1: # End of path break 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() new_chunk.position = Vector3(x * 101, 0, y * 101) row.append(new_chunk) add_child(new_chunk) chunks.append(row) 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 if next: var revdir = vector_to_dir(curr - next) chunks[next.x][next.y].exits[revdir] = true chunks[next.x][next.y].update() var dir = vector_to_dir(next - curr) chunks[curr.x][curr.y].exits[dir] = true chunks[curr.x][curr.y].update() # Called every frame. 'delta' is the elapsed time since the previous frame. func _process(delta: float) -> void: pass