source: trunk/game/scripts/gamemap.py @ 627

Revision 627, 6.2 KB checked in by beliar, 9 years ago (diff)

Patch by Beliar.

  • Added SettingsVersion? to settings. This contains the version (currently a date of the last change) of the settings. The version will be checked with the version in settings-dist.xml and if the versions don't match the settings.xml file will be renamed to settings-old.xml. The version string has to be set manually when editing settings-dist.xml.
  • Property svn:eol-style set to native
Line 
1#!/usr/bin/env 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
19
20from local_loaders.loaders import loadMapFile
21from scripts.common.utils import loadSettings
22
23
24Settings = loadSettings()
25
26class GameMap(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.model = 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.fife_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 model 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.model)
81        self.agent_layer = self.map.getLayer('ObjectLayer')
82        self.top_layer = self.map.getLayer('TopLayer')     
83           
84        # it's possible there's no transition layer
85        size = len('TransitionLayer')
86        for layer in self.map.getLayers():
87            # could be many layers, but hopefully no more than 3
88            if(layer.getId()[:size] == 'TransitionLayer'):
89                self.transitions.append(self.map.getLayer(layer.getId()))
90
91        """ Initialize the camera.
92        Note that if we have more than one camera in a map file
93        we will have to rework how self.my_cam_id works. To make sure
94        the proper camera is set as the 'main' camera.
95        At this point we also set the viewport to the current resolution."""
96        for cam in self.map.getCameras():
97            width = self.engine.getSettings().getScreenWidth()
98            height = self.engine.getSettings().getScreenHeight() 
99            viewport = fife.Rect(0, 0, width, height)
100            cam.setViewPort(viewport)
101            self.my_cam_id = cam.getId()
102            self.cameras[self.my_cam_id] = cam
103            cam.resetRenderers()
104       
105        self.target_rotation = self.cameras[self.my_cam_id].getRotation()
106
107        self.outline_renderer = fife.InstanceRenderer.\
108                                        getInstance(
109                                                    self.cameras[
110                                                                 self.my_cam_id
111                                                                 ])
112
113        # set the render text
114        rend = fife.FloatingTextRenderer.getInstance(self.cameras[
115                                                                  self.my_cam_id
116                                                                  ])
117        text = self.engine.getGuiManager().\
118                        createFont('fonts/rpgfont.png', 0, \
119                                   Settings.get("FIFE", "FontGlyphs"))
120        rend.changeDefaultFont(text)
121        rend.activateAllLayers(self.map)
122        rend.setEnabled(True)
123       
124        # Activate the grid renderer on all layers
125        rend = self.cameras['map_camera'].getRenderer('GridRenderer')
126        rend.activateAllLayers(self.map)
127         
128        # Activate the grid renderer on all layers
129        rend = fife.CoordinateRenderer.getInstance(self.cameras[
130                                                                  self.my_cam_id
131                                                                  ])
132        rend.setColor(0, 0, 0)
133        rend.addActiveLayer(self.map.getLayer("GroundLayer"))
134
135        # Make World aware that this is now the active map.
136        self.model.active_map = self
137
138    def addPC(self):
139        """Add the player character to the map
140           @return: None"""
141        # Update gamestate.player_character
142        self.model.game_state.player_character.behaviour.onNewMap(self.agent_layer)
143        self.cameras[self.my_cam_id].attach(self.model.game_state.
144                                            player_character.behaviour.agent)
145
146    def toggleRenderer(self, r_name):
147        """Enable or disable a renderer.
148           @return: None"""
149        renderer = self.cameras[self.my_cam_id].getRenderer(str(r_name))
150        renderer.setEnabled(not renderer.isEnabled())
151
152    def togglePause(self):
153        """ Pause/Unpause the game.
154            @return: nothing"""
155        # Time multiplier is a float, never do equals on floats
156        if self.map.getTimeMultiplier() >= 1.0:
157            self.map.setTimeMultiplier(0.0)
158        else:
159            self.map.setTimeMultiplier(1.0)
Note: See TracBrowser for help on using the repository browser.