changeset 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 b78020d31186
children afde89c1d50b
files demos/shooter/gui/highscores.xml demos/shooter/scripts/scene.py demos/shooter/scripts/ships/enemies.py demos/shooter/scripts/ships/player.py demos/shooter/scripts/ships/shipbase.py
diffstat 5 files changed, 87 insertions(+), 63 deletions(-) [+]
line wrap: on
line diff
--- a/demos/shooter/gui/highscores.xml	Tue Apr 20 20:36:58 2010 +0000
+++ b/demos/shooter/gui/highscores.xml	Wed Apr 21 16:49:59 2010 +0000
@@ -8,81 +8,81 @@
 		<HBox>
 			<Label name="1" border_size="5" text="1" min_size="20,0">
 			</Label>
-			<Label name="1name" border_size="5" text="WAY" min_size="50,0">
+			<Label name="1name" border_size="5" text="aaa" min_size="50,0">
 			</Label>
-			<Label name="1score" border_size="5" text="6900" min_size="150,0">
+			<Label name="1score" border_size="5" text="1000" min_size="150,0">
 			</Label>
 		</HBox>
 		<HBox>
 			<Label name="2" border_size="5" text="2" min_size="20,0">
 			</Label>
-			<Label name="2name" border_size="5" text="way" min_size="50,0">
+			<Label name="2name" border_size="5" text="bbb" min_size="50,0">
 			</Label>
-			<Label name="2score" border_size="5" text="5650" min_size="150,0">
+			<Label name="2score" border_size="5" text="1000" min_size="150,0">
 			</Label>
 		</HBox>
 		<HBox>
 			<Label name="3" border_size="5" text="3" min_size="20,0">
 			</Label>
-			<Label name="3name" border_size="5" text="dfd" min_size="50,0">
+			<Label name="3name" border_size="5" text="ccc" min_size="50,0">
 			</Label>
-			<Label name="3score" border_size="5" text="4750" min_size="150,0">
+			<Label name="3score" border_size="5" text="1000" min_size="150,0">
 			</Label>
 		</HBox>
 		<HBox>
 			<Label name="4" border_size="5" text="4" min_size="20,0">
 			</Label>
-			<Label name="4name" border_size="5" text="d" min_size="50,0">
+			<Label name="4name" border_size="5" text="ddd" min_size="50,0">
 			</Label>
-			<Label name="4score" border_size="5" text="4450" min_size="150,0">
+			<Label name="4score" border_size="5" text="1000" min_size="150,0">
 			</Label>
 		</HBox>
 		<HBox>
 			<Label name="5" border_size="5" text="5" min_size="20,0">
 			</Label>
-			<Label name="5name" border_size="5" text="t" min_size="50,0">
+			<Label name="5name" border_size="5" text="eee" min_size="50,0">
 			</Label>
-			<Label name="5score" border_size="5" text="4150" min_size="150,0">
+			<Label name="5score" border_size="5" text="1000" min_size="150,0">
 			</Label>
 		</HBox>
 		<HBox>
 			<Label name="6" border_size="5" text="6" min_size="20,0">
 			</Label>
-			<Label name="6name" border_size="5" text="df" min_size="50,0">
+			<Label name="6name" border_size="5" text="fff" min_size="50,0">
 			</Label>
-			<Label name="6score" border_size="5" text="3650" min_size="150,0">
+			<Label name="6score" border_size="5" text="1000" min_size="150,0">
 			</Label>
 		</HBox>
 		<HBox>
 			<Label name="7" border_size="5" text="7" min_size="20,0">
 			</Label>
-			<Label name="7name" border_size="5" text="gs" min_size="50,0">
+			<Label name="7name" border_size="5" text="ggg" min_size="50,0">
 			</Label>
-			<Label name="7score" border_size="5" text="2600" min_size="150,0">
+			<Label name="7score" border_size="5" text="1000" min_size="150,0">
 			</Label>
 		</HBox>
 		<HBox>
 			<Label name="8" border_size="5" text="8" min_size="20,0">
 			</Label>
-			<Label name="8name" border_size="5" text="boo" min_size="50,0">
+			<Label name="8name" border_size="5" text="hhh" min_size="50,0">
 			</Label>
-			<Label name="8score" border_size="5" text="2150" min_size="150,0">
+			<Label name="8score" border_size="5" text="1000" min_size="150,0">
 			</Label>
 		</HBox>
 		<HBox>
 			<Label name="9" border_size="5" text="9" min_size="20,0">
 			</Label>
