changeset 454:bd7e8f708adf

Time is now managed by the Scene object. Enemy projectiles are now a bit slower.
author prock@33b003aa-7bff-0310-803a-e67f0ece8222
date Tue, 06 Apr 2010 21:25:10 +0000
parents cf53848fb187
children e686b82d93d0
files demos/shooter/scripts/common/baseobject.py demos/shooter/scripts/scene.py demos/shooter/scripts/ships/enemies.py demos/shooter/scripts/ships/player.py demos/shooter/scripts/ships/shipbase.py demos/shooter/scripts/weapons.py demos/shooter/scripts/world.py
diffstat 7 files changed, 90 insertions(+), 81 deletions(-) [+]
line wrap: on
line diff
--- a/demos/shooter/scripts/common/baseobject.py	Tue Apr 06 19:12:41 2010 +0000
+++ b/demos/shooter/scripts/common/baseobject.py	Tue Apr 06 21:25:10 2010 +0000
@@ -35,7 +35,6 @@
 		self._yscale = self._layer.getCellGrid().getYScale()		
 		self._velocity = fife.DoublePoint(0,0)
 		self._maxvelocity = 1.25
-		self._timedelta = 0
 		self._boundingBox = Rect(0,0,0,0)
 		self._running = False
 		self._changedPosition = False
@@ -50,15 +49,13 @@
 		if self._instance:
 			self._running = True
 
-	def update(self, timedelta):
+	def update(self):
 		if self._running:
-			self._timedelta = timedelta
-		
 			shiploc = self.location
 			exactloc = shiploc.getExactLayerCoordinates()
 		
-			exactloc.x += self._velocity.x * (timedelta/1000.0)/self._xscale
-			exactloc.y += self._velocity.y * (timedelta/1000.0)/self._yscale
+			exactloc.x += self._velocity.x * (self._scene.timedelta/1000.0)/self._xscale
+			exactloc.y += self._velocity.y * (self._scene.timedelta/1000.0)/self._yscale
 		
 			self._boundingBox.x = (exactloc.x - self._boundingBox.w/2) * self._xscale
 			self._boundingBox.y = (exactloc.y - self._boundingBox.h/2) * self._yscale
@@ -79,9 +76,9 @@
 		self._running = False
 		self._layer.deleteInstance(self._instance)
 		
-	def applyThrust(self, vector, timedelta):
-		self._velocity.x += (vector.x * (timedelta/1000.0))/self._xscale
-		self._velocity.y += (vector.y * (timedelta/1000.0))/self._yscale
+	def applyThrust(self, vector):
+		self._velocity.x += (vector.x * (self._scene.timedelta/1000.0))/self._xscale
+		self._velocity.y += (vector.y * (self._scene.timedelta/1000.0))/self._yscale
 		
 		if self._velocity.length() > self._maxvelocity:
 			norm = normalize(self._velocity)
@@ -89,7 +86,7 @@
 			self._velocity.y = norm.y * self._maxvelocity
 		
 	
-	def applyBrake(self, brakingForce, timedelta):
+	def applyBrake(self, brakingForce):
 
 		if self._velocity.length() <= .01:
 			self._velocity.x = 0
@@ -111,8 +108,8 @@
 		norm.x *= brakingForce
 		norm.y *= brakingForce
 		
-		self._velocity.x += (norm.x * (timedelta/1000.0))/self._xscale
-		self._velocity.y += (norm.y * (timedelta/1000.0))/self._yscale
+		self._velocity.x += (norm.x * (self._scene.timedelta/1000.0))/self._xscale
+		self._velocity.y += (norm.y * (self._scene.timedelta/1000.0))/self._yscale
 
 	def _isRunning(self):
 		return self._running
--- a/demos/shooter/scripts/scene.py	Tue Apr 06 19:12:41 2010 +0000
+++ b/demos/shooter/scripts/scene.py	Tue Apr 06 21:25:10 2010 +0000
@@ -50,22 +50,24 @@
 		self._layer = objectLayer
 		self._nodes = list()
 		
-		self._player = Player(self, 'player')
-		self._player.width = 0.075
-		self._player.height = 0.075
-		self._player.start()
-		
+		self._player = None
 		self._projectiles = list()
-		self._lasttime = 0
 		
 		self._maxnodes = 128
 		self._xscale = 0
 		
 		self._time = 0
 		self._timedelta = 0
