annotate demos/shooter/scripts/ships/player.py @ 474:10aff123c62c

Some minor code cleanups. Changed the player ship controls a little to feel more natural.
author prock@33b003aa-7bff-0310-803a-e67f0ece8222
date Wed, 21 Apr 2010 16:49:59 +0000
parents 7a79dc2a0592
children afde89c1d50b
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 # ####################################################################
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
4 # Copyright (C) 2005-2009 by the FIFE team
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
5 # http://www.fifengine.de
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
462
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
25 from scripts.ships.shipbase import *
474
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
26 from scripts.common.helpers 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
450
ba6817013343 Added score keeping ability.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 449
diff changeset
46 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
47 self._maxvelocity = 1.5
474
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
48 self._acceleration = 1.0
454
bd7e8f708adf Time is now managed by the Scene object.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 453
diff changeset
49
460
5e1d6e40d19d Fixed bounding boxes.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 459
diff changeset
50 self.width = 0.22
5e1d6e40d19d Fixed bounding boxes.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 459
diff changeset
51 self.height = 0.12
5e1d6e40d19d Fixed bounding boxes.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 459
diff changeset
52
474
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
53 self._isplayer = True
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
54
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()
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
59
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
60 def init(self):
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
61 self._hitpoints = 2
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
62 self._dead = False
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
63 self._invulnerable = False
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
64
454
bd7e8f708adf Time is now managed by the Scene object.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 453
diff changeset
65 #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
66 self.weapon = Cannon(self._scene, self, 200)
474
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
67
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)
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
88
474
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
89 def setInvulnerable(self, milliseconds):
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
90 #50 is defined in the players "flash" animation file
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
91 #2 is the number of frames in the animation
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
92 #TODO: read these values somehow from the animation
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
93 number = (milliseconds / 50) / 2
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
94
462
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
95 self._invulnerable = True
474
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
96 self.flash(number)
462
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
97
450
ba6817013343 Added score keeping ability.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 449
diff changeset
98 def applyScore(self, sc):
ba6817013343 Added score keeping ability.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 449
diff changeset
99 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
100
474
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
101 def applyHit(self, hp):
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
102 if not self._invulnerable and not self._dead:
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
103 super(Player, self).applyHit(hp)
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
104 if not self._dead:
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
105 self.flash(1)
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
106
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
107 def destroy(self):
462
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
108 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
109 self._instance.act('explode', self._instance.getFacingLocation())
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
110 self._dead = True
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
111 self._invulnerable = True
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
112 self._lives -= 1
459
302a69c5141d Player death is now handled a bit nicer.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 457
diff changeset
113
471
7a79dc2a0592 Adding the credits window (still needs to be written)
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 462
diff changeset
114 def fire(self, direction):
7a79dc2a0592 Adding the credits window (still needs to be written)
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 462
diff changeset
115 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
116
454
bd7e8f708adf Time is now managed by the Scene object.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 453
diff changeset
117 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
118
474
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
119 NSkey = False
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
120 EWkey = False
459
302a69c5141d Player death is now handled a bit nicer.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 457
diff changeset
121
302a69c5141d Player death is now handled a bit nicer.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 457
diff changeset
122 #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
123 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
124 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
125
446
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
126 oldpos = self.location
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
127
462
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
128 if not self._dead:
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
129 if self._scene.keystate['UP']:
474
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
130 self.applyThrust(fife.DoublePoint(0,-1*self._acceleration))
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
131 NSkey = True
462
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
132 if self._scene.keystate['DOWN']:
474
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
133 self.applyThrust(fife.DoublePoint(0,self._acceleration))
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
134 NSkey = True
462
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
135 if self._scene.keystate['LEFT']:
474
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
136 self.applyThrust(fife.DoublePoint(-1*self._acceleration,0))
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
137 EWkey = True
462
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
138 if self._scene.keystate['RIGHT']:
474
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
139 self.applyThrust(fife.DoublePoint(self._acceleration,0))
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
140 EWkey = True
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
141
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
142 if NSkey and not EWkey:
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
143 if self._velocity.x != 0:
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
144 vel = self._acceleration * cmp(self._velocity.x, 0) * -1
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
145 self.applyThrust(fife.DoublePoint(vel, 0))
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
146 elif EWkey and not NSkey:
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
147 if self._velocity.y != 0:
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
148 vel = self._acceleration * cmp(self._velocity.y, 0) * -1
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
149 self.applyThrust(fife.DoublePoint(0, vel))
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
150 elif not NSkey and not EWkey:
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
151 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
152
462
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
153 #fire the currently selected gun
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
154 if self._scene.keystate['SPACE']:
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
155 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
156
474
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
157 if self._dead and self._velocity.length() > 0:
10aff123c62c Some minor code cleanups.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 471
diff changeset
158 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
159
454
bd7e8f708adf Time is now managed by the Scene object.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 453
diff changeset
160 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
161
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
162 #set up the players camera bounds
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
163 #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
164 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
165 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
166
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
167 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
168
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
169 #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
170 camrect.x += self._boundingBox.w
f463ab431cc0 Movement shouldn't be dependent on framerate anymore.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 450
diff changeset
171 camrect.y += self._boundingBox.h
f463ab431cc0 Movement shouldn't be dependent on framerate anymore.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 450
diff changeset
172 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
173 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
174
460
5e1d6e40d19d Fixed bounding boxes.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 459
diff changeset
175 pos = oldpos.getExactLayerCoordinates()
5e1d6e40d19d Fixed bounding boxes.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 459
diff changeset
176
451
f463ab431cc0 Movement shouldn't be dependent on framerate anymore.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 450
diff changeset
177 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
178 if (self._boundingBox.x + self._boundingBox.w) < camrect.x:
460
5e1d6e40d19d Fixed bounding boxes.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 459
diff changeset
179 self._velocity.x = 0
5e1d6e40d19d Fixed bounding boxes.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 459
diff changeset
180 pos.x += (camrect.x - (self._boundingBox.x + self._boundingBox.w))/self._xscale + 0.03
5e1d6e40d19d Fixed bounding boxes.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 459
diff changeset
181 pos.y += self._velocity.y * (self._scene.timedelta/1000.0)/self._yscale
5e1d6e40d19d Fixed bounding boxes.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 459
diff changeset
182 oldpos.setExactLayerCoordinates(pos)
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
183
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
184 # elif (bbox.y + bbox.h) < camrect.y or (bbox.y - bbox.h) > camrect.y:
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
185 # pos.x += self._velocity.x * (timedelta/1000.0)
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
186 # oldpos.setExactLayerCoordinates(pos)
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
187 # self._velocity.y = 0
460
5e1d6e40d19d Fixed bounding boxes.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 459
diff changeset
188 elif (self._boundingBox.y + self._boundingBox.h) < camrect.y or self._boundingBox.y > (camrect.y + camrect.h):
5e1d6e40d19d Fixed bounding boxes.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 459
diff changeset
189 pos.x += self._velocity.x * (self._scene.timedelta/1000.0)/self._xscale
5e1d6e40d19d Fixed bounding boxes.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 459
diff changeset
190 self._velocity.y = 0
5e1d6e40d19d Fixed bounding boxes.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 459
diff changeset
191 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
192 else:
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
193 self._velocity.x = 0
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
194 self._velocity.y = 0
446
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
195
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
196 self.location = oldpos
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
197
455
e686b82d93d0 Added the ability to pause the game.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 454
diff changeset
198 def _getLives(self):
e686b82d93d0 Added the ability to pause the game.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 454
diff changeset
199 return self._lives
459
302a69c5141d Player death is now handled a bit nicer.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 457
diff changeset
200
302a69c5141d Player death is now handled a bit nicer.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 457
diff changeset
201 def _getInvulnerable(self):
302a69c5141d Player death is now handled a bit nicer.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 457
diff changeset
202 return self._invulnerable
450
ba6817013343 Added score keeping ability.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 449
diff changeset
203
462
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
204 def _setInvulnerable(self, inv):
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
205 self._invulnerable = inv
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
206
455
e686b82d93d0 Added the ability to pause the game.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 454
diff changeset
207 score = property(_getScore)
459
302a69c5141d Player death is now handled a bit nicer.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 457
diff changeset
208 lives = property(_getLives)
462
c4f745a566d6 Added player ship animations including flash and explode.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 461
diff changeset
209 invulnerable = property(_getInvulnerable, _setInvulnerable)