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
File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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  
Note: See TracChangeset for help on using the changeset viewer.