+		self._lasttime = 0
+		
+		self._xscale = self._layer.getCellGrid().getXScale()
+		self._yscale = self._layer.getCellGrid().getYScale()
 
 	def initScene(self, mapobj):
-		self._xscale = self._layer.getCellGrid().getXScale()
+		self._player = Player(self, 'player')
+		self._player.width = 0.075
+		self._player.height = 0.075
+		self._player.start()
 				
 		enemies = self._layer.getInstances('enemy')
 		
@@ -168,16 +170,14 @@
 
 		#update objects on the screen
 		for obj in screenlist:
-			obj.update(timedelta)
+			obj.update()
 			if obj.changedposition:
 				self.moveObjectInScene(obj)
 
-				#Testing enemy fire				
-				#prjct = obj.fire(time, fife.DoublePoint(-1,0))
-				#if prjct:
-				#	self._projectiles.append(prjct)
+			if obj != self._player:
+				#TODO: enemy should fire weapon in their update function
+				obj.fire(fife.DoublePoint(-1,0))
 			
-			if obj != self._player:
 				if obj.boundingbox.intersects(self._player.boundingbox):
 					#player touched an enemy.  Destroy player and 
 					#re-initialize scene
@@ -187,7 +187,7 @@
 		#update the list of projectiles
 		projtodelete = list()
 		for p in self._projectiles:
-			p.update(timedelta)
+			p.update()
 			#check to see if the projectile hit any object on the screen
 			for o in screenlist:
 				#cant get hit by your own bullet
@@ -196,7 +196,7 @@
 						self._player.applyScore(100)
 						p.destroy()
 						o.destroy()
-						#temporary...  the destroy functions should spawn an explosion
+						#TODO:  the destroy functions should spawn an explosion
 						#and also destroy the instance and remove itself from the scene
 						self.removeObjectFromScene(o)
 			
--- a/demos/shooter/scripts/ships/enemies.py	Tue Apr 06 19:12:41 2010 +0000
+++ b/demos/shooter/scripts/ships/enemies.py	Tue Apr 06 21:25:10 2010 +0000
@@ -24,7 +24,7 @@
 from fife import fife
 from scripts.ships.shipbase import Ship
 from scripts.common.helpers import Rect
-from scripts.weapons import Weapon
+from scripts.weapons import *
 
 
 class Saucer1(Ship):
@@ -36,13 +36,14 @@
 		self.height = 0.075
 		self.velocity.x = -0.5
 		
-		self.weapon = Weapon(self._scene, self, 1000)
+		self.weapon = Cannon(self._scene, self, 1000)
+		self.weapon.projectilevelocity = 0.4
 				
-	def update(self, timedelta):	
+	def update(self):	
 		if self._dir == 1:
-			self.applyThrust(fife.DoublePoint(0,-0.5), timedelta)
+			self.applyThrust(fife.DoublePoint(0,-0.5))
 		elif self._dir == 0:
-			self.applyThrust(fife.DoublePoint(0,0.5), timedelta)
+			self.applyThrust(fife.DoublePoint(0,0.5))
 					
 		if self._time >= 1000:
 			if self._dir == 1:
@@ -52,9 +53,9 @@
 				
 			self._time = 0
 		
-		self._time += timedelta
+		self._time += self._scene.timedelta
 		
-		super(Saucer1, self).update(timedelta)
+		super(Saucer1, self).update()
 		
 class Saucer2(Ship):
 	def __init__(self, scene, name, findInstance=True):
@@ -65,13 +66,14 @@
 		self.height = 0.2
 		self.velocity.x = -0.1
 		
-		self.weapon = Weapon(self._scene, self, 2000)
+		self.weapon = Cannon(self._scene, self, 2000)
+		self.weapon.projectilevelocity = 0.4
 				
-	def update(self, timedelta):	
+	def update(self):	
 		if self._dir == 1:
-			self.applyThrust(fife.DoublePoint(0,-0.25), timedelta)
+			self.applyThrust(fife.DoublePoint(0,-0.25))
 		elif self._dir == 0:
-			self.applyThrust(fife.DoublePoint(0,0.25), timedelta)
+			self.applyThrust(fife.DoublePoint(0,0.25))
 					
 		if self._time >= 2000:
 			if self._dir == 1:
