Gain more roaches
authorNeil <neil@dip.sun.ac.za>
Sat, 5 Mar 2016 15:58:50 +0000 (17:58 +0200)
committerNeil <neil@dip.sun.ac.za>
Sat, 5 Mar 2016 16:23:04 +0000 (18:23 +0200)
koperkapel/gamelib/friendlyroach.py [new file with mode: 0644]
koperkapel/gamelib/level.py
koperkapel/levels/level1.json
koperkapel/loaders/levelloader.py
koperkapel/scenes/base.py
koperkapel/scenes/level.py
koperkapel/world.py

diff --git a/koperkapel/gamelib/friendlyroach.py b/koperkapel/gamelib/friendlyroach.py
new file mode 100644 (file)
index 0000000..b9ddd6e
--- /dev/null
@@ -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
index a8fe447494f64a6c7e3e5b04f046696f3cb90036..eb9e8b232e3ff6b5ec6aafafb8222df3685b3503 100644 (file)
@@ -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
index 1dea2c4f03755d02719ddd04465cb18e719e036e..69a3af4e8b2203a3d9d6dc3c45b74b46238ffafa 100644 (file)
       ]
    ], 
    "tileset": "bunker", 
-   "starting pos": [
-      5, 
-      6
-   ], 
    "door_info": [
       {
          "keypads": [
             ]
          ]
       }
+   ], 
+   "starting pos": [
+      5, 
+      6
+   ], 
+   "friendly roaches": [
+      [
+         2, 
+         9
+      ], 
+      [
+         2, 
+         3
+      ], 
+      [
+         2, 
+         13
+      ], 
+      [
+         9, 
+         13
+      ], 
+      [
+         21, 
+         8
+      ]
    ]
 }
index 524769535fccd97b87396f056146ac364a352f26..4c786572807ced5fb222420169d68f7ba66cece0 100644 (file)
@@ -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):
index 8bc803ae2ba0fbdc29db684f643be3a046300b17..14998034ce7557a883bb14ee478f5c6e67bab647 100644 (file)
@@ -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. """
index 54449dcc8513f7d2d91b5768d7549307f54e1e94..6a8e232284b14746b6b592b5b9c8239754b2ad93 100644 (file)
@@ -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
index 8097eb2ccf993b2a15fd91231717c042d5fe8c68..befc1a0a6b6d22ac51c9a46133a01309315c42ae 100644 (file)
@@ -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)