# HG changeset patch # User Neil Muller # Date 1302343579 -7200 # Node ID e499a10eb41f781c502d943c0b7ddbe132cd21b4 # Parent 971c1726c53080e808b3163ab8963b73b90e1607 Time based animation for npcs and monsters. Make oni attack faster diff -r 971c1726c530 -r e499a10eb41f skaapsteker/sprites/base.py --- a/skaapsteker/sprites/base.py Sat Apr 09 02:16:03 2011 +0200 +++ b/skaapsteker/sprites/base.py Sat Apr 09 12:06:19 2011 +0200 @@ -1,6 +1,7 @@ """Basic sprite classes.""" import re +import time from pygame import Rect import pygame.transform @@ -52,13 +53,15 @@ wants_updates = True + frame_pause = 0.1 # default time between animation frames + 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._last_time = 0 self._animation = self.animation_regexes[0][0] if self.facings and self._animation in self.facings: self.facing = self.facings[self._animation][0][0] @@ -115,14 +118,16 @@ self.init_pos() def update(self): - if self._tick > 10: - self._tick = 0 - self._frame += 1 - force = False - if self._animation == 'attacking': - force = True - self._update_image(force) - self._tick += 1 + if self._last_time is not None: + if time.time() - self._last_time > self.frame_pause: + self._frame += 1 + self._last_time = time.time() + force = False + if self._animation == 'attacking': + force = True + self._update_image(force) + else: + self._last_time = time.time() class Monster(AnimatedGameSprite): @@ -142,6 +147,7 @@ self.floor_rect = Rect(self.collide_rect.topleft, (self.collide_rect.width, 2)) self._layer = Layers.PLAYER self.health = 10 + self._done_attack = False self.setup(**opts) @@ -152,13 +158,12 @@ 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 + if self._frame == 0 and self._done_attack and (self._last_time - self._start_attack_time) > self.frame_pause: # We've just looped through the animation sequence self._animation = self._old_state self.facing = self._old_facing self._update_image(True) - elif self._frame == self.attack_frame and self._tick == 5: + elif self._frame == self.attack_frame and not self._done_attack: # Attack the player self.do_attack() @@ -166,18 +171,20 @@ """Overriden by monster classes""" if self.check_collides(self._target): self._target.damage(self.attack_damage) + self._done_attack = True def start_attack(self, player): if self._animation == 'attacking': return # We're already attacking elif self.attack_frame is not None: + self._done_attack = False 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 self._update_image(True) + self._last_time = self._start_attack_time = time.time() else: player.damage(1) # collision damage diff -r 971c1726c530 -r e499a10eb41f skaapsteker/sprites/enemies.py --- a/skaapsteker/sprites/enemies.py Sat Apr 09 02:16:03 2011 +0200 +++ b/skaapsteker/sprites/enemies.py Sat Apr 09 12:06:19 2011 +0200 @@ -7,6 +7,7 @@ attack_frame = 1 attack_damage = 10 + frame_pause = 0.05 # Fast attacks facings = { 'running' : (('left', None),