@@ -81,6 +83,6 @@
 				
 			self._time = 0
 		
-		self._time += timedelta
+		self._time += self._scene.timedelta
 		
-		super(Saucer2, self).update(timedelta)
\ No newline at end of file
+		super(Saucer2, self).update()
\ No newline at end of file
--- a/demos/shooter/scripts/ships/player.py	Tue Apr 06 19:12:41 2010 +0000
+++ b/demos/shooter/scripts/ships/player.py	Tue Apr 06 21:25:10 2010 +0000
@@ -24,6 +24,7 @@
 from fife import fife
 from scripts.ships.shipbase import Ship
 from scripts.common.helpers import Rect
+from scripts.weapons import *
 
 
 class Player(Ship):
@@ -32,6 +33,9 @@
 
 		self._score = 0
 		self._maxvelocity = 1.5
+		
+		#give player the default weapon (the cannon)
+		self.weapon = Cannon(self._scene, self, 200)
 	
 	def _getScore(self):
 		return self._score
@@ -42,34 +46,32 @@
 	def destroy(self):
 		print "player has been destroyed!"
 	
-	def update(self, timedelta):
+	def update(self):
 		key = False
 
 		oldpos = self.location
 		
 		if self._scene.keystate['UP']:
-			self.applyThrust(fife.DoublePoint(0,-1.5), timedelta)
+			self.applyThrust(fife.DoublePoint(0,-1.5))
 			key = True
 		if self._scene.keystate['DOWN']:
-			self.applyThrust(fife.DoublePoint(0,1.5), timedelta)
+			self.applyThrust(fife.DoublePoint(0,1.5))
 			key = True
 		if self._scene.keystate['LEFT']:
-			self.applyThrust(fife.DoublePoint(-1.5,0), timedelta)
+			self.applyThrust(fife.DoublePoint(-1.5,0))
 			key = True
 		if self._scene.keystate['RIGHT']:
-			self.applyThrust(fife.DoublePoint(1.5,0), timedelta)
+			self.applyThrust(fife.DoublePoint(1.5,0))
 			key = True
 		
 		#fire the currently selected gun
 		if self._scene.keystate['SPACE']:
-			prjct = self.fire(self._scene.time, fife.DoublePoint(1,0))
-			if prjct:
-				self._scene.addProjectileToScene(prjct)
+			self.fire(fife.DoublePoint(1,0))
 			
 		if not key and self._velocity.length() > 0:
-			self.applyBrake(1.5, timedelta)
+			self.applyBrake(1.5)
 		
-		super(Player, self).update(timedelta)
+		super(Player, self).update()
 		
 		#set up the players camera bounds
 		#TODO: grab screen resolution from somewhere
@@ -78,11 +80,6 @@
 
 		camrect = Rect(topleft.x, topleft.y, bottomright.x - topleft.x, bottomright.y - topleft.y)
 	
-		#player bounding box
-		xscale = self._layer.getCellGrid().getXScale()
-		yscale = self._layer.getCellGrid().getYScale()
-
-
 		#add the padding to the edge
 		camrect.x += self._boundingBox.w
 		camrect.y += self._boundingBox.h
@@ -93,7 +90,7 @@
 			if (self._boundingBox.x + self._boundingBox.w) < camrect.x:
 				#pos.x = (bbox.x + bbox.w/2 + 0.1) / xscale
 				#oldpos.setExactLayerCoordinates(pos)
-				self._velocity.x = (timedelta * 0.01) / xscale
+				self._velocity.x = (self._scene.timedelta * 0.01) / self._xscale
 			
 #			elif (bbox.y + bbox.h) < camrect.y or (bbox.y - bbox.h) > camrect.y:
 #				pos.x += self._velocity.x * (timedelta/1000.0)
--- a/demos/shooter/scripts/ships/shipbase.py	Tue Apr 06 19:12:41 2010 +0000
+++ b/demos/shooter/scripts/ships/shipbase.py	Tue Apr 06 21:25:10 2010 +0000
@@ -31,19 +31,18 @@
 class Ship(SpaceObject):
 	def __init__(self, scene, name, findInstance=True):
 		super(Ship, self).__init__(scene, name, findInstance)
-
-		self._timedelta = 0
+		
 		self._weapon = None
 	
 	def _setWeapon(self, weapon):
 		self._weapon = weapon
 		
