comparison demos/rpg/scripts/gamecontroller.py @ 540:2e739ae9a8bc

Some misc code cleanup. Added the EnterPortalAction action. You can now move between town and level1.
author prock@33b003aa-7bff-0310-803a-e67f0ece8222
date Tue, 01 Jun 2010 19:17:30 +0000
parents 764510a6d2f9
children cb7ec12214a9
comparison
equal deleted inserted replaced
539:4c7b5eee211c 540:2e739ae9a8bc
31 from fife.extensions.soundmanager import SoundManager 31 from fife.extensions.soundmanager import SoundManager
32 from fife.extensions.loaders import loadImportFile 32 from fife.extensions.loaders import loadImportFile
33 33
34 from scripts.scene import Scene 34 from scripts.scene import Scene
35 from scripts.guicontroller import GUIController 35 from scripts.guicontroller import GUIController
36 from scripts.actors.baseactor import TalkAction, PickUpItemAction 36 from scripts.actors.baseactor import TalkAction, PickUpItemAction, EnterPortalAction
37 from scripts.objects.baseobject import GameObjectTypes 37 from scripts.objects.baseobject import GameObjectTypes
38 from scripts.misc.exceptions import ObjectNotFoundError, ObjectAlreadyInSceneError 38 from scripts.misc.exceptions import ObjectNotFoundError, ObjectAlreadyInSceneError
39 39
40 40
41 class KeyState(object): 41 class KeyState(object):
58 def __init__(self, gamecontroller): 58 def __init__(self, gamecontroller):
59 self._engine = gamecontroller.engine 59 self._engine = gamecontroller.engine
60 self._gamecontroller = gamecontroller 60 self._gamecontroller = gamecontroller
61 self._settings = gamecontroller.settings 61 self._settings = gamecontroller.settings
62 self._eventmanager = self._engine.getEventManager() 62 self._eventmanager = self._engine.getEventManager()
63 self._soundmanager = SoundManager(self._engine)
64 63
65 fife.IMouseListener.__init__(self) 64 fife.IMouseListener.__init__(self)
66 fife.IKeyListener.__init__(self) 65 fife.IKeyListener.__init__(self)
67 66
68 self._attached = False 67 self._attached = False
106 if item_instances: 105 if item_instances:
107 obj = self._gamecontroller.scene.objectlist[item_instances[0].getId()] 106 obj = self._gamecontroller.scene.objectlist[item_instances[0].getId()]
108 if obj.type == GameObjectTypes["ITEM"]: 107 if obj.type == GameObjectTypes["ITEM"]:
109 action = PickUpItemAction(self._gamecontroller.scene.player, obj) 108 action = PickUpItemAction(self._gamecontroller.scene.player, obj)
110 self._gamecontroller.scene.player.nextaction = action 109 self._gamecontroller.scene.player.nextaction = action
110 elif obj.type == GameObjectTypes["PORTAL"]:
111 action = EnterPortalAction(self._gamecontroller.scene.player, obj)
112 self._gamecontroller.scene.player.nextaction = action
111 113
112
113 if (event.getButton() == fife.MouseEvent.RIGHT): 114 if (event.getButton() == fife.MouseEvent.RIGHT):
114 instances = self._gamecontroller.scene.getInstancesAt(clickpoint, self._gamecontroller.scene.actorlayer) 115 instances = self._gamecontroller.scene.getInstancesAt(clickpoint, self._gamecontroller.scene.actorlayer)
115 if instances: 116 if instances:
116 self._gamecontroller.logger.log_debug("Selected instance on actor layer: " + instances[0].getId()) 117 self._gamecontroller.logger.log_debug("Selected instance on actor layer: " + instances[0].getId())
117 118
182 def __init__(self, application, engine, settings): 183 def __init__(self, application, engine, settings):
183 self._application = application 184 self._application = application
184 self._engine = engine 185 self._engine = engine
185 self._settings = settings 186 self._settings = settings
186 187
188 self._soundmanager = SoundManager(self._engine)
189
187 self._keystate = KeyState() 190 self._keystate = KeyState()
188 191
189 self._guicontroller = GUIController(self) 192 self._guicontroller = GUIController(self)
190 193
191 self._listener = GameListener(self) 194 self._listener = GameListener(self)
193 self._guicontroller.showMainMenu() 196 self._guicontroller.showMainMenu()
194 197
195 self._scene = None 198 self._scene = None
196 self._instancerenderer = None 199 self._instancerenderer = None
197 self._floatingtextrenderer = None 200 self._floatingtextrenderer = None
201
202 self._switchmaprequested = False
203 self._newmap = None
198 204
199 def onConsoleCommand(self, command): 205 def onConsoleCommand(self, command):
200 """ 206 """
201 Might be useful if you want to have the game parse a command. 207 Might be useful if you want to have the game parse a command.
202 Not sure if I am going to keep this or not. 208 Not sure if I am going to keep this or not.
250 return result 256 return result
251 257
252 def newGame(self): 258 def newGame(self):
253 self._guicontroller.hideMainMenu() 259 self._guicontroller.hideMainMenu()
254 260
261 mapname = self._settings.get("RPG", "TownMapFile", "town")
262 self.loadMap(mapname)
263
264
265 def loadMap(self, mapname):
266 if self._listener:
267 self._listener.detach()
268
255 self._keystate.reset() 269 self._keystate.reset()
256 270
257 if self._scene: 271 if self._scene:
258 self._scene.destroyScene() 272 self._scene.destroyScene()
259 self._scene = None 273 self._scene = None
260 274
261 self._scene = Scene(self) 275 self._scene = Scene(self)
262 self._scene.createScene(self._settings.get("RPG", "TownMapFile", "maps/town.xml")) 276 self._scene.createScene("maps/" + mapname + ".xml")
263 277
264 self._instancerenderer = fife.InstanceRenderer.getInstance(self._scene.cameras[self._settings.get("RPG", "DefaultCameraName", "camera1")]) 278 self._instancerenderer = fife.InstanceRenderer.getInstance(self._scene.cameras[self._settings.get("RPG", "DefaultCameraName", "camera1")])
265 self._floatingtextrenderer = fife.FloatingTextRenderer.getInstance(self._scene.cameras[self._settings.get("RPG", "DefaultCameraName", "camera1")]) 279 self._floatingtextrenderer = fife.FloatingTextRenderer.getInstance(self._scene.cameras[self._settings.get("RPG", "DefaultCameraName", "camera1")])
266 self._floatingtextrenderer.addActiveLayer(self._scene.actorlayer) 280 self._floatingtextrenderer.addActiveLayer(self._scene.actorlayer)
267 281
268 #start listening to events 282 if self._listener:
269 self._listener.attach() 283 self._listener.attach()
270 284
285 def switchMap(self, newmapname):
286 self._switchmaprequested = True
287 self._newmap = newmapname
288
271 def endGame(self): 289 def endGame(self):
272 if self._scene: 290 if self._scene:
291 self._listener.detach()
273 self._scene.destroyScene() 292 self._scene.destroyScene()
274 self._scene = None 293 self._scene = None
275 self._instancerenderer = None 294 self._instancerenderer = None
276 self._floatingtextrenderer = None 295 self._floatingtextrenderer = None
277 296
278 def quit(self): 297 def quit(self):
279 self.endGame() 298 self.endGame()
280 self._application.requestQuit() 299 self._application.requestQuit()
281 300
282 def pump(self): 301 def pump(self):
302 if self._switchmaprequested:
303 self.loadMap(self._newmap)
304 self._newmap = None
305 self._switchmaprequested = False
306
283 if self._scene: 307 if self._scene:
284 self._scene.updateScene() 308 self._scene.updateScene()
285 309
286 310
287 def _getGUIController(self): 311 def _getGUIController(self):