Mercurial > skaapsteker
changeset 239:6a2f366d5e62
Add facing support for attack animations
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Thu, 07 Apr 2011 14:51:05 +0200 |
parents | df306bfd632e |
children | 9adf5076f3a2 |
files | skaapsteker/sprites/base.py skaapsteker/sprites/enemies.py |
diffstat | 2 files changed, 42 insertions(+), 3 deletions(-) [+] |
line wrap: on
line diff
--- a/skaapsteker/sprites/base.py Thu Apr 07 14:50:17 2011 +0200 +++ b/skaapsteker/sprites/base.py Thu Apr 07 14:51:05 2011 +0200 @@ -44,19 +44,36 @@ wants_updates = True + facings = {} + def __init__(self, pos, **opts): Sprite.__init__(self) self._animations = dict((k, []) for k, r in self.animation_regexes) self._frame = 0 self._tick = 0 # TODO: hack to show some animation; kill shortly self._animation = self.animation_regexes[0][0] + if self.facings and self._animation in self.facings: + self.facing = self.facings[self._animation][0][0] + else: + self.facing = None for image in data.get_files(self.image_dir): for name, pattern in self.animation_regexes: if re.match(pattern, image): img = data.load_image("%s/%s" % (self.image_dir, image)) - collide_rect = img.get_bounding_rect(1).inflate(-2,-2) - self._animations[name].append((img, collide_rect)) + if self.facings and name in self.facings: + if not self._animations[name]: + self._animations[name] = dict((k, []) for k, t in self.facings[name]) + for facing, transform in self.facings[name]: + if transform: + mod_img = transform(img) + else: + mod_img = img + collide_rect = mod_img.get_bounding_rect(1).inflate(-2,-2) + self._animations[name][facing].append((mod_img, collide_rect)) + else: + collide_rect = img.get_bounding_rect(1).inflate(-2,-2) + self._animations[name].append((img, collide_rect)) self.collide_rect = Rect((0, 0), (2, 2)) self.collide_rect.midbottom = (pos[0]*TILE_SIZE[0]+TILE_SIZE[0]/2, (pos[1]+1)*TILE_SIZE[1]) @@ -64,7 +81,10 @@ self.setup(**opts) def _update_image(self): - images = self._animations[self._animation] + if self.facing: + images = self._animations[self._animation][self.facing] + else: + images = self._animations[self._animation] if self._frame >= len(images): self._frame = 0 cand_image, cand_collide_rect = images[self._frame] @@ -125,6 +145,7 @@ # FIXME: This will need to change when AnimatedGameSprite changes # We've just looped through the animation sequence self._animation = self._old_state + self.facing = self._old_facing elif self._frame == self.attack_frame and self._tick == 5: # Attack the player self.do_attack() @@ -140,6 +161,7 @@ elif self.attack_frame is not None: self._target = player self._old_state = self._animation + self._old_facing = self.facing self._animation = 'attacking' self._tick = 1 self._frame = 0 # Start the attack from the beginning
--- a/skaapsteker/sprites/enemies.py Thu Apr 07 14:50:17 2011 +0200 +++ b/skaapsteker/sprites/enemies.py Thu Apr 07 14:51:05 2011 +0200 @@ -1,4 +1,5 @@ from base import Monster +from pygame import transform class Dummy(Monster): @@ -7,5 +8,21 @@ attack_frame = 1 attack_damage = 10 + facings = { + 'running' : (('left', None), + ('right', lambda x: transform.flip(x, True, False))), + 'attacking' : (('left', None), + ('right', lambda x: transform.flip(x, True, False))), + } + def setup(self, direction): self.facing = direction + + def start_attack(self, player): + if self._animation != 'attacking': + # Turn to face the player we're attacking + if player.collide_rect.centerx > self.collide_rect.centerx: + self.facing = 'right' + else: + self.facing = 'left' + Monster.start_attack(self, player)