comparison clients/rio_de_hola/scripts/world.py @ 247:040387b7167f

More documentation. Refactoring.
author phoku@33b003aa-7bff-0310-803a-e67f0ece8222
date Wed, 25 Mar 2009 21:41:43 +0000
parents 8156de05a544
children 51cc05d862f2
comparison
equal deleted inserted replaced
246:8156de05a544 247:040387b7167f
91 """ 91 """
92 Just loads the menu from an XML file 92 Just loads the menu from an XML file
93 and hooks the events up. 93 and hooks the events up.
94 The buttons are removed and later re-added if appropiate. 94 The buttons are removed and later re-added if appropiate.
95 """ 95 """
96 self.hide_instancemenu()
96 dynamicbuttons = ('moveButton', 'talkButton', 'kickButton', 'inspectButton') 97 dynamicbuttons = ('moveButton', 'talkButton', 'kickButton', 'inspectButton')
97 self.instancemenu = pychan.loadXML('gui/instancemenu.xml') 98 self.instancemenu = pychan.loadXML('gui/instancemenu.xml')
98 self.instancemenu.mapEvents({ 99 self.instancemenu.mapEvents({
99 'moveButton' : self.onMoveButtonPress, 100 'moveButton' : self.onMoveButtonPress,
100 'talkButton' : self.onTalkButtonPress, 101 'talkButton' : self.onTalkButtonPress,
211 self.cam2_scrolling_right = True 212 self.cam2_scrolling_right = True
212 self.target_rotation = self.cameras['main'].getRotation() 213 self.target_rotation = self.cameras['main'].getRotation()
213 214
214 def save(self, filename): 215 def save(self, filename):
215 saveMapFile(filename, self.engine, self.map) 216 saveMapFile(filename, self.engine, self.map)
217
218 def getInstancesAt(self, clickpoint):
219 """
220 Query the main camera for instances on our active(agent) layer.
221 """
222 return self.cameras['main'].getMatchingInstances(clickpoint, self.agentlayer)
223
224 def getLocationAt(self, clickpoint):
225 """
226 Query the main camera for the Map location (on the agent layer)
227 that a screen point refers to.
228 """
229 target_mapcoord = self.cameras['main'].toMapCoordinates(clickpoint, False)
230 target_mapcoord.z = 0
231 location = fife.Location(self.agentlayer)
232 location.setMapCoordinates(target_mapcoord)
233 return location
216 234
217 def keyPressed(self, evt): 235 def keyPressed(self, evt):
218 keyval = evt.getKey().getValue() 236 keyval = evt.getKey().getValue()
219 keystr = evt.getKey().getAsString().lower() 237 keystr = evt.getKey().getAsString().lower()
220 if keystr == 't': 238 if keystr == 't':
247 def mouseWheelMovedDown(self, evt): 265 def mouseWheelMovedDown(self, evt):
248 if self.ctrldown: 266 if self.ctrldown:
249 self.cameras['main'].setZoom(self.cameras['main'].getZoom() / 1.05) 267 self.cameras['main'].setZoom(self.cameras['main'].getZoom() / 1.05)
250 268
251 def changeRotation(self): 269 def changeRotation(self):
270 """
271 Smoothly change the main cameras rotation until
272 the current target rotation is reached.
273 """
252 currot = self.cameras['main'].getRotation() 274 currot = self.cameras['main'].getRotation()
253 if self.target_rotation != currot: 275 if self.target_rotation != currot:
254 self.cameras['main'].setRotation((currot + 5) % 360) 276 self.cameras['main'].setRotation((currot + 5) % 360)
255 277
256 def mousePressed(self, evt): 278 def mousePressed(self, evt):
258 return 280 return
259 281
260 clickpoint = fife.ScreenPoint(evt.getX(), evt.getY()) 282 clickpoint = fife.ScreenPoint(evt.getX(), evt.getY())
261 if (evt.getButton() == fife.MouseEvent.LEFT): 283 if (evt.getButton() == fife.MouseEvent.LEFT):
262 self.hide_instancemenu() 284 self.hide_instancemenu()
263 target_mapcoord = self.cameras['main'].toMapCoordinates(clickpoint, False) 285 self.hero.run( self.getLocationAt(clickpoint) )
264 target_mapcoord.z = 0
265 l = fife.Location(self.agentlayer)
266 l.setMapCoordinates(target_mapcoord)
267 self.hero.run(l)
268 286
269 if (evt.getButton() == fife.MouseEvent.RIGHT): 287 if (evt.getButton() == fife.MouseEvent.RIGHT):
270 instances = self.cameras['main'].getMatchingInstances(clickpoint, self.agentlayer) 288 instances = self.getInstancesAt(clickpoint)
271 print "selected instances on agent layer: ", [i.getObject().getId() for i in instances] 289 print "selected instances on agent layer: ", [i.getObject().getId() for i in instances]
272 if instances: 290 if instances:
273 self.show_instancemenu(clickpoint, instances[0]) 291 self.show_instancemenu(clickpoint, instances[0])
274 292
275 def mouseMoved(self, evt): 293 def mouseMoved(self, evt):
276 renderer = fife.InstanceRenderer.getInstance(self.cameras['main']) 294 renderer = fife.InstanceRenderer.getInstance(self.cameras['main'])
277 renderer.removeAllOutlines() 295 renderer.removeAllOutlines()
278 296
279 pt = fife.ScreenPoint(evt.getX(), evt.getY()) 297 pt = fife.ScreenPoint(evt.getX(), evt.getY())
280 instances = self.cameras['main'].getMatchingInstances(pt, self.agentlayer); 298 instances = self.getInstancesAt(pt);
281 for i in instances: 299 for i in instances:
282 if i.getObject().getId() in ('girl', 'beekeeper'): 300 if i.getObject().getId() in ('girl', 'beekeeper'):
283 renderer.addOutlined(i, 173, 255, 47, 2) 301 renderer.addOutlined(i, 173, 255, 47, 2)
284 302
285 def onConsoleCommand(self, command): 303 def onConsoleCommand(self, command):
299 self.hide_instancemenu() 317 self.hide_instancemenu()
300 instance = self.instancemenu.instance 318 instance = self.instancemenu.instance
301 self.hero.talk(instance.getLocationRef()) 319 self.hero.talk(instance.getLocationRef())
302 if instance.getObject().getId() == 'beekeeper': 320 if instance.getObject().getId() == 'beekeeper':
303 beekeeperTexts = TDS.readSetting("beekeeperTexts", type='list', text=True) 321 beekeeperTexts = TDS.readSetting("beekeeperTexts", type='list', text=True)
304 txtindex = random.randint(0, len(beekeeperTexts) - 1) 322 instance.say(random.choice(beekeeperTexts), 5000)
305 instance.say(beekeeperTexts[txtindex], 5000)
306 if instance.getObject().getId() == 'girl': 323 if instance.getObject().getId() == 'girl':
307 girlTexts = TDS.readSetting("girlTexts", type='list', text=True) 324 girlTexts = TDS.readSetting("girlTexts", type='list', text=True)
308 txtindex = random.randint(0, len(girlTexts) - 1) 325 instance.say(random.choice(girlTexts), 5000)
309 instance.say(girlTexts[txtindex], 5000)
310 326
311 def onKickButtonPress(self): 327 def onKickButtonPress(self):
312 self.hide_instancemenu() 328 self.hide_instancemenu()
313 self.hero.kick(self.instancemenu.instance.getLocationRef()) 329 self.hero.kick(self.instancemenu.instance.getLocationRef())
314 self.instancemenu.instance.say('Hey!', 1000) 330 self.instancemenu.instance.say('Hey!', 1000)
322 saytext.append(' ID %s and' % inst.getFifeId()) 338 saytext.append(' ID %s and' % inst.getFifeId())
323 saytext.append(' object name %s' % inst.getObject().getId()) 339 saytext.append(' object name %s' % inst.getObject().getId())
324 self.hero.agent.say('\n'.join(saytext), 3500) 340 self.hero.agent.say('\n'.join(saytext), 3500)
325 341
326 def pump(self): 342 def pump(self):
343 """
344 Called every frame.
345
346 Scroll the small demo camera and
347 (smoothly) update the rotation.
348 """
327 if self.cameras['small'].isEnabled(): 349 if self.cameras['small'].isEnabled():
328 loc = self.cameras['small'].getLocation() 350 loc = self.cameras['small'].getLocation()
329 c = loc.getExactLayerCoordinatesRef() 351 c = loc.getExactLayerCoordinatesRef()
330 if self.cam2_scrolling_right: 352 if self.cam2_scrolling_right:
331 self.cur_cam2_x = c.x = c.x+0.1 353 self.cur_cam2_x = c.x = c.x+0.1