source: branches/active/character_customization/game/parpg/gamemap.py @ 755

Revision 755, 6.5 KB checked in by aspidites, 8 years ago (diff)

Patch by Aspidites:

  • no longer generates settings.xml files
  • removed more references to old Settings API from model classes
  • 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 fife.extensions.loaders import loadMapFile
21
22class GameMap(fife.MapChangeListener):
23    """Map class used to flag changes in the map"""
24    def __init__(self, engine, model):
25        # init mapchange listener
26        fife.MapChangeListener.__init__(self)
27        self.map = None
28        self.engine = engine
29        self.model = model
30
31        # init map attributes
32        self.my_cam_id = None
33        self.cameras = {}
34        self.agent_layer = None
35        self.top_layer = None
36        self.fife_model = engine.getModel()
37        self.transitions = []
38        self.cur_cam2_x = 0
39        self.initial_cam2_x = 0
40        self.cam2_scrolling_right = True
41        self.target_rotation = 0
42        self.outline_renderer = None
43       
44    def reset(self):
45        """Reset the model to default settings.
46           @return: None"""
47        # We have to delete the map in Fife.
48        if self.map:
49            self.model.deleteObjects()
50            self.model.deleteMap(self.map)
51        self.transitions = []
52        self.map = None
53        self.agent_layer = None       
54        self.top_layer = None
55        # We have to clear the cameras in the view as well, or we can't reuse
56        # camera names like 'main'
57        #self.view.clearCameras()
58        self.initial_cam2_x = 0
59        self.cam2_scrolling_right = True
60        #self.cameras = {}
61        self.cur_cam2_x = 0
62        self.target_rotation = 0
63        self.outline_renderer = None
64       
65    def makeActive(self):
66        """Makes this map the active one.
67           @return: None"""
68        self.cameras[self.my_cam_id].setEnabled(True)
69       
70    def load(self, filename):
71        """Load a map given the filename.
72           @type filename: String
73           @param filename: Name of map to load
74           @return: None"""
75        self.reset()
76        self.map = loadMapFile(filename, self.engine)
77        self.agent_layer = self.map.getLayer('ObjectLayer')
78        self.top_layer = self.map.getLayer('TopLayer')     
79           
80        # it's possible there's no transition layer
81        size = len('TransitionLayer')
82        for layer in self.map.getLayers():
83            # could be many layers, but hopefully no more than 3
84            if(layer.getId()[:size] == 'TransitionLayer'):
85                self.transitions.append(self.map.getLayer(layer.getId()))
86
87        """ Initialize the camera.
88        Note that if we have more than one camera in a map file
89        we will have to rework how self.my_cam_id works. To make sure
90        the proper camera is set as the 'main' camera.
91        At this point we also set the viewport to the current resolution."""
92        for cam in self.map.getCameras():
93            width = self.model.settings.fife.ScreenWidth
94            height = self.model.settings.fife.ScreenHeight
95            viewport = fife.Rect(0, 0, width, height)
96            cam.setViewPort(viewport)
97            self.my_cam_id = cam.getId()
98            self.cameras[self.my_cam_id] = cam
99            cam.resetRenderers()
100       
101        self.target_rotation = self.cameras[self.my_cam_id].getRotation()
102
103        self.outline_renderer = fife.InstanceRenderer.\
104                                        getInstance(
105                                                    self.cameras[
106                                                                 self.my_cam_id
107                                                                 ])
108
109        # set the render text
110        rend = fife.FloatingTextRenderer.getInstance(self.cameras[
111                                                                  self.my_cam_id
112                                                                  ])
113        text = self.engine.getGuiManager().\
114                        createFont('fonts/rpgfont.png', 0, \
115                                   self.model.settings.fife.FontGlyphs)
116        rend.changeDefaultFont(text)
117        rend.activateAllLayers(self.map)
118        rend.setEnabled(True)
119       
120        # Activate the grid renderer on all layers
121        rend = self.cameras['map_camera'].getRenderer('GridRenderer')
122        rend.activateAllLayers(self.map)
123         
124        # Activate the grid renderer on all layers
125        rend = fife.CoordinateRenderer.getInstance(self.cameras[
126                                                                  self.my_cam_id
127                                                                  ])
128        rend.setColor(0, 0, 0)
129        rend.addActiveLayer(self.map.getLayer("GroundLayer"))
130
131        # Make World aware that this is now the active map.
132        self.model.active_map = self
133
134    def addPC(self):
135        """Add the player character to the map
136           @return: None"""
137        # Update gamestate.player_character
138        self.model.game_state.player_character.behaviour.onNewMap(self.agent_layer)
139        self.centerCameraOnPlayer()
140
141    def toggleRenderer(self, r_name):
142        """Enable or disable a renderer.
143           @return: None"""
144        renderer = self.cameras[self.my_cam_id].getRenderer(str(r_name))
145        renderer.setEnabled(not renderer.isEnabled())
146
147    def isPaused(self):
148        """Returns wheter the map is currentply paused or not"""
149        # Time multiplier is a float, never do equals on floats
150        return not self.map.getTimeMultiplier() >= 1.0
151   
152    def pause(self, paused):
153        """ Pause/Unpause the game.
154        @return: nothing"""
155        if paused:
156            self.map.setTimeMultiplier(0.0)
157        if not paused and self.isPaused():
158            self.map.setTimeMultiplier(1.0)
159       
160    def togglePause(self):
161        """ Toggle paused state.
162        @return: nothing"""
163        self.pause(not self.isPaused())
164
165    def centerCameraOnPlayer(self):
166        """Center the camera on the player"""
167        camera = self.cameras[self.my_cam_id]
168        player_agent = self.model.game_state.player_character.behaviour.agent
169        camera.setLocation(player_agent.getLocation())
Note: See TracBrowser for help on using the repository browser.