
Level builder
GDScript Randomization and seeds Vector mathTask
As part of my working on the mobile game Portal Ninja with Cloud Base Games Ltd. I was tasked to create a level builder, which would allow for simple and fast creation of levels that both level designers, as well as players could use. It had to provide variety in terms of rooms, elements and assets being used, but also intuitive and simple level iteration.
Solution
I decided that the optimal solution for this would be to create a level builder, where levels could be made with a drawing style application, where each bloc/pixel would represent a 4x4 area in terms of the game level. The levels are built in a few steps. First the level shape is determined with drawing in the black area, or by generating one at random or with a seed. Then, the corridors are created (blue blocks), made wither by hand or generated. Next based on the corridors drawn in the blueprint, rooms are generated with passages to ensure they are all accessible either through the corridors or other rooms. Finally, the assets are chosen for the level with other options before the whole level is generated.
The project is not publicly available, but some code snippets from the project are available below:
func get_corridors(img) -> Array:
var corridors : Array = []
img.lock()
for l in range(50):
for m in range(50):
if img.get_pixel(l, m) == Color.blue:
corridors.append(Vector2(l, m))
img.unlock()
return corridors
func remove_done_chunks():
var opts = get_options()
img0.lock()
while opts.size() > 0:
find_neighbor_cells(opts[0], opts)
for i in recursive_buff:
opts.erase(i)
if recursive_buff.size() < 15 + 80 * get_node("densityHScrollBar").value / 10:
for i in recursive_buff:
img0.set_pixelv(i, Color.green)
recursive_buff = []
img0.unlock()
func find_neighbor_cells(pos, data):
recursive_buff.append(pos)
if recursive_buff.size() < 100:
if not recursive_buff.has(Vector2(pos.x - 1, pos.y)) and data.has(Vector2(pos.x - 1, pos.y)):
find_neighbor_cells(Vector2(pos.x - 1, pos.y), data)
if not recursive_buff.has(Vector2(pos.x + 1, pos.y)) and data.has(Vector2(pos.x + 1, pos.y)):
find_neighbor_cells(Vector2(pos.x + 1, pos.y), data)
if not recursive_buff.has(Vector2(pos.x, pos.y - 1)) and data.has(Vector2(pos.x, pos.y - 1)):
find_neighbor_cells(Vector2(pos.x, pos.y - 1), data)
if not recursive_buff.has(Vector2(pos.x, pos.y + 1)) and data.has(Vector2(pos.x, pos.y + 1)):
find_neighbor_cells(Vector2(pos.x, pos.y + 1), data)