From c3d7223fd37b42f88390aa2c299588a10daa9984 Mon Sep 17 00:00:00 2001 From: Neil Date: Sat, 5 Mar 2016 21:40:04 +0200 Subject: [PATCH] Enemies appear --- koperkapel/gamelib/enemy_generator.py | 20 +++++++++++++++--- koperkapel/gamelib/enemy_roach.py | 11 ++++++++++ koperkapel/gamelib/level.py | 29 +++++++++++++++++++++++++++ koperkapel/loaders/levelloader.py | 2 +- koperkapel/roaches.py | 5 +++-- koperkapel/scenes/level.py | 15 +++++++++++++- 6 files changed, 75 insertions(+), 7 deletions(-) create mode 100644 koperkapel/gamelib/enemy_roach.py diff --git a/koperkapel/gamelib/enemy_generator.py b/koperkapel/gamelib/enemy_generator.py index 3003064..1329660 100644 --- a/koperkapel/gamelib/enemy_generator.py +++ b/koperkapel/gamelib/enemy_generator.py @@ -4,17 +4,21 @@ import os 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): @@ -22,8 +26,18 @@ class EnemyGenerator(Actor): 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 diff --git a/koperkapel/gamelib/enemy_roach.py b/koperkapel/gamelib/enemy_roach.py new file mode 100644 index 0000000..95b3e19 --- /dev/null +++ b/koperkapel/gamelib/enemy_roach.py @@ -0,0 +1,11 @@ +# 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 diff --git a/koperkapel/gamelib/level.py b/koperkapel/gamelib/level.py index bcc319a..3df9fe0 100644 --- a/koperkapel/gamelib/level.py +++ b/koperkapel/gamelib/level.py @@ -1,5 +1,7 @@ """ Class holding the level info """ +import random + class Level(object): @@ -84,3 +86,30 @@ 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) + diff --git a/koperkapel/loaders/levelloader.py b/koperkapel/loaders/levelloader.py index a775838..14db3be 100644 --- a/koperkapel/loaders/levelloader.py +++ b/koperkapel/loaders/levelloader.py @@ -73,7 +73,7 @@ class LevelLoader(ResourceLoader): 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 diff --git a/koperkapel/roaches.py b/koperkapel/roaches.py index 680d2a6..1d77563 100644 --- a/koperkapel/roaches.py +++ b/koperkapel/roaches.py @@ -89,8 +89,9 @@ class RoachFactory: 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) diff --git a/koperkapel/scenes/level.py b/koperkapel/scenes/level.py index a684466..e5d102a 100644 --- a/koperkapel/scenes/level.py +++ b/koperkapel/scenes/level.py @@ -146,7 +146,9 @@ class GameLevelScene(BaseLevelScene): 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': @@ -198,11 +200,22 @@ class GameLevelScene(BaseLevelScene): 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 -- 2.34.1