if self._time_since_last_update > self.rate:
if len(self._made_enemies) < self.max_enemies:
self._make_enemy()
+ self._time_since_last_update = 0
def _make_enemy(self):
if self.enemy_type == 'roach':
- roach = get_enemy_roach()
+ roach = get_enemy_roach(self.level)
self._made_enemies.append(roach)
self.level.add_enemy(roach, *self.gen_pos)
def killed(self, enemy):
if enemy in self._made_enemies:
self._made_enemies.remove(enemy)
+ # Delay next respawn so we don't flood the player with enemies
+ self._time_since_last_update = 0
def pause(self):
self.paused = True
# Roach utilities
+import random
+
+from pgzero.clock import each_tick
+from functools import partial
+
from ..roaches import t32_roaches, WorldRoach
-def get_enemy_roach():
- # red
+def get_enemy_roach(level):
roach = t32_roaches.assemble(WorldRoach(), color=(255, 0, 0, 255))
roach.anchor = (0, 0)
roach.game_pos = (0, 0)
roach.health = 5
+ roach.level = level
+ roach.move = partial(move, roach)
+ roach.last_moved = 0
+ roach.start_pos = None
+ each_tick(roach.move)
return roach
+
+
+def move(roach, dt):
+ """Enemy roach move method"""
+ roach.last_moved += dt
+ if roach.last_moved > 0.5:
+ if not roach.start_pos:
+ roach.start_pos = roach.game_pos
+ roach.last_moved = 0
+ attempt = 0
+ while attempt < 4:
+ attempt += 1
+ dx = random.randint(-1, 1)
+ dy = random.randint(-1, 1)
+ if abs(roach.game_pos[0] + dx - roach.start_pos[0]) > 2:
+ continue
+ if abs(roach.game_pos[1] + dy - roach.start_pos[1]) > 2:
+ continue
+ if roach.level.can_walk(roach.game_pos[0] + dx, roach.game_pos[1] + dy, 'floor'):
+ enemy = roach.level.get_enemy(roach.game_pos[0] + dx, roach.game_pos[1] + dy)
+ if enemy and enemy is not roach:
+ continue
+ roach.game_pos = (roach.game_pos[0] + dx, roach.game_pos[1] + dy)
+ break
def remove_enemy(self, enemy):
self.enemies.remove(enemy)
+ for generator in self.enemy_generators:
+ generator.killed(enemy)
def add_enemy(self, enemy, x, y):
"""Add an enemy to an empty floor space near x, y"""