comparison demos/shooter/scripts/scene.py @ 478:41f7754f2a4b

Fixed a small bug where projectiles wouldn't be removed from the scene after passing the far right edge of the screen. Added a garbage collection routine. Removed the remaining traces of the projectile specific code in Scene.
author prock@33b003aa-7bff-0310-803a-e67f0ece8222
date Thu, 22 Apr 2010 21:04:13 +0000
parents 6b33d80b468b
children ab28994820dd
comparison
equal deleted inserted replaced
477:6b33d80b468b 478:41f7754f2a4b
49 self._model = engine.getModel() 49 self._model = engine.getModel()
50 self._layer = objectLayer 50 self._layer = objectLayer
51 self._nodes = list() 51 self._nodes = list()
52 52
53 self._player = None 53 self._player = None
54 self._projectiles = list()
55 self._objectstodelete = list() 54 self._objectstodelete = list()
56 55
57 self._maxnodes = 128 56 self._maxnodes = 128
58 self._xscale = 0 57 self._xscale = 0
59 58
78 for obj in node.spaceobjects: 77 for obj in node.spaceobjects:
79 objtodelete.append(node) 78 objtodelete.append(node)
80 79
81 for obj in objtodelete: 80 for obj in objtodelete:
82 if obj in node.spaceobjects: 81 if obj in node.spaceobjects:
82 if obj.instance:
83 self._layer.deleteInstance(obj.instance)
84 obj.instance = None
83 node.spaceobjects.remove(obj) 85 node.spaceobjects.remove(obj)
84 86
85 objtodelete = list() 87 objtodelete = list()
86 88
87 for node in nodestodelete: 89 for node in nodestodelete:
88 if node in self._nodes: 90 if node in self._nodes:
89 self._nodes.remove(node) 91 self._nodes.remove(node)
90 92
91 self.removeAllProjectiles()
92
93 def initScene(self, mapobj): 93 def initScene(self, mapobj):
94 #initialize our scene array to some arbitrary size 94 #initialize our scene array to some arbitrary size
95 for i in range(0,self._maxnodes): 95 for i in range(0,self._maxnodes):
96 self._nodes.append(SceneNode()) 96 self._nodes.append(SceneNode())
97 97
168 def endLevel(self): 168 def endLevel(self):
169 self._world.endLevel() 169 self._world.endLevel()
170 170
171 def queueObjectForRemoval(self, obj): 171 def queueObjectForRemoval(self, obj):
172 self._objectstodelete.append(obj) 172 self._objectstodelete.append(obj)
173
174 def removeAllProjectiles(self):
175 projtodelete = list()
176 for p in self._projectiles:
177 p.destroy()
178 projtodelete.append(p)
179
180 for p in projtodelete:
181 if p in self._projectiles:
182 self._projectiles.remove(p)
183 173
184 def getObjectsInNode(self, nodeindex): 174 def getObjectsInNode(self, nodeindex):
185 return self._nodes[nodeindex].instances 175 return self._nodes[nodeindex].instances
186 176
187 def getObjectsInRange(self, rangeL, rangeR): 177 def getObjectsInRange(self, rangeL, rangeR):
197 loc = obj.instance.getLocation().getExactLayerCoordinates() 187 loc = obj.instance.getLocation().getExactLayerCoordinates()
198 nodeindex = int(loc.x * self._xscale) 188 nodeindex = int(loc.x * self._xscale)
199 189
200 if nodeindex >= 0: 190 if nodeindex >= 0:
201 obj.scenenodeid = nodeindex 191 obj.scenenodeid = nodeindex
202 self._nodes[nodeindex].spaceobjects.append(obj) 192 if not obj in self._nodes[nodeindex].spaceobjects:
193 self._nodes[nodeindex].spaceobjects.append(obj)
203 else: 194 else:
204 self.queueObjectForRemoval(obj) 195 self.queueObjectForRemoval(obj)
205 196
206 def moveObjectInScene(self, obj): 197 def moveObjectInScene(self, obj):
207 loc = obj.instance.getLocation().getExactLayerCoordinates() 198 loc = obj.instance.getLocation().getExactLayerCoordinates()
234 def stopCamera(self): 225 def stopCamera(self):
235 self._cameraspeed = 0 226 self._cameraspeed = 0
236 227
237 def startCamera(self): 228 def startCamera(self):
238 self._cameraspeed = 0.001 229 self._cameraspeed = 0.001
230
231 def collectGarbage(self):
232 for obj in self._objectstodelete:
233 self.removeObjectFromScene(obj)
234
235 self._objectstodelete = list()
239 236
240 def update(self, time, keystate): 237 def update(self, time, keystate):
241 timedelta = (time - self._timemod) - self._time 238 timedelta = (time - self._timemod) - self._time
242 self._timedelta = timedelta 239 self._timedelta = timedelta
243 self._time = time - self._timemod 240 self._time = time - self._timemod
244 241
245 self._keystate = keystate 242 self._keystate = keystate
246 243
247 244
248 #some garbage cleanup 245 #some garbage cleanup
249 for obj in self._objectstodelete: 246 self.collectGarbage()
250 self.removeObjectFromScene(obj)
251
252 self._objectstodelete = list()
253 247
254 #update camera location 248 #update camera location
255 loc = self._camera.getLocation() 249 loc = self._camera.getLocation()
256 exactloc = self._camera.getLocation().getExactLayerCoordinates() 250 exactloc = self._camera.getLocation().getExactLayerCoordinates()
257 #slowly move to the right 251 #slowly move to the right
293 #collision damage of 1 287 #collision damage of 1
294 self.playerHit(1) 288 self.playerHit(1)
295 obj.applyHit(1) 289 obj.applyHit(1)
296 290
297 elif obj.type == SHTR_PROJECTILE: 291 elif obj.type == SHTR_PROJECTILE:
292
293 #projectile went off the right edge of the screen. Remove it from the scene.
294 if obj.scenenodeid >= rightnode:
295 self.queueObjectForRemoval(obj)
296 continue
297
298 #could probably just get the nodes in the projectiles scenenode. 298 #could probably just get the nodes in the projectiles scenenode.
299 #use a range to be sure. 299 #use a range to be sure.
300 pcollide = self.getObjectsInRange(obj.scenenodeid - 1, obj.scenenodeid + 1) 300 pcollide = self.getObjectsInRange(obj.scenenodeid - 1, obj.scenenodeid + 1)
301 301
302 for o in pcollide: 302 for o in pcollide: