changeset 380:1586eccdefe4

Ripped out legacy animation infrastructure in favour of layered sprites.
author Jeremy Thurgood <firxen@gmail.com>
date Sun, 25 Oct 2009 10:56:01 +0000
parents a8a7ada27fa2
children 7a58dadfd251
files gamelib/animal.py gamelib/animations.py gamelib/equipment.py gamelib/gameboard.py
diffstat 4 files changed, 15 insertions(+), 62 deletions(-) [+]
line wrap: on
line diff
--- a/gamelib/animal.py	Sat Oct 24 21:43:15 2009 +0000
+++ b/gamelib/animal.py	Sun Oct 25 10:56:01 2009 +0000
@@ -45,7 +45,7 @@
         if hasattr(self, 'DEATH_SOUND'):
             sound.play_sound(self.DEATH_SOUND)
         if hasattr(self, 'DEATH_ANIMATION'):
-            gameboard.animations.append(self.DEATH_ANIMATION(self.pos))
+            self.DEATH_ANIMATION(gameboard.tv, self.pos.to_tuple())
         self._game_death(gameboard)
 
     def _game_death(self, gameboard):
@@ -474,7 +474,7 @@
         """Setup dig parameters, to be overridden if needed"""
         self.tick = 0 # Costs us nothing to go through a fence.
         self.dig_pos = dig_pos
-        gameboard.animations.append(self.DIG_ANIMATION(dig_pos))
+        self.DIG_ANIMATION(gameboard.tv, dig_pos.to_tuple())
         self._make_hole(gameboard)
 
 class GreedyFox(Fox):
--- a/gamelib/animations.py	Sat Oct 24 21:43:15 2009 +0000
+++ b/gamelib/animations.py	Sun Oct 25 10:56:01 2009 +0000
@@ -17,31 +17,21 @@
        # will cause issues as this will overrun the next move loop.
        # (assuming all animations are triggered by the move loop, of course)
 
-    def __init__(self, tile_pos, sequence=None):
+    def __init__(self, tv, tile_pos, sequence=None, layer='animations'):
         # Create the first frame
         if sequence is None:
             sequence = self.SEQUENCE
         self.iter = iter(sequence)
-        Sprite.__init__(self, self.iter.next(), (-1000, -1000))
-        if hasattr(tile_pos, 'to_tuple'):
-            self.pos = tile_pos
-        else:
-            self.pos = Position(tile_pos[0], tile_pos[1])
-        self.removed = False
+        self.layer = layer
+        Sprite.__init__(self, self.iter.next(), tv.tile_to_view(tile_pos))
+        tv.sprites.append(self, layer=self.layer)
 
-    def fix_pos(self, tv):
-        ppos = tv.tile_to_view(self.pos.to_tuple())
-        self.rect.x = ppos[0]
-        self.rect.y = ppos[1]
-
-    def animate(self):
-        """Step to the next frame.
-
-           Set removed flag when we hit the end of the sequence"""
+    def loop(self, tv, s):
+        """Step to the next frame, removing sprite when done."""
         try:
             self.setimage(self.iter.next())
         except StopIteration:
-            self.removed = True
+            tv.sprites.remove(self, layer=self.layer)
 
 class MuzzleFlash(Animation):
 
@@ -52,11 +42,11 @@
     SEQUENCE_LEFT = [FLASH_LEFT, FLASH_LEFT]
     SEQUENCE_RIGHT = [FLASH_RIGHT, FLASH_RIGHT]
 
-    def __init__(self, chicken):
+    def __init__(self, tv, chicken, layer='animations'):
         if chicken.facing == 'right':
-            Animation.__init__(self, chicken.pos, self.SEQUENCE_RIGHT)
+            Animation.__init__(self, tv, chicken.pos.to_tuple(), self.SEQUENCE_RIGHT, layer=layer)
         else:
-            Animation.__init__(self, chicken.pos, self.SEQUENCE_LEFT)
+            Animation.__init__(self, tv, chicken.pos.to_tuple(), self.SEQUENCE_LEFT, layer=layer)
 
 class FenceExplosion(Animation):
     FLASH_1 = imagecache.load_image('sprites/boom1.png')
--- a/gamelib/equipment.py	Sat Oct 24 21:43:15 2009 +0000
+++ b/gamelib/equipment.py	Sun Oct 25 10:56:01 2009 +0000
@@ -67,7 +67,7 @@
         if hasattr(self, 'HIT_SOUND'):
             sound.play_sound(self.HIT_SOUND)
         if hasattr(self, 'ANIMATION'):
-            gameboard.animations.append(self.ANIMATION(wielder))
+            self.ANIMATION(gameboard.tv, wielder)
         roll = random.randint(1, 100)
         base_hit = self._get_parameter('BASE_HIT', wielder)
         range_penalty = self._get_parameter('RANGE_PENALTY', wielder)
--- a/gamelib/gameboard.py	Sat Oct 24 21:43:15 2009 +0000
+++ b/gamelib/gameboard.py	Sun Oct 25 10:56:01 2009 +0000
@@ -253,44 +253,9 @@
 
     def paint(self, surface):
         self.vid.paint(surface)
-        # Blit animation frames on top of the drawing
-        x, y = self.vid.view.x, self.vid.view.y
-        for anim in self.gameboard.animations:
-            anim.fix_pos(self.vid)
-            anim.irect.x = anim.rect.x - anim.shape.x
-            anim.irect.y = anim.rect.y - anim.shape.y
-            surface.blit(anim.image, (anim.irect.x - x, anim.irect.y - y))
-            # We don't store anim._irect, since we only update anims if the
-            # image changes, which kills irect
 
     def update(self, surface):
-        us = []
-        x, y = self.vid.view.x, self.vid.view.y
-        for anim in self.gameboard.animations[:]:
-            if (anim.updated or anim.removed) and \
-                    self.gameboard.in_bounds(anim.pos):
-                # We flag that we need to redraw stuff undeneath the animation
-                anim.irect.x = anim.rect.x - anim.shape.x
-                anim.irect.y = anim.rect.y - anim.shape.y
-                us.append(pygame.Rect(anim.irect.x - x, anim.irect.y - y,
-                    anim.irect.width, anim.irect.height))
-                self.vid.alayer[anim.pos.y][anim.pos.x]=1
-                self.vid.updates.append(anim.pos.to_tuple())
-            if anim.removed:
-                # Remove the animation from the draw loop
-                self.gameboard.animations.remove(anim)
-        us.extend(self.vid.update(surface))
-        for anim in self.gameboard.animations:
-            if anim.updated:
-                # setimage has happened, so redraw
-                anim.updated = 0
-                anim.fix_pos(self.vid)
-                if self.gameboard.in_bounds(anim.pos): 
-                    surface.blit(anim.image, (anim.irect.x - x, anim.irect.y - y))
-                # This is enough, because sprite changes happen disjoint
-                # from the animation sequence, so we don't need to worry
-                # other changes forcing us to redraw the animation frame.
-        return us
+        return self.vid.update(surface)
 
     def move_view(self, x, y):
         self.vid.view.move_ip((x, y))
@@ -337,7 +302,6 @@
         self.chickens = set()
         self.foxes = set()
         self.buildings = []
-        self.animations = []
         self.cash = 0
         self.eggs = 0
         self.days = 0
@@ -826,8 +790,7 @@
         self.foxes = set() # Remove all the foxes
 
     def run_animations(self):
-        for anim in self.animations:
-            anim.animate()
+        # For legacy.
         if self.toolbar.anim_clear_tool:
             self.toolbar.clear_tool()