changeset 390:93f13f7d97f2

Initial fire rate-limiting
author Stefano Rivera <stefano@rivera.za.net>
date Sat, 09 Apr 2011 16:18:08 +0200
parents 64d8e49e9a86
children d5381be5079f
files skaapsteker/constants.py skaapsteker/levelscene.py skaapsteker/sprites/player.py
diffstat 3 files changed, 52 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/skaapsteker/constants.py	Sat Apr 09 16:10:39 2011 +0200
+++ b/skaapsteker/constants.py	Sat Apr 09 16:18:08 2011 +0200
@@ -17,6 +17,8 @@
 # This is for both key down time and for gap between taps
 DOUBLE_TAP_TIME = 0.15
 
+RECHARGE_TIME = 1
+
 # Layer defination
 class Layers(object):
     BACKGROUND = 0  # Absolute background
@@ -28,10 +30,6 @@
 
 class FoxHud(object):
     TEXT = pygame.Color(255, 255, 255, 196)
-    HEALTH_BACKGROUND = pygame.Color(128, 64, 0, 128)
-    HEALTH_FOREGROUND = pygame.Color(255, 64, 0, 196)
-    HEALTH_HEIGHT = 160
-    HEALTH_WIDTH = 20
 
     BG_ALPHA = 120
     BG_MARGIN = 8
@@ -54,3 +52,16 @@
     TAILS_WIDTH = 51
     TAILS_HEIGHT = 8 * TAIL_OFFSET
 
+    HEALTH_BACKGROUND = pygame.Color(128, 64, 0, 128)
+    HEALTH_FOREGROUND = pygame.Color(255, 64, 0, 196)
+    HEALTH_HEIGHT = 160
+    HEALTH_WIDTH = 20
+
+    RECHARGE_BACKGROUND = pygame.Color(64, 128, 0, 128)
+    RECHARGE_FOREGROUND = pygame.Color(64, 255, 0, 196)
+    RECHARGE_TOP = 220
+    RECHARGE_WIDTH = 50
+    RECHARGE_HEIGHT = 15
+
+    SCROLL_TOP = 240
+    TOFU_TOP = 270
--- a/skaapsteker/levelscene.py	Sat Apr 09 16:10:39 2011 +0200
+++ b/skaapsteker/levelscene.py	Sat Apr 09 16:18:08 2011 +0200
@@ -199,14 +199,6 @@
         fox = self.game_state.world.fox
         fox_hud = constants.FoxHud
 