-			<Label name="9name" border_size="5" text="way" min_size="50,0">
+			<Label name="9name" border_size="5" text="iii" min_size="50,0">
 			</Label>
-			<Label name="9score" border_size="5" text="1850" min_size="150,0">
+			<Label name="9score" border_size="5" text="1000" min_size="150,0">
 			</Label>
 		</HBox>
 		<HBox>
 			<Label name="10" border_size="5" text="10" min_size="20,0">
 			</Label>
-			<Label name="10name" border_size="5" text="df" min_size="50,0">
+			<Label name="10name" border_size="5" text="jjj" min_size="50,0">
 			</Label>
-			<Label name="10score" border_size="5" text="1500" min_size="150,0">
+			<Label name="10score" border_size="5" text="1000" min_size="150,0">
 			</Label>
 		</HBox>
 		<Button name="close" border_size="0" text="Close" min_size="100,0">
--- a/demos/shooter/scripts/scene.py	Tue Apr 20 20:36:58 2010 +0000
+++ b/demos/shooter/scripts/scene.py	Wed Apr 21 16:49:59 2010 +0000
@@ -163,14 +163,10 @@
 		self._paused = False
 		
 	def playerHit(self):
-		self._player.destroy()
+		self._player.applyHit(1)
 		if self._player.lives <= -1:
 			self._gameover = True
 			self._world.gameOver()
-			#self.removeAllProjectiles()
-			return
-
-		#self._player.setInvulnerable(2)
 		
 	def endLevel(self):
 		self._world.endLevel()
--- a/demos/shooter/scripts/ships/enemies.py	Tue Apr 20 20:36:58 2010 +0000
+++ b/demos/shooter/scripts/ships/enemies.py	Wed Apr 21 16:49:59 2010 +0000
@@ -32,21 +32,18 @@
 		super(EnemyActionListener, self).__init__(ship)
 
 	def onInstanceActionFinished(self, instance, action):
-		if action.getId() == 'explode':
-			self._ship.removeFromScene()
-			
 		super(EnemyActionListener, self).onInstanceActionFinished(instance, action)
 
+
 class BossActionListener(ShipActionListener):
 	def __init__(self, ship):
 		super(BossActionListener, self).__init__(ship)
 
 	def onInstanceActionFinished(self, instance, action):
+		super(BossActionListener, self).onInstanceActionFinished(instance, action)
+
 		if action.getId() == 'explode':
-			self._ship.removeFromScene()
 			self._ship.endLevel()
-		
-		super(BossActionListener, self).onInstanceActionFinished(instance, action)
 
 				
 class Saucer1(Ship):
--- a/demos/shooter/scripts/ships/player.py	Tue Apr 20 20:36:58 2010 +0000
+++ b/demos/shooter/scripts/ships/player.py	Wed Apr 21 16:49:59 2010 +0000
@@ -23,7 +23,7 @@
 
 from fife import fife
 from scripts.ships.shipbase import *
-from scripts.common.helpers import Rect
+from scripts.common.helpers import *
 from scripts.weapons import *
 
 
@@ -32,10 +32,11 @@
 		super(PlayerActionListener, self).__init__(ship)
 
 	def onInstanceActionFinished(self, instance, action):
+		super(PlayerActionListener, self).onInstanceActionFinished(instance, action)
+		
 		if action.getId() == 'explode':
 			self._ship.respawn()
-		
-		super(PlayerActionListener, self).onInstanceActionFinished(instance, action)
+
 		
 		
 class Player(Ship):
@@ -44,31 +45,34 @@
 
 		self._score = 0
 		self._maxvelocity = 1.5
+		self._acceleration = 1.0
 		
 		self.width = 0.22
 		self.height = 0.12		
 		
+		self._isplayer = True
+		
+		self._actionlistener = PlayerActionListener(self)
+
+		self._lives = 3		
+		self.init()
+		
+	def init(self):
+		self._hitpoints = 2
+		self._dead = False
+		self._invulnerable = False
+
 		#give player the default weapon (the cannon)
 		self.weapon = Cannon(self._scene, self, 200)
-		
-		self._lives = 3
-		self._invulnerable = False
-		
-		self._isplayer = True
-		self._dead = False
-		
-		self._actionlistener = PlayerActionListener(self)
-		
-	def init(self):
-		self._lives = 3
+
 	
 	def _getScore(self):
 		return self._score
 		
 	def respawn(self):
 		if self._lives >= 0:
