changeset 236:9528c6fc7f75

Hook up attack anaimation (needs facing support still)
author Neil Muller <drnlmuller@gmail.com>
date Thu, 07 Apr 2011 12:38:45 +0200
parents 169bacba9f48
children e06c54d7701f
files skaapsteker/sprites/base.py skaapsteker/sprites/enemies.py
diffstat 2 files changed, 40 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/skaapsteker/sprites/base.py	Thu Apr 07 12:38:22 2011 +0200
+++ b/skaapsteker/sprites/base.py	Thu Apr 07 12:38:45 2011 +0200
@@ -39,6 +39,7 @@
         # TODO: swap back once we know how to swap
         ("running", r"^.*_\d+.png$"),
         ("standing", r"^.*_standing.png$"),
+        ("attacking", r"^.*_attacking.png$"),
     ]
 
     wants_updates = True
@@ -76,7 +77,7 @@
         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):
+        if not self.check_collide_rect(cand_collide_rect, cand_rect, cand_image) and not self._animation == 'attacking':
             return
 
         self.image = cand_image
@@ -93,7 +94,7 @@
         self._tick += 1
 
 
-class Monster(GameSprite):
+class Monster(AnimatedGameSprite):
 
     collision_layer = MONSTER_LAYER
     collides_with = set([PC_LAYER])
@@ -102,8 +103,11 @@
 
     block = True
 
+    attack_frame = None  # Mark a spefici frame in the animatio n as when the attack lands
+    attack_damage = 1
+
     def __init__(self, pos, **opts):
-        GameSprite.__init__(self, pos, **opts)
+        AnimatedGameSprite.__init__(self, pos, **opts)
         self.floor_rect = Rect(self.collide_rect.topleft, (self.collide_rect.width, 2))
         self._layer = Layers.PLAYER
         self.health = 10
@@ -112,7 +116,35 @@
 
     def collided_player(self, player):
         print "%s collided with player" % self
-        player.damage(1)
+        self.start_attack(player)
+
+    def update(self):
+        AnimatedGameSprite.update(self)
+        if self._animation == 'attacking':
+            if self._frame == 0 and self._tick == 1:
+                # FIXME: This will need to change when AnimatedGameSprite changes
+                # We've just looped through the animation sequence
+                self._animation = self._old_state
+            elif self._frame == self.attack_frame and self._tick == 5:
+                # Attack the player
+                self.do_attack()
+
+    def do_attack(self):
+        """Overriden by monster classes"""
+        if self.check_collides(self._target):
+            self._target.damage(self.attack_damage)
+
+    def start_attack(self, player):
+        if self._animation == 'attacking':
+            return # We're already attacking
+        elif self.attack_frame is not None:
+            self._target = player
+            self._old_state = self._animation
+            self._animation = 'attacking'
+            self._tick = 1
+            self._frame = 0  # Start the attack from the beginning
+        else:
+            player.damage(1)  # collision damage
 
     def damage(self, damage):
         print 'Damaged by ', damage
--- a/skaapsteker/sprites/enemies.py	Thu Apr 07 12:38:22 2011 +0200
+++ b/skaapsteker/sprites/enemies.py	Thu Apr 07 12:38:45 2011 +0200
@@ -2,7 +2,10 @@
 
 
 class Dummy(Monster):
-    image_file = 'oni red/oni-red-01.png'
+    image_dir = 'sprites/oni red'
+
+    attack_frame = 1
+    attack_damage = 10
 
     def setup(self, direction):
         self.facing = direction