Mercurial > fife-parpg
comparison demos/shooter/scripts/world.py @ 472:3164715a0621
Added the FireBallBurst and FireBallSpread weapons which are now used by the boss.
Added rotatePoint function.
author | prock@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Fri, 16 Apr 2010 21:44:18 +0000 |
parents | 7a79dc2a0592 |
children | afde89c1d50b |
comparison
equal
deleted
inserted
replaced
471:7a79dc2a0592 | 472:3164715a0621 |
---|---|
49 | 49 |
50 """ | 50 """ |
51 def __init__(self, app, engine): | 51 def __init__(self, app, engine): |
52 super(World, self).__init__(engine, regMouse=True, regKeys=True) | 52 super(World, self).__init__(engine, regMouse=True, regKeys=True) |
53 self._applictaion = app | 53 self._applictaion = app |
54 self.engine = engine | 54 self._engine = engine |
55 self.timemanager = engine.getTimeManager() | 55 self._timemanager = engine.getTimeManager() |
56 self.eventmanager = engine.getEventManager() | 56 self._eventmanager = engine.getEventManager() |
57 self.model = engine.getModel() | 57 self._model = engine.getModel() |
58 self.filename = '' | 58 self._filename = '' |
59 self.keystate = { 'UP': False, 'DOWN': False, 'LEFT': False, 'RIGHT': False, 'CTRL': False, 'SPACE': False, } | 59 self._keystate = { 'UP': False, 'DOWN': False, 'LEFT': False, 'RIGHT': False, 'CTRL': False, 'SPACE': False, } |
60 self.dynamic_widgets = {} | 60 self._pump_ctr = 0 |
61 self.pump_ctr = 0 | 61 self._map = None |
62 self.map = None | 62 self._scene = None |
63 self.scene = None | |
64 self._paused = True | 63 self._paused = True |
65 self._pausedtime = 0 | 64 self._pausedtime = 0 |
66 self._starttime = 0 | 65 self._starttime = 0 |
67 | 66 |
68 self._mainmenu = MainMenu(self) | 67 self._mainmenu = MainMenu(self) |
86 self._genericrenderer = None | 85 self._genericrenderer = None |
87 | 86 |
88 self._gamecomplete = False | 87 self._gamecomplete = False |
89 | 88 |
90 def showMainMenu(self): | 89 def showMainMenu(self): |
91 if self.scene: | 90 if self._scene: |
92 self._paused = True | 91 self._paused = True |
93 cont = True | 92 cont = True |
94 else: | 93 else: |
95 cont = False | 94 cont = False |
96 | 95 |
105 def quit(self): | 104 def quit(self): |
106 self.reset() | 105 self.reset() |
107 self._applictaion.requestQuit() | 106 self._applictaion.requestQuit() |
108 | 107 |
109 def reset(self): | 108 def reset(self): |
110 if self.map: | 109 if self._map: |
111 self.model.deleteMap(self.map) | 110 self._model.deleteMap(self._map) |
112 self.map = None | 111 self._map = None |
113 | 112 |
114 self.cameras = {} | 113 self.cameras = {} |
115 | 114 |
116 if self.scene: | 115 if self._scene: |
117 self.scene.destroyScene() | 116 self._scene.destroyScene() |
118 self.scene = None | 117 self._scene = None |
119 | 118 |
120 self._gamecomplete = False | 119 self._gamecomplete = False |
121 | 120 |
122 def loadLevel(self, filename): | 121 def loadLevel(self, filename): |
123 """ | 122 """ |
124 Load a xml map and setup cameras. | 123 Load a xml map and setup cameras. |
125 """ | 124 """ |
126 | 125 |
127 self.resetKeys() | 126 self.resetKeys() |
128 | 127 |
129 self.filename = filename | 128 self._filename = filename |
130 self.reset() | 129 self.reset() |
131 self.map = loadMapFile(self.filename, self.engine) | 130 self._map = loadMapFile(self._filename, self._engine) |
132 | 131 |
133 self.scene = Scene(self, self.engine, self.map.getLayer('objects')) | 132 self._scene = Scene(self, self._engine, self._map.getLayer('objects')) |
134 self.scene.initScene(self.map) | 133 self._scene.initScene(self._map) |
135 | 134 |
136 self.initCameras() | 135 self.initCameras() |
137 | 136 |
138 self._hudwindow.show() | 137 self._hudwindow.show() |
139 self._gameover.hide() | 138 self._gameover.hide() |
140 self._winner.hide() | 139 self._winner.hide() |
141 | 140 |
142 self._starttime = self.timemanager.getTime() | 141 self._starttime = self._timemanager.getTime() |
143 | 142 |
144 self._genericrenderer = fife.GenericRenderer.getInstance(self.cameras['main']) | 143 self._genericrenderer = fife.GenericRenderer.getInstance(self.cameras['main']) |
145 self._genericrenderer.clearActiveLayers() | 144 self._genericrenderer.clearActiveLayers() |
146 self._genericrenderer.addActiveLayer(self.map.getLayer('objects')) | 145 self._genericrenderer.addActiveLayer(self._map.getLayer('objects')) |
147 self._genericrenderer.setEnabled(True) | 146 self._genericrenderer.setEnabled(True) |
148 | 147 |
149 | 148 |
150 def renderBoundingBox(self, obj): | 149 def renderBoundingBox(self, obj): |
151 bbox = copy.copy(obj.boundingbox) | 150 bbox = copy.copy(obj.boundingbox) |
160 obj_topright = fife.ExactModelCoordinate(bbox.x + bbox.w, bbox.y) | 159 obj_topright = fife.ExactModelCoordinate(bbox.x + bbox.w, bbox.y) |
161 obj_bottomright = fife.ExactModelCoordinate(bbox.x + bbox.w, bbox.y + bbox.h) | 160 obj_bottomright = fife.ExactModelCoordinate(bbox.x + bbox.w, bbox.y + bbox.h) |
162 obj_bottomleft = fife.ExactModelCoordinate(bbox.x, bbox.y + bbox.h) | 161 obj_bottomleft = fife.ExactModelCoordinate(bbox.x, bbox.y + bbox.h) |
163 | 162 |
164 loc_topleft = fife.Location() | 163 loc_topleft = fife.Location() |
165 loc_topleft.setLayer(self.map.getLayer('boundingbox')) | 164 loc_topleft.setLayer(self._map.getLayer('boundingbox')) |
166 loc_topleft.setExactLayerCoordinates(obj_topleft) | 165 loc_topleft.setExactLayerCoordinates(obj_topleft) |
167 | 166 |
168 loc_topright = fife.Location() | 167 loc_topright = fife.Location() |
169 loc_topright.setLayer(self.map.getLayer('boundingbox')) | 168 loc_topright.setLayer(self._map.getLayer('boundingbox')) |
170 loc_topright.setExactLayerCoordinates(obj_topright) | 169 loc_topright.setExactLayerCoordinates(obj_topright) |
171 | 170 |
172 loc_bottomright = fife.Location() | 171 loc_bottomright = fife.Location() |
173 loc_bottomright.setLayer(self.map.getLayer('boundingbox')) | 172 loc_bottomright.setLayer(self._map.getLayer('boundingbox')) |
174 loc_bottomright.setExactLayerCoordinates(obj_bottomright) | 173 loc_bottomright.setExactLayerCoordinates(obj_bottomright) |
175 | 174 |
176 loc_bottomleft = fife.Location() | 175 loc_bottomleft = fife.Location() |
177 loc_bottomleft.setLayer(self.map.getLayer('boundingbox')) | 176 loc_bottomleft.setLayer(self._map.getLayer('boundingbox')) |
178 loc_bottomleft.setExactLayerCoordinates(obj_bottomleft) | 177 loc_bottomleft.setExactLayerCoordinates(obj_bottomleft) |
179 | 178 |
180 node_topleft = fife.GenericRendererNode(loc_topleft) | 179 node_topleft = fife.GenericRendererNode(loc_topleft) |
181 node_topright = fife.GenericRendererNode(loc_topright) | 180 node_topright = fife.GenericRendererNode(loc_topright) |
182 node_bottomright = fife.GenericRendererNode(loc_bottomright) | 181 node_bottomright = fife.GenericRendererNode(loc_bottomright) |
217 self._hudwindow.hide() | 216 self._hudwindow.hide() |
218 | 217 |
219 def saveScore(self): | 218 def saveScore(self): |
220 self._gamecomplete = False | 219 self._gamecomplete = False |
221 | 220 |
222 if self._highscores.isHighScore(self.scene.player.score): | 221 if self._highscores.isHighScore(self._scene.player.score): |
223 score = self.scene.player.score | 222 score = self._scene.player.score |
224 | 223 |
225 dlg = pychan.loadXML('gui/highscoredialog.xml') | 224 dlg = pychan.loadXML('gui/highscoredialog.xml') |
226 dlg.execute({ 'okay' : "Yay!" }) | 225 dlg.execute({ 'okay' : "Yay!" }) |
227 name = dlg.findChild(name='name').text | 226 name = dlg.findChild(name='name').text |
228 | 227 |
243 """ | 242 """ |
244 Before we can actually see something on screen we have to specify the render setup. | 243 Before we can actually see something on screen we have to specify the render setup. |
245 This is done through Camera objects which offer a viewport on the map. | 244 This is done through Camera objects which offer a viewport on the map. |
246 """ | 245 """ |
247 | 246 |
248 for cam in self.map.getCameras(): | 247 for cam in self._map.getCameras(): |
249 if cam.getId() == 'main': | 248 if cam.getId() == 'main': |
250 self.cameras['main'] = cam | 249 self.cameras['main'] = cam |
251 | 250 |
252 self.scene.attachCamera(self.cameras['main']) | 251 self._scene.attachCamera(self.cameras['main']) |
253 | 252 |
254 def resetKeys(self): | 253 def resetKeys(self): |
255 self.keystate['UP'] = False | 254 self._keystate['UP'] = False |
256 self.keystate['DOWN'] = False | 255 self._keystate['DOWN'] = False |
257 self.keystate['LEFT'] = False | 256 self._keystate['LEFT'] = False |
258 self.keystate['RIGHT'] = False | 257 self._keystate['RIGHT'] = False |
259 self.keystate['SPACE'] = False | 258 self._keystate['SPACE'] = False |
260 self.keystate['CTRL'] = False | 259 self._keystate['CTRL'] = False |
261 | 260 |
262 | 261 |
263 def keyPressed(self, evt): | 262 def keyPressed(self, evt): |
264 keyval = evt.getKey().getValue() | 263 keyval = evt.getKey().getValue() |
265 keystr = evt.getKey().getAsString().lower() | 264 keystr = evt.getKey().getAsString().lower() |
266 if keyval == fife.Key.UP: | 265 if keyval == fife.Key.UP: |
267 self.keystate['UP'] = True | 266 self._keystate['UP'] = True |
268 elif keyval == fife.Key.DOWN: | 267 elif keyval == fife.Key.DOWN: |
269 self.keystate['DOWN'] = True | 268 self._keystate['DOWN'] = True |
270 elif keyval == fife.Key.LEFT: | 269 elif keyval == fife.Key.LEFT: |
271 self.keystate['LEFT'] = True | 270 self._keystate['LEFT'] = True |
272 elif keyval == fife.Key.RIGHT: | 271 elif keyval == fife.Key.RIGHT: |
273 self.keystate['RIGHT'] = True | 272 self._keystate['RIGHT'] = True |
274 elif keyval == fife.Key.SPACE: | 273 elif keyval == fife.Key.SPACE: |
275 self.keystate['SPACE'] = True | 274 self._keystate['SPACE'] = True |
276 elif keyval == fife.Key.P: | 275 elif keyval == fife.Key.P: |
277 self._paused = not self._paused | 276 self._paused = not self._paused |
278 self._pausedtime += self.timemanager.getTime() | 277 self._pausedtime += self._timemanager.getTime() |
279 elif keyval in (fife.Key.LEFT_CONTROL, fife.Key.RIGHT_CONTROL): | 278 elif keyval in (fife.Key.LEFT_CONTROL, fife.Key.RIGHT_CONTROL): |
280 self.keystate['CTRL'] = True | 279 self._keystate['CTRL'] = True |
281 | 280 |
282 def keyReleased(self, evt): | 281 def keyReleased(self, evt): |
283 keyval = evt.getKey().getValue() | 282 keyval = evt.getKey().getValue() |
284 if keyval == fife.Key.UP: | 283 if keyval == fife.Key.UP: |
285 self.keystate['UP'] = False | 284 self._keystate['UP'] = False |
286 elif keyval == fife.Key.DOWN: | 285 elif keyval == fife.Key.DOWN: |
287 self.keystate['DOWN'] = False | 286 self._keystate['DOWN'] = False |
288 elif keyval == fife.Key.LEFT: | 287 elif keyval == fife.Key.LEFT: |
289 self.keystate['LEFT'] = False | 288 self._keystate['LEFT'] = False |
290 elif keyval == fife.Key.RIGHT: | 289 elif keyval == fife.Key.RIGHT: |
291 self.keystate['RIGHT'] = False | 290 self._keystate['RIGHT'] = False |
292 elif keyval == fife.Key.SPACE: | 291 elif keyval == fife.Key.SPACE: |
293 self.keystate['SPACE'] = False | 292 self._keystate['SPACE'] = False |
294 elif keyval in (fife.Key.LEFT_CONTROL, fife.Key.RIGHT_CONTROL): | 293 elif keyval in (fife.Key.LEFT_CONTROL, fife.Key.RIGHT_CONTROL): |
295 self.keystate['CTRL'] = False | 294 self._keystate['CTRL'] = False |
296 | 295 |
297 def mouseWheelMovedUp(self, evt): | 296 def mouseWheelMovedUp(self, evt): |
298 if self.keystate['CTRL']: | 297 if self._keystate['CTRL']: |
299 self.cameras['main'].setZoom(self.cameras['main'].getZoom() * 1.05) | 298 self.cameras['main'].setZoom(self.cameras['main'].getZoom() * 1.05) |
300 | 299 |
301 def mouseWheelMovedDown(self, evt): | 300 def mouseWheelMovedDown(self, evt): |
302 if self.keystate['CTRL']: | 301 if self._keystate['CTRL']: |
303 self.cameras['main'].setZoom(self.cameras['main'].getZoom() / 1.05) | 302 self.cameras['main'].setZoom(self.cameras['main'].getZoom() / 1.05) |
304 | 303 |
305 def mousePressed(self, evt): | 304 def mousePressed(self, evt): |
306 if evt.isConsumedByWidgets(): | 305 if evt.isConsumedByWidgets(): |
307 return | 306 return |
322 | 321 |
323 if self._genericrenderer: | 322 if self._genericrenderer: |
324 self._genericrenderer.removeAll("quads") | 323 self._genericrenderer.removeAll("quads") |
325 | 324 |
326 | 325 |
327 if not self.scene: | 326 if not self._scene: |
328 return | 327 return |
329 | 328 |
330 #update the scene | 329 #update the scene |
331 if not self._paused: | 330 if not self._paused: |
332 if self.scene.paused: | 331 if self._scene.paused: |
333 self.scene.unpause(self.timemanager.getTime() - self._starttime) | 332 self._scene.unpause(self._timemanager.getTime() - self._starttime) |
334 | 333 |
335 self.scene.update(self.timemanager.getTime() - self._starttime, self.keystate) | 334 self._scene.update(self._timemanager.getTime() - self._starttime, self._keystate) |
336 | 335 |
337 | 336 |
338 #update the HUD | 337 #update the HUD |
339 avgframe = self.timemanager.getAverageFrameTime() | 338 avgframe = self._timemanager.getAverageFrameTime() |
340 if avgframe > 0: | 339 if avgframe > 0: |
341 fps = 1 / (avgframe / 1000) | 340 fps = 1 / (avgframe / 1000) |
342 else: | 341 else: |
343 fps = 0 | 342 fps = 0 |
344 fpstxt = "%3.2f" % fps | 343 fpstxt = "%3.2f" % fps |
345 self._hudwindow.setFPSText(unicode(fpstxt)) | 344 self._hudwindow.setFPSText(unicode(fpstxt)) |
346 | 345 |
347 player = self.scene.player | 346 player = self._scene.player |
348 exactcoords = player.location.getExactLayerCoordinates() | 347 exactcoords = player.location.getExactLayerCoordinates() |
349 pos = "%1.2f" % exactcoords.x + ", %1.2f" % exactcoords.y | 348 pos = "%1.2f" % exactcoords.x + ", %1.2f" % exactcoords.y |
350 self._hudwindow.setPositionText(unicode(pos)) | 349 self._hudwindow.setPositionText(unicode(pos)) |
351 | 350 |
352 vel = "%1.2f" % player.velocity.x + ", %1.2f" % player.velocity.y | 351 vel = "%1.2f" % player.velocity.x + ", %1.2f" % player.velocity.y |
360 ilives = 0 | 359 ilives = 0 |
361 lives = unicode(str(ilives)) | 360 lives = unicode(str(ilives)) |
362 self._hudwindow.setLivesText(lives) | 361 self._hudwindow.setLivesText(lives) |
363 | 362 |
364 else: | 363 else: |
365 if not self.scene.paused: | 364 if not self._scene.paused: |
366 self.scene.pause(self.timemanager.getTime() - self._starttime) | 365 self._scene.pause(self._timemanager.getTime() - self._starttime) |
367 | 366 |
368 | 367 |
369 self.pump_ctr += 1 | 368 self._pump_ctr += 1 |