source: trunk/game/scripts/map.py @ 357

Revision 356, 6.2 KB checked in by Kaydeth_parpg, 10 years ago (diff)

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]

  • Property svn:eol-style set to native
Line 
1#!/usr/bin/python
2
3#   This file is part of PARPG.
4
5#   PARPG is free software: you can redistribute it and/or modify
6#   it under the terms of the GNU General Public License as published by
7#   the Free Software Foundation, either version 3 of the License, or
8#   (at your option) any later version.
9
10#   PARPG is distributed in the hope that it will be useful,
11#   but WITHOUT ANY WARRANTY; without even the implied warranty of
12#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13#   GNU General Public License for more details.
14
15#   You should have received a copy of the GNU General Public License
16#   along with PARPG.  If not, see <http://www.gnu.org/licenses/>.
17
18import fife
19import time
20from local_loaders.loaders import loadMapFile
21from scripts.common.eventlistenerbase import EventListenerBase
22
23from settings import Setting
24TDS = Setting()
25
26class Map(fife.MapChangeListener):
27    """Map class used to flag changes in the map"""
28    def __init__(self, engine, data):
29        # init mapchange listener
30        fife.MapChangeListener.__init__(self)
31        self.map = None
32        self.engine = engine
33        self.data = data
34
35        # init map attributes
36        self.my_cam_id = None
37        self.cameras = {}
38        self.agent_layer = None
39        self.model = engine.getModel()
40        self.view = engine.getView()
41        self.transitions = []
42        self.cur_cam2_x = 0
43        self.initial_cam2_x = 0
44        self.cam2_scrolling_right = True
45        self.target_rotation = 0
46        self.outline_renderer = None
47       
48    def reset(self):
49        """Reset the data to default settings.
50           @return: None"""
51        # We have to delete the map in Fife.
52        if self.map:
53            self.model.deleteObjects()
54            self.model.deleteMap(self.map)
55        self.transitions = []
56        self.map = None
57        self.agent_layer = None       
58        # We have to clear the cameras in the view as well, or we can't reuse
59        # camera names like 'main'
60        #self.view.clearCameras()
61        self.initial_cam2_x = 0
62        self.cam2_scrolling_right = True
63        #self.cameras = {}
64        self.cur_cam2_x = 0
65        self.target_rotation = 0
66        self.outline_renderer = None
67       
68    def makeActive(self):
69        """Makes this map the active one.
70        """
71        self.cameras[self.my_cam_id].setEnabled(True)
72
73       
74    def load(self, filename):
75        """Load a map given the filename.
76           @type filename: String
77           @param filename: Name of map to load
78           @return: None"""
79        self.reset()
80        self.map = loadMapFile(filename, self.engine, self.data)
81        self.agent_layer = self.map.getLayer('ObjectLayer')
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:
93            x = float(self.data.target_position[0])
94            y = float(self.data.target_position[1])
95            z = 0
96            pc_obj = self.model.getObject("player", "PARPG")
97            inst = self.agent_layer.createInstance(pc_obj,\
98                                            fife.ExactModelCoordinate(x,y,z),\
99                                            "PC")
100            fife.InstanceVisual.create(inst)
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           
112        # it's possible there's no transition layer
113        size = len('TransitionLayer')
114        for layer in self.map.getLayers():
115            # could be many layers, but hopefully no more than 3
116            if(layer.getId()[:size] == 'TransitionLayer'):
117                self.transitions.append(self.map.getLayer(layer.getId()))
118
119        """ Initialize the camera.
120        Note that if we have more than one camera in a map file
121        we will have to rework how self.my_cam_id works. To make sure
122        the proper camera is set as the 'main' camera."""
123        for cam in self.view.getCameras():
124            self.my_cam_id = cam.getId()
125            self.cameras[self.my_cam_id] = cam
126        self.view.resetRenderers()
127        self.target_rotation = self.cameras[self.my_cam_id].getRotation()
128
129        self.outline_render = fife.InstanceRenderer.\
130                                        getInstance(self.cameras[self.my_cam_id])
131
132        # set the render text
133        rend = fife.FloatingTextRenderer.getInstance(self.cameras[self.my_cam_id])
134        text = self.engine.getGuiManager().\
135                        createFont('fonts/rpgfont.png', 0, \
136                                   str(TDS.readSetting("FontGlyphs", \
137                                                       strip=False)))
138        rend.changeDefaultFont(text)
139       
140        # Make World aware that this is now the active map.
141        self.data.view.active_map = self
142               
143    def addPC(self):
144        """Add the player character to the map
145           @return: None"""
146
147        # Update gamestate.PC
148        self.data.game_state.PC.behaviour.onNewMap(self.agent_layer)
149
150        # actually this is real easy, we just have to
151        # attach the main camera to the PC, if a camera
152        # was already used, we simply recycle it.
153        if self.cameras[self.my_cam_id].getAttached() == None:
154            self.cameras[self.my_cam_id].attach(self.data.game_state.PC.behaviour.agent)
155
156       
157    def toggle_renderer(self, r_name):
158        """Enable or disable a renderer.
159           @return: None"""
160        renderer = self.cameras[self.my_cam_id].getRenderer(str(r_name))
161        renderer.setEnabled(not renderer.isEnabled())
162
Note: See TracBrowser for help on using the repository browser.