changeset 204:0f9715a2f07b

Make fire 1 do the attacking animation
author Neil Muller <drnlmuller@gmail.com>
date Wed, 06 Apr 2011 22:49:25 +0200
parents 0a793c4ac341
children 466147799786
files skaapsteker/sprites/player.py
diffstat 1 files changed, 30 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/skaapsteker/sprites/player.py	Wed Apr 06 22:30:38 2011 +0200
+++ b/skaapsteker/sprites/player.py	Wed Apr 06 22:49:25 2011 +0200
@@ -24,6 +24,7 @@
         self._animation_frame = 0.0
         self._last_time = time.time()
         # State flags and such
+        self.attacking = 0
         self.running = False
         self.jumping = False
         self.flying = False
@@ -47,7 +48,6 @@
             cur_pos = self.collide_rect.midbottom
         else:
             cur_pos = (0, 0)
-
         # TODO: can save a lot of calculation here by caching collision rects
         cand_image = images[int(self._animation_frame)]
         cand_collide_rect = cand_image.get_bounding_rect(1).inflate(-2,-2)
@@ -55,9 +55,9 @@
         cand_rect_offset = cand_rect.centerx - cand_collide_rect.centerx, cand_rect.bottom - cand_collide_rect.bottom
         cand_rect.midbottom = cur_pos[0] + cand_rect_offset[0], cur_pos[1] + cand_rect_offset[1]
         cand_collide_rect.midbottom = cur_pos
-        if not self.check_collide_rect(cand_collide_rect, cand_rect, cand_image):
+        # We always allow the attacking animation frames
+        if not self.check_collide_rect(cand_collide_rect, cand_rect, cand_image) and not self.attacking:
             return
-
         self.image = cand_image
         self.collide_rect = cand_collide_rect
         self.rect = cand_rect
@@ -67,20 +67,29 @@
     def update(self):
         v_x, v_y = self.velocity
         # Never animate slower than !7 fps, never faster than ~15 fps
-        old_frame = self._animation_frame
-        self._animation_frame += abs(v_x) / 300
-        time_diff = time.time() - self._last_time
-        if int(self._animation_frame) - int(old_frame) > 0:
-            # Check time diff
-            if time_diff < 0.10:
-                # Delay animation frame jump
-                self._animation_frame -= abs(v_x) / 300
+        if self.attacking > 0:
+            if self._last_time:
+                if time.time() - self._last_time > 0.15:
+                    self._animation_frame += 1
+                    self.attacking -= 1
+                    self._last_time = time.time()
             else:
                 self._last_time = time.time()
-        elif time_diff > 0.20:
-            # Force animation frame jump
-            self._animation_frame = old_frame + 1
-            self._last_time = time.time()
+        else:
+            old_frame = self._animation_frame
+            self._animation_frame += abs(v_x) / 300
+            time_diff = time.time() - self._last_time
+            if int(self._animation_frame) - int(old_frame) > 0:
+                # Check time diff
+                if time_diff < 0.10:
+                    # Delay animation frame jump
+                    self._animation_frame -= abs(v_x) / 300
+                else:
+                    self._last_time = time.time()
+            elif time_diff > 0.20:
+                # Force animation frame jump
+                self._animation_frame = old_frame + 1
+                self._last_time = time.time()
         if abs(v_x) < 80:
             # Clamp when we're not moving at least 5 pixel / s
             self.velocity = (0, v_y)
@@ -165,12 +174,18 @@
         self.deltav((0.0, 100.0))
 
     def action_fire1(self):
+        if self.tails < 2:
+            # Only have a bite attack
+            print 'attacking'
+            self.attacking = 2
         print "F1"
 
     def action_fire2(self):
         print "F2"
 
     def _get_action(self):
+        if self.attacking:
+            return 'attacking'
         if self.running:
             return 'running'
         if self.jumping: