comparison demos/shooter/scripts/ships/shipbase.py @ 462:c4f745a566d6

Added player ship animations including flash and explode. Removed the old flashing routine. The player now gets moved to the left side of the screen after dying and is invulnerable for a short period of time.
author prock@33b003aa-7bff-0310-803a-e67f0ece8222
date Mon, 12 Apr 2010 19:01:41 +0000
parents f87f686b5b59
children ac0f62a07a3e
comparison
equal deleted inserted replaced
461:f87f686b5b59 462:c4f745a566d6
26 from fife import fife 26 from fife import fife
27 from scripts.common.baseobject import SpaceObject 27 from scripts.common.baseobject import SpaceObject
28 from scripts.weapons import Weapon 28 from scripts.weapons import Weapon
29 29
30 30
31 class ShipActionListener(fife.InstanceActionListener):
32 def __init__(self, ship):
33 fife.InstanceActionListener.__init__(self)
34
35 self._ship = ship
36 self._ship.instance.addActionListener(self)
37
38 def onInstanceActionFinished(self, instance, action):
39 pass
40
31 class Ship(SpaceObject): 41 class Ship(SpaceObject):
32 def __init__(self, scene, name, findInstance=True): 42 def __init__(self, scene, name, findInstance=True):
33 super(Ship, self).__init__(scene, name, findInstance) 43 super(Ship, self).__init__(scene, name, findInstance)
34 44
35 self._weapon = None 45 self._weapon = None
36 self._flashrate = 0 46
37 self._flashnumber = 0 47 self._flashnumber = 0
38 self._flashing = False 48 self._flashing = False
39 self._flashtime = 0
40
41 #1 = on, 0 = invisible (off)
42 self._flashstate = 1
43 49
44 self._isplayer = False 50 self._isplayer = False
45 51
46 def _setWeapon(self, weapon): 52 def _setWeapon(self, weapon):
47 self._weapon = weapon 53 self._weapon = weapon
48 54
49 def _getWeapon(self): 55 def _getWeapon(self):
50 return self._weapon 56 return self._weapon
51 57
58 def flash(self, number):
59 self._instance.act('flash', self._instance.getFacingLocation())
60 self._flashnumber = number
61 self._flashing = True
62
52 def fire(self, direction): 63 def fire(self, direction):
53 if self._weapon: 64 if self._weapon:
54 return self._weapon.fire(direction) 65 return self._weapon.fire(direction)
55 66
56 return None 67 return None
57 68
58 def flash(self, rate, number):
59 """
60 Flash rate is measured in flashes per second. A single flash
61 would be an off and on cycle.
62 """
63 self._flashing = True
64 self._flashrate = rate * 2
65 self._flashnumber = number * 2
66
67 def destroy(self): 69 def destroy(self):
68 self._scene.removeObjectFromScene(self) 70 self._scene.removeObjectFromScene(self)
69 super(Ship, self).destroy() 71 super(Ship, self).destroy()
70 72
71 def update(self):
72 if self._flashing:
73 if self._flashnumber <= 0:
74 self._flashing = False
75 self._instance.get2dGfxVisual().setVisible(True)
76 else:
77 self._flashtime += self._scene.timedelta
78 if self._flashtime >= 1000/self._flashrate:
79 if self._flashstate == 1:
80 self._flashstate = 0
81 self._instance.get2dGfxVisual().setVisible(False)
82 else:
83 self._flashstate = 1
84 self._instance.get2dGfxVisual().setVisible(True)
85
86 self._flashtime = 0
87 self._flashnumber -= 1
88
89 super(Ship, self).update()
90
91 def _isPlayer(self): 73 def _isPlayer(self):
92 return self._isplayer 74 return self._isplayer
93 75
94 isplayer = property(_isPlayer) 76 isplayer = property(_isPlayer)
95 weapon = property(_getWeapon, _setWeapon) 77 weapon = property(_getWeapon, _setWeapon)