# HG changeset patch # User Simon Cross # Date 1259274502 0 # Node ID 393e30ea016515b468d84967f41f8d84635a21d2 # Parent 4c281ce4fc5f9f5a8d67f78d7823eaeef2cff22d Save game loading working again. diff -r 4c281ce4fc5f -r 393e30ea0165 gamelib/engine.py --- a/gamelib/engine.py Thu Nov 26 22:26:08 2009 +0000 +++ b/gamelib/engine.py Thu Nov 26 22:28:22 2009 +0000 @@ -42,6 +42,13 @@ self.level) self.open_window(self.gameboard.get_top_widget()) + def switch_gameboard(self, gameboard): + """Switch over to a new gameboard.""" + self.gameboard = gameboard + self.gameboard.disp = self.main_app + self.gameboard.create_display() + self.open_window(self.gameboard.get_top_widget()) + def set_main_menu(self): """Open the main menu""" self.scoreboard = gameover.ScoreTable(self.level) @@ -189,6 +196,9 @@ return pygame.event.post(START_NIGHT) elif events_equal(e, GO_MAIN_MENU): return MainMenuState(self.game) + elif e.type is DO_LOAD_SAVEGAME: + self.game.switch_gameboard(e.gameboard) + return elif e.type is not QUIT: self.game.main_app.event(e) @@ -320,6 +330,7 @@ FAST_FORWARD = pygame.event.Event(USEREVENT, name="FAST_FORWARD") MOVE_FOX_ID = USEREVENT + 1 MOVE_FOXES = pygame.event.Event(MOVE_FOX_ID, name="MOVE_FOXES") +DO_LOAD_SAVEGAME = USEREVENT + 2 QUIT = pygame.event.Event(QUIT) # Due to the way pgu's loop timing works, these will only get proceesed diff -r 4c281ce4fc5f -r 393e30ea0165 gamelib/gameboard.py --- a/gamelib/gameboard.py Thu Nov 26 22:26:08 2009 +0000 +++ b/gamelib/gameboard.py Thu Nov 26 22:28:22 2009 +0000 @@ -80,9 +80,14 @@ 'level', 'tv', 'max_foxes', + #'selected_tool', + #'sprite_cursor', + 'selected_chickens', + 'stored_selections', 'chickens', + 'foxes', 'buildings', - 'foxes', + #'_pos_cache', 'cash', 'wood', 'eggs', @@ -131,6 +136,48 @@ self.tv.run_codes(cdata, (0,0,width,height)) + @classmethod + def unsimplify(cls, *args, **kwargs): + """Override default Simplifiable unsimplification.""" + obj = super(GameBoard, cls).unsimplify(*args, **kwargs) + + obj.tv.png_folder_load_tiles('tiles') + obj.calculate_wood_groat_exchange_rate() + + obj._pos_cache = AnimalPositionCache(obj) + obj._cache_animal_positions() + + obj.sprite_cursor = None + obj.set_selected_tool(None, None) + + obj.disp = None + + # put chickens, foxes and buildings into sprite list + + existing_chickens = obj.chickens + obj.chickens = set() + for chicken in existing_chickens: + obj.add_chicken(chicken) + + existing_foxes = obj.foxes + obj.foxes = set() + for fox in existing_foxes: + obj.add_fox(fox) + + existing_buildings = obj.buildings + obj.buildings = set() + for building in existing_buildings: + obj.add_building(building) + + # self.disp is not set properly here + # whoever unsimplifies the gameboard needs to arrange for it to be + # set and then call .create_display() and so create: + # - .toolbar + # - .tvw + # - .top_widget + + return obj + def get_top_widget(self): return self.top_widget @@ -143,6 +190,7 @@ self.tvw = VidWidget(self, self.tv, width=width-constants.TOOLBAR_WIDTH, height=height) tbl.td(self.tvw) self.top_widget = tbl + self.redraw_counters() def change_toolbar(self, new_toolbar): """Replace the toolbar""" @@ -1012,34 +1060,10 @@ if 'refid' not in data or 'class' not in data or data['class'] != self.__class__.__name__: raise ValueError("Invalid save game.") - # clear old state - self.clear_chickens() - self.clear_buildings() - - # set new state - newself = serializer.unsimplify(data) - - #import pdb - #pdb.set_trace() + new_gameboard = serializer.unsimplify(data) - 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() + import engine + pygame.event.post(pygame.event.Event(engine.DO_LOAD_SAVEGAME, gameboard=new_gameboard)) class TextDialog(gui.Dialog):