comparison gamelib/gameboard.py @ 504:393e30ea0165

Save game loading working again.
author Simon Cross <hodgestar@gmail.com>
date Thu, 26 Nov 2009 22:28:22 +0000
parents 4d980444ebfb
children 3b5717d742b2
comparison
equal deleted inserted replaced
503:4c281ce4fc5f 504:393e30ea0165
78 78
79 SIMPLIFY = [ 79 SIMPLIFY = [
80 'level', 80 'level',
81 'tv', 81 'tv',
82 'max_foxes', 82 'max_foxes',
83 #'selected_tool',
84 #'sprite_cursor',
85 'selected_chickens',
86 'stored_selections',
83 'chickens', 87 'chickens',
88 'foxes',
84 'buildings', 89 'buildings',
85 'foxes', 90 #'_pos_cache',
86 'cash', 91 'cash',
87 'wood', 92 'wood',
88 'eggs', 93 'eggs',
89 'days', 94 'days',
90 'killed_foxes', 95 'killed_foxes',
129 for tn in equipment.EQUIP_MAP: 134 for tn in equipment.EQUIP_MAP:
130 cdata[tn] = (self.add_start_chickens, tn) 135 cdata[tn] = (self.add_start_chickens, tn)
131 136
132 self.tv.run_codes(cdata, (0,0,width,height)) 137 self.tv.run_codes(cdata, (0,0,width,height))
133 138
139 @classmethod
140 def unsimplify(cls, *args, **kwargs):
141 """Override default Simplifiable unsimplification."""
142 obj = super(GameBoard, cls).unsimplify(*args, **kwargs)
143
144 obj.tv.png_folder_load_tiles('tiles')
145 obj.calculate_wood_groat_exchange_rate()
146
147 obj._pos_cache = AnimalPositionCache(obj)
148 obj._cache_animal_positions()
149
150 obj.sprite_cursor = None
151 obj.set_selected_tool(None, None)
152
153 obj.disp = None
154
155 # put chickens, foxes and buildings into sprite list
156
157 existing_chickens = obj.chickens
158 obj.chickens = set()
159 for chicken in existing_chickens:
160 obj.add_chicken(chicken)
161
162 existing_foxes = obj.foxes
163 obj.foxes = set()
164 for fox in existing_foxes:
165 obj.add_fox(fox)
166
167 existing_buildings = obj.buildings
168 obj.buildings = set()
169 for building in existing_buildings:
170 obj.add_building(building)
171
172 # self.disp is not set properly here
173 # whoever unsimplifies the gameboard needs to arrange for it to be
174 # set and then call .create_display() and so create:
175 # - .toolbar
176 # - .tvw
177 # - .top_widget
178
179 return obj
180
134 def get_top_widget(self): 181 def get_top_widget(self):
135 return self.top_widget 182 return self.top_widget
136 183
137 def create_display(self): 184 def create_display(self):
138 width, height = self.disp.rect.w, self.disp.rect.h 185 width, height = self.disp.rect.w, self.disp.rect.h
141 self.toolbar = toolbar.DefaultToolBar(self, width=constants.TOOLBAR_WIDTH) 188 self.toolbar = toolbar.DefaultToolBar(self, width=constants.TOOLBAR_WIDTH)
142 tbl.td(self.toolbar, valign=-1) 189 tbl.td(self.toolbar, valign=-1)
143 self.tvw = VidWidget(self, self.tv, width=width-constants.TOOLBAR_WIDTH, height=height) 190 self.tvw = VidWidget(self, self.tv, width=width-constants.TOOLBAR_WIDTH, height=height)
144 tbl.td(self.tvw) 191 tbl.td(self.tvw)
145 self.top_widget = tbl 192 self.top_widget = tbl
193 self.redraw_counters()
146 194
147 def change_toolbar(self, new_toolbar): 195 def change_toolbar(self, new_toolbar):
148 """Replace the toolbar""" 196 """Replace the toolbar"""
149 td = self.toolbar.container 197 td = self.toolbar.container
150 td.remove(self.toolbar) 198 td.remove(self.toolbar)
1010 1058
1011 def restore_game(self, data): 1059 def restore_game(self, data):
1012 if 'refid' not in data or 'class' not in data or data['class'] != self.__class__.__name__: 1060 if 'refid' not in data or 'class' not in data or data['class'] != self.__class__.__name__:
1013 raise ValueError("Invalid save game.") 1061 raise ValueError("Invalid save game.")
1014 1062
1015 # clear old state 1063 new_gameboard = serializer.unsimplify(data)
1016 self.clear_chickens() 1064
1017 self.clear_buildings() 1065 import engine
1018 1066 pygame.event.post(pygame.event.Event(engine.DO_LOAD_SAVEGAME, gameboard=new_gameboard))
1019 # set new state
1020 newself = serializer.unsimplify(data)
1021
1022 #import pdb
1023 #pdb.set_trace()
1024
1025 for attr in self.SIMPLIFY:
1026 if attr in ('chickens', 'buildings'):
1027 continue
1028 setattr(self, attr, getattr(newself, attr))
1029
1030 self.tv.png_folder_load_tiles('tiles')
1031 self.tvw.vid = self.tv
1032 self.tvw.vid.bounds = pygame.Rect((0, 0), self.tv.tile_to_view(self.tv.size))
1033
1034 for chicken in newself.chickens:
1035 self.add_chicken(chicken)
1036
1037 for building in newself.buildings:
1038 self.add_building(building)
1039
1040 self.reset_states()
1041 self.redraw_counters()
1042 self.update()
1043 1067
1044 1068
1045 class TextDialog(gui.Dialog): 1069 class TextDialog(gui.Dialog):
1046 def __init__(self, title, text, **params): 1070 def __init__(self, title, text, **params):
1047 title_label = gui.Label(title) 1071 title_label = gui.Label(title)