Ticket #66: map_change.patch

File map_change.patch, 11.7 KB (added by Saritor, 10 years ago)

This patch implements Kaydeth's camera fix as well as a fix for letting us be able to remove the PC entry on individual map files. However the very first map a player starts the game on will need such an entry.

  • scripts/objects/actors.py

     
    8989    def onNewMap(self, layer): 
    9090        """Sets the agent onto the new layer. 
    9191        """ 
    92         self.agent = layer.getInstance(self.parent.name) 
     92        self.agent.removeActionListener(self) 
     93        self.agent = layer.getInstance(self.parent.ID) 
    9394        self.agent.addActionListener(self) 
    9495        self.state = _AGENT_STATE_NONE 
    9596        self.idle_counter = 1 
  • scripts/engine.py

     
    4545        self.target_position = None 
    4646        self.target_map_name = None 
    4747        self.target_map_file = None 
    48          
    49     def reset(self): 
    50         """Clears the data on a map reload so we don't have objects/npcs from 
    51            other maps hanging around. 
    52            @return: None""" 
    5348 
    5449    def save(self, path, filename): 
    5550        """Writes the saver to a file. 
     
    259254        self.game_state.current_map_file = map_file 
    260255        self.game_state.current_map_name = map_name 
    261256        self.view.loadMap(map_name, str(map_file)) 
    262         self.view.setActiveMap(map_name) 
    263         self.reset() 
    264          
     257 
    265258        # If the map has been loaded, we might need to add some 
    266259        # agents to the PC and NPS 
    267260        for map_id in self.game_state.objects: 
     
    274267            self.game_state.PC.createBehaviour(self.view.active_map.agent_layer) 
    275268 
    276269        # create the PC agent 
    277         self.view.active_map.addPC(self.game_state.PC.behaviour.agent) 
     270        self.view.active_map.addPC() 
    278271        self.game_state.PC.start() 
    279272 
    280273    def handleMouseClick(self,position): 
  • scripts/map.py

     
    3131        self.map = None 
    3232        self.engine = engine 
    3333        self.data = data 
    34          
     34 
    3535        # init map attributes 
     36        self.my_cam_id = None 
    3637        self.cameras = {} 
    3738        self.agent_layer = None 
    3839        self.model = engine.getModel() 
     
    5455            self.model.deleteMap(self.map) 
    5556        self.transitions = [] 
    5657        self.map = None 
    57         self.agent_layer = None 
     58        self.agent_layer = None         
    5859        # We have to clear the cameras in the view as well, or we can't reuse 
    5960        # camera names like 'main' 
    60         self.view.clearCameras() 
    61         self.cameras = {} 
    62         self.cur_cam2_x = 0 
     61        #self.view.clearCameras() 
    6362        self.initial_cam2_x = 0 
    6463        self.cam2_scrolling_right = True 
     64        #self.cameras = {} 
     65        self.cur_cam2_x = 0 
    6566        self.target_rotation = 0 
    6667        self.outline_renderer = None 
    6768         
    6869    def makeActive(self): 
    6970        """Makes this map the active one. 
    7071        """ 
    71         pass 
     72        self.cameras[self.my_cam_id].setEnabled(True) 
     73 
    7274         
    7375    def load(self, filename): 
    7476        """Load a map given the filename. 
     
    7779           @return: None""" 
    7880        self.reset() 
    7981        self.map = loadMapFile(filename, self.engine, self.data) 
    80           
    81         # there must be a PC object on the objects layer! 
    8282        self.agent_layer = self.map.getLayer('ObjectLayer') 
    83          
     83 
     84        # if this is not the very first map load in the game carry over the PC instance 
     85        if self.data.target_position: 
     86            x = float(self.data.target_position[0]) 
     87            y = float(self.data.target_position[1]) 
     88            z = 0 
     89            pc_obj = self.model.getObject("player", "PARPG") 
     90            inst = self.agent_layer.createInstance(pc_obj,\ 
     91                                            fife.ExactModelCoordinate(x,y,z),\ 
     92                                            "PC") 
     93            inst.setRotation(self.data.game_state.PC.behaviour.agent.getRotation()) 
     94            fife.InstanceVisual.create(inst) 
     95 
    8496        # it's possible there's no transition layer 
    8597        size = len('TransitionLayer') 
    8698        for layer in self.map.getLayers(): 
    8799            # could be many layers, but hopefully no more than 3 
    88100            if(layer.getId()[:size] == 'TransitionLayer'): 
    89101                self.transitions.append(self.map.getLayer(layer.getId())) 
    90                  
    91         # init the camera 
     102 
     103        """ Initialize the camera. 
     104        Note that if we have more than one camera in a map file 
     105        we will have to rework how self.my_cam_id works. To make sure 
     106        the proper camera is set as the 'main' camera.""" 
    92107        for cam in self.view.getCameras(): 
    93             self.cameras[cam.getId()] = cam 
     108            self.my_cam_id = cam.getId() 
     109            self.cameras[self.my_cam_id] = cam 
    94110        self.view.resetRenderers() 
    95         self.target_rotation = self.cameras['main'].getRotation() 
    96          
     111        self.target_rotation = self.cameras[self.my_cam_id].getRotation() 
     112 
    97113        self.outline_render = fife.InstanceRenderer.\ 
    98                                         getInstance(self.cameras['main']) 
    99          
     114                                        getInstance(self.cameras[self.my_cam_id]) 
     115 
    100116        # set the render text 
    101         rend = fife.FloatingTextRenderer.getInstance(self.cameras['main']) 
     117        rend = fife.FloatingTextRenderer.getInstance(self.cameras[self.my_cam_id]) 
    102118        text = self.engine.getGuiManager().\ 
    103119                        createFont('fonts/rpgfont.png', 0, \ 
    104120                                   str(TDS.readSetting("FontGlyphs", \ 
    105121                                                       strip=False))) 
    106122        rend.changeDefaultFont(text) 
     123 
     124        # Make World aware that this is now the active map. 
     125        self.data.view.active_map = self 
    107126                 
    108     def addPC(self, agent): 
     127    def addPC(self): 
    109128        """Add the player character to the map 
    110129           @type agent: Fife.instance of PC 
    111130           @return: None""" 
     131 
     132        # Update gamestate.PC 
     133        self.data.game_state.PC.behaviour.onNewMap(self.agent_layer) 
     134 
    112135        # actually this is real easy, we just have to 
    113136        # attach the main camera to the PC, if a camera 
    114137        # was already used, we simply recycle it.  
    115         if self.cameras['main'].getAttached() == None: 
    116             self.cameras['main'].attach(agent) 
     138        if self.cameras[self.my_cam_id].getAttached() == None: 
     139            self.cameras[self.my_cam_id].attach(self.data.game_state.PC.behaviour.agent) 
    117140 
    118141         
    119142    def toggle_renderer(self, r_name): 
    120143        """Enable or disable a renderer. 
    121144           @return: None""" 
    122         renderer = self.cameras['main'].getRenderer(str(r_name)) 
     145        renderer = self.cameras[self.my_cam_id].getRenderer(str(r_name)) 
    123146        renderer.setEnabled(not renderer.isEnabled()) 
    124147 
  • scripts/world.py

     
    9898         
    9999    def loadMap(self, map_name, filename): 
    100100        """Loads a map and stores it under the given name in the maps list. 
    101            @type map_name: text 
     101           @type map_name: String 
    102102           @param map_name: The name of the map to load  
    103            @type filename: text 
     103           @type filename: String 
    104104           @param filename: File which contains the map to be loaded 
    105            @return: None 
    106         """ 
     105           @return: None""" 
    107106        if not map_name in self.maps: 
    108             """Need to set active map before we load it because the map  
    109             loader uses call backs that expect to find an active map.  
    110             This needs to be reworked. 
    111             """ 
    112107            map = Map(self.engine, self.data) 
    113108            self.maps[map_name] = map         
    114             self.setActiveMap(map_name) 
    115109            map.load(filename) 
     110        else: 
     111            self.setActiveMap(map_name) 
    116112     
    117113    def setActiveMap(self, map_name): 
    118114        """Sets the active map that is to be rendered. 
    119            @type map_name: text 
     115           @type map_name: String 
    120116           @param map_name: The name of the map to load  
    121            @return: None 
    122         """ 
     117           @return: None""" 
     118        # Turn off the camera on the old map before we turn on the camera 
     119        # on the new map. 
     120        self.active_map.cameras[self.active_map.my_cam_id].setEnabled(False) 
     121        # Make the new map active. 
    123122        self.active_map = self.maps[map_name] 
    124123        self.active_map.makeActive() 
    125124 
     
    127126        """Display on screen the text of the object over the object. 
    128127           @type obj: fife.instance 
    129128           @param obj: object to draw over 
    130            @type text: text 
     129           @type text: String 
    131130           @param text: text to display over object 
    132131           @return: None""" 
    133132        obj.say(str(text), 1000) 
     
    192191            self.data.handleMouseClick(self.getCoords(scr_point))       
    193192        elif(evt.getButton() == fife.MouseEvent.RIGHT): 
    194193            # is there an object here? 
    195             instances = self.active_map.cameras['main'].\ 
     194            instances = self.active_map.cameras[self.active_map.my_cam_id].\ 
    196195                            getMatchingInstances(scr_point, \ 
    197196                                                 self.active_map.agent_layer) 
    198197            info = None 
     
    221220           to be created as its own method down the road. 
    222221           @type position: String Tuple 
    223222           @param position: X,Y coordinates passed from engine.changeMap 
    224            @return: fife.Location 
    225         """ 
     223           @return: fife.Location""" 
    226224        coord = fife.DoublePoint3D(float(position[0]), float(position[1]), 0) 
    227225        location = fife.Location(self.active_map.agent_layer) 
    228226        location.setMapCoordinates(coord) 
     
    234232           @param evt: The event that fife caught 
    235233           @return: None""" 
    236234        click = fife.ScreenPoint(evt.getX(), evt.getY()) 
    237         i=self.active_map.cameras['main'].getMatchingInstances(click, \ 
     235        i=self.active_map.cameras[self.active_map.my_cam_id].getMatchingInstances(click, \ 
    238236                                                self.active_map.agent_layer) 
    239237        # no object returns an empty tuple 
    240238        if(i != ()): 
     
    273271           @param click: Screen coordinates 
    274272           @rtype: fife.Location 
    275273           @return: The map coordinates""" 
    276         coord = self.active_map.cameras["main"].toMapCoordinates(click, False) 
     274        coord = self.active_map.cameras[self.active_map.my_cam_id].toMapCoordinates(click, False) 
    277275        coord.z = 0 
    278276        location = fife.Location(self.active_map.agent_layer) 
    279277        location.setMapCoordinates(coord) 
  • maps/map2.xml

     
    10321032            <i x="-0" o="shantyh" z="0.0" y="6" r="0"></i> 
    10331033            <i x="1" o="shantyi" z="0.0" y="6" r="0"></i> 
    10341034 
    1035             <!-- There must be one and one only PC character --> 
    1036             <i x="0.0" o="player" z="0.0" y="0.0" r="0" id="PC" object_type="PlayerCharacter"></i> 
    1037  
    10381035            <!-- A sample door --> 
    10391036            <i x="-2.0" o="shanty-door" z="0.0" y="6.0" r="0" id="shanty-door01" object_type="ShantyDoor" is_open="False" 
    10401037                name="Shanty Door" text="Looks like the entrance to the building." target_map_name="main-map" target_map="maps/map.xml" 
     
    10421039 
    10431040        </instances> 
    10441041    </layer> 
    1045         <camera ref_cell_width="72" zoom="1.0" tilt="-60.0" id="main" ref_layer_id="ObjectLayer" ref_cell_height="38" rotation="45.0"> 
     1042        <camera ref_cell_width="72" zoom="1.0" tilt="-60.0" id="map2" ref_layer_id="ObjectLayer" ref_cell_height="38" rotation="45.0"> 
    10461043        </camera> 
    10471044</map> 
    10481045