Skip to content

Instantly share code, notes, and snippets.

@RamiAwar
Created May 10, 2020 07:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RamiAwar/a3db8749eadfcc94a4a83e4843645b75 to your computer and use it in GitHub Desktop.
Save RamiAwar/a3db8749eadfcc94a4a83e4843645b75 to your computer and use it in GitHub Desktop.
func _create_random_path():
var max_iterations = 1000
var itr = 0
var walker = Vector2.ZERO
var current_stride = 0
var current_direction = Vector2.ZERO
var max_steps_direction = 5;
while itr < max_iterations:
# Perform random walk
# 1- choose random direction
# 2- check that direction is in bounds
# 3- move in that direction
if current_stride == 0:
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
current_direction = random_direction;
grid[walker.x][walker.y] = Tiles.floor
current_stride += 1
else:
current_stride += 1
if current_stride == max_steps_direction:
current_stride = 0
if (walker.x + current_direction.x >= 0 and
walker.x + current_direction.x < width and
walker.y + current_direction.y >= 0 and
walker.y + current_direction.y < height):
walker += current_direction
grid[walker.x][walker.y] = Tiles.floor
itr += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment