Mercurial > fife-parpg
changeset 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.
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/demos/shooter/objects/ships/player/explode/animation.xml Mon Apr 12 19:01:41 2010 +0000 @@ -0,0 +1,9 @@ +<animation delay="200" namespace="http://www.fifengine.de/xml/tutorial" id="ship1:explode" x_offset="0" y_offset="0"> + <frame source="ship1_0.png" /> + <frame source="ship1_1.png" /> + <frame source="ship1_2.png" /> + <frame source="ship1_3.png" /> + <frame source="ship1_4.png" /> + <frame source="ship1_5.png" /> +</animation> +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/demos/shooter/objects/ships/player/flash/animation.xml Mon Apr 12 19:01:41 2010 +0000 @@ -0,0 +1,5 @@ +<animation delay="50" namespace="http://www.fifengine.de/xml/tutorial" id="ship1:flash" x_offset="0" y_offset="0"> + <frame source="ship1_0.png" /> + <frame source="ship1_1.png" /> +</animation> +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/demos/shooter/objects/ships/player/fly/animation.xml Mon Apr 12 19:01:41 2010 +0000 @@ -0,0 +1,3 @@ +<animation delay="2000" namespace="http://www.fifengine.de/xml/tutorial" id="ship1:fly" x_offset="0" y_offset="0"> + <frame source="ship1.png" /> +</animation>
--- a/demos/shooter/objects/ships/player/object.xml Mon Apr 12 12:21:27 2010 +0000 +++ b/demos/shooter/objects/ships/player/object.xml Mon Apr 12 19:01:41 2010 +0000 @@ -1,4 +1,12 @@ <?fife type="object"?> -<object id="ship1" namespace="http://www.fifengine.de/xml/tutorial" blocking="0" static="1"> - <image source="ship1.png" direction="0" /> +<object id="ship1" namespace="http://www.fifengine.de/xml/tutorial" blocking="0" static="0"> + <action id="fly"> + <animation source="fly/animation.xml" direction="0" /> + </action> + <action id="explode"> + <animation source="explode/animation.xml" direction="0" /> + </action> + <action id="flash"> + <animation source="flash/animation.xml" direction="0" /> + </action> </object>
--- a/demos/shooter/scripts/scene.py Mon Apr 12 12:21:27 2010 +0000 +++ b/demos/shooter/scripts/scene.py Mon Apr 12 19:01:41 2010 +0000 @@ -138,7 +138,7 @@ #self.removeAllProjectiles() return - self._player.setInvulnerable(2) + #self._player.setInvulnerable(2) def removeAllProjectiles(self):
--- a/demos/shooter/scripts/ships/enemies.py Mon Apr 12 12:21:27 2010 +0000 +++ b/demos/shooter/scripts/ships/enemies.py Mon Apr 12 19:01:41 2010 +0000 @@ -22,11 +22,18 @@ # #################################################################### from fife import fife -from scripts.ships.shipbase import Ship +from scripts.ships.shipbase import * from scripts.common.helpers import Rect from scripts.weapons import * +class EnemyActionListener(ShipActionListener): + def __init__(self, ship): + super(PlayerActionListener, self).__init__(ship) + + def onInstanceActionFinished(self, instance, action): + pass + class Saucer1(Ship): def __init__(self, scene, name, findInstance=True): super(Saucer1, self).__init__(scene, name, findInstance)
--- a/demos/shooter/scripts/ships/player.py Mon Apr 12 12:21:27 2010 +0000 +++ b/demos/shooter/scripts/ships/player.py Mon Apr 12 19:01:41 2010 +0000 @@ -22,11 +22,26 @@ # #################################################################### from fife import fife -from scripts.ships.shipbase import Ship +from scripts.ships.shipbase import * from scripts.common.helpers import Rect from scripts.weapons import * +class PlayerActionListener(ShipActionListener): + def __init__(self, ship): + super(PlayerActionListener, self).__init__(ship) + + def onInstanceActionFinished(self, instance, action): + if action.getId() == 'explode': + self._ship.respawn() + if action.getId() == 'flash': + if self._ship._flashnumber > 0: + self._ship.instance.act('flash', self._ship.instance.getFacingLocation()) + self._ship._flashnumber -= 1 + else: + self._ship._flashing = False + + class Player(Ship): def __init__(self, scene, playerName): super(Player, self).__init__(scene, playerName) @@ -44,53 +59,74 @@ self._invulnerable = False self._isplayer = True - + self._dead = False + + self._actionlistener = PlayerActionListener(self) + def init(self): self._lives = 3 def _getScore(self): return self._score + def respawn(self): + if self._lives >= 0: + self._dead = False + self.setInvulnerable(20) + + campos = self._scene.camera.getLocation().getExactLayerCoordinates() + location = self.location + playerpos = location.getExactLayerCoordinates() + + playerpos.x = campos.x - 6.5 + playerpos.y = campos.y + location.setExactLayerCoordinates(playerpos) + self.location = location + + else: + self._instance.get2dGfxVisual().setVisible(False) + + def setInvulnerable(self, seconds): + self._invulnerable = True + self.flash(seconds) + def applyScore(self, sc): self._score += sc def destroy(self): - if not self._flashing: - self._lives -= 1 - - if self._lives < 0: - self._lives = -1 - - def setInvulnerable(self, seconds): - self.flash(20, 20*seconds) - self._invulnerable = True + if not self._invulnerable and not self._dead: + self._instance.act('explode', self._instance.getFacingLocation()) + self._dead = True + self._invulnerable = True + self._lives -= 1 def update(self): key = False #player is no longer invulnerable - if not self._flashing and self._invulnerable: + if not self._flashing and self._invulnerable and not self._dead: self._invulnerable = False oldpos = self.location - if self._scene.keystate['UP']: - self.applyThrust(fife.DoublePoint(0,-1.5)) - key = True - if self._scene.keystate['DOWN']: - self.applyThrust(fife.DoublePoint(0,1.5)) - key = True - if self._scene.keystate['LEFT']: - self.applyThrust(fife.DoublePoint(-1.5,0)) - key = True - if self._scene.keystate['RIGHT']: - self.applyThrust(fife.DoublePoint(1.5,0)) - key = True + if not self._dead: + if self._scene.keystate['UP']: + self.applyThrust(fife.DoublePoint(0,-1.5)) + key = True + if self._scene.keystate['DOWN']: + self.applyThrust(fife.DoublePoint(0,1.5)) + key = True + if self._scene.keystate['LEFT']: + self.applyThrust(fife.DoublePoint(-1.5,0)) + key = True + if self._scene.keystate['RIGHT']: + self.applyThrust(fife.DoublePoint(1.5,0)) + key = True - #fire the currently selected gun - if self._scene.keystate['SPACE']: - self.fire(fife.DoublePoint(1,0)) + #fire the currently selected gun + if self._scene.keystate['SPACE']: + self.fire(fife.DoublePoint(1,0)) if not key and self._velocity.length() > 0: self.applyBrake(1.5) @@ -139,6 +175,9 @@ def _getInvulnerable(self): return self._invulnerable + def _setInvulnerable(self, inv): + self._invulnerable = inv + score = property(_getScore) lives = property(_getLives) - invulnerable = property(_getInvulnerable) \ No newline at end of file + invulnerable = property(_getInvulnerable, _setInvulnerable) \ No newline at end of file
--- a/demos/shooter/scripts/ships/shipbase.py Mon Apr 12 12:21:27 2010 +0000 +++ b/demos/shooter/scripts/ships/shipbase.py Mon Apr 12 19:01:41 2010 +0000 @@ -28,18 +28,24 @@ from scripts.weapons import Weapon +class ShipActionListener(fife.InstanceActionListener): + def __init__(self, ship): + fife.InstanceActionListener.__init__(self) + + self._ship = ship + self._ship.instance.addActionListener(self) + + def onInstanceActionFinished(self, instance, action): + pass + class Ship(SpaceObject): def __init__(self, scene, name, findInstance=True): super(Ship, self).__init__(scene, name, findInstance) self._weapon = None - self._flashrate = 0 + self._flashnumber = 0 self._flashing = False - self._flashtime = 0 - - #1 = on, 0 = invisible (off) - self._flashstate = 1 self._isplayer = False @@ -48,46 +54,22 @@ def _getWeapon(self): return self._weapon - + + def flash(self, number): + self._instance.act('flash', self._instance.getFacingLocation()) + self._flashnumber = number + self._flashing = True + def fire(self, direction): if self._weapon: return self._weapon.fire(direction) return None - def flash(self, rate, number): - """ - Flash rate is measured in flashes per second. A single flash - would be an off and on cycle. - """ - self._flashing = True - self._flashrate = rate * 2 - self._flashnumber = number * 2 - def destroy(self): self._scene.removeObjectFromScene(self) super(Ship, self).destroy() - def update(self): - if self._flashing: - if self._flashnumber <= 0: - self._flashing = False - self._instance.get2dGfxVisual().setVisible(True) - else: - self._flashtime += self._scene.timedelta - if self._flashtime >= 1000/self._flashrate: - if self._flashstate == 1: - self._flashstate = 0 - self._instance.get2dGfxVisual().setVisible(False) - else: - self._flashstate = 1 - self._instance.get2dGfxVisual().setVisible(True) - - self._flashtime = 0 - self._flashnumber -= 1 - - super(Ship, self).update() - def _isPlayer(self): return self._isplayer
--- a/demos/shooter/scripts/world.py Mon Apr 12 12:21:27 2010 +0000 +++ b/demos/shooter/scripts/world.py Mon Apr 12 19:01:41 2010 +0000 @@ -299,7 +299,10 @@ score = unicode(str(player.score)) self._hudwindow.setScoreText(score) - lives = unicode(str(player.lives)) + ilives = player.lives + if ilives < 0: + ilives = 0 + lives = unicode(str(ilives)) self._hudwindow.setLivesText(lives) else: