changeset 463:e3408c803b12

Save and restore tilevid when loading / saving games.
author Simon Cross <hodgestar@gmail.com>
date Mon, 23 Nov 2009 22:43:55 +0000
parents aba7f1439571
children 3a8a64bbe2d8
files gamelib/gameboard.py gamelib/tiles.py
diffstat 2 files changed, 54 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/gamelib/gameboard.py	Mon Nov 23 21:06:16 2009 +0000
+++ b/gamelib/gameboard.py	Mon Nov 23 22:43:55 2009 +0000
@@ -76,6 +76,7 @@
     BROKEN_FENCE = tiles.REVERSE_TILE_MAP['broken fence']
 
     SIMPLIFY = [
+        'tv',
         'chickens',
         'buildings',
         'foxes',
@@ -849,26 +850,42 @@
         self.wood_sell_price, self.wood_buy_price = int(sell_price), int(buy_price)
 
     def save_game(self):
+        # clear selected animals and tool states before saving
+        self.reset_states()
         return serializer.simplify(self)
 
     def restore_game(self, data):
         if 'refid' not in data or 'class' not in data or data['class'] != self.__class__.__name__:
-            import pprint
-            pprint.pprint(data)
-            print self.__class__.__name__
             raise ValueError("Invalid save game.")
-        newself = serializer.unsimplify(data)
+
+        # clear old state
         self.clear_chickens()
         self.clear_buildings()
-        for chicken in newself.chickens:
-            self.add_chicken(chicken)
-        for building in newself.buildings:
-            self.add_building(building)
+
+        # set new state
+        newself = serializer.unsimplify(data)
+
+        #import pdb
+        #pdb.set_trace()
+
         for attr in self.SIMPLIFY:
             if attr in ('chickens', 'buildings'):
                 continue
             setattr(self, attr, getattr(newself, attr))
+
+        self.tv.png_folder_load_tiles('tiles')
+        self.tvw.vid = self.tv
+        self.tvw.vid.bounds = pygame.Rect((0, 0), self.tv.tile_to_view(self.tv.size))
+
+        for chicken in newself.chickens:
+            self.add_chicken(chicken)
+
+        for building in newself.buildings:
+            self.add_building(building)
+
+        self.reset_states()
         self.redraw_counters()
+        self.update()
 
 
 class TextDialog(gui.Dialog):
--- a/gamelib/tiles.py	Mon Nov 23 21:06:16 2009 +0000
+++ b/gamelib/tiles.py	Mon Nov 23 22:43:55 2009 +0000
@@ -4,6 +4,7 @@
 import os
 import data
 import imagecache
+import serializer
 
 class TileMap(object):
     """Helper class for describing all the game tiles."""
@@ -88,14 +89,41 @@
                 yield sprite
 
 
-class FarmVid(tilevid.Tilevid):
+class FarmVid(tilevid.Tilevid, serializer.Simplifiable):
     """Extension of pgu's TileVid class to handle the complications
        of raising chickens.
        """
+
+    SIMPLIFY = [
+        'size',
+        'layers',
+        'tlayer',
+        'alayer',
+        'blayer',
+        'clayer',
+    ]
+
     def __init__(self):
         tilevid.Tilevid.__init__(self)
         self.sprites = LayeredSprites(['buildings', 'animals', 'animations', 'cursor'], 'animals')
 
+    def make(cls):
+        """Override default Simplifiable object creation."""
+        return cls()
+    make = classmethod(make)
+
+    def unsimplify(cls, *args, **kwargs):
+        """Override default Simplifiable unsimplification."""
+        obj = serializer.Simplifiable.unsimplify(*args, **kwargs)
+
+        obj.view.x, obj.view.y = 0,0
+        obj._view.x, obj._view.y = 0,0
+        obj.bounds = None
+        obj.updates = []
+
+        return obj
+    unsimplify = classmethod(unsimplify)
+
     def png_folder_load_tiles(self, path):
         """Load tiles from a folder of PNG files."""
         full_path = data.filepath(path)