changeset 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.
author prock@33b003aa-7bff-0310-803a-e67f0ece8222
date Wed, 31 Mar 2010 21:13:07 +0000
parents 2046a1f2f5f2
children 5e2ec84902a7
files demos/shooter/maps/shooter_map1.xml demos/shooter/objects/ships/saucer1/object.xml demos/shooter/objects/ships/saucer1/saucer1.png demos/shooter/objects/ships/saucer2/object.xml demos/shooter/objects/ships/saucer2/saucer2.png demos/shooter/scripts/scene.py demos/shooter/scripts/ships/player.py demos/shooter/scripts/ships/shipbase.py demos/shooter/scripts/weapons.py
diffstat 9 files changed, 73 insertions(+), 45 deletions(-) [+]
line wrap: on
line diff
--- a/demos/shooter/maps/shooter_map1.xml	Wed Mar 31 15:40:00 2010 +0000
+++ b/demos/shooter/maps/shooter_map1.xml	Wed Mar 31 21:13:07 2010 +0000
@@ -2,15 +2,12 @@
 <map id="tutorial1" format="1.0">
 	<import file="../objects/backgrounds/starfield/object.xml"></import>
 	<import file="../objects/ships/player/object.xml"></import>
+	<import file="../objects/ships/saucer1/object.xml"></import>
+	<import file="../objects/ships/saucer2/object.xml"></import>
 	<import file="../objects/projectiles/bullet1/object.xml"></import>
 	<layer y_scale="1.0" y_offset="0.0" pathing="cell_edges_and_diagonals" grid_type="square" id="background" rotation="0.0" x_scale="1.0" x_offset="0.0" transparency="0">
 		<instances>
 			<i o="starfield" ns="http://www.fifengine.de/xml/tutorial" x="-2.0" r="0" y="-1.0" z="0.0"></i>
-			<i r="0" x="-2.0" o="starfield" z="0.0" y="0.0"></i>
-			<i r="0" x="-2.0" o="starfield" z="0.0" y="1.0"></i>
-			<i r="0" x="-1.0" o="starfield" z="0.0" y="1.0"></i>
-			<i r="0" x="-1.0" o="starfield" z="0.0" y="0.0"></i>
-			<i r="0" x="-1.0" o="starfield" z="0.0" y="-1.0"></i>
 			<i r="0" x="0.0" o="starfield" z="0.0" y="-1.0"></i>
 			<i r="0" x="0.0" o="starfield" z="0.0" y="0.0"></i>
 			<i r="0" x="0.0" o="starfield" z="0.0" y="1.0"></i>
@@ -21,10 +18,7 @@
 			<i r="0" x="3.0" o="starfield" z="0.0" y="-1.0"></i>
 			<i r="0" x="3.0" o="starfield" z="0.0" y="0.0"></i>
 			<i r="0" x="2.0" o="starfield" z="0.0" y="0.0"></i>
-			<i r="0" x="2.0" o="starfield" z="0.0" y="1.0"></i>
 			<i r="0" x="3.0" o="starfield" z="0.0" y="1.0"></i>
-			<i r="0" x="-2.0" o="starfield" z="0.0" y="2.0"></i>
-			<i r="0" x="-1.0" o="starfield" z="0.0" y="2.0"></i>
 			<i r="0" x="0.0" o="starfield" z="0.0" y="2.0"></i>
 			<i r="0" x="1.0" o="starfield" z="0.0" y="2.0"></i>
 			<i r="0" x="2.0" o="starfield" z="0.0" y="2.0"></i>
@@ -33,8 +27,6 @@
 			<i r="0" x="4.0" o="starfield" z="0.0" y="0.0"></i>
 			<i r="0" x="4.0" o="starfield" z="0.0" y="1.0"></i>
 			<i r="0" x="4.0" o="starfield" z="0.0" y="2.0"></i>
-			<i r="0" x="-2.0" o="starfield" z="0.0" y="-2.0"></i>
-			<i r="0" x="-1.0" o="starfield" z="0.0" y="-2.0"></i>
 			<i r="0" x="0.0" o="starfield" z="0.0" y="-2.0"></i>
 			<i r="0" x="1.0" o="starfield" z="0.0" y="-2.0"></i>
 			<i r="0" x="2.0" o="starfield" z="0.0" y="-2.0"></i>
