Changeset 213:c5c4306593d8


Ignore:
Timestamp:
04/06/11 22:32:25 (12 years ago)
Author:
Simon Cross <hodgestar@…>
Branch:
default
Phase:
public
Message:

Attempt to animate NPCs a bit.

Location:
skaapsteker/sprites
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • skaapsteker/sprites/base.py

    r210 r213  
    22
    33from pygame import Rect
     4import re
    45
    56from skaapsteker.physics import Sprite
     
    2526        Sprite.__init__(self)
    2627        self.image = data.load_image(self.image_dir + self.image_file)
    27         self.starting_tile_pos = pos
    2828        self.rect = self.image.get_rect(midbottom=(pos[0]*TILE_SIZE[0]+TILE_SIZE[0]/2, (pos[1]+1)*TILE_SIZE[1]))
    2929        self.collide_rect = self.rect.move(0, 0)
    3030        self.setup(**opts)
    3131
     32
     33class AnimatedGameSprite(Sprite):
     34    # folder for animation files, e.g. sprites/foo
     35    image_dir = None
     36
     37    # first item is the starting animation
     38    animation_regexes = [
     39        # TODO: swap back once we know how to swap
     40        ("running", r"^.*_\d+.png$"),
     41        ("standing", r"^.*_standing.png$"),
     42    ]
     43
     44    wants_updates = True
     45
     46    def __init__(self, pos, **opts):
     47        Sprite.__init__(self)
     48        self._animations = dict((k, []) for k, r in self.animation_regexes)
     49        self._frame = 0
     50        self._tick = 0 # TODO: hack to show some animation; kill shortly
     51        self._animation = self.animation_regexes[0][0]
     52
     53        for image in data.get_files(self.image_dir):
     54            print image
     55            for name, pattern in self.animation_regexes:
     56                if re.match(pattern, image):
     57                    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))
     60
     61        self.collide_rect = Rect((0, 0), (2, 2))
     62        self.collide_rect.midbottom = (pos[0]*TILE_SIZE[0]+TILE_SIZE[0]/2, (pos[1]+1)*TILE_SIZE[1])
     63        self._update_image()
     64        self.setup(**opts)
     65
     66    def _update_image(self):
     67        images = self._animations[self._animation]
     68        if self._frame >= len(images):
     69            self._frame = 0
     70        cand_image, cand_collide_rect = images[self._frame]
     71        cand_collide_rect = cand_collide_rect.move(0, 0) # copy collide rect before we move it
     72
     73        cur_pos = self.collide_rect.midbottom
     74
     75        cand_rect = cand_image.get_rect()
     76        cand_rect_offset = cand_rect.centerx - cand_collide_rect.centerx, cand_rect.bottom - cand_collide_rect.bottom
     77        cand_rect.midbottom = cur_pos[0] + cand_rect_offset[0], cur_pos[1] + cand_rect_offset[1]
     78        cand_collide_rect.midbottom = cur_pos
     79
     80        import pdb
     81        # pdb.set_trace()
     82
     83        if not self.check_collide_rect(cand_collide_rect, cand_rect, cand_image):
     84            return
     85
     86        self.image = cand_image
     87        self.collide_rect = cand_collide_rect
     88        self.rect = cand_rect
     89        self.rect_offset = cand_rect_offset
     90        self.init_pos()
     91
     92    def update(self):
     93        if self._tick > 10:
     94            self._tick = 0
     95            self._frame += 1
     96            self._update_image()
     97        self._tick += 1
    3298
    3399
     
    61127
    62128
    63 class NPC(GameSprite):
    64 
    65     image_file = None
     129class NPC(AnimatedGameSprite):
     130
    66131    collision_layer = NPC_LAYER
    67132    collides_with = set([PC_LAYER])
     
    72137
    73138    def __init__(self, pos, **opts):
    74         GameSprite.__init__(self, pos, **opts)
     139        AnimatedGameSprite.__init__(self, pos, **opts)
    75140        self._layer = Layers.PLAYER
    76 
    77141
    78142    def setup(self, name, world, dsm):
  • skaapsteker/sprites/npcs.py

    r197 r213  
    33
    44class Monk(NPC):
    5     image_file = 'monk/monk.png'
     5    image_dir = 'sprites/monk'
     6    animation_regexes = [
     7        ("meditating", "monk.png"),
     8    ]
    69
    710
    811class Guard(NPC):
    9     image_file = 'guard/guard_standing.png'
     12    image_dir = 'sprites/guard'
    1013
    1114
    1215class Hattori(NPC):
    13     image_file = 'hattori/hattori_standing.png'
     16    image_dir = 'sprites/hattori'
    1417
    1518
    1619class Ichiro(NPC):
    17     image_file = 'ichiro/ichiro_standing.png'
     20    image_dir = 'sprites/ichiro'
    1821
    1922
    2023class Kaneda(NPC):
    21     image_file = 'kaneda/kaneda_standing.png'
     24    image_dir = 'sprites/kaneda'
    2225
    2326
    2427class Kumiko(NPC):
    25     image_file = 'geisha/geisha_01.png'
     28    image_dir = 'sprites/geisha'
    2629
    2730
    2831class Actor(NPC):
    29     image_file = 'dummy.png'
     32    image_dir = 'dummy.png' # TODO: fix.
    3033
    3134
    3235class Sasuke(NPC):
    33     image_file = 'sasuke/sasuke_standing.png'
     36    image_dir = 'sprites/sasuke'
Note: See TracChangeset for help on using the changeset viewer.