Hack keyboard behaviour to be different. Also incidently allow rotating in place
authorNeil <neil@dip.sun.ac.za>
Sat, 5 Mar 2016 19:05:07 +0000 (21:05 +0200)
committerNeil <neil@dip.sun.ac.za>
Sat, 5 Mar 2016 19:05:18 +0000 (21:05 +0200)
koperkapel/scenes/level.py

index a1515997dc48d470044925e0193495cc904d4add..606a0db038b1dae954e1d51d57e6826c862ab24b 100644 (file)
@@ -84,6 +84,7 @@ class BaseLevelScene(Scene):
 class GameLevelScene(BaseLevelScene):
 
     def enter(self, world):
+        self._held_keys = set()
         if self._level is not None:
             for generator in self._generators:
                 generator.unpause()
@@ -100,6 +101,8 @@ class GameLevelScene(BaseLevelScene):
         self._init_items()
         self._init_friendly_roaches()
         self._init_generators()
+        self._key_rate = 0.2
+        self._last_key_down = 0
         return self._init_roaches(world)
 
     def _init_items(self):
@@ -179,9 +182,20 @@ class GameLevelScene(BaseLevelScene):
         for item in self._items:
             item.pos = self.calc_offset(
                 item.game_pos[0] * TILE_SIZE, item.game_pos[1] * TILE_SIZE)
+        more = self._check_held_keys(dt)
+        if more:
+            events.extend(more)
         return events
 
-    def _movement_key(self, key):
+    def _check_held_keys(self, dt):
+        for key in self._held_keys:
+            self._last_key_down += dt
+            if key in (keys.DOWN, keys.UP, keys.LEFT, keys.RIGHT):
+                return self._movement_key(key, dt)
+            elif key == keys.X:
+                return self._fire_key(dt)
+
+    def _movement_key(self, key, dt):
         x, y = self._player_pos
         for k, dp, angle in (
             (keys.DOWN, (0, 1), 180),
@@ -190,12 +204,17 @@ class GameLevelScene(BaseLevelScene):
             (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])
+                if self._angle == angle and self._last_key_down > self._key_rate:
+                    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)
+                        self._last_key_down = 0
+                        return [MoveViewportEvent(offset)]
+                else:
+                    # just turn
                     self._set_angle(angle)
-                    return [MoveViewportEvent(offset)]
 
     def _activate_key(self):
         x, y = self._player_pos
@@ -219,8 +238,10 @@ class GameLevelScene(BaseLevelScene):
             next_level = self._level.get_exit_level()
             return [WorldEvent('set', {'level.name': next_level}), ChangeSceneEvent(GameLevelScene())]
 
-    def _fire_key(self):
-        print('Boom')
+    def _fire_key(self, dt):
+        if self._last_key_down > self._key_rate:
+            self._last_key_down = 0
+            print('Boom')
 
     def _vehicle_management_key(self):
         from .roach_management import RoachesScene
@@ -229,13 +250,24 @@ class GameLevelScene(BaseLevelScene):
     def on_key_down(self, key, mod, unicode):
         x, y = self._player_pos
         if key in (keys.DOWN, keys.UP, keys.LEFT, keys.RIGHT):
-            return self._movement_key(key)
+            self._held_keys.clear()
+            self._held_keys.add(key)
+            # We do this so pressing the key has an instant effect, and can then be held
+            self._last_key_down = self._key_rate + 0.01
+            return self._movement_key(key, 0.01)
         elif key == keys.C:
             return self._activate_key()
         elif key == keys.X:
-            return self._fire_key()
+            self._held_keys.clear()
+            self._held_keys.add(key)
+            self._last_key_down = self._key_rate + 0.01
+            return self._fire_key(0.01)
         elif key == keys.V:
             return self._change_vehicle()
         elif key == keys.Z:
             return self._vehicle_management_key()
         return super(GameLevelScene, self).on_key_down(key, mod, unicode)
+
+    def on_key_up(self, key, mode):
+        self._held_keys.discard(key)
+        self._last_key_down = 0