From 0a4ee2b80d2af7627366380919dbe6d5204e2999 Mon Sep 17 00:00:00 2001 From: Neil Date: Sat, 5 Mar 2016 17:58:50 +0200 Subject: [PATCH] Gain more roaches --- koperkapel/gamelib/friendlyroach.py | 10 +++++++ koperkapel/gamelib/level.py | 15 +++++++++++ koperkapel/levels/level1.json | 30 ++++++++++++++++++--- koperkapel/loaders/levelloader.py | 6 +++++ koperkapel/scenes/base.py | 9 +++++++ koperkapel/scenes/level.py | 15 ++++++++++- koperkapel/world.py | 41 ++++++++++++++++++++++++++--- 7 files changed, 118 insertions(+), 8 deletions(-) create mode 100644 koperkapel/gamelib/friendlyroach.py diff --git a/koperkapel/gamelib/friendlyroach.py b/koperkapel/gamelib/friendlyroach.py new file mode 100644 index 0000000..b9ddd6e --- /dev/null +++ b/koperkapel/gamelib/friendlyroach.py @@ -0,0 +1,10 @@ +# A doors / keypads set + +from ..roaches import default_roaches, WorldRoach + + +def get_friendly_roach(x, y): + roach = default_roaches.assemble(WorldRoach()) + roach.anchor = (0, 0) + roach.game_pos = (x, y) + return roach diff --git a/koperkapel/gamelib/level.py b/koperkapel/gamelib/level.py index a8fe447..eb9e8b2 100644 --- a/koperkapel/gamelib/level.py +++ b/koperkapel/gamelib/level.py @@ -14,6 +14,8 @@ class Level(object): self.grates = [] self.tileset = None self.start_pos = (0, 0) + self.enemies = [] + self.friends = [] def get_neighbors(self, x, y): # 4 -connected neighbors @@ -59,3 +61,16 @@ class Level(object): for keypad in self.keypads: if (x, y) == keypad.game_pos: keypad.activate(roaches) + + def get_friends(self): + return self._friends[:] + + def is_on_friend(self, x, y): + return (x, y) in [x.game_pos for x in self.friends] + + def remove_friend(self, x, y): + for friend in self.friends[:]: + if friend.game_pos == (x, y): + self.friends.remove(friend) + return friend + return None diff --git a/koperkapel/levels/level1.json b/koperkapel/levels/level1.json index 1dea2c4..69a3af4 100644 --- a/koperkapel/levels/level1.json +++ b/koperkapel/levels/level1.json @@ -4286,10 +4286,6 @@ ] ], "tileset": "bunker", - "starting pos": [ - 5, - 6 - ], "door_info": [ { "keypads": [ @@ -4337,5 +4333,31 @@ ] ] } + ], + "starting pos": [ + 5, + 6 + ], + "friendly roaches": [ + [ + 2, + 9 + ], + [ + 2, + 3 + ], + [ + 2, + 13 + ], + [ + 9, + 13 + ], + [ + 21, + 8 + ] ] } diff --git a/koperkapel/loaders/levelloader.py b/koperkapel/loaders/levelloader.py index 5247695..4c78657 100644 --- a/koperkapel/loaders/levelloader.py +++ b/koperkapel/loaders/levelloader.py @@ -8,6 +8,7 @@ from ..gamelib.tiles import Wall, Floor, Tunnel, Underground, Grate from ..gamelib.level import Level from ..gamelib.door import Door from ..gamelib.keypad import Keypad +from ..gamelib.friendlyroach import get_friendly_roach @@ -34,6 +35,8 @@ class LevelLoader(ResourceLoader): self._level.tiles = level_data['tiles'] self._level.tileset = level_data['tileset'] self._level.start_pos = level_data["starting pos"] + self._level.enemies = [] + self._level.friends = [] # Consistency check, so we can assume things are correct # in the level renderer for row, row_data in enumerate(self._level.tiles): @@ -60,6 +63,9 @@ class LevelLoader(ResourceLoader): for keypad in door_info['keypads']: new_keypad = Keypad(keypad[0], keypad[1], doors) self._level.keypads.append(new_keypad) + for pos in level_data['friendly roaches']: + roach = get_friendly_roach(pos[0], pos[1]) + self._level.friends.append(roach) return self._level def _load_tile_images(self): diff --git a/koperkapel/scenes/base.py b/koperkapel/scenes/base.py index 8bc803a..1499803 100644 --- a/koperkapel/scenes/base.py +++ b/koperkapel/scenes/base.py @@ -20,6 +20,7 @@ class Engine: self._app = app self._scene = scene self._world = world + self._update_vehicle = False def _apply_events(self, events): if not events: @@ -42,6 +43,9 @@ class Engine: def move_screen(self, offset): self._scene.move_screen(offset) + def add_roach(self): + self._world.add_new_roach() + @apply_events def update(self, dt): return self._scene.update(self._world.proxy(), self, dt) @@ -106,6 +110,11 @@ class MoveViewportEvent(Event): ENGINE_METHOD = "move_screen" +class AddRoachEvent(Event): + """ Change to a new scene. """ + + ENGINE_METHOD = "add_roach" + class Layer: """ A single layer of actors. """ diff --git a/koperkapel/scenes/level.py b/koperkapel/scenes/level.py index 54449dc..6a8e232 100644 --- a/koperkapel/scenes/level.py +++ b/koperkapel/scenes/level.py @@ -4,9 +4,10 @@ from pgzero.constants import keys from pygame import Surface import pygame.locals as pgl from ..loaders.levelloader import levels -from .base import Scene, ChangeSceneEvent, MoveViewportEvent, defer_to_update +from .base import Scene, ChangeSceneEvent, MoveViewportEvent, AddRoachEvent, defer_to_update from ..constants import TILE_SIZE, WIDTH, HEIGHT from ..vehicles.base import Vehicle +from ..roaches import RoachFactory, WorldRoach class BaseLevelScene(Scene): @@ -87,6 +88,7 @@ class GameLevelScene(BaseLevelScene): return super().enter(world) self._roaches = self.actors.add_layer("roaches", level=10) + self._friends = self.actors.add_layer("friendly roaches", level=9) self._vehicle = Vehicle.current(world) self._mode = 'walk' self._angle = 0 @@ -99,6 +101,8 @@ class GameLevelScene(BaseLevelScene): self._set_pos(x, y) self._avatar.pos = (WIDTH // 2, HEIGHT // 2) self._roaches.add(self._avatar) + for friend in self._level.friends: + self._friends.add(friend) # Fix viewport offset return [ MoveViewportEvent(( @@ -107,6 +111,7 @@ class GameLevelScene(BaseLevelScene): def _set_pos(self, x, y): self._player_pos = (x, y) + #print('At ', (x, y)) def _can_move(self, x, y): if self._mode == 'walk': @@ -138,6 +143,9 @@ class GameLevelScene(BaseLevelScene): def update(self, world, engine, dt): super().update(world, engine, dt) events = world.pop_events() + for friend in self._friends: + friend.pos = self.calc_offset( + friend.game_pos[0] * TILE_SIZE, friend.game_pos[1] * TILE_SIZE) return events def on_key_down(self, key, mod, unicode): @@ -176,6 +184,11 @@ class GameLevelScene(BaseLevelScene): self._mode = 'walk' elif self._level.is_keypad(x, y): self._level.press_keypad(x, y, self._roaches) + elif self._level.is_on_friend(x, y): + friend = self._level.remove_friend(x, y) + self._friends.remove(friend) + self._vehicle_changed() + return [AddRoachEvent()] return elif key == keys.X: # Fire diff --git a/koperkapel/world.py b/koperkapel/world.py index 8097eb2..befc1a0 100644 --- a/koperkapel/world.py +++ b/koperkapel/world.py @@ -3,6 +3,33 @@ from .scenes.base import WorldEvent +NAMES =[ + "roupert", + "roachel", + "roeginald", + "roichard", + "rory", + "roalph", + "roabia", + "roafi", + "roaman", + "roemus", + "roadley", + "roanaell", + "roashwan", + "roashid", + "roaphael", + "roenfield", + "roani", + "roaya", + "roaza", + "robekka", + "rogan", + "roiana", + "roberta", + ] + + class World: """ World and player state. """ @@ -20,9 +47,9 @@ class World: def _build_initial_state(self): state = {} state["roaches"] = [ - self._build_roach("roachel", smart=True), - self._build_roach("roeginald", strong=True), - self._build_roach("roichard", fast=True), + #self._build_roach("roachel", smart=True), + #self._build_roach("roeginald", strong=True), + #self._build_roach("roichard", fast=True), self._build_roach("roupert"), ] state["serums"] = [ @@ -69,6 +96,14 @@ class World: def proxy(self): return WorldDictProxy(self._state) + def add_new_roach(self): + roach_names = [x['name'] for x in self.roaches] + for cand in NAMES: + if cand not in roach_names: + roach = self._build_roach(cand) + self._state['roaches'].append(roach) + break + def apply_event(self, action, *args, **kw): if action == "set": return self._apply_set(*args, **kw) -- 2.34.1