-        # Draw the health bar
-        health_bottom = self._clip_rect.right - 30, self._clip_rect.top + 200
-        bar = pygame.Rect(0, 0, constants.FoxHud.HEALTH_WIDTH, constants.FoxHud.HEALTH_HEIGHT)
-        bar.bottomleft = health_bottom
-        pygame.draw.rect(self._level_surface, constants.FoxHud.HEALTH_BACKGROUND, bar)
-        bar.height = int(constants.FoxHud.HEALTH_HEIGHT * float(max(0, fox.cur_health))/fox.max_health)
-        bar.bottomleft = health_bottom
-        pygame.draw.rect(self._level_surface, constants.FoxHud.HEALTH_FOREGROUND, bar)
         # Inventory bg
         bgsurf = pygame.Surface((fox_hud.INVENTORY_SIZE + 2 * 8,
                                  fox_hud.INVENTORY_SIZE + 2 * 8),
@@ -229,6 +221,7 @@
             if inv_pos.height < fox_hud.INVENTORY_SIZE:
                 inv_pos.top += (fox_hud.INVENTORY_SIZE - inv_pos.height) / 2
             self._level_surface.blit(self._player.inventory_image, inv_pos)
+
         # Tail bg
         bgsurf = pygame.Surface((self._tail.get_size()[0] + 2 * fox_hud.BG_MARGIN,
                                  fox_hud.TAILS_HEIGHT + fox_hud.BG_MARGIN),
@@ -246,23 +239,38 @@
             tail_pos = self._clip_rect.left + 8, self._clip_rect.top + fox_hud.TAIL_POSITIONS[tail]
             self._level_surface.blit(self._tail, tail_pos)
 
-        # Draw scroll count
+        # Draw the health bar
+        health_bottom = self._clip_rect.right - 30, self._clip_rect.top + 200
+        bar = pygame.Rect(0, 0, fox_hud.HEALTH_WIDTH, fox_hud.HEALTH_HEIGHT)
+        bar.bottomleft = health_bottom
+        pygame.draw.rect(self._level_surface, fox_hud.HEALTH_BACKGROUND, bar)
+        bar.height = int(fox_hud.HEALTH_HEIGHT * float(max(0, fox.cur_health))/fox.max_health)
+        bar.bottomleft = health_bottom
+        pygame.draw.rect(self._level_surface, fox_hud.HEALTH_FOREGROUND, bar)
 
-        pos = self._clip_rect.right - 20, self._clip_rect.top + 225
+        # Draw fire recharge bar
+        recharge = pygame.Rect(self._clip_rect.right - 55,
+                               self._clip_rect.top + fox_hud.RECHARGE_TOP,
+                               fox_hud.RECHARGE_WIDTH, fox_hud.RECHARGE_HEIGHT)
+        pygame.draw.rect(self._level_surface, fox_hud.RECHARGE_BACKGROUND, recharge)
+        recharge.width = int(fox_hud.RECHARGE_WIDTH * self._player.recharge_level())
+        pygame.draw.rect(self._level_surface, fox_hud.RECHARGE_FOREGROUND, recharge)
+
+        # Draw scroll count
+        pos = self._clip_rect.right - 20, self._clip_rect.top + fox_hud.SCROLL_TOP + 5
         count = Text("%s" % len(fox.scrolls), pos)
         count.draw(self._level_surface)
-        pos = self._clip_rect.right - 55, self._clip_rect.top + 220
+        pos = self._clip_rect.right - 55, self._clip_rect.top + fox_hud.SCROLL_TOP
         self._level_surface.blit(self._scroll, pos)
 
         # Draw tofu count
-        pos = self._clip_rect.right - 20, self._clip_rect.top + 255
+        pos = self._clip_rect.right - 20, self._clip_rect.top + fox_hud.TOFU_TOP + 5
         count = Text("%s" % fox.tofu, pos)
         count.draw(self._level_surface)
-        pos = self._clip_rect.right - 55, self._clip_rect.top + 250
+        pos = self._clip_rect.right - 55, self._clip_rect.top + fox_hud.TOFU_TOP
         self._level_surface.blit(self._tofu, pos)
 
 
-
     def _update_clip_rect(self):
         cr = self._clip_rect
         lr = self._level_surface.get_rect()
--- a/skaapsteker/sprites/player.py	Sat Apr 09 16:10:39 2011 +0200
+++ b/skaapsteker/sprites/player.py	Sat Apr 09 16:18:08 2011 +0200
@@ -6,7 +6,7 @@
 from ..sprites.base import find_sprite, Monster, TILE_SIZE, PC_LAYER, MONSTER_LAYER, PROJECTILE_LAYER
 from ..sprites.projectiles import Fireball, Lightning
 from ..physics import Sprite
-from ..constants import Layers, FoxHud, DOUBLE_TAP_TIME
+from ..constants import Layers, FoxHud, DOUBLE_TAP_TIME, RECHARGE_TIME
 from ..data import get_files, load_image
 from ..engine import PlayerDied, AddSpriteEvent
 
@@ -29,6 +29,7 @@
         self._soundsystem.load_sound('yelp', 'sounds/yelp.ogg')
         self._animation_frame = 0.0
         self._last_time = time.time()
+        self._last_fired = time.time()
         # State flags and such
         self.attacking = 0
         self.running = False
@@ -299,17 +300,30 @@
         self._launch_projectile(Lightning)
 
     def action_fire1(self):
+        if not self.check_fire_rate():
+            return
         if "fireball" not in self._me.tails:
             self._bite_attack()
         else:
             self._fireball_attack()
 
     def action_fire2(self):
+        if not self.check_fire_rate():
+            return
         if "lightning" not in self._me.tails:
             self._bite_attack()
         else:
             self._lightning_attack()
 
+    def check_fire_rate(self):
+        if self.recharge_level() < 1:
+            return False
+        self._last_fired = time.time()
+        return True
+
+    def recharge_level(self):
+        return min((time.time() - self._last_fired) / RECHARGE_TIME, 1)
+
     def _get_action(self):
         if self.attacking:
             return 'attacking'