comparison demos/shooter/scripts/weapons.py @ 453:cf53848fb187

Scene now gets updated when an object moves from one node to another. Player is now part of the scene. Projectiles can be files by both player and enemies. Some code cleanup.
author prock@33b003aa-7bff-0310-803a-e67f0ece8222
date Tue, 06 Apr 2010 19:12:41 +0000
parents f463ab431cc0
children bd7e8f708adf
comparison
equal deleted inserted replaced
452:f07d779362da 453:cf53848fb187
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.ships.shipbase import SpaceObject
26 from scripts.common.helpers import normalize
26 27
27 class Projectile(SpaceObject): 28 class Projectile(SpaceObject):
28 def __init__(self, model, projectileName, layer, timeToLive): 29 def __init__(self, scene, owner, projectileName, timeToLive):
29 super(Projectile, self).__init__(model, projectileName, layer, False) 30 super(Projectile, self).__init__(scene, projectileName, False)
30 self._name = projectileName
31 self._layer = layer
32 31
33 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")
34 33
35 self._ttl = timeToLive 34 self._ttl = timeToLive
36 self._starttime = 0 35 self._starttime = 0
37 self._totaltime = 0 36 self._totaltime = 0
37
38 self._owner = owner
39
40 self.width = 0.025
41 self.height = 0.025
38 42
39 def create(self, location): 43 def create(self, location):
40 self._instance = self._layer.createInstance(self._obj, location.getExactLayerCoordinates(), "bullet") 44 self._instance = self._layer.createInstance(self._obj, location.getExactLayerCoordinates(), "bullet")
41 fife.InstanceVisual.create(self._instance) 45 fife.InstanceVisual.create(self._instance)
42 46
56 self._layer.deleteInstance(self._instance) 60 self._layer.deleteInstance(self._instance)
57 self._running = False 61 self._running = False
58 62
59 def _getTTL(self): 63 def _getTTL(self):
60 return self._ttl 64 return self._ttl
65
66 def _getOwner(self):
67 return self._owner
61 68
62 def update(self, timedelta): 69 def update(self, timedelta):
63 self._totaltime += timedelta 70 self._totaltime += timedelta
64 if self._running and (self._totaltime - self._starttime) < self._ttl: 71 if self._running and (self._totaltime - self._starttime) < self._ttl:
65 super(Projectile, self).update(timedelta) 72 super(Projectile, self).update(timedelta)
66 else: 73 else:
67 self.destroy() 74 self.destroy()
68 75
69 ttl = property(_getTTL) 76 ttl = property(_getTTL)
77 owner = property(_getOwner)
70 78
71 class Weapon(object): 79 class Weapon(object):
72 def __init__(self, model, layer, ship, firerate): 80 def __init__(self, scene, ship, firerate):
73 self._model = model 81 self._scene = scene
74 self._layer = layer 82 self._model = self._scene.model
83 self._layer = self._scene.objectlayer
75 self._ship = ship 84 self._ship = ship
76 self._firerate = firerate 85 self._firerate = firerate
77 self._lastfired = 0 86 self._lastfired = 0
78 self._projectileVelocity = fife.DoublePoint(0.75,0) 87 self._projectileVelocity = 0.75
79 88
80 def fire(self, curtime): 89 def fire(self, curtime, direction):
90 velocity = normalize(direction)
91 velocity.x = velocity.x * self._projectileVelocity
92 velocity.y = velocity.y * self._projectileVelocity
93
81 if (curtime - self._lastfired) > self._firerate: 94 if (curtime - self._lastfired) > self._firerate:
82 pjctl = Projectile(self._model, "bullet1", self._layer, 2000 ) 95 pjctl = Projectile(self._scene, self._ship, "bullet1", 2000 )
83 pjctl.width = 0.025 96 pjctl.run(velocity, self._ship.location, curtime)
84 pjctl.height = 0.025
85 pjctl.run(fife.DoublePoint(self._projectileVelocity.x,self._projectileVelocity.y), self._ship.location, curtime)
86 self._lastfired = curtime 97 self._lastfired = curtime
87 return pjctl 98 return pjctl
88 99
89 return None 100 return None
90 101