Mercurial > fife-parpg
comparison clients/rio_de_hola/run.py @ 0:4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
author | mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Sun, 29 Jun 2008 18:44:17 +0000 |
parents | |
children | 214e3eb81eb2 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4a0efb7baf70 |
---|---|
1 #!/usr/bin/env python | |
2 # coding: utf-8 | |
3 # This is the rio de hola client for FIFE. | |
4 | |
5 import sys, os, re, math, random | |
6 | |
7 def _jp(path): | |
8 return os.path.sep.join(path.split('/')) | |
9 | |
10 _paths = ('../../engine/swigwrappers/python', '../../engine/extensions') | |
11 for p in _paths: | |
12 if p not in sys.path: | |
13 sys.path.append(_jp(p)) | |
14 | |
15 import fife, fifelog | |
16 from scripts import world, eventlistenerbase | |
17 from basicapplication import ApplicationBase | |
18 import pychan | |
19 import pychan.widgets as widgets | |
20 import settings as TDS | |
21 | |
22 | |
23 class ApplicationListener(eventlistenerbase.EventListenerBase): | |
24 def __init__(self, engine, world): | |
25 super(ApplicationListener, self).__init__(engine,regKeys=True,regCmd=True, regMouse=False, regConsole=True, regWidget=True) | |
26 self.engine = engine | |
27 self.world = world | |
28 engine.getEventManager().setNonConsumableKeys([ | |
29 fife.Key.ESCAPE, | |
30 fife.Key.F10, | |
31 fife.Key.LEFT, | |
32 fife.Key.RIGHT, | |
33 fife.Key.UP, | |
34 fife.Key.DOWN]) | |
35 | |
36 self.quit = False | |
37 self.aboutWindow = None | |
38 | |
39 self.rootpanel = pychan.loadXML('gui/rootpanel.xml') | |
40 self.rootpanel.mapEvents({ | |
41 'quitButton' : self.onQuitButtonPress, | |
42 'aboutButton' : self.onAboutButtonPress, | |
43 }) | |
44 self.rootpanel.show() | |
45 | |
46 def keyPressed(self, evt): | |
47 keyval = evt.getKey().getValue() | |
48 keystr = evt.getKey().getAsString().lower() | |
49 consumed = False | |
50 if keyval == fife.Key.ESCAPE: | |
51 self.quit = True | |
52 evt.consume() | |
53 elif keyval == fife.Key.F10: | |
54 self.engine.getGuiManager().getConsole().toggleShowHide() | |
55 evt.consume() | |
56 elif keystr == 'p': | |
57 self.engine.getRenderBackend().captureScreen('screenshot.png') | |
58 evt.consume() | |
59 | |
60 def onCommand(self, command): | |
61 self.quit = (command.getCommandType() == fife.CMD_QUIT_GAME) | |
62 if self.quit: | |
63 command.consume() | |
64 | |
65 def onConsoleCommand(self, command): | |
66 result = '' | |
67 if command.lower() in ('quit', 'exit'): | |
68 self.quit = True | |
69 result = 'quitting' | |
70 elif command.lower() in ( 'help', 'help()' ): | |
71 self.engine.getGuiManager().getConsole().println( open( 'misc/infotext.txt', 'r' ).read() ) | |
72 result = "-- End of help --" | |
73 else: | |
74 result = self.world.onConsoleCommand(command) | |
75 if not result: | |
76 try: | |
77 result = str(eval(command)) | |
78 except: | |
79 pass | |
80 if not result: | |
81 result = 'no result' | |
82 return result | |
83 | |
84 def onQuitButtonPress(self): | |
85 cmd = fife.Command() | |
86 cmd.setSource(None) | |
87 cmd.setCommandType(fife.CMD_QUIT_GAME) | |
88 self.engine.getEventManager().dispatchCommand(cmd) | |
89 | |
90 def onAboutButtonPress(self): | |
91 if not self.aboutWindow: | |
92 self.aboutWindow = pychan.loadXML('gui/help.xml') | |
93 self.aboutWindow.mapEvents({ 'closeButton' : self.aboutWindow.hide }) | |
94 self.aboutWindow.distributeData({ 'helpText' : open("misc/infotext.txt").read() }) | |
95 self.aboutWindow.show() | |
96 | |
97 | |
98 class IslandDemo(ApplicationBase): | |
99 def __init__(self): | |
100 super(IslandDemo,self).__init__() | |
101 pychan.init(self.engine, debug=TDS.PychanDebug) | |
102 self.world = world.World(self.engine) | |
103 self.listener = ApplicationListener(self.engine, self.world) | |
104 self.world.load(TDS.MapFile) | |
105 | |
106 self.soundmanager = self.engine.getSoundManager() | |
107 self.soundmanager.init() | |
108 | |
109 if TDS.PlaySounds: | |
110 # play track as background music | |
111 emitter = self.soundmanager.createEmitter() | |
112 id = self.engine.getSoundClipPool().addResourceFromFile('music/rio_de_hola.ogg') | |
113 emitter.setSoundClip(id) | |
114 emitter.setLooping(True) | |
115 emitter.play() | |
116 | |
117 def createListener(self): | |
118 pass # already created in constructor | |
119 | |
120 def _pump(self): | |
121 if self.listener.quit: | |
122 self.breakRequested = True | |
123 self.world.save('maps/savefile.xml') | |
124 else: | |
125 self.world.pump() | |
126 | |
127 def main(): | |
128 app = IslandDemo() | |
129 app.run() | |
130 | |
131 | |
132 if __name__ == '__main__': | |
133 if TDS.UsePsyco: | |
134 # Import Psyco if available | |
135 try: | |
136 import psyco | |
137 psyco.full() | |
138 print "Psyco acceleration in use" | |
139 except ImportError: | |
140 print "Psyco acceleration not used" | |
141 else: | |
142 print "Psyco acceleration not used" | |
143 main() |