Changeset 356 for trunk/game/scripts


Ignore:
Timestamp:
10/31/09 15:11:33 (10 years ago)
Author:
Kaydeth_parpg
Message:

Ticket #73. Patch by Kaydeth. Most of the changes were made to the map loading logic in the Engine, World, and Map classes to make sure it could handle loading maps after the game state has been restored by a load game. Also added a floating door back to both maps so map changing can still be tested in game. fixes[s:trac, t:73]

Location:
trunk/game/scripts
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/game/scripts/engine.py

    r341 r356  
    5454        fname = '/'.join([path, filename]) 
    5555        try: 
    56             f = open(fname, 'w') 
     56            f = open(fname, 'wb') 
    5757        except(IOError): 
    5858            sys.stderr.write("Error: Can't find save game: " + fname + "\n") 
    5959            return 
     60         
     61        # save the PC coordinates before we destroy the behaviour 
     62        coords = self.game_state.PC.behaviour.agent.getLocation().\ 
     63                    getMapCoordinates() 
     64        self.game_state.saved_pc_coordinates = (coords.x, coords.y) 
    6065         
    6166        # can't pickle SwigPyObjects 
     
    8489        self.game_state.PC.behaviour = behaviour_player 
    8590 
    86     def load(self, filename): 
     91    def load(self, path, filename): 
    8792        """Loads a saver from a file. 
    8893           @type filename: string 
    8994           @param filename: the name of the file (including path) to load from 
    9095           @return: None""" 
    91          
    92         # TODO 
    93         # Remove what we are currently working with  
    94         print "TODO: load" 
    95         self.view.active_map.view.clearCameras() 
    96         self.view.active_map.model.deleteMaps() 
    97          
     96             
     97        fname = '/'.join([path, filename]) 
     98 
    9899        try: 
    99             f = open(filename, 'r') 
     100            f = open(fname, 'rb') 
    100101        except(IOError): 
    101102            sys.stderr.write("Error: Can't find save game file\n") 
    102103            return 
     104         
     105        #Remove all currently loaded maps so we can start fresh 
     106        self.view.deleteMaps(); 
     107         
    103108        self.game_state = pickle.load(f) 
    104109        f.close() 
    105110         
    106         # Make sure we're on the right map 
     111        # Recreate all the behaviours. These can't be saved because FIFE 
     112        # objects cannot be pickled 
     113        for map_id in self.game_state.objects: 
     114            for (object_id, npc) in self.game_state.objects[map_id].items(): 
     115                if npc.trueAttr("NPC"): 
     116                    npc.createBehaviour(self.view.active_map.agent_layer) 
     117 
     118        # Fix the player behaviour 
     119        self.game_state.PC.createBehaviour(self.view.active_map.agent_layer) 
     120         
     121        #In most maps we'll create the PC Instance internally. In these 
     122        #cases we need a target position 
     123        self.target_position = self.game_state.saved_pc_coordinates 
     124         
     125        #Load the current map 
    107126        if self.game_state.current_map_file: 
    108127            self.loadMap(self.game_state.current_map_name, \ 
    109128                         self.game_state.current_map_file)  
    110  
     129             
    111130    def createObject (self, layer, attributes, instance): 
    112131        """Create an object and add it to the current map. 
     
    258277        self.view.loadMap(map_name, str(map_file)) 
    259278 
    260         # If the map has been loaded, we might need to add some 
    261         # agents to the PC and NPS 
    262         for map_id in self.game_state.objects: 
    263             for (object_id, npc) in self.game_state.objects[map_id].items(): 
    264                 if npc.trueAttr("NPC") and npc.behaviour == None: 
    265                     npc.createBehaviour(self.view.active_map.agent_layer) 
    266  
    267         # Fix the player behaviour 
    268         if self.game_state.PC.behaviour == None: 
    269             self.game_state.PC.createBehaviour(self.view.active_map.agent_layer) 
    270  
    271279        # create the PC agent 
    272280        self.view.active_map.addPC() 
  • trunk/game/scripts/filebrowser.py

    r313 r356  
    113113        self._widget.hide() 
    114114        selection = self._widget.collectData('fileList') 
    115         data = self._widget.collectData('saveField') 
    116115 
    117116        if self.save_file: 
     117            data = self._widget.collectData('saveField') 
    118118            if data: 
    119119                if (data.split('.')[1] == 'dat'): 
  • trunk/game/scripts/gamestate.py

    r314 r356  
    2626        self.current_map_file = None 
    2727        self.current_map_name = None 
     28        #these coordinates are only used for saving and loading the game. 
     29        #They do not represent the pc's active location 
     30        self.saved_pc_coordinates = (0,0) 
    2831         
    2932    def getObjectsFromMap(self, map_id): 
     
    4548        if obj_id in self.objects[map_id]: 
    4649            return self.objects[map_id][obj_id] 
    47  
     50         
  • trunk/game/scripts/map.py

    r340 r356  
    8080        self.map = loadMapFile(filename, self.engine, self.data) 
    8181        self.agent_layer = self.map.getLayer('ObjectLayer') 
    82  
    83         # if this is not the very first map load in the game carry over the PC instance 
    84         if self.data.target_position: 
     82         
     83        #find out if a PC instance was created when the map was loaded 
     84        found = False 
     85        for inst in self.agent_layer.getInstances(): 
     86            if inst.getId() == "PC": 
     87                found = True 
     88                break 
     89                 
     90        #If there is not a PC instance created yet than we construct the PC 
     91        #instance from what we have saved in the PC Game Object 
     92        if not found: 
    8593            x = float(self.data.target_position[0]) 
    8694            y = float(self.data.target_position[1]) 
     
    9098                                            fife.ExactModelCoordinate(x,y,z),\ 
    9199                                            "PC") 
    92             inst.setRotation(self.data.game_state.PC.behaviour.agent.getRotation()) 
    93100            fife.InstanceVisual.create(inst) 
    94  
     101        #If the PC instance exists already then make sure it's set to correct 
     102        #location for this new map 
     103        elif self.data.target_position is not None: 
     104            pos = self.data.target_position 
     105            coord = fife.DoublePoint3D(float(pos[0]), float(pos[1]), 0) 
     106            location = fife.Location(self.agent_layer) 
     107            location.setMapCoordinates(coord) 
     108            inst.setLocation(location) 
     109        #else we are loading the first map and the PC position were set by 
     110        #the coordinates in the Map file 
     111             
    95112        # it's possible there's no transition layer 
    96113        size = len('TransitionLayer') 
     
    120137                                                       strip=False))) 
    121138        rend.changeDefaultFont(text) 
    122  
     139         
    123140        # Make World aware that this is now the active map. 
    124141        self.data.view.active_map = self 
  • trunk/game/scripts/objects/actors.py

    r340 r356  
    6868        self.speed = float(TDS.readSetting("PCSpeed")) # TODO: rework/improve 
    6969        self.nextAction = None 
     70        self.agent = None 
    7071         
    7172    def onInstanceActionFinished(self, instance, action): 
     
    9091        """Sets the agent onto the new layer. 
    9192        """ 
    92         self.agent.removeActionListener(self) 
     93        if self.agent is not None: 
     94            self.agent.removeActionListener(self) 
     95             
    9396        self.agent = layer.getInstance(self.parent.ID) 
    9497        self.agent.addActionListener(self) 
  • trunk/game/scripts/world.py

    r340 r356  
    235235           @return: None""" 
    236236        click = fife.ScreenPoint(evt.getX(), evt.getY()) 
    237         i=self.active_map.cameras[self.active_map.my_cam_id].getMatchingInstances(click, \ 
    238                                                 self.active_map.agent_layer) 
     237        i=self.active_map.cameras[self.active_map.my_cam_id].\ 
     238                getMatchingInstances(click, self.active_map.agent_layer) 
    239239        # no object returns an empty tuple 
    240240        if(i != ()): 
     
    248248                    obj_map_coords = \ 
    249249                                      obj.getLocation().getMapCoordinates() 
    250                     obj_screen_coords = self.active_map.cameras["main"]\ 
    251                                     .toScreenCoordinates(obj_map_coords) 
     250                    obj_screen_coords = self.active_map.\ 
     251                        cameras[self.active_map.my_cam_id]\ 
     252                        .toScreenCoordinates(obj_map_coords) 
    252253                     
    253254                    if obj_screen_coords.y > front_y: 
     
    274275           @rtype: fife.Location 
    275276           @return: The map coordinates""" 
    276         coord = self.active_map.cameras[self.active_map.my_cam_id].toMapCoordinates(click, False) 
     277        coord = self.active_map.cameras[self.active_map.my_cam_id].\ 
     278                    toMapCoordinates(click, False) 
    277279        coord.z = 0 
    278280        location = fife.Location(self.active_map.agent_layer) 
    279281        location.setMapCoordinates(coord) 
    280282        return location 
     283     
     284    def deleteMaps(self): 
     285        """Clear all currently loaded maps from FIFE as well as clear our 
     286            local map cache 
     287            @return: nothing""" 
     288        self.engine.getView().clearCameras() 
     289        self.engine.getModel().deleteMaps() 
     290        self.engine.getModel().deleteObjects() 
     291 
     292        self.maps = {} 
    281293 
    282294    def pump(self): 
Note: See TracChangeset for help on using the changeset viewer.