maze-generator-assets-lib/generator.gd
megamichi 7bb83a1340 init
2025-02-28 17:13:53 +01:00

55 lines
1.5 KiB
GDScript

extends Sprite2D
var maze_pos = Vector2i(0, 0)
var dir_history = []
var directions = [Vector2i.UP, Vector2i.RIGHT, Vector2i.DOWN, Vector2i.LEFT]
var tile_b = Vector2i(1, 0) # border # black
var tile_n = Vector2i(0, 0) # no visited # grey
var tile_v = Vector2i(0, 1) # visited # blue
var tile_s = Vector2i(1, 1) # visited and stuck # green
signal done
@onready var Maze = $"../maze"
func _ready() -> void:
Maze.set_cell(maze_pos,0,tile_v)
func get_neighbors():
var dlist = []
for dir in directions:
var next_pos = maze_pos + dir * 2
if $"../maze".get_cell_atlas_coords(next_pos) == tile_n:
#$"../maze".set_cell(next_pos,0,tile_v)
#print(next_pos)
dlist.append(dir)
print(dlist)
return dlist
func _process(delta: float) -> void:
var dlist = get_neighbors()
if dlist.is_empty():
if dir_history.is_empty():
print("Maze generation completed")
done.emit()
set_process(false)
return
var removed_item = dir_history.pop_back()
$"../maze".set_cell(maze_pos-removed_item, 0, tile_s)
$"../maze".set_cell(maze_pos, 0, tile_s)
maze_pos = maze_pos - removed_item*2
else:
# 2.1 pick random neighbar
var dir = dlist.pick_random()
# Wand zwischen Zellen entfernen
$"../maze".set_cell(maze_pos + dir, 0, tile_v)
# add to stack
dir_history.append(dir)
# set new cell
maze_pos += dir * 2
$"../maze".set_cell(maze_pos, 0, tile_v)
position = 16 * maze_pos+Vector2i(1, 1)