comparison demos/rpg/scripts/gamecontroller.py @ 550:d0282579668c

Added QuestManager. The player can now move from map to map and the state of the quests remains persistent. Both quests the NPC gives you are now completable. Still have to clean up the quest loading code. Started adding more comments.
author prock@33b003aa-7bff-0310-803a-e67f0ece8222
date Tue, 15 Jun 2010 17:53:20 +0000
parents e59ece21ab3e
children 3b933753cba8
comparison
equal deleted inserted replaced
549:c9113e23b004 550:d0282579668c
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, EnterPortalAction 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 from scripts.quests.questmanager import QuestManager
39 40
40 41
41 class KeyState(object): 42 class KeyState(object):
43 """
44 Holds the current state of the keys on the keyboard (down or up).
45 False = down, True = up.
46 """
42 def __init__(self): 47 def __init__(self):
43 self._keystate = { } 48 self._keystate = { }
44 49
45 def updateKey(self, key, state): 50 def updateKey(self, key, state):
46 self._keystate[key] = state 51 self._keystate[key] = state
50 return self._keystate[key] 55 return self._keystate[key]
51 else: 56 else:
52 return False 57 return False
53 58
54 def reset(self): 59 def reset(self):
60 """
61 Empties the keystate dictionary (assumes all keys are False)
62 """
55 self._keystate.clear() 63 self._keystate.clear()
56 64
57 class GameListener(fife.IKeyListener, fife.IMouseListener): 65 class GameListener(fife.IKeyListener, fife.IMouseListener):
66 """
67 Main game listener. Listens for Mouse and Keyboard events.
68
69 This class also has the ability to attach and detach itself from
70 the event manager in cases where you do not want input processed (i.e. when
71 the main menu is visible). It is NOT attached by default.
72 """
58 def __init__(self, gamecontroller): 73 def __init__(self, gamecontroller):
59 self._engine = gamecontroller.engine 74 self._engine = gamecontroller.engine
60 self._gamecontroller = gamecontroller 75 self._gamecontroller = gamecontroller
61 self._settings = gamecontroller.settings 76 self._settings = gamecontroller.settings
62 self._eventmanager = self._engine.getEventManager() 77 self._eventmanager = self._engine.getEventManager()
67 self._attached = False 82 self._attached = False
68 83
69 self._lastmousepos = (0.0,0.0) 84 self._lastmousepos = (0.0,0.0)
70 85
71 def attach(self): 86 def attach(self):
87 """
88 Attaches to the event manager.
89 """
72 if not self._attached: 90 if not self._attached:
73 self._gamecontroller.keystate.reset() 91 self._gamecontroller.keystate.reset()
74 self._eventmanager.addMouseListenerFront(self) 92 self._eventmanager.addMouseListenerFront(self)
75 self._eventmanager.addKeyListenerFront(self) 93 self._eventmanager.addKeyListenerFront(self)
76 self._attached = True 94 self._attached = True
77 95
78 def detach(self): 96 def detach(self):
97 """
98 Detaches from the event manager.
99 """
79 if self._attached: 100 if self._attached:
80 self._eventmanager.removeMouseListener(self) 101 self._eventmanager.removeMouseListener(self)
81 self._eventmanager.removeKeyListener(self) 102 self._eventmanager.removeKeyListener(self)
82 self._attached = False 103 self._attached = False
83 104
178 keystr = event.getKey().getAsString().lower() 199 keystr = event.getKey().getAsString().lower()
179 200
180 self._gamecontroller.keystate.updateKey(keyval, False) 201 self._gamecontroller.keystate.updateKey(keyval, False)
181 202
182 class GameController(object): 203 class GameController(object):
204 """
205 The main game class.
206
207 This handles all game related code including setting up the scene, displaying user
208 interfaces, managing sound, etc etc.
209 """
183 def __init__(self, application, engine, settings): 210 def __init__(self, application, engine, settings):
184 self._application = application 211 self._application = application
185 self._engine = engine 212 self._engine = engine
186 self._settings = settings 213 self._settings = settings
187 214
193 220
194 self._listener = GameListener(self) 221 self._listener = GameListener(self)
195 222
196 self._guicontroller.showMainMenu() 223 self._guicontroller.showMainMenu()
197 224
225 self._questmanager = QuestManager(self)
226
198 self._scene = None 227 self._scene = None
199 self._instancerenderer = None 228 self._instancerenderer = None
200 self._floatingtextrenderer = None 229 self._floatingtextrenderer = None
201 230
202 self._switchmaprequested = False 231 self._switchmaprequested = False
203 self._newmap = None 232 self._newmap = None
204 233
205 def onConsoleCommand(self, command): 234 def onConsoleCommand(self, command):
206 """ 235 """
207 Might be useful if you want to have the game parse a command. 236 Parses game related console commands.
208 Not sure if I am going to keep this or not.
209 """ 237 """
210 238
211 result = "" 239 result = ""
212 240
213 args = command.split(" ") 241 args = command.split(" ")
255 283
256 284
257 return result 285 return result
258 286
259 def newGame(self): 287 def newGame(self):
288 """
289 Removes any save games and starts a new game.
290 """
291
260 self._guicontroller.hideMainMenu() 292 self._guicontroller.hideMainMenu()
261 293
262 for filename in glob.glob(os.path.join("saves" , "*.xml")): 294 for filename in glob.glob(os.path.join("saves" , "*.xml")):
263 os.remove(filename) 295 os.remove(filename)
264 296
265 mapname = self._settings.get("RPG", "TownMapFile", "town") 297 mapname = self._settings.get("RPG", "TownMapFile", "town")
266 self.loadMap(mapname) 298 self.loadMap(mapname)
267 299
268 300
269 def loadMap(self, mapname): 301 def loadMap(self, mapname):
302 """
303 Creates the scene for the map and attaches the listener.
304 """
305
270 if self._listener: 306 if self._listener:
271 self._listener.detach() 307 self._listener.detach()
272 308
273 self._keystate.reset() 309 self._keystate.reset()
274 310
286 322
287 if self._listener: 323 if self._listener:
288 self._listener.attach() 324 self._listener.attach()
289 325
290 def switchMap(self, newmapname): 326 def switchMap(self, newmapname):
327 """
328 Queues a map switch for next frame. This must be done next frame to ensure
329 all events pertaining to the current frame have finished being processed.
330 """
291 self._switchmaprequested = True 331 self._switchmaprequested = True
292 self._newmap = newmapname 332 self._newmap = newmapname
293 333
294 #save before switching scenes 334 #save before switching scenes
295 self._scene.serialize() 335 self._scene.serialize()
296 336
297 def endGame(self): 337 def endGame(self):
338 """
339 Saves the game state and destroys the scene.
340 """
298 if self._scene: 341 if self._scene:
299 self._scene.serialize() 342 self._scene.serialize()
300 343
301 self._listener.detach() 344 self._listener.detach()
302 self._scene.destroyScene() 345 self._scene.destroyScene()
319 362
320 363
321 def _getGUIController(self): 364 def _getGUIController(self):
322 return self._guicontroller 365 return self._guicontroller
323 366
367 def _getQuestManager(self):
368 return self._questmanager
369
324 def _getEngine(self): 370 def _getEngine(self):
325 return self._engine 371 return self._engine
326 372
327 def _getSettings(self): 373 def _getSettings(self):
328 return self._settings 374 return self._settings
338 384
339 def _getLogManager(self): 385 def _getLogManager(self):
340 return self._application.logger 386 return self._application.logger
341 387
342 guicontroller = property(_getGUIController) 388 guicontroller = property(_getGUIController)
389 questmanager = property(_getQuestManager)
343 engine = property(_getEngine) 390 engine = property(_getEngine)
344 settings = property(_getSettings) 391 settings = property(_getSettings)
345 scene = property(_getScene) 392 scene = property(_getScene)
346 keystate = property(_getKeyState) 393 keystate = property(_getKeyState)
347 instancerenderer = property(_getInstanceRenderer) 394 instancerenderer = property(_getInstanceRenderer)