comparison skaapsteker/sprites/base.py @ 242:c30fcf903d29

Add update force parameter, so we can transition out of attacking animations even if it introduce a new collision
author Neil Muller <drnlmuller@gmail.com>
date Thu, 07 Apr 2011 15:01:23 +0200
parents 4c5941cf2b7f
children 129afb4417cf
comparison
equal deleted inserted replaced
241:4c5941cf2b7f 242:c30fcf903d29
78 self.collide_rect = Rect((0, 0), (2, 2)) 78 self.collide_rect = Rect((0, 0), (2, 2))
79 self.collide_rect.midbottom = (pos[0]*TILE_SIZE[0]+TILE_SIZE[0]/2, (pos[1]+1)*TILE_SIZE[1]) 79 self.collide_rect.midbottom = (pos[0]*TILE_SIZE[0]+TILE_SIZE[0]/2, (pos[1]+1)*TILE_SIZE[1])
80 self._update_image() 80 self._update_image()
81 self.setup(**opts) 81 self.setup(**opts)
82 82
83 def _update_image(self): 83 def _update_image(self, force=False):
84 if self.facing: 84 if self.facing:
85 images = self._animations[self._animation][self.facing] 85 images = self._animations[self._animation][self.facing]
86 else: 86 else:
87 images = self._animations[self._animation] 87 images = self._animations[self._animation]
88 if self._frame >= len(images): 88 if self._frame >= len(images):
95 cand_rect = cand_image.get_rect() 95 cand_rect = cand_image.get_rect()
96 cand_rect_offset = cand_rect.centerx - cand_collide_rect.centerx, cand_rect.bottom - cand_collide_rect.bottom 96 cand_rect_offset = cand_rect.centerx - cand_collide_rect.centerx, cand_rect.bottom - cand_collide_rect.bottom
97 cand_rect.midbottom = cur_pos[0] + cand_rect_offset[0], cur_pos[1] + cand_rect_offset[1] 97 cand_rect.midbottom = cur_pos[0] + cand_rect_offset[0], cur_pos[1] + cand_rect_offset[1]
98 cand_collide_rect.midbottom = cur_pos 98 cand_collide_rect.midbottom = cur_pos
99 99
100 if not self.check_collide_rect(cand_collide_rect, cand_rect, cand_image) and not self._animation == 'attacking': 100 if not self.check_collide_rect(cand_collide_rect, cand_rect, cand_image) and not force:
101 return 101 return
102 102
103 self.image = cand_image 103 self.image = cand_image
104 self.collide_rect = cand_collide_rect 104 self.collide_rect = cand_collide_rect
105 self.rect = cand_rect 105 self.rect = cand_rect
108 108
109 def update(self): 109 def update(self):
110 if self._tick > 10: 110 if self._tick > 10:
111 self._tick = 0 111 self._tick = 0
112 self._frame += 1 112 self._frame += 1
113 self._update_image() 113 force = False
114 if self._animation == 'attacking':
115 force = True
116 self._update_image(force)
114 self._tick += 1 117 self._tick += 1
115 118
116 119
117 class Monster(AnimatedGameSprite): 120 class Monster(AnimatedGameSprite):
118 121
144 if self._frame == 0 and self._tick == 1: 147 if self._frame == 0 and self._tick == 1:
145 # FIXME: This will need to change when AnimatedGameSprite changes 148 # FIXME: This will need to change when AnimatedGameSprite changes
146 # We've just looped through the animation sequence 149 # We've just looped through the animation sequence
147 self._animation = self._old_state 150 self._animation = self._old_state
148 self.facing = self._old_facing 151 self.facing = self._old_facing
149 self._update_image() 152 self._update_image(True)
150 elif self._frame == self.attack_frame and self._tick == 5: 153 elif self._frame == self.attack_frame and self._tick == 5:
151 # Attack the player 154 # Attack the player
152 self.do_attack() 155 self.do_attack()
153 156
154 def do_attack(self): 157 def do_attack(self):
164 self._old_state = self._animation 167 self._old_state = self._animation
165 self._old_facing = self.facing 168 self._old_facing = self.facing
166 self._animation = 'attacking' 169 self._animation = 'attacking'
167 self._tick = 1 170 self._tick = 1
168 self._frame = 0 # Start the attack from the beginning 171 self._frame = 0 # Start the attack from the beginning
169 self._update_image() 172 self._update_image(True)
170 else: 173 else:
171 player.damage(1) # collision damage 174 player.damage(1) # collision damage
172 175
173 def damage(self, damage): 176 def damage(self, damage):
174 print 'Damaged by ', damage 177 print 'Damaged by ', damage