-			self._dead = False
-			self.setInvulnerable(20)
+			self.init()
+			self.setInvulnerable(1000)
 
 			campos = self._scene.camera.getLocation().getExactLayerCoordinates()
 			location = self.location
@@ -82,13 +86,24 @@
 		else:
 			self._instance.get2dGfxVisual().setVisible(False)
 		
-	def setInvulnerable(self, seconds):
+	def setInvulnerable(self, milliseconds):
+		#50 is defined in the players "flash" animation file
+		#2 is the number of frames in the animation
+		#TODO: read these values somehow from the animation
+		number = (milliseconds / 50) / 2
+		
 		self._invulnerable = True
-		self.flash(seconds)
+		self.flash(number)
 		
 	def applyScore(self, sc):
 		self._score += sc
 		
+	def applyHit(self, hp):
+		if not self._invulnerable and not self._dead:
+			super(Player, self).applyHit(hp)
+			if not self._dead:
+				self.flash(1)
+		
 	def destroy(self):
 		if not self._invulnerable and not self._dead:
 			self._instance.act('explode', self._instance.getFacingLocation())
@@ -101,7 +116,8 @@
 
 	def update(self):
 	
-		key = False
+		NSkey = False
+		EWkey = False
 		
 		#player is no longer invulnerable
 		if not self._flashing and self._invulnerable and not self._dead:
@@ -111,24 +127,35 @@
 		
 		if not self._dead:
 			if self._scene.keystate['UP']:
-				self.applyThrust(fife.DoublePoint(0,-1.5))
-				key = True
+				self.applyThrust(fife.DoublePoint(0,-1*self._acceleration))
+				NSkey = True
 			if self._scene.keystate['DOWN']:
-				self.applyThrust(fife.DoublePoint(0,1.5))
-				key = True
+				self.applyThrust(fife.DoublePoint(0,self._acceleration))
+				NSkey = True
 			if self._scene.keystate['LEFT']:
-				self.applyThrust(fife.DoublePoint(-1.5,0))
-				key = True
+				self.applyThrust(fife.DoublePoint(-1*self._acceleration,0))
+				EWkey = True
 			if self._scene.keystate['RIGHT']:
-				self.applyThrust(fife.DoublePoint(1.5,0))
-				key = True
+				self.applyThrust(fife.DoublePoint(self._acceleration,0))
+				EWkey = True
+		
+			if NSkey and not EWkey:
+				if self._velocity.x != 0:
+					vel = self._acceleration * cmp(self._velocity.x, 0) * -1
+					self.applyThrust(fife.DoublePoint(vel, 0))
+			elif EWkey and not NSkey:
+				if self._velocity.y != 0:
+					vel = self._acceleration * cmp(self._velocity.y, 0) * -1
+					self.applyThrust(fife.DoublePoint(0, vel))
+			elif not NSkey and not EWkey:
+				self.applyBrake(self._acceleration)
 		
 			#fire the currently selected gun
 			if self._scene.keystate['SPACE']:
 				self.fire(fife.DoublePoint(1,0))
 			
-		if not key and self._velocity.length() > 0:
-			self.applyBrake(1.5)
+		if self._dead and self._velocity.length() > 0:
+			self.applyBrake(self._acceleration)
 		
 		super(Player, self).update()
 		
--- a/demos/shooter/scripts/ships/shipbase.py	Tue Apr 20 20:36:58 2010 +0000
+++ b/demos/shooter/scripts/ships/shipbase.py	Wed Apr 21 16:49:59 2010 +0000
@@ -43,6 +43,9 @@
 			else:	
 				self._ship._flashing = False
 				self._ship._flashnumber = 0
+		
+		if action.getId() == 'explode' and not self._ship.isplayer:
+			self._ship.removeFromScene()
 
 class Ship(SpaceObject):
 	def __init__(self, scene, name, findInstance=True):
@@ -65,9 +68,10 @@
 		return self._weapon
 	
 	def flash(self, number):
-		self._instance.act('flash', self._instance.getFacingLocation())
-		self._flashnumber = number
-		self._flashing = True	
+		if self._running:
+			self._instance.act('flash', self._instance.getFacingLocation())
+			self._flashnumber = number
+			self._flashing = True	
 	
 	def fire(self, direction):
 		if self._weapon and self._hitpoints > 0: