Commit 9f26476d authored by tgw3ff's avatar tgw3ff
Browse files

Fix and Optimize Enemy Spawning Logic

The enemy spawning algorithm was not behaving as intended. This commit fixes
that as well as optimizing to logic to decrease related slow-downs.
parent e829612e
......@@ -2,7 +2,7 @@ const CELL = preload("res://src/Cell.gd")
const ROOM = preload("res://src/Room.gd")
# Percentage of cells in the map that should be enemy spawns
const ENEMY_FILL_PERCENT = 0.15
const ENEMY_FILL_PERCENT = 0.030
# Stores the empty coordinates on the map and which "room" they are each a part of
var rooms = []
var centers = []
......@@ -407,7 +407,10 @@ func spawn_holes():
randomize()
room = rooms[randi() % rooms.size()]
if room.count_elligible_hole > 0 && !room.spawn_room:
if rooms.size() > 1:
if room.count_elligible_hole > 0 && !room.spawn_room:
room_found = true
else:
room_found = true
room.spawn_hole(room.get_random_hole_spawn(), null)
......@@ -448,7 +451,8 @@ func spawn_enemies():
var spawn = elligible_spawns[i]
elligible_spawns.remove(i)
if coord_in_what_room(spawn).find_distance_individual(spawn, "PLAYER") < 4:
# Following logic removed as it too often causes the map generation to take incredible amounts of time to complete
if coord_in_what_room(spawn).is_distance_individual_greater_than(spawn, "PLAYER", 4):
enemy_spawns.append(spawn)
# Generates a new map
......
......@@ -90,6 +90,48 @@ func find_distance_individual(coord, value):
return distance_current
# Return the whether or not the given cell's distance from the nearest cell with
# the given content value is greater than the given max distance. If
# the value is "PLAYER", it will evaluate the distance from the player spawn
# cell.
func is_distance_individual_greater_than(coord, value, max_distance):
var distance_current = -1
var distance_found = false
var distance_greater_than = false
while !distance_found:
if str(value) != "PLAYER":
var surrounding_contents = []
distance_current += 1
for x_mod in range (-distance_current, distance_current + 1):
for y_mod in range (-distance_current, distance_current + 1):
if cells.keys().has(Vector2(coord.x + x_mod, coord.y + y_mod)):
surrounding_contents.append(cells[Vector2(coord.x + x_mod, coord.y + y_mod)].content)
elif value == CELL.WALL:
distance_found = true
if value in surrounding_contents:
distance_found = true
surrounding_contents = []
else:
if !spawn_room:
distance_found = true
distance_greater_than = true
else:
distance_current += 1
for x_mod in range (-distance_current, distance_current + 1):
for y_mod in range (-distance_current, distance_current + 1):
if cells.keys().has(Vector2(coord.x + x_mod, coord.y + y_mod)) && cells[Vector2(coord.x + x_mod, coord.y + y_mod)].player_spawn:
distance_found = true
if distance_current > max_distance:
distance_found = true
distance_greater_than = true
return distance_greater_than
# For each cell, find how far it is away from the nearest wall tile
func find_distances_wall():
for cell in cells:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment