Skip to content

Instantly share code, notes, and snippets.

@RamiAwar
Last active April 18, 2021 23:26
Show Gist options
  • Save RamiAwar/1bc4c89f62b4518be107b831b1a4593b to your computer and use it in GitHub Desktop.
Save RamiAwar/1bc4c89f62b4518be107b831b1a4593b to your computer and use it in GitHub Desktop.
Basic random walker script
extends Node2D
onready var dirt_tilemap = $DirtTileMap
onready var wall_tilemap = $WallTileMap
var rng = RandomNumberGenerator.new()
var CellSize = Vector2(16, 16)
var width = 1024/CellSize.x
var height = 1024/CellSize.y
var grid = []
var Tiles = {
"empty": -1,
"wall": 0,
"floor": 1
}
func _init_grid():
grid = []
for x in width:
grid.append([])
for y in height:
grid[x].append(-1);
func GetRandomDirection():
var directions = [[-1, 0], [1, 0], [0, 1], [0, -1]]
var direction = directions[rng.randi()%4]
return Vector2(direction[0], direction[1])
func _create_random_path():
var max_iterations = 1000
var itr = 0
var walker = Vector2.ZERO
while itr < max_iterations:
# Perform random walk
# 1- choose random direction
# 2- check that direction is in bounds
# 3- move in that direction
var random_direction = GetRandomDirection()
if (walker.x + random_direction.x >= 0 and
walker.x + random_direction.x < width and
walker.y + random_direction.y >= 0 and
walker.y + random_direction.y < height):
walker += random_direction
grid[walker.x][walker.y] = Tiles.floor
itr += 1
func _spawn_tiles():
for x in width:
for y in height:
match grid[x][y]:
Tiles.empty:
pass
Tiles.floor:
dirt_tilemap.set_cellv(Vector2(x, y), 0)
Tiles.wall:
pass
func _clear_tilemaps():
for x in width:
for y in height:
dirt_tilemap.clear()
wall_tilemap.clear()
dirt_tilemap.update_bitmask_region()
wall_tilemap.update_bitmask_region()
func _ready():
rng.randomize()
_init_grid()
_clear_tilemaps()
_create_random_path()
_spawn_tiles()
@m0hmad
Copy link

m0hmad commented Apr 18, 2021

Didn't work for me update_bitmask_region(). This worked when I moved to line 65 in the functions _spawn_tiles()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment