annotate gamelib/animations.py @ 237:4a87bfa5cc63

Twiddle animation cycle
author Neil Muller <drnlmuller@gmail.com>
date Sat, 05 Sep 2009 09:48:48 +0000
parents d0760fccce14
children 1a7000c8211c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
202
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
1 """Animation Loops"""
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
2
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
3 from pgu.vid import Sprite
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
4
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
5 import imagecache
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
6 from misc import Position
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
7
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
8 class Animation(Sprite):
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
9 """Animation loop.
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
10
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
11 These are derived from sprites, since they behave similiary in most
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
12 respects, but, to ensure draw ordering, we don't add them to
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
13 the sprites list.
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
14
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
15 Ideally, animations should be quite short."""
237
4a87bfa5cc63 Twiddle animation cycle
Neil Muller <drnlmuller@gmail.com>
parents: 235
diff changeset
16 # In the current implementation, sequences longer than 4 frames
233
d3d5352f5853 Twek speed loop. Document animation assumptions
Neil Muller <drnlmuller@gmail.com>
parents: 202
diff changeset
17 # will cause issues as this will overrun the next move loop.
237
4a87bfa5cc63 Twiddle animation cycle
Neil Muller <drnlmuller@gmail.com>
parents: 235
diff changeset
18 # (assuming all animations are triggered by the move loop, of course)
202
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
19
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
20 def __init__(self, sequence, tile_pos):
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
21 # Create the first frame
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
22 self.iter = iter(sequence)
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
23 Sprite.__init__(self, self.iter.next(), (-1000, -1000))
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
24 if hasattr(tile_pos, 'to_tuple'):
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
25 self.pos = tile_pos
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
26 else:
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
27 self.pos = Position(tile_pos[0], tile_pos[1])
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
28 self.removed = False
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
29
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
30 def fix_pos(self, tv):
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
31 ppos = tv.tile_to_view(self.pos.to_tuple())
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
32 self.rect.x = ppos[0]
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
33 self.rect.y = ppos[1]
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
34
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
35 def animate(self):
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
36 """Step to the next frame.
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
37
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
38 Set removed flag when we hit the end of the sequence"""
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
39 try:
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
40 self.setimage(self.iter.next())
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
41 except StopIteration:
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
42 self.removed = True
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
43
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
44 class MuzzleFlash(Animation):
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
45
235
d0760fccce14 Hold flash for a bit longer
Neil Muller <drnlmuller@gmail.com>
parents: 233
diff changeset
46 FLASH_LEFT = imagecache.load_image('sprites/muzzle_flash.png')
d0760fccce14 Hold flash for a bit longer
Neil Muller <drnlmuller@gmail.com>
parents: 233
diff changeset
47 FLASH_RIGHT = imagecache.load_image('sprites/muzzle_flash.png',
d0760fccce14 Hold flash for a bit longer
Neil Muller <drnlmuller@gmail.com>
parents: 233
diff changeset
48 ("right_facing",))
d0760fccce14 Hold flash for a bit longer
Neil Muller <drnlmuller@gmail.com>
parents: 233
diff changeset
49
d0760fccce14 Hold flash for a bit longer
Neil Muller <drnlmuller@gmail.com>
parents: 233
diff changeset
50 SEQUENCE_LEFT = [FLASH_LEFT, FLASH_LEFT]
d0760fccce14 Hold flash for a bit longer
Neil Muller <drnlmuller@gmail.com>
parents: 233
diff changeset
51 SEQUENCE_RIGHT = [FLASH_RIGHT, FLASH_RIGHT]
202
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
52
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
53 def __init__(self, chicken):
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
54 if chicken.facing == 'right':
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
55 Animation.__init__(self, self.SEQUENCE_RIGHT, chicken.pos)
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
56 else:
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
57 Animation.__init__(self, self.SEQUENCE_LEFT, chicken.pos)
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
58