from pgzero.actor import Actor
from pgzero.clock import each_tick
+from .enemy_roach import get_enemy_roach
+
class EnemyGenerator(Actor):
"""Generators are currently invisble, but we want the update hook."""
- def __init__(self, info):
+ def __init__(self, info, level):
self.gen_pos = info['pos']
+ self.level = level
self.enemy_type = info['type']
self.rate = info['rate']
self.max_enemies = info['max']
self._time_since_last_update = 0
self.paused = False
each_tick(self.update)
+ self._made_enemies = []
super().__init__(os.path.join('weapons', 'blank'))
def update(self, dt):
return
self._time_since_last_update += dt
if self._time_since_last_update > self.rate:
- print('A horrible monster (%s) appears' % self.enemy_type, self.gen_pos)
- self._time_since_last_update = 0
+ if len(self._made_enemies) < self.max_enemies:
+ self._make_enemy()
+
+ def _make_enemy(self):
+ if self.enemy_type == 'roach':
+ roach = get_enemy_roach()
+ 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)
def pause(self):
self.paused = True
--- /dev/null
+# Roach utilities
+
+from ..roaches import t32_roaches, WorldRoach
+
+
+def get_enemy_roach():
+ # red
+ roach = t32_roaches.assemble(WorldRoach(), color=(255, 0, 0, 255))
+ roach.anchor = (0, 0)
+ roach.game_pos = (0, 0)
+ return roach
""" Class holding the level info """
+import random
+
class Level(object):
def get_exit_level(self):
return self.exit["next level"]
+
+ def is_on_enemy(self, x, y):
+ for enemy in self.enemies:
+ if (x, y) == enemy.game_pos:
+ return True
+ return False
+
+ def get_enemy(self, x, y):
+ for enemy in self.enemies:
+ if (x, y) == enemy.game_pos:
+ return enemy
+ return None
+
+ def add_enemy(self, enemy, x, y):
+ """Add an enemy to an empty floor space near x, y"""
+ added = False
+ while not added:
+ if self.can_walk(x, y, 'floor'):
+ if not self.is_on_friend(x, y):
+ if not self.is_on_enemy(x, y):
+ added = True
+ enemy.game_pos = (x, y)
+ self.enemies.append(enemy)
+ added = True
+ x += random.randint(-1, 1)
+ y += random.randint(-1, 1)
+
for item in level_data['items']:
self._level.items.append(get_item(item))
for generator in level_data['enemy generators']:
- enemy = EnemyGenerator(generator)
+ enemy = EnemyGenerator(generator, self._level)
self._level.enemy_generators.append(enemy)
return self._level
frame.blit(eyes, (0, 0))
return frame
- def assemble(self, roach_data, weapon=None):
- color = roach_serum_color(roach_data)
+ def assemble(self, roach_data, color=None, weapon=None):
+ if not color:
+ color = roach_serum_color(roach_data)
frames = []
frames = [
self.assemble_frame(i, color, roach_data, weapon)
def _can_move(self, x, y):
if self._mode == 'walk':
- return self._level.can_walk(x, y, self._level_layer)
+ if not self._level.is_on_enemy(x, y):
+ return self._level.can_walk(x, y, self._level_layer)
+ return False
elif self._mode == 'fly':
return self._level.can_fly(x, y, self._level_layer)
elif self._mode == 'crawl':
for item in self._items:
item.pos = self.calc_offset(
item.game_pos[0] * TILE_SIZE, item.game_pos[1] * TILE_SIZE)
+ self._check_enemies()
+ for enemy in self._enemies:
+ enemy.pos = self.calc_offset(
+ enemy.game_pos[0] * TILE_SIZE, enemy.game_pos[1] * TILE_SIZE)
more = self._check_held_keys(dt)
if more:
events.extend(more)
return events
+ def _check_enemies(self):
+ if len(self._level.enemies) != len(self._enemies):
+ # New nemy has spawned
+ for enemy in self._level.enemies:
+ if enemy not in self._enemies:
+ self._enemies.add(enemy)
+
def _check_held_keys(self, dt):
for key in self._held_keys:
self._last_key_down += dt