@@ -235,11 +227,6 @@
 			<i r="0" x="40.0" o="starfield" z="0.0" y="-1.0"></i>
 			<i r="0" x="41.0" o="starfield" z="0.0" y="-1.0"></i>
 			<i r="0" x="42.0" o="starfield" z="0.0" y="-1.0"></i>
-			<i r="0" x="-3.0" o="starfield" z="0.0" y="-2.0"></i>
-			<i r="0" x="-3.0" o="starfield" z="0.0" y="-1.0"></i>
-			<i r="0" x="-3.0" o="starfield" z="0.0" y="0.0"></i>
-			<i r="0" x="-3.0" o="starfield" z="0.0" y="1.0"></i>
-			<i r="0" x="-3.0" o="starfield" z="0.0" y="2.0"></i>
 			<i r="0" x="44.0" o="starfield" z="0.0" y="2.0"></i>
 			<i r="0" x="45.0" o="starfield" z="0.0" y="2.0"></i>
 			<i r="0" x="46.0" o="starfield" z="0.0" y="2.0"></i>
@@ -445,11 +432,18 @@
 			<i r="0" x="82.0" o="starfield" z="0.0" y="0.0"></i>
 			<i r="0" x="81.0" o="starfield" z="0.0" y="0.0"></i>
 			<i r="0" x="80.0" o="starfield" z="0.0" y="0.0"></i>
+			<i r="0" x="2.0" o="starfield" z="0.0" y="1.0"></i>
 		</instances>
 	</layer>
-	<layer y_scale="1.0" y_offset="0.0" pathing="cell_edges_and_diagonals" grid_type="square" id="objects" rotation="0.0" x_scale="1.0" x_offset="0.0" transparency="0">
+	<layer y_scale="0.25" y_offset="0.0" pathing="cell_edges_and_diagonals" grid_type="square" id="objects" rotation="0.0" x_scale="0.25" x_offset="0.0" transparency="0">
 		<instances>
-			<i o="ship1" z="0.0" x="-1.0" r="0" y="0.0" id="player"></i>
+			<i o="ship1" z="0.0" x="7.0" r="0" y="0.0" id="player"></i>
+			<i r="0" x="20.0" o="saucer1" z="0.0" y="-2.0"></i>
+			<i r="0" x="20.0" o="saucer1" z="0.0" y="1.0"></i>
+			<i r="0" x="20.0" o="saucer1" z="0.0" y="4.0"></i>
+			<i r="0" x="20.0" o="saucer1" z="0.0" y="-5.0"></i>
+			<i r="0" x="26.0" o="saucer2" z="0.0" y="-3.0"></i>
+			<i r="0" x="26.0" o="saucer2" z="0.0" y="2.0"></i>
 		</instances>
 	</layer>
 	<camera ref_cell_height="256" zoom="1" rotation="0.0" ref_layer_id="background" ref_cell_width="256" id="main" tilt="0.0">
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/demos/shooter/objects/ships/saucer1/object.xml	Wed Mar 31 21:13:07 2010 +0000
@@ -0,0 +1,4 @@
+<?fife type="object"?>
+<object id="saucer1" namespace="http://www.fifengine.de/xml/tutorial" blocking="0" static="1">
+	<image source="saucer1.png" direction="0" />
+</object>
Binary file demos/shooter/objects/ships/saucer1/saucer1.png has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/demos/shooter/objects/ships/saucer2/object.xml	Wed Mar 31 21:13:07 2010 +0000
@@ -0,0 +1,4 @@
+<?fife type="object"?>
+<object id="saucer2" namespace="http://www.fifengine.de/xml/tutorial" blocking="0" static="1">
+	<image source="saucer2.png" direction="0" />
+</object>
Binary file demos/shooter/objects/ships/saucer2/saucer2.png has changed
--- a/demos/shooter/scripts/scene.py	Wed Mar 31 15:40:00 2010 +0000
+++ b/demos/shooter/scripts/scene.py	Wed Mar 31 21:13:07 2010 +0000
@@ -26,6 +26,9 @@
 from scripts.ships.player import Player
 from scripts.common.helpers import Rect
 
