Mercurial > skaapsteker
changeset 213:c5c4306593d8
Attempt to animate NPCs a bit.
author | Simon Cross <hodgestar@gmail.com> |
---|---|
date | Thu, 07 Apr 2011 00:32:25 +0200 |
parents | d5a1471a38a7 |
children | 6e7faba82c21 |
files | skaapsteker/sprites/base.py skaapsteker/sprites/npcs.py |
diffstat | 2 files changed, 80 insertions(+), 13 deletions(-) [+] |
line wrap: on
line diff
--- a/skaapsteker/sprites/base.py Thu Apr 07 00:31:53 2011 +0200 +++ b/skaapsteker/sprites/base.py Thu Apr 07 00:32:25 2011 +0200 @@ -1,6 +1,7 @@ """Basic sprite classes.""" from pygame import Rect +import re from skaapsteker.physics import Sprite from skaapsteker.constants import Layers @@ -24,12 +25,77 @@ def __init__(self, pos, **opts): Sprite.__init__(self) self.image = data.load_image(self.image_dir + self.image_file) - self.starting_tile_pos = pos self.rect = self.image.get_rect(midbottom=(pos[0]*TILE_SIZE[0]+TILE_SIZE[0]/2, (pos[1]+1)*TILE_SIZE[1])) self.collide_rect = self.rect.move(0, 0) self.setup(**opts) +class AnimatedGameSprite(Sprite): + # folder for animation files, e.g. sprites/foo + image_dir = None + + # first item is the starting animation + animation_regexes = [ + # TODO: swap back once we know how to swap + ("running", r"^.*_\d+.png$"), + ("standing", r"^.*_standing.png$"), + ] + + wants_updates = True + + 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] + + for image in data.get_files(self.image_dir): + print image + 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)) + + 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]) + self._update_image() + self.setup(**opts) + + def _update_image(self): + images = self._animations[self._animation] + if self._frame >= len(images): + self._frame = 0 + cand_image, cand_collide_rect = images[self._frame] + cand_collide_rect = cand_collide_rect.move(0, 0) # copy collide rect before we move it + + cur_pos = self.collide_rect.midbottom + + cand_rect = cand_image.get_rect() + cand_rect_offset = cand_rect.centerx - cand_collide_rect.centerx, cand_rect.bottom - cand_collide_rect.bottom + cand_rect.midbottom = cur_pos[0] + cand_rect_offset[0], cur_pos[1] + cand_rect_offset[1] + cand_collide_rect.midbottom = cur_pos + + import pdb + # pdb.set_trace() + + if not self.check_collide_rect(cand_collide_rect, cand_rect, cand_image): + return + + self.image = cand_image + self.collide_rect = cand_collide_rect + self.rect = cand_rect + self.rect_offset = cand_rect_offset + self.init_pos() + + def update(self): + if self._tick > 10: + self._tick = 0 + self._frame += 1 + self._update_image() + self._tick += 1 + class Monster(GameSprite): @@ -60,9 +126,8 @@ self.kill() -class NPC(GameSprite): +class NPC(AnimatedGameSprite): - image_file = None collision_layer = NPC_LAYER collides_with = set([PC_LAYER]) @@ -71,10 +136,9 @@ block = False def __init__(self, pos, **opts): - GameSprite.__init__(self, pos, **opts) + AnimatedGameSprite.__init__(self, pos, **opts) self._layer = Layers.PLAYER - def setup(self, name, world, dsm): self.dsm = dialogue.DSM(name, world, dsm)
--- a/skaapsteker/sprites/npcs.py Thu Apr 07 00:31:53 2011 +0200 +++ b/skaapsteker/sprites/npcs.py Thu Apr 07 00:32:25 2011 +0200 @@ -2,32 +2,35 @@ class Monk(NPC): - image_file = 'monk/monk.png' + image_dir = 'sprites/monk' + animation_regexes = [ + ("meditating", "monk.png"), + ] class Guard(NPC): - image_file = 'guard/guard_standing.png' + image_dir = 'sprites/guard' class Hattori(NPC): - image_file = 'hattori/hattori_standing.png' + image_dir = 'sprites/hattori' class Ichiro(NPC): - image_file = 'ichiro/ichiro_standing.png' + image_dir = 'sprites/ichiro' class Kaneda(NPC): - image_file = 'kaneda/kaneda_standing.png' + image_dir = 'sprites/kaneda' class Kumiko(NPC): - image_file = 'geisha/geisha_01.png' + image_dir = 'sprites/geisha' class Actor(NPC): - image_file = 'dummy.png' + image_dir = 'dummy.png' # TODO: fix. class Sasuke(NPC): - image_file = 'sasuke/sasuke_standing.png' + image_dir = 'sprites/sasuke'