Changeset 333:e499a10eb41f


Ignore:
Timestamp:
04/09/11 10:06:19 (12 years ago)
Author:
Neil Muller <drnlmuller@…>
Branch:
default
Phase:
public
Message:

Time based animation for npcs and monsters. Make oni attack faster

Location:
skaapsteker/sprites
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • skaapsteker/sprites/base.py

    r302 r333  
    22
    33import re
     4import time
    45
    56from pygame import Rect
     
    5354    wants_updates = True
    5455
     56    frame_pause = 0.1  # default time between animation frames
     57
    5558    facings = {}
    5659
     
    5962        self._animations = dict((k, []) for k, r in self.animation_regexes)
    6063        self._frame = 0
    61         self._tick = 0 # TODO: hack to show some animation; kill shortly
     64        self._last_time = 0
    6265        self._animation = self.animation_regexes[0][0]
    6366        if self.facings and self._animation in self.facings:
     
    116119
    117120    def update(self):
    118         if self._tick > 10:
    119             self._tick = 0
    120             self._frame += 1
    121             force = False
    122             if self._animation == 'attacking':
    123                 force = True
    124             self._update_image(force)
    125         self._tick += 1
     121        if self._last_time is not None:
     122            if time.time() - self._last_time > self.frame_pause:
     123                self._frame += 1
     124                self._last_time = time.time()
     125                force = False
     126                if self._animation == 'attacking':
     127                    force = True
     128                self._update_image(force)
     129        else:
     130            self._last_time = time.time()
    126131
    127132
     
    143148        self._layer = Layers.PLAYER
    144149        self.health = 10
     150        self._done_attack = False
    145151        self.setup(**opts)
    146152
     
    153159        AnimatedGameSprite.update(self)
    154160        if self._animation == 'attacking':
    155             if self._frame == 0 and self._tick == 1:
    156                 # FIXME: This will need to change when AnimatedGameSprite changes
     161            if self._frame == 0 and self._done_attack and (self._last_time - self._start_attack_time) > self.frame_pause:
    157162                # We've just looped through the animation sequence
    158163                self._animation = self._old_state
    159164                self.facing = self._old_facing
    160165                self._update_image(True)
    161             elif self._frame == self.attack_frame and self._tick == 5:
     166            elif self._frame == self.attack_frame and not self._done_attack:
    162167                # Attack the player
    163168                self.do_attack()
     
    167172        if self.check_collides(self._target):
    168173            self._target.damage(self.attack_damage)
     174            self._done_attack = True
    169175
    170176    def start_attack(self, player):
     
    172178            return # We're already attacking
    173179        elif self.attack_frame is not None:
     180            self._done_attack = False
    174181            self._target = player
    175182            self._old_state = self._animation
    176183            self._old_facing = self.facing
    177184            self._animation = 'attacking'
    178             self._tick = 1
    179185            self._frame = 0  # Start the attack from the beginning
    180186            self._update_image(True)
     187            self._last_time = self._start_attack_time = time.time()
    181188        else:
    182189            player.damage(1)  # collision damage
  • skaapsteker/sprites/enemies.py

    r240 r333  
    88    attack_frame = 1
    99    attack_damage = 10
     10    frame_pause = 0.05  # Fast attacks
    1011
    1112    facings = {
Note: See TracChangeset for help on using the changeset viewer.