comparison gamemap.py @ 0:7a89ea5404b1

Initial commit of parpg-core.
author M. George Hansen <technopolitica@gmail.com>
date Sat, 14 May 2011 01:12:35 -0700
parents
children 06145a6ee387
comparison
equal deleted inserted replaced
-1:000000000000 0:7a89ea5404b1
1 # This file is part of PARPG.
2
3 # PARPG is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation, either version 3 of the License, or
6 # (at your option) any later version.
7
8 # PARPG is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12
13 # You should have received a copy of the GNU General Public License
14 # along with PARPG. If not, see <http://www.gnu.org/licenses/>.
15
16 from fife import fife
17
18 from fife.extensions.loaders import loadMapFile
19
20 class GameMap(fife.MapChangeListener):
21 """Map class used to flag changes in the map"""
22 def __init__(self, engine, model):
23 # init mapchange listener
24 fife.MapChangeListener.__init__(self)
25 self.map = None
26 self.engine = engine
27 self.model = model
28
29 # init map attributes
30 self.my_cam_id = None
31 self.cameras = {}
32 self.agent_layer = None
33 self.top_layer = None
34 self.fife_model = engine.getModel()
35 self.transitions = []
36 self.cur_cam2_x = 0
37 self.initial_cam2_x = 0
38 self.cam2_scrolling_right = True
39 self.target_rotation = 0
40 self.outline_renderer = None
41
42 def reset(self):
43 """Reset the model to default settings.
44 @return: None"""
45 # We have to delete the map in Fife.
46 if self.map:
47 self.model.deleteObjects()
48 self.model.deleteMap(self.map)
49 self.transitions = []
50 self.map = None
51 self.agent_layer = None
52 self.top_layer = None
53 # We have to clear the cameras in the view as well, or we can't reuse
54 # camera names like 'main'
55 #self.view.clearCameras()
56 self.initial_cam2_x = 0
57 self.cam2_scrolling_right = True
58 #self.cameras = {}
59 self.cur_cam2_x = 0
60 self.target_rotation = 0
61 self.outline_renderer = None
62
63 def makeActive(self):
64 """Makes this map the active one.
65 @return: None"""
66 self.cameras[self.my_cam_id].setEnabled(True)
67
68 def load(self, filename):
69 """Load a map given the filename.
70 @type filename: String
71 @param filename: Name of map to load
72 @return: None"""
73 self.reset()
74 self.map = loadMapFile(filename, self.engine)
75 self.agent_layer = self.map.getLayer('ObjectLayer')
76 self.top_layer = self.map.getLayer('TopLayer')
77
78 # it's possible there's no transition layer
79 size = len('TransitionLayer')
80 for layer in self.map.getLayers():
81 # could be many layers, but hopefully no more than 3
82 if(layer.getId()[:size] == 'TransitionLayer'):
83 self.transitions.append(self.map.getLayer(layer.getId()))
84
85 """ Initialize the camera.
86 Note that if we have more than one camera in a map file
87 we will have to rework how self.my_cam_id works. To make sure
88 the proper camera is set as the 'main' camera.
89 At this point we also set the viewport to the current resolution."""
90 for cam in self.map.getCameras():
91 width = self.model.settings.fife.ScreenWidth
92 height = self.model.settings.fife.ScreenHeight
93 viewport = fife.Rect(0, 0, width, height)
94 cam.setViewPort(viewport)
95 self.my_cam_id = cam.getId()
96 self.cameras[self.my_cam_id] = cam
97 cam.resetRenderers()
98
99 self.target_rotation = self.cameras[self.my_cam_id].getRotation()
100
101 self.outline_renderer = fife.InstanceRenderer.\
102 getInstance(
103 self.cameras[
104 self.my_cam_id
105 ])
106
107 # set the render text
108 rend = fife.FloatingTextRenderer.getInstance(self.cameras[
109 self.my_cam_id
110 ])
111 text = self.engine.getGuiManager().\
112 createFont('fonts/rpgfont.png', 0, \
113 self.model.settings.fife.FontGlyphs)
114 rend.changeDefaultFont(text)
115 rend.activateAllLayers(self.map)
116 rend.setEnabled(True)
117
118 # Activate the grid renderer on all layers
119 rend = self.cameras['map_camera'].getRenderer('GridRenderer')
120 rend.activateAllLayers(self.map)
121
122 # Activate the grid renderer on all layers
123 rend = fife.CoordinateRenderer.getInstance(self.cameras[
124 self.my_cam_id
125 ])
126 rend.setColor(0, 0, 0)
127 rend.addActiveLayer(self.map.getLayer("GroundLayer"))
128
129 # Make World aware that this is now the active map.
130 self.model.active_map = self
131
132 def addPC(self):
133 """Add the player character to the map
134 @return: None"""
135 # Update gamestate.player_character
136 self.model.game_state.player_character.behaviour.onNewMap(self.agent_layer)
137 self.centerCameraOnPlayer()
138
139 def toggleRenderer(self, r_name):
140 """Enable or disable a renderer.
141 @return: None"""
142 renderer = self.cameras[self.my_cam_id].getRenderer(str(r_name))
143 renderer.setEnabled(not renderer.isEnabled())
144
145 def isPaused(self):
146 """Returns wheter the map is currentply paused or not"""
147 # Time multiplier is a float, never do equals on floats
148 return not self.map.getTimeMultiplier() >= 1.0
149
150 def pause(self, paused):
151 """ Pause/Unpause the game.
152 @return: nothing"""
153 if paused:
154 self.map.setTimeMultiplier(0.0)
155 if not paused and self.isPaused():
156 self.map.setTimeMultiplier(1.0)
157
158 def togglePause(self):
159 """ Toggle paused state.
160 @return: nothing"""
161 self.pause(not self.isPaused())
162
163 def centerCameraOnPlayer(self):
164 """Center the camera on the player"""
165 camera = self.cameras[self.my_cam_id]
166 player_agent = self.model.game_state.player_character.behaviour.agent
167 camera.setLocation(player_agent.getLocation())