comparison clients/rio_de_hola/scripts/world.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 427bb0de3e29
comparison
equal deleted inserted replaced
-1:000000000000 0:4a0efb7baf70
1 import fife, math, random
2 import pychan
3 import pychan.widgets as widgets
4
5 from eventlistenerbase import EventListenerBase
6 from loaders import loadMapFile
7 from savers import saveMapFile
8 from hero import Hero
9 from girl import Girl
10 from cloud import Cloud
11 from beekeeper import Beekeeper
12 from agent import create_anonymous_agents
13 import settings as TDS
14
15 class MapListener(fife.MapChangeListener):
16 def __init__(self, map):
17 fife.MapChangeListener.__init__(self)
18 map.addChangeListener(self)
19
20 def onMapChanged(self, map, changedLayers):
21 return
22 print "Changes on map ", map.getId()
23 for layer in map.getLayers():
24 print layer.getId()
25 print " ", ["%s, %x" % (i.getObject().getId(), i.getChangeInfo()) for i in layer.getChangedInstances()]
26
27 def onLayerCreate(self, map, layer):
28 pass
29
30 def onLayerDelete(self, map, layer):
31 pass
32
33
34 class World(EventListenerBase):
35 def __init__(self, engine):
36 super(World, self).__init__(engine, regMouse=True, regKeys=True)
37 self.engine = engine
38 self.eventmanager = engine.getEventManager()
39 self.model = engine.getModel()
40 self.view = self.engine.getView()
41 self.filename = ''
42 self.pump_ctr = 0 # for testing purposis
43 self.ctrldown = False
44 self.instancemenu = None
45 self.instance_to_agent = {}
46 self.dynamic_widgets = {}
47
48 def show_instancemenu(self, clickpoint, instance):
49 if instance.getFifeId() == self.hero.agent.getFifeId():
50 return
51
52 dynamicbuttons = ('moveButton', 'talkButton', 'kickButton', 'inspectButton')
53 if not self.instancemenu:
54 self.instancemenu = pychan.loadXML('gui/instancemenu.xml')
55 self.instancemenu.mapEvents({
56 'moveButton' : self.onMoveButtonPress,
57 'talkButton' : self.onTalkButtonPress,
58 'kickButton' : self.onKickButtonPress,
59 'inspectButton' : self.onInspectButtonPress,
60 })
61 for btn in dynamicbuttons:
62 self.dynamic_widgets[btn] = self.instancemenu.findChild(name=btn)
63 for btn in dynamicbuttons:
64 try:
65 self.instancemenu.removeChild(self.dynamic_widgets[btn])
66 except pychan.exceptions.RuntimeError:
67 pass
68
69 self.instancemenu.clickpoint = clickpoint
70 self.instancemenu.instance = instance
71
72 self.instancemenu.addChild(self.dynamic_widgets['inspectButton'])
73 target_distance = self.hero.agent.getLocationRef().getLayerDistanceTo(instance.getLocationRef())
74 if target_distance > 3.0:
75 self.instancemenu.addChild(self.dynamic_widgets['moveButton'])
76 else:
77 if self.instance_to_agent.has_key(instance.getFifeId()):
78 self.instancemenu.addChild(self.dynamic_widgets['talkButton'])
79 self.instancemenu.addChild(self.dynamic_widgets['kickButton'])
80 self.instancemenu.position = (clickpoint.x, clickpoint.y)
81 self.instancemenu.show()
82
83 def hide_instancemenu(self):
84 if self.instancemenu:
85 self.instancemenu.hide()
86
87 def reset(self):
88 self.map, self.agentlayer = None, None
89 self.cameras = {}
90 self.hero, self.girl, self.clouds, self.beekeepers = None, None, [], []
91 self.cur_cam2_x, self.initial_cam2_x, self.cam2_scrolling_right = 0, 0, True
92 self.target_rotation = 0
93 self.instance_to_agent = {}
94
95 def load(self, filename):
96 self.filename = filename
97 self.reset()
98 self.map = loadMapFile(filename, self.engine)
99 self.maplistener = MapListener(self.map)
100
101 self.agentlayer = self.map.getLayer('TechdemoMapObjectLayer')
102 self.hero = Hero(self.model, 'PC', self.agentlayer)
103 self.instance_to_agent[self.hero.agent.getFifeId()] = self.hero
104 self.hero.start()
105
106 self.girl = Girl(self.model, 'NPC:girl', self.agentlayer)
107 self.instance_to_agent[self.girl.agent.getFifeId()] = self.girl
108 self.girl.start()
109
110 self.beekeepers = create_anonymous_agents(self.model, 'Beekeeper', self.agentlayer, Beekeeper)
111 for beekeeper in self.beekeepers:
112 self.instance_to_agent[beekeeper.agent.getFifeId()] = beekeeper
113 beekeeper.start()
114
115 cloudlayer = self.map.getLayer('TechdemoMapTileLayer')
116 self.clouds = create_anonymous_agents(self.model, 'Cloud', cloudlayer, Cloud)
117 for cloud in self.clouds:
118 cloud.start(0.1, 0.05)
119
120 for cam in self.view.getCameras():
121 self.cameras[cam.getId()] = cam
122 self.cameras['main'].attach(self.hero.agent)
123
124 self.view.resetRenderers()
125 renderer = fife.FloatingTextRenderer.getInstance(self.cameras['main'])
126 textfont = self.engine.getGuiManager().createFont('fonts/rpgfont.png', 0, TDS.FontGlyphs);
127 renderer.changeDefaultFont(textfont)
128
129 renderer = fife.FloatingTextRenderer.getInstance(self.cameras['small'])
130 renderer.changeDefaultFont(None)
131
132 renderer = self.cameras['main'].getRenderer('CoordinateRenderer')
133 renderer.clearActiveLayers()
134 renderer.addActiveLayer(self.map.getLayer(TDS.CoordinateLayerName))
135
136 renderer = self.cameras['main'].getRenderer('QuadTreeRenderer')
137 renderer.setEnabled(True)
138 renderer.clearActiveLayers()
139 if TDS.QuadTreeLayerName:
140 renderer.addActiveLayer(self.map.getLayer(TDS.QuadTreeLayerName))
141
142 self.cameras['small'].getLocationRef().setExactLayerCoordinates( fife.ExactModelCoordinate( 40.0, 40.0, 0.0 ))
143 self.initial_cam2_x = self.cameras['small'].getLocation().getExactLayerCoordinates().x
144 self.cur_cam2_x = self.initial_cam2_x
145 self.cam2_scrolling_right = True
146 self.cameras['small'].setEnabled(False)
147
148 self.target_rotation = self.cameras['main'].getRotation()
149
150 def save(self, filename):
151 saveMapFile(filename, self.engine, self.map)
152
153 def keyPressed(self, evt):
154 keyval = evt.getKey().getValue()
155 keystr = evt.getKey().getAsString().lower()
156 if keystr == 't':
157 r = self.cameras['main'].getRenderer('GridRenderer')
158 r.setEnabled(not r.isEnabled())
159 elif keystr == 'c':
160 r = self.cameras['main'].getRenderer('CoordinateRenderer')
161 r.setEnabled(not r.isEnabled())
162 elif keystr == 's':
163 c = self.cameras['small']
164 c.setEnabled(not c.isEnabled())
165 elif keystr == 'r':
166 pass
167 # self.model.deleteMaps()
168 # self.metamodel.deleteDatasets()
169 # self.view.clearCameras()
170 # self.load(self.filename)
171 elif keystr == 'o':
172 self.target_rotation = (self.target_rotation + 90) % 360
173 elif keyval in (fife.Key.LEFT_CONTROL, fife.Key.RIGHT_CONTROL):
174 self.ctrldown = True
175
176 def keyReleased(self, evt):
177 keyval = evt.getKey().getValue()
178 if keyval in (fife.Key.LEFT_CONTROL, fife.Key.RIGHT_CONTROL):
179 self.ctrldown = False
180
181 def mouseWheelMovedUp(self, evt):
182 if self.ctrldown:
183 self.cameras['main'].setZoom(self.cameras['main'].getZoom() * 1.05)
184
185 def mouseWheelMovedDown(self, evt):
186 if self.ctrldown:
187 self.cameras['main'].setZoom(self.cameras['main'].getZoom() / 1.05)
188
189 def changeRotation(self):
190 currot = self.cameras['main'].getRotation()
191 if self.target_rotation != currot:
192 self.cameras['main'].setRotation((currot + 5) % 360)
193
194 def mousePressed(self, evt):
195 clickpoint = fife.ScreenPoint(evt.getX(), evt.getY())
196 if (evt.getButton() == fife.MouseEvent.LEFT):
197 self.hide_instancemenu()
198 target_mapcoord = self.cameras['main'].toMapCoordinates(clickpoint, False)
199 target_mapcoord.z = 0
200 l = fife.Location(self.agentlayer)
201 l.setMapCoordinates(target_mapcoord)
202 self.hero.run(l)
203
204 if (evt.getButton() == fife.MouseEvent.RIGHT):
205 instances = self.cameras['main'].getMatchingInstances(clickpoint, self.agentlayer)
206 print "selected instances on agent layer: ", [i.getObject().getId() for i in instances]
207 if instances:
208 self.show_instancemenu(clickpoint, instances[0])
209
210 def mouseMoved(self, evt):
211 renderer = fife.InstanceRenderer.getInstance(self.cameras['main'])
212 renderer.removeAllOutlines()
213
214 # pt = fife.ScreenPoint(evt.getX(), evt.getY())
215 # instances = self.cameras['main'].getMatchingInstances(pt, self.agentlayer);
216 # for i in instances:
217 # if i.getObject().getId() in ('Girl', 'Beekeeper'):
218 # renderer.addOutlined(i, 173, 255, 47, 2)
219
220 def onConsoleCommand(self, command):
221 result = ''
222 try:
223 result = str(eval(command))
224 except:
225 pass
226 return result
227
228 def onMoveButtonPress(self):
229 self.hide_instancemenu()
230 self.hero.run(self.instancemenu.instance.getLocationRef())
231
232 def onTalkButtonPress(self):
233 self.hide_instancemenu()
234 instance = self.instancemenu.instance
235 self.hero.talk(instance.getLocationRef())
236 if instance.getObject().getId() == 'Beekeeper':
237 txtindex = random.randint(0, len(TDS.beekeeperTexts) - 1)
238 instance.say(TDS.beekeeperTexts[txtindex], 5000)
239 if instance.getObject().getId() == 'Girl':
240 txtindex = random.randint(0, len(TDS.girlTexts) - 1)
241 instance.say(TDS.girlTexts[txtindex], 5000)
242
243 def onKickButtonPress(self):
244 self.hide_instancemenu()
245 self.hero.kick(self.instancemenu.instance.getLocationRef())
246 self.instancemenu.instance.say('Hey!', 1000)
247
248 def onInspectButtonPress(self):
249 self.hide_instancemenu()
250 inst = self.instancemenu.instance
251 saytext = ['Engine told me that this instance has']
252 if inst.getId():
253 saytext.append(' name %s,' % inst.getId())
254 saytext.append(' ID %s and' % inst.getFifeId())
255 saytext.append(' object name %s' % inst.getObject().getId())
256 self.hero.agent.say('\n'.join(saytext), 3500)
257
258 def pump(self):
259 if self.cameras['small'].isEnabled():
260 loc = self.cameras['small'].getLocation()
261 c = loc.getExactLayerCoordinatesRef()
262 if self.cam2_scrolling_right:
263 self.cur_cam2_x = c.x = c.x+0.1
264 if self.cur_cam2_x > self.initial_cam2_x+10:
265 self.cam2_scrolling_right = False
266 else:
267 self.cur_cam2_x = c.x = c.x-0.1
268 if self.cur_cam2_x < self.initial_cam2_x-10:
269 self.cam2_scrolling_right = True
270 self.cameras['small'].setLocation(loc)
271 self.changeRotation()
272 self.pump_ctr += 1