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

Revision 501, 7.0 KB checked in by beliar, 10 years ago (diff)

Patch by barra_parpg, fixes black game screen issue that appeared after recent fife changes(http://fife.trac.cvsdude.com/engine/changeset/3228)

  • 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
18from fife import fife
19import time
20from local_loaders.loaders import loadMapFile
21from scripts.common.eventlistenerbase import EventListenerBase
22from settings import Setting
23
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.top_layer = None
40        self.model = engine.getModel()
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        self.top_layer = None
59        # We have to clear the cameras in the view as well, or we can't reuse
60        # camera names like 'main'
61        #self.view.clearCameras()
62        self.initial_cam2_x = 0
63        self.cam2_scrolling_right = True
64        #self.cameras = {}
65        self.cur_cam2_x = 0
66        self.target_rotation = 0
67        self.outline_renderer = None
68       
69    def makeActive(self):
70        """Makes this map the active one.
71           @return: None"""
72        self.cameras[self.my_cam_id].setEnabled(True)
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        self.top_layer = self.map.getLayer('TopLayer')
83       
84        #find out if a PC instance was created when the map was loaded
85        found = False
86        for inst in self.agent_layer.getInstances():
87            if inst.getId() == "PC":
88                found = True
89                break
90               
91        #If there is not a PC instance created yet than we construct the PC
92        #instance from what we have saved in the PC Game Object
93        if not found:
94            x = float(self.data.target_position[0])
95            y = float(self.data.target_position[1])
96            z = 0
97            pc_obj = self.model.getObject("player", "PARPG")
98            inst = self.agent_layer.createInstance(pc_obj,\
99                                            fife.ExactModelCoordinate(x,y,z),\
100                                            "PC")
101            fife.InstanceVisual.create(inst)
102        #If the PC instance exists already then make sure it's set to correct
103        #location for this new map
104        elif self.data.target_position is not None:
105            pos = self.data.target_position
106            coord = fife.DoublePoint3D(float(pos[0]), float(pos[1]), 0)
107            location = fife.Location(self.agent_layer)
108            location.setMapCoordinates(coord)
109            inst.setLocation(location)
110        #else we are loading the first map and the PC position were set by
111        #the coordinates in the Map file
112           
113        # it's possible there's no transition layer
114        size = len('TransitionLayer')
115        for layer in self.map.getLayers():
116            # could be many layers, but hopefully no more than 3
117            if(layer.getId()[:size] == 'TransitionLayer'):
118                self.transitions.append(self.map.getLayer(layer.getId()))
119
120        """ Initialize the camera.
121        Note that if we have more than one camera in a map file
122        we will have to rework how self.my_cam_id works. To make sure
123        the proper camera is set as the 'main' camera.
124        At this point we also set the viewport to the current resolution."""
125        for cam in self.map.getCameras():
126            width = int(TDS.readSetting(name="ScreenWidth"))
127            height = int(TDS.readSetting(name="ScreenHeight"))
128            viewport = fife.Rect(0,0,width,height)
129            cam.setViewPort(viewport)
130            self.my_cam_id = cam.getId()
131            self.cameras[self.my_cam_id] = cam
132            cam.resetRenderers()
133       
134        self.target_rotation = self.cameras[self.my_cam_id].getRotation()
135
136        self.outline_render = fife.InstanceRenderer.\
137                                        getInstance(self.cameras[self.my_cam_id])
138
139        # set the render text
140        rend = fife.FloatingTextRenderer.getInstance(self.cameras[self.my_cam_id])
141        text = self.engine.getGuiManager().\
142                        createFont('fonts/rpgfont.png', 0, \
143                                   str(TDS.readSetting("FontGlyphs", \
144                                                       strip=False)))
145        rend.changeDefaultFont(text)
146        rend.activateAllLayers(self.map)
147         
148        # Activate the grid renderer on all layers
149        rend = self.cameras['map_camera'].getRenderer('GridRenderer')
150        rend.activateAllLayers(self.map)
151         
152        # Make World aware that this is now the active map.
153        self.data.view.active_map = self
154
155    def addPC(self):
156        """Add the player character to the map
157           @return: None"""
158        # Update gamestate.PC
159        self.data.game_state.PC.behaviour.onNewMap(self.agent_layer)
160
161        # actually this is real easy, we just have to
162        # attach the main camera to the PC, if a camera
163        # was already used, we simply recycle it.
164        if self.cameras[self.my_cam_id].getAttached() == None:
165            self.cameras[self.my_cam_id].attach(self.data.game_state.PC.behaviour.agent)
166
167    def toggleRenderer(self, r_name):
168        """Enable or disable a renderer.
169           @return: None"""
170        renderer = self.cameras[self.my_cam_id].getRenderer(str(r_name))
171        renderer.setEnabled(not renderer.isEnabled())
172
173    def togglePause(self):
174        """ Pause/Unpause the game.
175            @return: nothing"""
176        # Time multiplier is a float, never do equals on floats
177        if self.map.getTimeMultiplier() >= 1.0:
178            self.map.setTimeMultiplier(0.0)
179        else:
180            self.map.setTimeMultiplier(1.0)
Note: See TracBrowser for help on using the repository browser.