From: Neil Date: Wed, 2 Mar 2016 21:55:18 +0000 (+0200) Subject: Steps towards switching views between the level layers X-Git-Url: https://git.ctpug.org.za/?a=commitdiff_plain;h=3c18a0b415092a835678e04ed352b6cb7f0f7e08;p=koperkapel.git Steps towards switching views between the level layers --- diff --git a/koperkapel/loaders/levelloader.py b/koperkapel/loaders/levelloader.py index 839c171..699b520 100644 --- a/koperkapel/loaders/levelloader.py +++ b/koperkapel/loaders/levelloader.py @@ -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') diff --git a/koperkapel/scenes/level.py b/koperkapel/scenes/level.py index 5c86629..620e290 100644 --- a/koperkapel/scenes/level.py +++ b/koperkapel/scenes/level.py @@ -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: diff --git a/koperkapel/scenes/viewlevel.py b/koperkapel/scenes/viewlevel.py index 46b8a00..88e9217 100644 --- a/koperkapel/scenes/viewlevel.py +++ b/koperkapel/scenes/viewlevel.py @@ -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)