From 9aca3c6140fda947a208d75ffb2390b2a5e88939 Mon Sep 17 00:00:00 2001 From: Simon Cross Date: Sat, 5 Mar 2016 19:18:46 +0200 Subject: [PATCH] Refactor giant on_key_down. --- koperkapel/scenes/level.py | 95 +++++++++++++++++++------------------- 1 file changed, 48 insertions(+), 47 deletions(-) diff --git a/koperkapel/scenes/level.py b/koperkapel/scenes/level.py index 31e1269..a17de55 100644 --- a/koperkapel/scenes/level.py +++ b/koperkapel/scenes/level.py @@ -153,57 +153,58 @@ class GameLevelScene(BaseLevelScene): friend.game_pos[0] * TILE_SIZE, friend.game_pos[1] * TILE_SIZE) return events + def _movement_key(self, key): + x, y = self._player_pos + for k, dp, angle in ( + (keys.DOWN, (0, 1), 180), + (keys.UP, (0, -1), 0), + (keys.LEFT, (-1, 0), 90), + (keys.RIGHT, (1, 0), -90), + ): + if key == k: + nx, ny = x + dp[0], y + dp[1] + if self._can_move(nx, ny): + self._set_pos(nx, ny) + offset = (TILE_SIZE * dp[0], TILE_SIZE * dp[1]) + self._set_angle(angle) + return [MoveViewportEvent(offset)] + + def _activate_key(self): + x, y = self._player_pos + if self._level.is_grate(x, y): + if (self._level_layer == 'floor' and + self._level.can_crawl(x, y, 'floor')): + if self._level.can_crawl(x, y, 'tunnels'): + self._level_layer = 'tunnels' + self._mode = 'crawl' + elif self._level.can_crawl(x, y, 'floor'): + # Must be in the tunnels already + self._level_layer = 'floor' + self._mode = 'walk' + elif self._level.is_keypad(x, y): + self._level.press_keypad(x, y, self._roaches) + elif self._level.is_on_friend(x, y): + friend = self._level.remove_friend(x, y) + self._friends.remove(friend) + self._add_roach() + + def _fire_key(self): + print('Boom') + + def _vehicle_management_key(self): + from .roach_management import RoachesScene + return [ChangeSceneEvent(RoachesScene(level_scene=self))] + def on_key_down(self, key, mod, unicode): - offset = None x, y = self._player_pos - if key == keys.DOWN: - if self._can_move(x, y + 1): - self._set_pos(x, y + 1) - offset = (0, TILE_SIZE) - self._set_angle(180) - elif key == keys.UP: - if self._can_move(x, y - 1): - self._set_pos(x, y - 1) - offset = (0, -TILE_SIZE) - self._set_angle(0) - elif key == keys.LEFT: - if self._can_move(x - 1, y): - self._set_pos(x - 1, y) - offset = (-TILE_SIZE, 0) - self._set_angle(90) - elif key == keys.RIGHT: - if self._can_move(x + 1, y): - self._set_pos(x + 1, y) - offset = (TILE_SIZE, 0) - self._set_angle(270) + if key in (keys.DOWN, keys.UP, keys.LEFT, keys.RIGHT): + return self._movement_key(key) elif key == keys.C: - # Activate - if self._level.is_grate(x, y): - if self._level_layer == 'floor' and self._level.can_crawl(x, y, 'floor'): - if self._level.can_crawl(x, y, 'tunnels'): - self._level_layer = 'tunnels' - self._mode = 'crawl' - elif self._level.can_crawl(x, y, 'floor'): - # Must be in the tunnels already - self._level_layer = 'floor' - self._mode = 'walk' - elif self._level.is_keypad(x, y): - self._level.press_keypad(x, y, self._roaches) - elif self._level.is_on_friend(x, y): - friend = self._level.remove_friend(x, y) - self._friends.remove(friend) - self._add_roach() - return + return self._activate_key() elif key == keys.X: - # Fire - print('Boom') + return self._fire_key() elif key == keys.V: - # Leave vehicle - self._change_vehicle() + return self._change_vehicle() elif key == keys.Z: - # Vehicle management - from .roach_management import RoachesScene - return[ChangeSceneEvent(RoachesScene(level_scene=self))] - if offset: - return [MoveViewportEvent(offset)] + return self._vehicle_management_key() return super(GameLevelScene, self).on_key_down(key, mod, unicode) -- 2.34.1