A roach wanders into the world
authorNeil <neil@dip.sun.ac.za>
Fri, 4 Mar 2016 22:09:53 +0000 (00:09 +0200)
committerNeil <neil@dip.sun.ac.za>
Fri, 4 Mar 2016 22:10:07 +0000 (00:10 +0200)
koperkapel/scenes/level.py

index 82fff6581665f43c12b4124a926ee3634db8c0b1..a800ba0d842d0f5c3d130b16acbef92f6c0aa958 100644 (file)
@@ -4,8 +4,9 @@ from pgzero.constants import keys
 from pygame import Surface
 import pygame.locals as pgl
 from ..loaders.levelloader import levels
-from .base import Scene, ChangeSceneEvent
+from .base import Scene, ChangeSceneEvent, MoveViewportEvent
 from ..constants import TILE_SIZE, WIDTH, HEIGHT
+from ..roaches import default_roaches
 
 
 class BaseLevelScene(Scene):
@@ -48,6 +49,7 @@ class BaseLevelScene(Scene):
             screen.surface.blit(self._overlay, (0, 0),
                          area=(viewport[0], viewport[1], WIDTH, HEIGHT), 
                          special_flags=pgl.BLEND_MULT)
+        self.actors.draw(screen)
 
     def on_key_down(self, key, mod, unicode):
         if key == keys.ESCAPE:
@@ -56,4 +58,53 @@ class BaseLevelScene(Scene):
 
 
 class GameLevelScene(BaseLevelScene):
-    pass
+
+    def enter(self, world):
+        super().enter(world)
+        self._roaches = self.actors.add_layer("roaches", level=10)
+        self._init_roaches(world.roaches)
+
+    def _init_roaches(self, roaches):
+        x, y = self._level.start_pos
+        for roach in roaches:
+            roach_actor = self._roaches.add(default_roaches.assemble(roach))
+            roach_actor.anchor = (0, 0)
+            roach_actor.pos = (x * TILE_SIZE, y * TILE_SIZE)
+        self._set_pos(x, y)
+        self._level_layer = 'floor'
+
+    def _set_pos(self, x, y):
+        self._player_pos = (x, y)
+
+    def on_key_down(self, key, mod, unicode):
+        offset = None
+        x, y = self._player_pos
+        if key == keys.DOWN:
+            if self._level.can_walk(x, y + 1, self._level_layer):
+                self._set_pos(x, y + 1)
+                offset = (0, TILE_SIZE)
+        elif key == keys.UP:
+            if self._level.can_walk(x, y - 1, self._level_layer):
+                self._set_pos(x, y - 1)
+                offset = (0, -TILE_SIZE)
+        elif key == keys.LEFT:
+            if self._level.can_walk(x - 1, y, self._level_layer):
+                self._set_pos(x - 1, y)
+                offset = (-TILE_SIZE, 0)
+        elif key == keys.RIGHT:
+            if self._level.can_walk(x + 1, y, self._level_layer):
+                self._set_pos(x + 1, y)
+                offset = (TILE_SIZE, 0)
+        elif key == keys.S:
+            if self._level_layer == 'floor':
+                if self._level.can_walk(x, y, 'tunnels'):
+                    self._level_layer = 'tunnels'
+            elif self._level.can_walk(x, y, 'floor'):
+                # Must be in the tunnels already
+                self._level_layer = 'floor'
+            return
+        if offset:
+            return [MoveViewportEvent(offset)]
+        return super(GameLevelScene, self).on_key_down(key, mod, unicode)
+        
+