annotate demos/shooter/scripts/ships/player.py @ 575:872a7a94563e

- Updated the soundmanager extension to reflect changes made in last commit. - Made actor walk speed part of the ActorAttributes class. - The ActorAttributes class now gets serialized correctly.
author prock@33b003aa-7bff-0310-803a-e67f0ece8222
date Fri, 02 Jul 2010 14:41:27 +0000
parents e241d7553496
children 291ba2946c73
rev   line source
446
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
1 # -*- coding: utf-8 -*-
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
2
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
3 # ####################################################################
491
c4168eb47a44 Adding some comments to the shooter demo. Updated the copyright date and FIFE URL as well.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 485
diff changeset
4 # Copyright (C) 2005-2010 by the FIFE team
c4168eb47a44 Adding some comments to the shooter demo. Updated the copyright date and FIFE URL as well.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 485
diff changeset
5 # http://www.fifengine.net
446
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
6 # This file is part of FIFE.
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
7 #
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
8 # FIFE is free software; you can redistribute it and/or
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
9 # modify it under the terms of the GNU Lesser General Public
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
10 # License as published by the Free Software Foundation; either
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
11 # version 2.1 of the License, or (at your option) any later version.
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
12 #
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
13 # This library is distributed in the hope that it will be useful,
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
16 # Lesser General Public License for more details.
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
17 #
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
18 # You should have received a copy of the GNU Lesser General Public
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
19 # License along with this library; if not, write to the
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
20 # Free Software Foundation, Inc.,
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
22 # ####################################################################
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
23
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
24 from fife import fife
477
6b33d80b468b Projectiles are now part of the scene.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 475
diff changeset
25 from scripts.common.baseobject import *
462
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
26 from scripts.ships.shipbase import *
492
16ceb3228324 Moved the SoundManager and the 2D math function (helpers) from the shooter demo to the fife extensions.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 491
diff changeset
27 from fife.extensions.fife_math import *
454
bd7e8f708adf Time is now managed by the Scene object.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 453
diff changeset
28 from scripts.weapons import *
446
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
29
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
30
462
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
31 class PlayerActionListener(ShipActionListener):
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
32 def __init__(self, ship):
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
33 super(PlayerActionListener, self).__init__(ship)
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
34
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
35 def onInstanceActionFinished(self, instance, action):
474
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
36 super(PlayerActionListener, self).onInstanceActionFinished(instance, action)
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
37
462
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
38 if action.getId() == 'explode':
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
39 self._ship.respawn()
474
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
40
462
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
41
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
42
446
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
43 class Player(Ship):
453
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 451
diff changeset
44 def __init__(self, scene, playerName):
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 451
diff changeset
45 super(Player, self).__init__(scene, playerName)
451
f463ab431cc0 Movement shouldn't be dependent on framerate anymore.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 450
diff changeset
46
477
6b33d80b468b Projectiles are now part of the scene.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 475
diff changeset
47 self._type = SHTR_PLAYER
6b33d80b468b Projectiles are now part of the scene.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 475
diff changeset
48
450
ba6817013343 Added score keeping ability.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 449
diff changeset
49 self._score = 0
453
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 451
diff changeset
50 self._maxvelocity = 1.5
474
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
51 self._acceleration = 1.0
454
bd7e8f708adf Time is now managed by the Scene object.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 453
diff changeset
52
460
5e1d6e40d19d Fixed bounding boxes.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 459
diff changeset
53 self.width = 0.22
5e1d6e40d19d Fixed bounding boxes.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 459
diff changeset
54 self.height = 0.12
5e1d6e40d19d Fixed bounding boxes.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 459
diff changeset
55
474
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
56 self._actionlistener = PlayerActionListener(self)
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
57
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
58 self._lives = 3
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
59 self.init()
485
d365eb58f3d6 SoundClip can now fire a callback after a sound has completed being played (i.e. the duration of the sound has passed by). I have added an example of this with the scene music. [t:346]
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 479
diff changeset
60
474
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
61
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
62 def init(self):
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
63 self._hitpoints = 2
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
64 self._dead = False
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
65 self._invulnerable = False
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
66
454
bd7e8f708adf Time is now managed by the Scene object.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 453
diff changeset
67 #give player the default weapon (the cannon)
bd7e8f708adf Time is now managed by the Scene object.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 453
diff changeset
68 self.weapon = Cannon(self._scene, self, 200)
457
597b066d5ccb Player now has 3 lives and will receive a game over message once they are used up.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 455
diff changeset
69
450
ba6817013343 Added score keeping ability.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 449
diff changeset
70 def _getScore(self):
ba6817013343 Added score keeping ability.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 449
diff changeset
71 return self._score
446
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
72
462
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
73 def respawn(self):
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
74 if self._lives >= 0:
474
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
75 self.init()
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
76 self.setInvulnerable(1000)
462
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
77
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
78 campos = self._scene.camera.getLocation().getExactLayerCoordinates()
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
79 location = self.location
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
80 playerpos = location.getExactLayerCoordinates()
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
81
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
82 playerpos.x = campos.x - 6.5
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
83 playerpos.y = campos.y
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
84 location.setExactLayerCoordinates(playerpos)
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
85 self.location = location
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
86
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
87 else:
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
88 self._instance.get2dGfxVisual().setVisible(False)
475
afde89c1d50b Switched to a damage system. Collisions with objects now cause 1 damage to the player.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 474
diff changeset
89 self._scene.gameOver()
462
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
90
474
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
91 def setInvulnerable(self, milliseconds):
494
e241d7553496 Fixing the epydoc markup. Did some more commenting.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 492
diff changeset
92 """
e241d7553496 Fixing the epydoc markup. Did some more commenting.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 492
diff changeset
93 50 is defined in the players "flash" animation file
e241d7553496 Fixing the epydoc markup. Did some more commenting.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 492
diff changeset
94 2 is the number of frames in the animation
e241d7553496 Fixing the epydoc markup. Did some more commenting.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 492
diff changeset
95
e241d7553496 Fixing the epydoc markup. Did some more commenting.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 492
diff changeset
96 @todo: read these values somehow from the animation
e241d7553496 Fixing the epydoc markup. Did some more commenting.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 492
diff changeset
97
e241d7553496 Fixing the epydoc markup. Did some more commenting.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 492
diff changeset
98 """
479
ab28994820dd Added some powerups including a spread weapon and an extra life.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 477
diff changeset
99 number = int((milliseconds / 50) / 2)
ab28994820dd Added some powerups including a spread weapon and an extra life.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 477
diff changeset
100
ab28994820dd Added some powerups including a spread weapon and an extra life.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 477
diff changeset
101 if number <= 0:
ab28994820dd Added some powerups including a spread weapon and an extra life.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 477
diff changeset
102 return
474
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
103
462
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
104 self._invulnerable = True
474
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
105 self.flash(number)
462
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
106
450
ba6817013343 Added score keeping ability.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 449
diff changeset
107 def applyScore(self, sc):
ba6817013343 Added score keeping ability.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 449
diff changeset
108 self._score += sc
453
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 451
diff changeset
109
474
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
110 def applyHit(self, hp):
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
111 if not self._invulnerable and not self._dead:
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
112 super(Player, self).applyHit(hp)
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
113 if not self._dead:
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
114 self.flash(1)
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
115
453
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 451
diff changeset
116 def destroy(self):
462
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
117 if not self._invulnerable and not self._dead:
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
118 self._instance.act('explode', self._instance.getFacingLocation())
492
16ceb3228324 Moved the SoundManager and the 2D math function (helpers) from the shooter demo to the fife extensions.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 491
diff changeset
119 self._explodclip.play()
462
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
120 self._dead = True
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
121 self._invulnerable = True
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
122 self._lives -= 1
459
302a69c5141d Player death is now handled a bit nicer.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 457
diff changeset
123
471
7a79dc2a0592 Adding the credits window (still needs to be written)
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 462
diff changeset
124 def fire(self, direction):
7a79dc2a0592 Adding the credits window (still needs to be written)
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 462
diff changeset
125 return self._weapon.fire(direction)
7a79dc2a0592 Adding the credits window (still needs to be written)
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 462
diff changeset
126
454
bd7e8f708adf Time is now managed by the Scene object.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 453
diff changeset
127 def update(self):
457
597b066d5ccb Player now has 3 lives and will receive a game over message once they are used up.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 455
diff changeset
128
474
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
129 NSkey = False
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
130 EWkey = False
459
302a69c5141d Player death is now handled a bit nicer.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 457
diff changeset
131
302a69c5141d Player death is now handled a bit nicer.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 457
diff changeset
132 #player is no longer invulnerable
462
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
133 if not self._flashing and self._invulnerable and not self._dead:
459
302a69c5141d Player death is now handled a bit nicer.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 457
diff changeset
134 self._invulnerable = False
453
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 451
diff changeset
135
446
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
136 oldpos = self.location
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
137
462
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
138 if not self._dead:
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
139 if self._scene.keystate['UP']:
474
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
140 self.applyThrust(fife.DoublePoint(0,-1*self._acceleration))
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
141 NSkey = True
462
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
142 if self._scene.keystate['DOWN']:
474
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
143 self.applyThrust(fife.DoublePoint(0,self._acceleration))
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
144 NSkey = True
462
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
145 if self._scene.keystate['LEFT']:
474
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
146 self.applyThrust(fife.DoublePoint(-1*self._acceleration,0))
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
147 EWkey = True
462
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
148 if self._scene.keystate['RIGHT']:
474
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
149 self.applyThrust(fife.DoublePoint(self._acceleration,0))
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
150 EWkey = True
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
151
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
152 if NSkey and not EWkey:
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
153 if self._velocity.x != 0:
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
154 vel = self._acceleration * cmp(self._velocity.x, 0) * -1
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
155 self.applyThrust(fife.DoublePoint(vel, 0))
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
156 elif EWkey and not NSkey:
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
157 if self._velocity.y != 0:
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
158 vel = self._acceleration * cmp(self._velocity.y, 0) * -1
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
159 self.applyThrust(fife.DoublePoint(0, vel))
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
160 elif not NSkey and not EWkey:
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
161 self.applyBrake(self._acceleration)
453
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 451
diff changeset
162
462
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
163 #fire the currently selected gun
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
164 if self._scene.keystate['SPACE']:
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
165 self.fire(fife.DoublePoint(1,0))
446
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
166
474
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
167 if self._dead and self._velocity.length() > 0:
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
168 self.applyBrake(self._acceleration)
446
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
169
454
bd7e8f708adf Time is now managed by the Scene object.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 453
diff changeset
170 super(Player, self).update()
446
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
171
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
172 #set up the players camera bounds
494
e241d7553496 Fixing the epydoc markup. Did some more commenting.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 492
diff changeset
173 #@todo: grab screen resolution from somewhere
453
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 451
diff changeset
174 topleft = self._scene.camera.toMapCoordinates(fife.ScreenPoint(0,0))
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 451
diff changeset
175 bottomright = self._scene.camera.toMapCoordinates(fife.ScreenPoint(1024,768))
446
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
176
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
177 camrect = Rect(topleft.x, topleft.y, bottomright.x - topleft.x, bottomright.y - topleft.y)
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
178
447
64676ea55472 Added the ability to set the scale of the object layer. Tweaked the player controls a little bit. A little more work needs to be done to keep the player within the bounds of the camera.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 446
diff changeset
179 #add the padding to the edge
451
f463ab431cc0 Movement shouldn't be dependent on framerate anymore.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 450
diff changeset
180 camrect.x += self._boundingBox.w
f463ab431cc0 Movement shouldn't be dependent on framerate anymore.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 450
diff changeset
181 camrect.y += self._boundingBox.h
f463ab431cc0 Movement shouldn't be dependent on framerate anymore.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 450
diff changeset
182 camrect.w -= 2*self._boundingBox.w
f463ab431cc0 Movement shouldn't be dependent on framerate anymore.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 450
diff changeset
183 camrect.h -= 2*self._boundingBox.h
446
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
184
460
5e1d6e40d19d Fixed bounding boxes.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 459
diff changeset
185 pos = oldpos.getExactLayerCoordinates()
5e1d6e40d19d Fixed bounding boxes.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 459
diff changeset
186
451
f463ab431cc0 Movement shouldn't be dependent on framerate anymore.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 450
diff changeset
187 if not self._boundingBox.intersects(camrect):
f463ab431cc0 Movement shouldn't be dependent on framerate anymore.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 450
diff changeset
188 if (self._boundingBox.x + self._boundingBox.w) < camrect.x:
479
ab28994820dd Added some powerups including a spread weapon and an extra life.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 477
diff changeset
189 if self._velocity.x < 0:
ab28994820dd Added some powerups including a spread weapon and an extra life.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 477
diff changeset
190 self._velocity.x = 0
460
5e1d6e40d19d Fixed bounding boxes.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 459
diff changeset
191 pos.x += (camrect.x - (self._boundingBox.x + self._boundingBox.w))/self._xscale + 0.03
479
ab28994820dd Added some powerups including a spread weapon and an extra life.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 477
diff changeset
192
ab28994820dd Added some powerups including a spread weapon and an extra life.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 477
diff changeset
193 if not ((self._boundingBox.y + self._boundingBox.h) < camrect.y) and not (self._boundingBox.y > (camrect.y + camrect.h)):
ab28994820dd Added some powerups including a spread weapon and an extra life.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 477
diff changeset
194 pos.y += self._velocity.y * (self._scene.timedelta/1000.0)/self._yscale
ab28994820dd Added some powerups including a spread weapon and an extra life.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 477
diff changeset
195
460
5e1d6e40d19d Fixed bounding boxes.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 459
diff changeset
196 oldpos.setExactLayerCoordinates(pos)
479
ab28994820dd Added some powerups including a spread weapon and an extra life.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 477
diff changeset
197 if self._boundingBox.x > ( camrect.x + camrect.w ):
ab28994820dd Added some powerups including a spread weapon and an extra life.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 477
diff changeset
198 self._velocity.x = 0
ab28994820dd Added some powerups including a spread weapon and an extra life.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 477
diff changeset
199
ab28994820dd Added some powerups including a spread weapon and an extra life.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 477
diff changeset
200 if (self._boundingBox.y + self._boundingBox.h) < camrect.y:
ab28994820dd Added some powerups including a spread weapon and an extra life.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 477
diff changeset
201 if self._velocity.y < 0:
ab28994820dd Added some powerups including a spread weapon and an extra life.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 477
diff changeset
202 self._velocity.y = 0
460
5e1d6e40d19d Fixed bounding boxes.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 459
diff changeset
203 pos.x += self._velocity.x * (self._scene.timedelta/1000.0)/self._xscale
479
ab28994820dd Added some powerups including a spread weapon and an extra life.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 477
diff changeset
204 oldpos.setExactLayerCoordinates(pos)
ab28994820dd Added some powerups including a spread weapon and an extra life.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 477
diff changeset
205 if self._boundingBox.y > (camrect.y + camrect.h):
ab28994820dd Added some powerups including a spread weapon and an extra life.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 477
diff changeset
206 if self._velocity.y > 0:
ab28994820dd Added some powerups including a spread weapon and an extra life.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 477
diff changeset
207 self._velocity.y = 0
ab28994820dd Added some powerups including a spread weapon and an extra life.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 477
diff changeset
208 pos.x += self._velocity.x * (self._scene.timedelta/1000.0)/self._xscale
460
5e1d6e40d19d Fixed bounding boxes.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 459
diff changeset
209 oldpos.setExactLayerCoordinates(pos)
446
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
210
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
211 self.location = oldpos
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
212
455
e686b82d93d0 Added the ability to pause the game.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 454
diff changeset
213 def _getLives(self):
e686b82d93d0 Added the ability to pause the game.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 454
diff changeset
214 return self._lives
459
302a69c5141d Player death is now handled a bit nicer.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 457
diff changeset
215
479
ab28994820dd Added some powerups including a spread weapon and an extra life.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 477
diff changeset
216 def _setLives(self, lives):
ab28994820dd Added some powerups including a spread weapon and an extra life.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 477
diff changeset
217 self._lives = lives
ab28994820dd Added some powerups including a spread weapon and an extra life.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 477
diff changeset
218
459
302a69c5141d Player death is now handled a bit nicer.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 457
diff changeset
219 def _getInvulnerable(self):
302a69c5141d Player death is now handled a bit nicer.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 457
diff changeset
220 return self._invulnerable
450
ba6817013343 Added score keeping ability.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 449
diff changeset
221
462
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
222 def _setInvulnerable(self, inv):
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
223 self._invulnerable = inv
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
224
455
e686b82d93d0 Added the ability to pause the game.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 454
diff changeset
225 score = property(_getScore)
479
ab28994820dd Added some powerups including a spread weapon and an extra life.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 477
diff changeset
226 lives = property(_getLives, _setLives)
491
c4168eb47a44 Adding some comments to the shooter demo. Updated the copyright date and FIFE URL as well.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 485
diff changeset
227 invulnerable = property(_getInvulnerable, _setInvulnerable)