Changeset 148


Ignore:
Timestamp:
06/18/09 02:42:08 (10 years ago)
Author:
meggie_parpg
Message:

Added primarily saving and loading functionality. Saving uses a pickle.
Currently, this is my-save.pic and is hard coded in world.py. We should
allow the user to specify a file, but that can come later and is
primarily a UI thing. You can also load (again, from the same pickle).

Right now, saving and loading only looks at the PC's position and the
position of NPCs on maps. That's really all the state we have to save.
We can add more functions later, it should be fairly modular.

Small Changes:

  • Changed the functionality of the door floor tiles because changing the layer they are on will be very difficult
  • Got rid of an extraneous function call in world
Location:
trunk/PARPG
Files:
1 added
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/PARPG/maps/map2_objects.xml

    r147 r148  
    1414    <door display="True" gfx="grass-a" xpos="-14" ypos="-17" 
    1515          id="tt01" carry="0" contain="0" text="Start Map" map="map" 
    16           txpos="-14" typos="10"></door> 
     16          txpos="-14" typos="9"></door> 
    1717</objects> 
    1818 
  • trunk/PARPG/maps/map_objects.xml

    r138 r148  
    3030    <door display="True" gfx="snow02" xpos="-14" ypos="10" 
    3131        id="tt01" text="New Map" map="map2" 
    32         txpos="-14" typos="-17"></door> 
     32        txpos="-14" typos="-16"></door> 
    3333</objects> 
    3434 
  • trunk/PARPG/run.py

    r147 r148  
    8484        self.listener = ApplicationListener(self.engine,self.world) 
    8585        self.world.quitFunction = self.listener.quitGame 
    86         self.model.loadMap(str(TDS.readSetting("MapFile")))    
     86        self.model.loadMap(str(TDS.readSetting("MapFile")), False)    
    8787 
    8888    def loadSettings(self): 
  • trunk/PARPG/scripts/agents/hero.py

    r147 r148  
    3737        self.speed = float(TDS.readSetting("PCSpeed")) 
    3838 
     39    def getX(self): 
     40        """ Get the Hero's position on the map. 
     41            @rtype: integer 
     42            @return: the x coordinate of the NPC's location""" 
     43        return self.agent.getLocation().getLayerCoordinates().x 
     44 
     45    def getY(self): 
     46        """ Get the Hero's position on the map. 
     47            @rtype: integer 
     48            @return: the y coordinate of the NPC's location""" 
     49        return self.agent.getLocation().getLayerCoordinates().y 
     50 
    3951    def onInstanceActionFinished(self, instance, action): 
    4052        """@type instance: ??? 
  • trunk/PARPG/scripts/agents/npc.py

    r147 r148  
    8686        """What the NPC does when it has finished an action. 
    8787           Called by the engine and required for InstanceActionListeners. 
    88            @type instance: ??? 
    89            @param instance: ??? 
     88           @type instance: fife.Instance 
     89           @param instance: self.agent (the NPC listener is listening for this 
     90            instance) 
    9091           @type action: ??? 
    9192           @param action: ??? 
  • trunk/PARPG/scripts/engine.py

    r147 r148  
    2121from objects import GameObject 
    2222from objLoader import LocalXMLParser 
     23from saver import Saver 
     24import pickle 
    2325 
    2426# design note: 
     
    6870        self.doors = [] 
    6971        self.PC_targLoc = None 
     72        self.saver = Saver() 
    7073 
    7174    def reset(self): 
     
    7780        self.objects = [] 
    7881        self.doors = [] 
     82 
     83    def save(self, filename): 
     84        """ Writes the saver to a file. 
     85            @type filename: string 
     86            @param filename: the name of the file to write to 
     87            @return: None""" 
     88        self.updateSaver() 
     89        f = open(filename, 'w') 
     90        pickle.dump(self.saver, f) 
     91        f.close() 
     92 
     93    def load(self, filename): 
     94        """ Loads a saver from a file. 
     95            @type filename: string 
     96            @param filename: the name of the file to load from 
     97            @return: None""" 
     98        f = open(filename, 'r') 
     99        self.saver = pickle.load(f) 
     100        f.close() 
     101        self.loadMap(self.saver.curMap+'.xml', False) 
     102 
     103    def updateSaver(self): 
     104        """ Saves existing object/npc/door/pc data to the saver by calling 
     105            the saver's update functions. 
     106            @return: None""" 
     107        # TODO: add calls to other functions and write those functions for  
     108        # other bits of state that change. For now, the only one we can do is 
     109        # NPC info 
     110        self.saver.updatePC(self.PC) 
     111        self.saver.updateNPCs(self.npcs) 
    79112 
    80113    def loadObjects(self, filename): 
     
    105138        self.addObjects(cur_handler.objects) 
    106139        self.addDoors(cur_handler.doors) 
    107         objects_file.close() 
     140        # Save the initial map state 
     141        self.saver.addData(cur_handler.pc, cur_handler.npcs, \ 
     142                cur_handler.objects, cur_handler.doors) 
    108143        return True 
     144    
     145    def loadFromSaved(self, data): 
     146        """ Loads objects from a previously stored SavedData object. 
     147            @type data: saver.SavedData 
     148            @param data: The stored entry from which to get information 
     149            @return: None""" 
     150        self.addPC(data.PC) 
     151        self.addNPCs(data.getList("npcs")) 
     152        self.addObjects(data.getList("objects")) 
     153        self.addDoors(data.getList("doors")) 
    109154 
    110155    def addPC(self,pc): 
     
    195240                # load the new map 
    196241                self.PC_targLoc = i.targ_coords 
    197                 self.loadMap(str(i.map)) 
     242                self.loadMap(str(i.map), True) 
    198243                return None 
    199244        # is it in our objects? 
     
    210255        #return actions 
    211256 
    212     def loadMap(self,map_file): 
     257    def loadMap(self, map_file, update): 
    213258        """Load a new map. TODO: needs some error checking 
    214259           @type map_file: string 
    215260           @param map_file: Name of map file to load 
    216            @return: None""" 
    217         # first we let FIFE load the rest of the map 
     261           @type update: bool 
     262           @param update: whether or not to update the saver 
     263           @return: None""" 
     264        # first we update anything that has to be updated (npcs, objects, etc) 
     265        if update: 
     266            self.updateSaver() 
     267        # then we let FIFE load the rest of the map 
    218268        self.view.load(map_file) 
    219269        # then we update FIFE with the PC, NPC and object details 
    220270        self.reset() 
    221         self.loadObjects(map_file[:-4]+"_objects.xml") 
     271        # we set the current map in the saver 
     272        self.saver.setCurMap(map_file[:-4]) 
     273        # we have to check if we have saved a previous version of this filename 
     274        savedData = self.saver.getData(map_file[:-4]) 
     275        # then we update FIFE with the PC, NPC and object details 
     276        if savedData: 
     277            self.loadFromSaved(savedData) 
     278        else: 
     279            self.loadObjects(map_file[:-4]+"_objects.xml") 
    222280 
    223281    def handleMouseClick(self,position): 
     
    227285           @return: None""" 
    228286        self.PC.run(position) 
    229  
  • trunk/PARPG/scripts/hud.py

    r147 r148  
    4444        """Initialize and show the main HUD 
    4545           @return: None""" 
    46         self.events_to_map = {"menuButton":self.displayMenu, "saveButton":self.saveGame, 
     46        self.events_to_map = {"menuButton":self.displayMenu, 
    4747                              "loadButton":self.loadGame} 
    4848        self.hud.mapEvents(self.events_to_map)  
     
    128128           @return: None""" 
    129129        self.main_menu = pychan.loadXML("gui/hud_main_menu.xml") 
    130         self.menu_events = {"resumeButton":self.hideMenu, "saveButton":self.saveGame, 
     130        self.menu_events = {"resumeButton":self.hideMenu, 
    131131                            "loadButton":self.loadGame,  
    132132                            "optionsButton":self.displayOptions, 
  • trunk/PARPG/scripts/world.py

    r147 r148  
    2929 
    3030TDS = Setting() 
     31SAVE_FILE = 'my-save.pic' 
    3132 
    3233# this file should be the meta-file for all FIFE-related code 
     
    6869        self.hud = hud.Hud(self.engine, TDS) 
    6970        self.hud.events_to_map["inventoryButton"] = cbwa(self.displayInventory, True) 
     71        self.hud.events_to_map["saveButton"] = self.saveGame 
     72        self.hud.events_to_map["loadButton"] = self.loadGame 
    7073        self.hud.hud.mapEvents(self.hud.events_to_map) 
    7174        self.hud.menu_events["quitButton"] = self.quitGame 
     75        self.hud.menu_events["saveButton"] = self.saveGame 
     76        self.hud.menu_events["loadButton"] = self.loadGame 
    7277        self.hud.main_menu.mapEvents(self.hud.menu_events) 
    7378        self.action_number = 1 
     
    312317            for obj in i: 
    313318                # check to see if this in our list at all 
    314                 if(self.data.objectActive(obj.getId())!=False): 
     319                item = self.data.objectActive(obj.getId()) 
     320                if(item!=False): 
    315321                    # yes, so outline     
    316322                    self.outline_render.addOutlined(obj, 0, 137, 255, 2) 
    317323                    # get the text 
    318                     item = self.data.objectActive(obj.getId()) 
    319                     if(item != False): 
    320                         self.displayObjectText(obj, item.text) 
     324                    self.displayObjectText(obj, item.text) 
    321325        else: 
    322326            # erase the outline 
     
    357361            self.quitFunction() 
    358362 
     363    def saveGame(self): 
     364        """ Called when the user wants to save the game. 
     365            TODO: allow the user to select a file 
     366            @return: None""" 
     367        self.data.save(SAVE_FILE) 
     368 
     369    def loadGame(self): 
     370        """ Called when the user wants to load a game. 
     371            TODO: allow the user to select a file 
     372            @return: None""" 
     373        self.data.load(SAVE_FILE) 
     374 
    359375    def pump(self): 
    360376        """Routine called during each frame. Our main loop is in ./run.py 
Note: See TracChangeset for help on using the changeset viewer.