Bullets move.
authorSimon Cross <hodgestar@gmail.com>
Sat, 5 Mar 2016 21:34:08 +0000 (23:34 +0200)
committerSimon Cross <hodgestar@gmail.com>
Sat, 5 Mar 2016 21:34:08 +0000 (23:34 +0200)
koperkapel/gamelib/level.py
koperkapel/scenes/level.py

index 34487af920d67e5ce59f97ddc5b8d0d1bb085fa2..cfbf45e1118c824f63cddac3294a09c9283419f0 100644 (file)
@@ -46,6 +46,14 @@ class Level(object):
     def can_crawl(self, x, y, layer):
         return 'crawl' in self.tiles[y][x][layer]['behaviour']
 
+    def can_bullet(self, x, y, layer):
+        if self.tiles[y][x][layer]['behaviour']:
+            for door in self.doors:
+                if (x, y) == door.game_pos and door.is_closed():
+                    return False
+            return True
+        return False
+
     def is_keypad(self, x, y):
         for keypad in self.keypads:
             if (x, y) == keypad.game_pos:
index 5db95bb2d3070debfb893439a05e489486455e9b..b17beb61231f1e2d817fe411d529e8e52eb535bc 100644 (file)
@@ -199,8 +199,21 @@ class GameLevelScene(BaseLevelScene):
         if len(self._bullets) >= 10:
             return
         bullet.game_pos = pos
+        bullet.game_dp = dp
+        bullet.dt = 0
+        bullet.level_layer = self._level_layer
         self._bullets.add(bullet)
 
+    def _update_bullet(self, bullet, dt):
+        bullet.dt += dt
+        if bullet.dt > 0.1:
+            bullet.dt = 0
+            bullet.game_pos = pos = (
+                bullet.game_pos[0] + bullet.game_dp[0],
+                bullet.game_pos[1] + bullet.game_dp[1])
+            if not self._level.can_bullet(pos[0], pos[1], bullet.level_layer):
+                self._bullets.remove(bullet)
+
     @defer_to_update
     def _fire_weapon(self, world):
         weapon = weapon_by_name(world.weapons.current)
@@ -235,7 +248,8 @@ class GameLevelScene(BaseLevelScene):
         for enemy in self._enemies:
             enemy.pos = self.calc_offset(
                 enemy.game_pos[0] * TILE_SIZE, enemy.game_pos[1] * TILE_SIZE)
-        for bullet in self._bullets:
+        for bullet in list(self._bullets):
+            self._update_bullet(bullet, dt)
             bullet.pos = self.calc_offset(
                 bullet.game_pos[0] * TILE_SIZE + (TILE_SIZE // 2),
                 bullet.game_pos[1] * TILE_SIZE + (TILE_SIZE // 2))