annotate demos/shooter/scripts/ships/player.py @ 697:ecaa4d98f05f tip

Abstracted the GUI code and refactored the GUIChan-specific code into its own module. * Most of the GUIChan code has been refactored into its own gui/guichan module. However, references to the GuiFont class still persist in the Engine and GuiManager code and these will need further refactoring. * GuiManager is now an abstract base class which specific implementations (e.g. GUIChan) should subclass. * The GUIChan GUI code is now a concrete implementation of GuiManager, most of which is in the new GuiChanGuiManager class. * The GUI code in the Console class has been refactored out of the Console and into the GUIChan module as its own GuiChanConsoleWidget class. The rest of the Console class related to executing commands was left largely unchanged. * Existing client code may need to downcast the GuiManager pointer received from FIFE::Engine::getGuiManager() to GuiChanGuiManager, since not all functionality is represented in the GuiManager abstract base class. Python client code can use the new GuiChanGuiManager.castTo static method for this purpose.
author M. George Hansen <technopolitica@gmail.com>
date Sat, 18 Jun 2011 00:28:40 -1000
parents 291ba2946c73
children
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 *
454
bd7e8f708adf Time is now managed by the Scene object.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 453
diff changeset
27 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
28
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
29
462
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
30 class PlayerActionListener(ShipActionListener):
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
31 def __init__(self, ship):
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
32 super(PlayerActionListener, self).__init__(ship)
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
33
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
34 def onInstanceActionFinished(self, instance, action):
474
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
35 super(PlayerActionListener, self).onInstanceActionFinished(instance, action)
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
36
462
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
37 if action.getId() == 'explode':
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
38 self._ship.respawn()
474
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
39
462
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
40
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
41
446
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
42 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
43 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
44 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
45
477
6b33d80b468b Projectiles are now part of the scene.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 475
diff changeset
46 self._type = SHTR_PLAYER
6b33d80b468b Projectiles are now part of the scene.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 475
diff changeset
47
450
ba6817013343 Added score keeping ability.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 449
diff changeset
48 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
49 self._maxvelocity = 1.5
474
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
50 self._acceleration = 1.0
454
bd7e8f708adf Time is now managed by the Scene object.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 453
diff changeset
51
460
5e1d6e40d19d Fixed bounding boxes.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 459
diff changeset
52 self.width = 0.22
5e1d6e40d19d Fixed bounding boxes.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 459
diff changeset
53 self.height = 0.12
5e1d6e40d19d Fixed bounding boxes.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 459
diff changeset
54
474
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
55 self._actionlistener = PlayerActionListener(self)
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
56
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
57 self._lives = 3
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
58 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
59
474
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
60
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
61 def init(self):
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
62 self._hitpoints = 2
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
63 self._dead = False
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
64 self._invulnerable = False
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
65
454
bd7e8f708adf Time is now managed by the Scene object.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 453
diff changeset
66 #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
67 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
68
450
ba6817013343 Added score keeping ability.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 449
diff changeset
69 def _getScore(self):
ba6817013343 Added score keeping ability.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 449
diff changeset
70 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
71
462
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
72 def respawn(self):
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
73 if self._lives >= 0:
474
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
74 self.init()
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
75 self.setInvulnerable(1000)
462
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
76
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
77 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
78 location = self.location
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
79 playerpos = location.getExactLayerCoordinates()
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
80
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
81 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
82 playerpos.y = campos.y
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
83 location.setExactLayerCoordinates(playerpos)
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
84 self.location = location
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
85
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
86 else:
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
87 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
88 self._scene.gameOver()
462
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
89
474
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
90 def setInvulnerable(self, milliseconds):
494
e241d7553496 Fixing the epydoc markup. Did some more commenting.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 492
diff changeset
91 """
e241d7553496 Fixing the epydoc markup. Did some more commenting.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 492
diff changeset
92 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
93 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
94
e241d7553496 Fixing the epydoc markup. Did some more commenting.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 492
diff changeset
95 @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
96
e241d7553496 Fixing the epydoc markup. Did some more commenting.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 492
diff changeset
97 """
479
ab28994820dd Added some powerups including a spread weapon and an extra life.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 477
diff changeset
98 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
99
ab28994820dd Added some powerups including a spread weapon and an extra life.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 477
diff changeset
100 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
101 return
474
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
102
462
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
103 self._invulnerable = True
474
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
104 self.flash(number)
462
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
105
450
ba6817013343 Added score keeping ability.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 449
diff changeset
106 def applyScore(self, sc):
ba6817013343 Added score keeping ability.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 449
diff changeset
107 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
108
474
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
109 def applyHit(self, hp):
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
110 if not self._invulnerable and not self._dead:
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
111 super(Player, self).applyHit(hp)
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
112 if not self._dead:
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
113 self.flash(1)
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
114
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
115 def destroy(self):
462
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
116 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
117 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
118 self._explodclip.play()
462
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
119 self._dead = True
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
120 self._invulnerable = True
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
121 self._lives -= 1
459
302a69c5141d Player death is now handled a bit nicer.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 457
diff changeset
122
471
7a79dc2a0592 Adding the credits window (still needs to be written)
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 462
diff changeset
123 def fire(self, direction):
7a79dc2a0592 Adding the credits window (still needs to be written)
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 462
diff changeset
124 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
125
454
bd7e8f708adf Time is now managed by the Scene object.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 453
diff changeset
126 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
127
474
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
128 NSkey = False
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
129 EWkey = False
459
302a69c5141d Player death is now handled a bit nicer.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 457
diff changeset
130
302a69c5141d Player death is now handled a bit nicer.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 457
diff changeset
131 #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
132 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
133 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
134
446
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
135 oldpos = self.location
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
136
462
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
137 if not self._dead:
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
138 if self._scene.keystate['UP']:
474
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
139 self.applyThrust(fife.DoublePoint(0,-1*self._acceleration))
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
140 NSkey = True
462
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
141 if self._scene.keystate['DOWN']:
474
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
142 self.applyThrust(fife.DoublePoint(0,self._acceleration))
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
143 NSkey = True
462
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
144 if self._scene.keystate['LEFT']:
474
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
145 self.applyThrust(fife.DoublePoint(-1*self._acceleration,0))
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
146 EWkey = True
462
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
147 if self._scene.keystate['RIGHT']:
474
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
148 self.applyThrust(fife.DoublePoint(self._acceleration,0))
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
149 EWkey = True
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
150
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
151 if NSkey and not EWkey:
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
152 if self._velocity.x != 0:
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
153 vel = self._acceleration * cmp(self._velocity.x, 0) * -1
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
154 self.applyThrust(fife.DoublePoint(vel, 0))
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
155 elif EWkey and not NSkey:
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
156 if self._velocity.y != 0:
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
157 vel = self._acceleration * cmp(self._velocity.y, 0) * -1
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
158 self.applyThrust(fife.DoublePoint(0, vel))
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
159 elif not NSkey and not EWkey:
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
160 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
161
462
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
162 #fire the currently selected gun
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
163 if self._scene.keystate['SPACE']:
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
164 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
165
474
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
166 if self._dead and self._velocity.length() > 0:
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
167 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
168
454
bd7e8f708adf Time is now managed by the Scene object.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 453
diff changeset
169 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
170
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
171 #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
172 #@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
173 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
174 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
175
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
176 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
177
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
178 #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
179 camrect.x += self._boundingBox.w
f463ab431cc0 Movement shouldn't be dependent on framerate anymore.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 450
diff changeset
180 camrect.y += self._boundingBox.h
f463ab431cc0 Movement shouldn't be dependent on framerate anymore.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 450
diff changeset
181 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
182 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
183
460
5e1d6e40d19d Fixed bounding boxes.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 459
diff changeset
184 pos = oldpos.getExactLayerCoordinates()
5e1d6e40d19d Fixed bounding boxes.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 459
diff changeset
185
451
f463ab431cc0 Movement shouldn't be dependent on framerate anymore.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 450
diff changeset
186 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
187 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
188 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
189 self._velocity.x = 0
460
5e1d6e40d19d Fixed bounding boxes.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 459
diff changeset
190 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
191
ab28994820dd Added some powerups including a spread weapon and an extra life.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 477
diff changeset
192 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
193 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
194
460
5e1d6e40d19d Fixed bounding boxes.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 459
diff changeset
195 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
196 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
197 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
198
ab28994820dd Added some powerups including a spread weapon and an extra life.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 477
diff changeset
199 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
200 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
201 self._velocity.y = 0
460
5e1d6e40d19d Fixed bounding boxes.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 459
diff changeset
202 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
203 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
204 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
205 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
206 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 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
208 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
209
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
210 self.location = oldpos
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
211
455
e686b82d93d0 Added the ability to pause the game.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 454
diff changeset
212 def _getLives(self):
e686b82d93d0 Added the ability to pause the game.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 454
diff changeset
213 return self._lives
459
302a69c5141d Player death is now handled a bit nicer.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 457
diff changeset
214
479
ab28994820dd Added some powerups including a spread weapon and an extra life.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 477
diff changeset
215 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
216 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
217
459
302a69c5141d Player death is now handled a bit nicer.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 457
diff changeset
218 def _getInvulnerable(self):
302a69c5141d Player death is now handled a bit nicer.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 457
diff changeset
219 return self._invulnerable
450
ba6817013343 Added score keeping ability.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 449
diff changeset
220
462
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
221 def _setInvulnerable(self, inv):
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
222 self._invulnerable = inv
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
223
455
e686b82d93d0 Added the ability to pause the game.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 454
diff changeset
224 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
225 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
226 invulnerable = property(_getInvulnerable, _setInvulnerable)