+
+
+
 class Scene(object):
 	def __init__(self, engine, objectLayer):
 		self._engine = engine
@@ -47,14 +50,14 @@
 		timedelta = time - self._lasttime
 		self._lasttime = time
 		
-		self._player.update(timedelta, keystate, self._camera)
-		
 		#update camera location
-		loc = self._player.location
+		loc = self._camera.getLocation()
 		exactloc = self._camera.getLocation().getExactLayerCoordinates()
-		exactloc.x += timedelta * 0.0005
+		exactloc.x += timedelta * 0.001
 		loc.setExactLayerCoordinates(exactloc)
 		self._camera.setLocation(loc)
+
+		self._player.update(timedelta, keystate, self._camera)
 		
 		#update the list of projectiles
 		todelete = list()
--- a/demos/shooter/scripts/ships/player.py	Wed Mar 31 15:40:00 2010 +0000
+++ b/demos/shooter/scripts/ships/player.py	Wed Mar 31 21:13:07 2010 +0000
@@ -37,16 +37,16 @@
 		oldpos = self.location
 		
 		if keystate['UP']:
-			self.applyThrust(fife.DoublePoint(0,-0.075))
+			self.applyThrust(fife.DoublePoint(0,-0.075), timedelta)
 			key = True
 		if keystate['DOWN']:
-			self.applyThrust(fife.DoublePoint(0,0.075))
+			self.applyThrust(fife.DoublePoint(0,0.075), timedelta)
 			key = True
 		if keystate['LEFT']:
-			self.applyThrust(fife.DoublePoint(-0.075,0))
+			self.applyThrust(fife.DoublePoint(-0.075,0), timedelta)
 			key = True
 		if keystate['RIGHT']:
-			self.applyThrust(fife.DoublePoint(0.075,0))
+			self.applyThrust(fife.DoublePoint(0.075,0), timedelta)
 			key = True
 			
 		if not key and self._velocity.length() > 0:
@@ -59,29 +59,41 @@
 		topleft = camera.toMapCoordinates(fife.ScreenPoint(0,0))
 		bottomright = camera.toMapCoordinates(fife.ScreenPoint(1024,768))
 
-		#add a little padding to the left edge
-		topleft.x += 0.1
-		
 		camrect = Rect(topleft.x, topleft.y, bottomright.x - topleft.x, bottomright.y - topleft.y)
 	
 		#player bounding box
 		#TODO: make this configurable
+		xscale = self._layer.getCellGrid().getXScale()
+		yscale = self._layer.getCellGrid().getYScale()
 		pos = self.location.getExactLayerCoordinates()
 		bbox = Rect()
-		bbox.x = pos.x - 0.005
-		bbox.y = pos.y - 0.005
-		bbox.w = 0.01
-		bbox.h = 0.01
+		bbox.x = pos.x*xscale - 0.175
+		bbox.y = pos.y*yscale - 0.175
+		bbox.w = 0.25
+		bbox.h = 0.25
+
+		#add the padding to the edge
+		camrect.x += bbox.w
+		camrect.y += bbox.h
+		camrect.w -= 2*bbox.w
+		camrect.h -= 2*bbox.h
+
 		
 		if not bbox.intersects(camrect):
-			if pos.x < topleft.x:
-				pos.x += timedelta * 0.0005
-				oldpos.setExactLayerCoordinates(pos)
-				self._velocity.x = timedelta * 0.0005
+			if (bbox.x + bbox.w) < camrect.x:
+				#pos.x = (bbox.x + bbox.w/2 + 0.1) / xscale
+				#oldpos.setExactLayerCoordinates(pos)
+				self._velocity.x = (timedelta * 0.001) / xscale
+			
+#			elif (bbox.y + bbox.h) < camrect.y or (bbox.y - bbox.h) > camrect.y:
+#				pos.x += self._velocity.x * (timedelta/1000.0)
+#				oldpos.setExactLayerCoordinates(pos)
+#				self._velocity.y = 0
 			else:
 				self._velocity.x = 0
