Changeset 239:6a2f366d5e62


Ignore:
Timestamp:
04/07/11 12:51:05 (12 years ago)
Author:
Neil Muller <drnlmuller@…>
Branch:
default
Phase:
public
Rebase:
33656166356637313539333037343965323438363839663761323463336332353163633231663031
Message:

Add facing support for attack animations

Location:
skaapsteker/sprites
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • skaapsteker/sprites/base.py

    r236 r239  
    4545    wants_updates = True
    4646
     47    facings = {}
     48
    4749    def __init__(self, pos, **opts):
    4850        Sprite.__init__(self)
     
    5153        self._tick = 0 # TODO: hack to show some animation; kill shortly
    5254        self._animation = self.animation_regexes[0][0]
     55        if self.facings and self._animation in self.facings:
     56            self.facing = self.facings[self._animation][0][0]
     57        else:
     58            self.facing = None
    5359
    5460        for image in data.get_files(self.image_dir):
     
    5662                if re.match(pattern, image):
    5763                    img = data.load_image("%s/%s" % (self.image_dir, image))
    58                     collide_rect = img.get_bounding_rect(1).inflate(-2,-2)
    59                     self._animations[name].append((img, collide_rect))
     64                    if self.facings and name in self.facings:
     65                        if not self._animations[name]:
     66                            self._animations[name] = dict((k, []) for k, t in self.facings[name])
     67                        for facing, transform in self.facings[name]:
     68                            if transform:
     69                                mod_img = transform(img)
     70                            else:
     71                                mod_img = img
     72                            collide_rect = mod_img.get_bounding_rect(1).inflate(-2,-2)
     73                            self._animations[name][facing].append((mod_img, collide_rect))
     74                    else:
     75                        collide_rect = img.get_bounding_rect(1).inflate(-2,-2)
     76                        self._animations[name].append((img, collide_rect))
    6077
    6178        self.collide_rect = Rect((0, 0), (2, 2))
     
    6582
    6683    def _update_image(self):
    67         images = self._animations[self._animation]
     84        if self.facing:
     85            images = self._animations[self._animation][self.facing]
     86        else:
     87            images = self._animations[self._animation]
    6888        if self._frame >= len(images):
    6989            self._frame = 0
     
    126146                # We've just looped through the animation sequence
    127147                self._animation = self._old_state
     148                self.facing = self._old_facing
    128149            elif self._frame == self.attack_frame and self._tick == 5:
    129150                # Attack the player
     
    141162            self._target = player
    142163            self._old_state = self._animation
     164            self._old_facing = self.facing
    143165            self._animation = 'attacking'
    144166            self._tick = 1
  • skaapsteker/sprites/enemies.py

    r236 r239  
    11from base import Monster
     2from pygame import transform
    23
    34
     
    89    attack_damage = 10
    910
     11    facings = {
     12            'running' : (('left', None),
     13                ('right', lambda x: transform.flip(x, True, False))),
     14            'attacking' : (('left', None),
     15                ('right', lambda x: transform.flip(x, True, False))),
     16                }
     17
    1018    def setup(self, direction):
    1119        self.facing = direction
     20
     21    def start_attack(self, player):
     22        if self._animation != 'attacking':
     23            # Turn to face the player we're attacking
     24            if player.collide_rect.centerx > self.collide_rect.centerx:
     25                self.facing = 'right'
     26            else:
     27                self.facing = 'left'
     28        Monster.start_attack(self, player)
Note: See TracChangeset for help on using the changeset viewer.