Somewhat doorish
authorNeil <neil@dip.sun.ac.za>
Sat, 5 Mar 2016 12:00:21 +0000 (14:00 +0200)
committerNeil <neil@dip.sun.ac.za>
Sat, 5 Mar 2016 12:00:31 +0000 (14:00 +0200)
koperkapel/gamelib/door.py
koperkapel/gamelib/keypad.py
koperkapel/gamelib/level.py
koperkapel/loaders/levelloader.py
koperkapel/scenes/base.py
koperkapel/scenes/level.py
koperkapel/scenes/roach_management.py

index 9deadcaf2ed1c0f1d75d5834db02eae2630dd0ee..9b6e56326b74359837b3357e8aa7f42a500bf021 100644 (file)
@@ -3,6 +3,7 @@
 import os
 from pgzero.loaders import images
 from ..actors.orientatedsurf import OrientatedSurfActor
+from ..constants import TILE_SIZE
 
 
 class Door(OrientatedSurfActor):
@@ -10,9 +11,9 @@ 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._closed_surf = images.load(os.path.join('fixtures', 'door'))
         self.game_pos = (x, y)
-        super().__init__(self._closed_surf, angle)
+        super().__init__(self._closed_surf, angle=angle, anchor=(0, 0))
 
     def open(self):
         self._closed = False
index 6c6d1c8cadeef2c4df1697bbdb92e0925aeb73ed..4667fcde95924490158653a8cc3355a61e19a6c7 100644 (file)
@@ -3,6 +3,7 @@
 import os
 from pgzero.loaders import images
 from ..actors.orientatedsurf import SurfActor
+from ..constants import TILE_SIZE
 
 
 class Keypad(SurfActor):
@@ -11,7 +12,7 @@ class Keypad(SurfActor):
         self._doors = doors
         self.game_pos = (x, y)
         surf = images.load(os.path.join('fixtures', 'keypad'))
-        super().__init__(surf)
+        super().__init__(surf, anchor=(0, 0))
 
     def activate(self):
         # FIXME: Check stats
index 4644e0498bbf89e2647c928f1de9ad89bb367364..39e456588e2a2a9a2186a8475f226af3b9f14573 100644 (file)
@@ -26,7 +26,7 @@ class Level(object):
         if 'walk' in self.tiles[y][x][layer]['behaviour']:
             # check doors
             for door in self.doors:
-                if (x, y) == door.pos and door.closed():
+                if (x, y) == door.game_pos and door.is_closed():
                     return False
             return True
         return False
@@ -34,7 +34,7 @@ class Level(object):
     def can_fly(self, x, y, layer):
         if 'fly' in self.tiles[y][x][layer]['behaviour']:
             for door in self.doors:
-                if (x, y) == door.pos and door.closed():
+                if (x, y) == door.game_pos and door.is_closed():
                     return False
             return True
 
@@ -45,6 +45,6 @@ class Level(object):
 
     def keypad_at(self, x, y):
         for keypad in self.keypads:
-            if (x, y) == keypad.pos:
+            if (x, y) == keypad.game_pos:
                 return keypad
         return None
index b5572ecde096a83c6fa902f81a62619fbd1f790a..190319e451bbffd235678a2ddc3a6e21510f5b4c 100644 (file)
@@ -47,11 +47,11 @@ class LevelLoader(ResourceLoader):
             doors = []
             for door in door_info['doors']:
                 # Sort out angle
-                new_door = Door(0, door[0], door[1])
+                new_door = Door(door[0], door[1], 0)
                 self._level.doors.append(new_door)
             # Add the keypads
             for keypad in door_info['keypads']:
-                new_keypad = Keypad(doors, keypad[0], keypad[1])
+                new_keypad = Keypad(keypad[0], keypad[1], doors)
                 self._level.keypads.append(new_keypad)
         return self._level
 
index 481850b2533153afa5ccbb12ea090aae5995bcf9..ade4d8bfddede638919ab9eb884423d38a9e55c6 100644 (file)
@@ -44,9 +44,13 @@ class Engine:
         self._viewport = (self._viewport[0] + offset[0],
                           self._viewport[1] + offset[1])
 
+    def calc_offset(self, x, y):
+        """Return a position offset by the viewport"""
+        return x - self._viewport[0], y - self._viewport[1]
+
     @apply_events
     def update(self, dt):
-        return self._scene.update(self._world.proxy(), dt)
+        return self._scene.update(self._world.proxy(), self, dt)
 
     def draw(self):
         self._scene.draw(self._app.screen, self._viewport)
@@ -182,7 +186,7 @@ class Scene:
     def exit(self, world):
         pass
 
-    def update(self, world, dt):
+    def update(self, world, engine, dt):
         pass
 
     def draw(self, screen, viewport):
index a16e99f9fec60ba6427faa011dd511edf6411624..70dd7be23931d70ff129952598c525c7c1bf65fd 100644 (file)
@@ -21,13 +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)
+        self._doors = self.actors.add_layer("doors", level=9)
+        self._keypads = self.actors.add_layer("keypads", level=8)
         # These are already Actors
         for door in self._level.doors:
-            self._doors.append(door)
+            self._doors.add(door)
         for keypad in self._level.keypads:
-            self._keypads.append(keypads)
+            self._keypads.add(keypad)
 
     def _render(self, layer):
         # We cache the rendered surface to avoid doing a large number
@@ -45,6 +45,13 @@ class BaseLevelScene(Scene):
                 surface.blit(tile[layer_key], pos)
         return surface.convert_alpha()
 
+    def update(self, world, engine, dt):
+        """Fix the door and keypad positions"""
+        for door in self._doors:
+            door.pos = engine.calc_offset(door.game_pos[0] * TILE_SIZE, door.game_pos[1] * TILE_SIZE)
+        for keypad in self._keypads:
+            keypad.pos = engine.calc_offset(keypad.game_pos[0] * TILE_SIZE, keypad.game_pos[1] * TILE_SIZE)
+
     def draw(self, screen, viewport):
         screen.clear()
         # Viewport is the position of the screen relative to the
index 21fc37c0432e3f77a19f5830c4a2cee48f1f7e0c..982ff30f8ee956d9641a885136eec32250a0dedc 100644 (file)
@@ -160,7 +160,7 @@ class RoachesScene(Scene):
     def _eject_roach(self):
         print("Eject roach.")
 
-    def update(self, world, dt):
+    def update(self, world, engine, dt):
         self._update_inventory(world)
         self._update_roaches(world)