+				self._velocity.y = 0
 
-			self._velocity.y = 0
+
 
 			self.location = oldpos
 
--- a/demos/shooter/scripts/ships/shipbase.py	Wed Mar 31 15:40:00 2010 +0000
+++ b/demos/shooter/scripts/ships/shipbase.py	Wed Mar 31 21:13:07 2010 +0000
@@ -21,6 +21,8 @@
 #  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 # ####################################################################
 
+from math import sqrt
+
 from fife import fife
 from scripts.common.helpers import normalize
 from scripts.weapons import Weapon
@@ -30,6 +32,9 @@
 		self._scene = scene
 		self._name = shipName
 		self._layer = layer
+		self._xscale = self._layer.getCellGrid().getXScale()
+		self._yscale = self._layer.getCellGrid().getYScale()
+
 		if uniqInMap:
 			self._instance = self._layer.getInstance(self._name)
 		else:
@@ -38,7 +43,7 @@
 		
 		#velocity as a vector
 		self._velocity = fife.DoublePoint(0,0)
-		self._maxvelocity = 0.025
+		self._maxvelocity = 0.025/sqrt(self._xscale * self._yscale)
 		self._timedelta = 0
 		self._weapon = None
 
@@ -46,7 +51,7 @@
 		return self._maxvelocity
 		
 	def _setMaxVelocity(self, maxvel):
-		self._maxvelocity = maxvel
+		self._maxvelocity = maxvel/sqrt(self._xscale * self._yscale)
 
 	def _getLocation(self):
 		return self._instance.getLocation()
@@ -66,9 +71,9 @@
 	def _getWeapon(self, weapon):
 		return self._weapon
 		
-	def applyThrust(self, vector):
-		self._velocity.x += vector.x * (self._timedelta/1000.0)
-		self._velocity.y += vector.y * (self._timedelta/1000.0)
+	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
 		
 		if self._velocity.length() > self._maxvelocity:
 			norm = normalize(self._velocity)
@@ -98,8 +103,8 @@
 		norm.x *= brakingForce
 		norm.y *= brakingForce
 		
-		self._velocity.x += norm.x * (self._timedelta/1000.0)
-		self._velocity.y += norm.y * (self._timedelta/1000.0)
+		self._velocity.x += (norm.x * (self._timedelta/1000.0))/self._xscale
+		self._velocity.y += (norm.y * (self._timedelta/1000.0))/self._yscale
 		
 	def fire(self, curtime):
 		if self._weapon:
--- a/demos/shooter/scripts/weapons.py	Wed Mar 31 15:40:00 2010 +0000
+++ b/demos/shooter/scripts/weapons.py	Wed Mar 31 21:13:07 2010 +0000
@@ -35,6 +35,9 @@
 		self._ttl = timeToLive
 		self._starttime = 0
 		
+		self._xscale = self._layer.getCellGrid().getXScale()
+		self._yscale = self._layer.getCellGrid().getYScale()
+		
 	def create(self, location):
 		self._instance = self._layer.createInstance(self._obj, location.getExactLayerCoordinates(), "bullet")
 		fife.InstanceVisual.create(self._instance)
@@ -42,6 +45,8 @@
 	def run(self, velocity, location, time):
 		if not self._running:
 			self._velocity = velocity
+			self._velocity.x /= self._xscale
+			self._velocity.y /= self._yscale
 		
 			self.create(location)
 			self._running = True
@@ -89,11 +94,12 @@
 		self._ship = ship
 		self._firerate = firerate
 		self._lastfired = 0
+		self._projectileVelocity = fife.DoublePoint(0.075,0)
 		
 	def fire(self, curtime):
 		if (curtime - self._lastfired) > self._firerate:
 			pjctl = Projectile(self._model, "bullet1", self._layer, 2000 )
-			pjctl.run(fife.DoublePoint(0.075,0), self._ship.location, curtime)
+			pjctl.run(fife.DoublePoint(self._projectileVelocity.x,self._projectileVelocity.y), self._ship.location, curtime)
 			self._lastfired = curtime
 			return pjctl