Steps towards switching views between the level layers
authorNeil <neil@dip.sun.ac.za>
Wed, 2 Mar 2016 21:55:18 +0000 (23:55 +0200)
committerNeil <neil@dip.sun.ac.za>
Thu, 3 Mar 2016 08:34:37 +0000 (10:34 +0200)
koperkapel/loaders/levelloader.py
koperkapel/scenes/level.py
koperkapel/scenes/viewlevel.py

index 839c171e8939bcfe3bc450cb2791a6067f9dfd87..699b520c06884f3fd293212569aa8ca7dd99f0c4 100644 (file)
@@ -73,7 +73,8 @@ class LevelLoader(ResourceLoader):
         """Load all the tile images"""
         for row_data in self._tiles:
             for tile in row_data:
-                tile['image'] = TILES[tile['base']].image()
+                for layer in ['floor', 'tunnels']:
+                    tile['%s image' % layer] = TILES[tile[layer]['base']].image()
 
 
 levels = LevelLoader('levels')
index 5c86629078551df0d69459b20d61b77b08093ffa..620e290deadfa894b749fda109af250952a85de1 100644 (file)
@@ -2,6 +2,7 @@
 
 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 ..constants import TILE_SIZE
@@ -13,22 +14,28 @@ class LevelScene(Scene):
     def enter(self, world):
         self._level_data = levels.load(world.level.name)
         self._tiles = self._level_data['tiles']
-        self._surface = None
-        self._render()
+        self._layer = 'floor'
+        self._surfaces = {}
+        self._overlay = {}
+        for layer in ['floor', 'tunnels']:
+            self._surfaces[layer] = self._render(layer)
+        self._overlay = self._surfaces['floor'].copy()
 
-    def _render(self):
+    def _render(self, layer):
         # We cache the rendered surface to avoid doing a large number
         # of blits each frame, as that introduces a large performance
         # overhead.
-        self._surface = Surface((len(self._tiles[0]) * TILE_SIZE,
-                                 len(self._tiles) * TILE_SIZE))
+        surface = Surface((len(self._tiles[0]) * TILE_SIZE,
+                           len(self._tiles) * TILE_SIZE))
+        layer_key = '%s image' % layer
         for y, row in enumerate(self._tiles):
             for x, tile in enumerate(row):
                 pos = (x * TILE_SIZE, y * TILE_SIZE)
-                if 'image' not in tile:
+                if layer_key not in tile:
                     # Skip broken tiles for now
                     continue
-                self._surface.blit(tile['image'], pos)
+                surface.blit(tile[layer_key], pos)
+        return surface.convert_alpha()
 
     def draw(self, screen, viewport):
         screen.clear()
@@ -36,7 +43,17 @@ class LevelScene(Scene):
         # surface. We need the position of the surface relative to
         # the screen for the blit, so this conversion
         screen_pos = -viewport[0], -viewport[1]
-        screen.blit(self._surface, screen_pos)
+        if self._layer == 'floor':
+            screen.blit(self._surfaces[self._layer], screen_pos)
+        else:
+            # blit tunnels, with translucent overlay
+            # We need to call pygame.Surface.blit ourselves,
+            # since pgzero's screen blit hides the blend flags
+            # from us
+            tunnels = self._surfaces[self._layer].copy()
+            tunnels.blit(self._overlay, (0, 0),
+                         special_flags=pgl.BLEND_MULT)
+            screen.blit(tunnels, screen_pos)
 
     def on_key_down(self, key, mod, unicode):
         if key == keys.ESCAPE:
index 46b8a002c365abf5efeb2e6726e628cfda5a6f8c..88e9217444a0fc64849a58763172131a514f73de 100644 (file)
@@ -19,6 +19,12 @@ class ViewLevelScene(LevelScene):
             offset = (-TILE_SIZE, 0)
         elif key == keys.RIGHT:
             offset = (TILE_SIZE, 0)
+        elif key == keys.S:
+            if self._layer == 'floor':
+                self._layer = 'tunnels'
+            else:
+                self._layer = 'floor'
+            return
         if offset:
             return [MoveViewportEvent(offset)]
         return super(ViewLevelScene, self).on_key_down(key, mod, unicode)