comparison demos/rpg/scripts/gamecontroller.py @ 513:edf5c0cf52f3

Added the Actor and Player classes. Actor is the base class that the player and all enemies + NPCs will inherit. Added a help file for the console. Added a KeyState class for keeping track of the state of the keys.
author prock@33b003aa-7bff-0310-803a-e67f0ece8222
date Fri, 21 May 2010 20:33:43 +0000
parents 6ddb1eb9dfa6
children d70fc46c8aa5
comparison
equal deleted inserted replaced
512:6ddb1eb9dfa6 513:edf5c0cf52f3
28 28
29 from fife import fife 29 from fife import fife
30 30
31 from scripts.scene import Scene 31 from scripts.scene import Scene
32 from scripts.guicontroller import GUIController 32 from scripts.guicontroller import GUIController
33
34 class KeyState(object):
35 def __init__(self):
36 self._keystate = { }
37
38 def updateKey(self, key, state):
39 self._keystate[key] = state
40
41 def checkKey(self, key):
42 if key in self._keystate:
43 return self._keystate[key]
44 else:
45 return False
46
47 def reset(self):
48 self._keystate.clear()
33 49
34 class GameListener(fife.IKeyListener, fife.IMouseListener): 50 class GameListener(fife.IKeyListener, fife.IMouseListener):
35 def __init__(self, gamecontroller): 51 def __init__(self, gamecontroller):
36 self._engine = gamecontroller.engine 52 self._engine = gamecontroller.engine
37 self._gamecontroller = gamecontroller 53 self._gamecontroller = gamecontroller
81 keystr = event.getKey().getAsString().lower() 97 keystr = event.getKey().getAsString().lower()
82 if keyval == fife.Key.ESCAPE: 98 if keyval == fife.Key.ESCAPE:
83 self.detach() 99 self.detach()
84 self._gamecontroller.guicontroller.showMainMenu() 100 self._gamecontroller.guicontroller.showMainMenu()
85 event.consume() 101 event.consume()
102
103 self._gamecontroller.keystate.updateKey(keyval, True)
86 104
87 def keyReleased(self, event): 105 def keyReleased(self, event):
88 pass 106 keyval = event.getKey().getValue()
107 keystr = event.getKey().getAsString().lower()
108
109 self._gamecontroller.keystate.updateKey(keyval, False)
110
89 111
90 class GameController(object): 112 class GameController(object):
91 def __init__(self, application, engine, settings): 113 def __init__(self, application, engine, settings):
92 self._application = application 114 self._application = application
93 self._engine = engine 115 self._engine = engine
94 self._settings = settings 116 self._settings = settings
117
118 self._keystate = KeyState()
95 119
96 self._guicontroller = GUIController(self) 120 self._guicontroller = GUIController(self)
97 121
98 self._listener = GameListener(self) 122 self._listener = GameListener(self)
99 123
110 return result 134 return result
111 135
112 def newGame(self): 136 def newGame(self):
113 self._guicontroller.hideMainMenu() 137 self._guicontroller.hideMainMenu()
114 138
139 self._keystate.reset()
140
115 if self._scene: 141 if self._scene:
116 self._scene.destroyScene() 142 self._scene.destroyScene()
117 self._scene = None 143 self._scene = None
118 144
119 self._scene = Scene(self) 145 self._scene = Scene(self)
120 self._scene.createScene(self._settings.get("RPG", "TownMapFile", "maps/town.xml")) 146 self._scene.createScene(self._settings.get("RPG", "TownMapFile", "maps/town.xml"))
121 147
122 #start listening to events 148 #start listening to events
123 self._listener.attach() 149 self._listener.attach()
124 150
151 def endGame(self):
152 if self._scene:
153 self._scene.destroyScene()
154 self._scene = None
155
125 def quit(self): 156 def quit(self):
157 self.endGame()
126 self._application.requestQuit() 158 self._application.requestQuit()
127 159
128 def pump(self): 160 def pump(self):
129 if self._scene: 161 if self._scene:
130 self._scene.updateScene() 162 self._scene.updateScene()
137 return self._engine 169 return self._engine
138 170
139 def _getSettings(self): 171 def _getSettings(self):
140 return self._settings 172 return self._settings
141 173
174 def _getScene(self):
175 return self._scene
176
177 def _getKeyState(self):
178 return self._keystate
179
142 guicontroller = property(_getGUIController) 180 guicontroller = property(_getGUIController)
143 engine = property(_getEngine) 181 engine = property(_getEngine)
144 settings = property(_getSettings) 182 settings = property(_getSettings)
183 scene = property(_getScene)
184 keystate = property(_getKeyState)