Door hacking
authorNeil <neil@dip.sun.ac.za>
Sat, 5 Mar 2016 11:35:36 +0000 (13:35 +0200)
committerNeil <neil@dip.sun.ac.za>
Sat, 5 Mar 2016 12:00:30 +0000 (14:00 +0200)
koperkapel/gamelib/door.py [new file with mode: 0644]
koperkapel/gamelib/keypad.py [new file with mode: 0644]
koperkapel/gamelib/level.py
koperkapel/levels/level1.json
koperkapel/loaders/levelloader.py
koperkapel/scenes/level.py

diff --git a/koperkapel/gamelib/door.py b/koperkapel/gamelib/door.py
new file mode 100644 (file)
index 0000000..9deadca
--- /dev/null
@@ -0,0 +1,22 @@
+# A doors / keypads set
+
+import os
+from pgzero.loaders import images
+from ..actors.orientatedsurf import OrientatedSurfActor
+
+
+class Door(OrientatedSurfActor):
+
+    def __init__(self, x, y, angle):
+        self._closed = True
+        self._open_surf = images.load(os.path.join('fixtures', 'door_open'))
+        self._closed_surf = images.load(os.path.join('fixtures', 'door_closed'))
+        self.game_pos = (x, y)
+        super().__init__(self._closed_surf, angle)
+
+    def open(self):
+        self._closed = False
+        self.surf = self._open_surf
+
+    def is_closed(self):
+        return self._closed
diff --git a/koperkapel/gamelib/keypad.py b/koperkapel/gamelib/keypad.py
new file mode 100644 (file)
index 0000000..6c6d1c8
--- /dev/null
@@ -0,0 +1,19 @@
+# A doors / keypads set
+
+import os
+from pgzero.loaders import images
+from ..actors.orientatedsurf import SurfActor
+
+
+class Keypad(SurfActor):
+
+    def __init__(self, x, y, doors):
+        self._doors = doors
+        self.game_pos = (x, y)
+        surf = images.load(os.path.join('fixtures', 'keypad'))
+        super().__init__(surf)
+
+    def activate(self):
+        # FIXME: Check stats
+        for door in self._doors:
+            door.open()
index 01c260e7d5859cd836cbc52a336116f8fe7398c6..4644e0498bbf89e2647c928f1de9ad89bb367364 100644 (file)
@@ -1,11 +1,16 @@
 """ Class holding the level info """
 
+from .keypad import Keypad
+from .door import Door
+
 
 class Level(object):
 
     def __init__(self):
         self.width = self.height = 0
         self.tiles = []
+        self.keypads = []
+        self.doors = []
         self.tileset = None
         self.start_pos = (0, 0)
 
@@ -18,10 +23,28 @@ class Level(object):
                ]
 
     def can_walk(self, x, y, layer):
-        return 'walk' in self.tiles[y][x][layer]['behaviour']
+        if 'walk' in self.tiles[y][x][layer]['behaviour']:
+            # check doors
+            for door in self.doors:
+                if (x, y) == door.pos and door.closed():
+                    return False
+            return True
+        return False
 
     def can_fly(self, x, y, layer):
-        return 'fly' in self.tiles[y][x][layer]['behaviour']
+        if 'fly' in self.tiles[y][x][layer]['behaviour']:
+            for door in self.doors:
+                if (x, y) == door.pos and door.closed():
+                    return False
+            return True
+
+        return False
 
     def can_crawl(self, x, y, layer):
         return 'crawl' in self.tiles[y][x][layer]['behaviour']
+
+    def keypad_at(self, x, y):
+        for keypad in self.keypads:
+            if (x, y) == keypad.pos:
+                return keypad
+        return None
index 38a872aa092c13e1119dfe052144d769c9c0b70b..b4a4a19637c6e3d1b6cf36f54f0f4c25c3936824 100644 (file)
    "starting pos": [
       5, 
       6
+   ], 
+   "door_info": [
+      {
+         "keypads": [
+            [
+               6, 
+               7
+            ]
+         ], 
+         "doors": [
+            [
+               7, 
+               8
+            ]
+         ]
+      }, 
+      {
+         "keypads": [
+            [
+               12, 
+               5
+            ]
+         ], 
+         "doors": [
+            [
+               12, 
+               7
+            ], 
+            [
+               13, 
+               4
+            ]
+         ]
+      }, 
+      {
+         "keypads": [
+            [
+               18, 
+               9
+            ]
+         ], 
+         "doors": [
+            [
+               19, 
+               8
+            ]
+         ]
+      }
    ]
 }
\ No newline at end of file
index 0addeccf5c54d678dadbaa8d796e904e26ebf01e..b5572ecde096a83c6fa902f81a62619fbd1f790a 100644 (file)
@@ -6,6 +6,8 @@ from pgzero.loaders import ResourceLoader
 
 from ..gamelib.tiles import Wall, Floor, Tunnel, Underground, Grate
 from ..gamelib.level import Level
+from ..gamelib.door import Door
+from ..gamelib.keypad import Keypad
 
         
 
@@ -40,6 +42,17 @@ class LevelLoader(ResourceLoader):
         for tile in TILES.values():
             tile.TILESET = self._level.tileset
         self._load_tile_images()
+        for door_info in level_data['door_info']:
+            # Create the doors first
+            doors = []
+            for door in door_info['doors']:
+                # Sort out angle
+                new_door = Door(0, door[0], door[1])
+                self._level.doors.append(new_door)
+            # Add the keypads
+            for keypad in door_info['keypads']:
+                new_keypad = Keypad(doors, keypad[0], keypad[1])
+                self._level.keypads.append(new_keypad)
         return self._level
 
     def _load_tile_images(self):
index 2484119350570d93fdfb49b37f5d0c02106876d6..a16e99f9fec60ba6427faa011dd511edf6411624 100644 (file)
@@ -21,6 +21,13 @@ class BaseLevelScene(Scene):
         for layer in ['floor', 'tunnels']:
             self._surfaces[layer] = self._render(layer)
         self._overlay = self._surfaces['floor'].copy()
+        self._doors = self.actors.add_layer("doors", level=15)
+        self._keypads = self.actors.add_layer("keypads", level=16)
+        # These are already Actors
+        for door in self._level.doors:
+            self._doors.append(door)
+        for keypad in self._level.keypads:
+            self._keypads.append(keypads)
 
     def _render(self, layer):
         # We cache the rendered surface to avoid doing a large number
@@ -78,6 +85,7 @@ class GameLevelScene(BaseLevelScene):
 
     def _set_pos(self, x, y):
         self._player_pos = (x, y)
+        print('Now at ', x, y)
 
     def _can_move(self, x, y):
         if self._mode == 'walk':