comparison demos/shooter/scripts/weapons.py @ 477:6b33d80b468b

Projectiles are now part of the scene. Cleaned up the scene update() function. Moved garbage collection to the beginning of the frame. Introduced global object types. Projectiles now only check for collisions in a limited number of scene nodes rather than the entire screen.
author prock@33b003aa-7bff-0310-803a-e67f0ece8222
date Thu, 22 Apr 2010 19:52:34 +0000
parents afde89c1d50b
children ab28994820dd
comparison
equal deleted inserted replaced
476:78a1eb57c074 477:6b33d80b468b
20 # Free Software Foundation, Inc., 20 # Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 # #################################################################### 22 # ####################################################################
23 23
24 from fife import fife 24 from fife import fife
25 from scripts.ships.shipbase import SpaceObject 25 from scripts.common.baseobject import *
26 from scripts.common.helpers import normalize, rotatePoint 26 from scripts.common.helpers import normalize, rotatePoint
27 27
28 class Projectile(SpaceObject): 28 class Projectile(SpaceObject):
29 def __init__(self, scene, owner, projectileName, timeToLive): 29 def __init__(self, scene, owner, projectileName, timeToLive):
30 super(Projectile, self).__init__(scene, projectileName, False) 30 super(Projectile, self).__init__(scene, projectileName, False)
31 31
32 self._obj = self._model.getObject(self._name, "http://www.fifengine.de/xml/tutorial") 32 self._obj = self._model.getObject(self._name, "http://www.fifengine.de/xml/tutorial")
33
34 self._type = SHTR_PROJECTILE
33 35
34 self._ttl = timeToLive 36 self._ttl = timeToLive
35 self._starttime = 0 37 self._starttime = 0
36 self._totaltime = 0 38 self._totaltime = 0
37 39
55 57
56 self.create(location) 58 self.create(location)
57 self._running = True 59 self._running = True
58 60
59 self._starttime = self._scene.time 61 self._starttime = self._scene.time
60
61 def destroy(self):
62 if self._instance:
63 self._layer.deleteInstance(self._instance)
64 self._instance = None
65
66 self._running = False
67 62
68 def _getTTL(self): 63 def _getTTL(self):
69 return self._ttl 64 return self._ttl
70 65
71 def _getOwner(self): 66 def _getOwner(self):
130 125
131 if (self._scene.time - self._lastfired) > self._firerate: 126 if (self._scene.time - self._lastfired) > self._firerate:
132 pjctl = Projectile(self._scene, self._ship, "bullet1", 3000 ) 127 pjctl = Projectile(self._scene, self._ship, "bullet1", 3000 )
133 pjctl.run(velocity, self._ship.location) 128 pjctl.run(velocity, self._ship.location)
134 self._lastfired = self._scene.time 129 self._lastfired = self._scene.time
135 self._scene.addProjectileToScene(pjctl) 130 self._scene.addObjectToScene(pjctl)
136 131
137 class FireBall(Weapon): 132 class FireBall(Weapon):
138 def __init__(self, scene, ship, firerate): 133 def __init__(self, scene, ship, firerate):
139 super(FireBall, self).__init__(scene, ship, firerate) 134 super(FireBall, self).__init__(scene, ship, firerate)
140 135
148 143
149 if (self._scene.time - self._lastfired) > self._firerate: 144 if (self._scene.time - self._lastfired) > self._firerate:
150 pjctl = Projectile(self._scene, self._ship, "fireball", 6000 ) 145 pjctl = Projectile(self._scene, self._ship, "fireball", 6000 )
151 pjctl.run(velocity, self._ship.location) 146 pjctl.run(velocity, self._ship.location)
152 self._lastfired = self._scene.time 147 self._lastfired = self._scene.time
153 self._scene.addProjectileToScene(pjctl) 148 self._scene.addObjectToScene(pjctl)
154 149
155 class FireBallBurst(Weapon): 150 class FireBallBurst(Weapon):
156 def __init__(self, scene, ship, firerate, burstrate, burstnumber): 151 def __init__(self, scene, ship, firerate, burstrate, burstnumber):
157 super(FireBallBurst, self).__init__(scene, ship, firerate) 152 super(FireBallBurst, self).__init__(scene, ship, firerate)
158 153
172 167
173 if (self._scene.time - self._lastfired) > self._firerate: 168 if (self._scene.time - self._lastfired) > self._firerate:
174 if (self._scene.time - self._lastburstfired) > self._burstrate and self._burstcount > 0: 169 if (self._scene.time - self._lastburstfired) > self._burstrate and self._burstcount > 0:
175 pjctl = Projectile(self._scene, self._ship, "fireball", 6000 ) 170 pjctl = Projectile(self._scene, self._ship, "fireball", 6000 )
176 pjctl.run(velocity, self._ship.location) 171 pjctl.run(velocity, self._ship.location)
177 self._scene.addProjectileToScene(pjctl) 172 self._scene.addObjectToScene(pjctl)
178 173
179 self._lastburstfired = self._scene.time 174 self._lastburstfired = self._scene.time
180 self._burstcount -= 1 175 self._burstcount -= 1
181 176
182 if self._burstcount <= 0: 177 if self._burstcount <= 0:
208 p6 = rotatePoint(origin, velocity, 20) 203 p6 = rotatePoint(origin, velocity, 20)
209 p7 = rotatePoint(origin, velocity, 30) 204 p7 = rotatePoint(origin, velocity, 30)
210 205
211 pjctl1 = Projectile(self._scene, self._ship, "fireball", 6000 ) 206 pjctl1 = Projectile(self._scene, self._ship, "fireball", 6000 )
212 pjctl1.run(p1, self._ship.location) 207 pjctl1.run(p1, self._ship.location)
213 self._scene.addProjectileToScene(pjctl1) 208 self._scene.addObjectToScene(pjctl1)
214 209
215 pjctl2 = Projectile(self._scene, self._ship, "fireball", 6000 ) 210 pjctl2 = Projectile(self._scene, self._ship, "fireball", 6000 )
216 pjctl2.run(p2, self._ship.location) 211 pjctl2.run(p2, self._ship.location)
217 self._scene.addProjectileToScene(pjctl2) 212 self._scene.addObjectToScene(pjctl2)
218 213
219 pjctl3 = Projectile(self._scene, self._ship, "fireball", 6000 ) 214 pjctl3 = Projectile(self._scene, self._ship, "fireball", 6000 )
220 pjctl3.run(p3, self._ship.location) 215 pjctl3.run(p3, self._ship.location)
221 self._scene.addProjectileToScene(pjctl3) 216 self._scene.addObjectToScene(pjctl3)
222 217
223 pjctl4 = Projectile(self._scene, self._ship, "fireball", 6000 ) 218 pjctl4 = Projectile(self._scene, self._ship, "fireball", 6000 )
224 pjctl4.run(p4, self._ship.location) 219 pjctl4.run(p4, self._ship.location)
225 self._scene.addProjectileToScene(pjctl4) 220 self._scene.addObjectToScene(pjctl4)
226 221
227 pjctl5 = Projectile(self._scene, self._ship, "fireball", 6000 ) 222 pjctl5 = Projectile(self._scene, self._ship, "fireball", 6000 )
228 pjctl5.run(p5, self._ship.location) 223 pjctl5.run(p5, self._ship.location)
229 self._scene.addProjectileToScene(pjctl5) 224 self._scene.addObjectToScene(pjctl5)
230 225
231 pjctl6 = Projectile(self._scene, self._ship, "fireball", 6000 ) 226 pjctl6 = Projectile(self._scene, self._ship, "fireball", 6000 )
232 pjctl6.run(p6, self._ship.location) 227 pjctl6.run(p6, self._ship.location)
233 self._scene.addProjectileToScene(pjctl6) 228 self._scene.addObjectToScene(pjctl6)
234 229
235 pjctl7 = Projectile(self._scene, self._ship, "fireball", 6000 ) 230 pjctl7 = Projectile(self._scene, self._ship, "fireball", 6000 )
236 pjctl7.run(p7, self._ship.location) 231 pjctl7.run(p7, self._ship.location)
237 self._scene.addProjectileToScene(pjctl7) 232 self._scene.addObjectToScene(pjctl7)
238 233
239 self._lastfired = self._scene.time 234 self._lastfired = self._scene.time
240 235