comparison clients/rio_de_hola/scripts/world.py @ 129:9a1529f9625e

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