extends Node3D @export var dimension: int = 10 @export var path_count: int = 2 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: randomize() var chunks: Array # Populate the array with [dimension(x)][dimension(y)]bool for x in range(dimension): var row: Array for y in range(dimension): row.append(false) chunks.append(row) # Decide position of station & spawn var station = Vector2i(randi() % dimension, 0) var spawn = Vector2i(dimension - station.x - 1, dimension - 1) 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) for path in paths: for cell in path: chunks[cell.x][cell.y] = true var chunk_scn = preload("res://chunk.tscn") for rowi in range(dimension): for coli in range(dimension): var chunk_inst = chunk_scn.instantiate() chunk_inst.position = Vector3( # TODO: Don't set the size by hand, get it from chunk rowi * 101, 0, coli * 101, ) #chunk_inst.get_node("n").position.y = -10 #chunk_inst.get_node("s").position.y = -10 #chunk_inst.get_node("w").position.y = -10 #chunk_inst.get_node("e").position.y = -10 for path in paths: for i in range(path.size()): var curr = path[i] var next = path.get(i+1) if rowi != curr.x or coli != curr.y or not next: continue var dir = vector_to_dir(next - curr) print(next-curr, dir) chunk_inst.exits[dir] = true chunk_inst.update() add_child(chunk_inst) # Called every frame. 'delta' is the elapsed time since the previous frame. func _process(delta: float) -> void: pass