-	def _getWeapon(self, weapon):
+	def _getWeapon(self):
 		return self._weapon
 		
-	def fire(self, curtime, direction):
+	def fire(self, direction):
 		if self._weapon:
-			return self._weapon.fire(curtime, direction)
+			return self._weapon.fire(direction)
 		
 		return None
 	
--- a/demos/shooter/scripts/weapons.py	Tue Apr 06 19:12:41 2010 +0000
+++ b/demos/shooter/scripts/weapons.py	Tue Apr 06 21:25:10 2010 +0000
@@ -44,7 +44,7 @@
 		self._instance = self._layer.createInstance(self._obj, location.getExactLayerCoordinates(), "bullet")
 		fife.InstanceVisual.create(self._instance)
 
-	def run(self, velocity, location, time):
+	def run(self, velocity, location):
 		if not self._running:
 			self._velocity = velocity
 			self._velocity.x /= self._xscale
@@ -53,7 +53,7 @@
 			self.create(location)
 			self._running = True
 			
-			self._starttime = time
+			self._starttime = self._scene.time
 		
 	def destroy(self):
 		if self._running and self._instance:
@@ -66,10 +66,10 @@
 	def _getOwner(self):
 		return self._owner
 
-	def update(self, timedelta):
-		self._totaltime += timedelta
-		if self._running and (self._totaltime - self._starttime) < self._ttl:
-			super(Projectile, self).update(timedelta)
+	def update(self):
+		self._totaltime += self._scene.timedelta
+		if self._running and self._totaltime < self._ttl:
+			super(Projectile, self).update()
 		else:
 			self.destroy()
 		
@@ -86,16 +86,34 @@
 		self._lastfired = 0
 		self._projectileVelocity = 0.75
 		
-	def fire(self, curtime, direction):
+	def fire(self, direction):
+		return None
+		
+	def _getProjectileVelocity(self):
+		return self._projectileVelocity
+	
+	def _setProjectileVelocity(self, vel):
+		self._projectileVelocity = vel
+		
+	projectilevelocity = property(_getProjectileVelocity, _setProjectileVelocity)
+	
+class Cannon(Weapon):
+	def __init__(self, scene, ship, firerate):
+		super(Cannon, self).__init__(scene, ship, firerate)
+		
+		#cannon's projectile velocity
+		self._projectileVelocity = 0.75
+
+		
+	def fire(self, direction):
 		velocity = normalize(direction)
 		velocity.x = velocity.x * self._projectileVelocity
 		velocity.y = velocity.y * self._projectileVelocity
 	
-		if (curtime - self._lastfired) > self._firerate:
-			pjctl = Projectile(self._scene, self._ship, "bullet1", 2000 )
-			pjctl.run(velocity, self._ship.location, curtime)
-			self._lastfired = curtime
-			return pjctl
-		
-		return None
-	
+		if (self._scene.time - self._lastfired) > self._firerate:
+			pjctl = Projectile(self._scene, self._ship, "bullet1", 6000 )
+			pjctl.run(velocity, self._ship.location)
+			self._lastfired = self._scene.time
+			self._scene.addProjectileToScene(pjctl)
+			
+
--- a/demos/shooter/scripts/world.py	Tue Apr 06 19:12:41 2010 +0000
+++ b/demos/shooter/scripts/world.py	Tue Apr 06 21:25:10 2010 +0000
@@ -33,7 +33,6 @@
 from scripts.ships.shipbase import Ship
 from scripts.ships.player import Player
 from scripts.scene import Scene
-from scripts.weapons import Weapon
 
 class World(EventListenerBase):
 	"""
@@ -76,8 +75,6 @@
 		self.map = loadMapFile(filename, self.engine)
 
 		self.scene = Scene(self.engine, self.map.getLayer('objects'))
-		#self.initPlayer()
-		self.initCameras()
 		
 		self.mainwindow = pychan.loadXML('gui/mainwindow.xml')
 		self.fpstext = self.mainwindow.findChild(name="fps")
@@ -87,9 +84,8 @@
 		self.mainwindow.position = (0,0)
 		self.mainwindow.show()
 		
-		#give player the default weapon
-		self.scene.player.weapon = Weapon(self.scene, self.scene.player, 200)
 		self.scene.initScene(self.map)
+		self.initCameras()
 		
 	def initCameras(self):
 		"""