diff demos/shooter/scripts/scene.py @ 448:5e2ec84902a7

Did a little re-factoring. Introduced the scene graph for collision detection. Changed the time scale to be accurate.
author prock@33b003aa-7bff-0310-803a-e67f0ece8222
date Thu, 01 Apr 2010 17:03:34 +0000
parents 64676ea55472
children 1cf56403347a
line wrap: on
line diff
--- a/demos/shooter/scripts/scene.py	Wed Mar 31 21:13:07 2010 +0000
+++ b/demos/shooter/scripts/scene.py	Thu Apr 01 17:03:34 2010 +0000
@@ -27,18 +27,73 @@
 from scripts.common.helpers import Rect
 
 
-
+class SceneNode(object):
+	def __init__(self, spaceobjects = None):
+		if not spaceobjects:
+			self._spaceobjects = list()
+		else:
+			self._spaceobjects = spaceobjects
+		
+	def _getObjects(self):
+		return self._spaceobjects
+		
+	def _setObjects(self, spaceobjects):
+		self._spaceobjects = spaceobjects
+		
+	spaceobjects = property(_getObjects, _setObjects)
 
 class Scene(object):
 	def __init__(self, engine, objectLayer):
 		self._engine = engine
 		self._model = engine.getModel()
 		self._layer = objectLayer
+		self._nodes = list()
 		
 		self._player = Player(self._model, 'player', self._layer)
 		self._projectiles = list()
 		self._lasttime = 0
 		
+		self._maxnodes = 128
+
+	def initScene(self, mapobj):
+		layer = mapobj.getLayer('objects')
+		xscale = layer.getCellGrid().getXScale()
+				
+		enemies = layer.getInstances('enemy')
+		
+		#initialize our scene array to some arbitrary size
+		for i in range(0,self._maxnodes):
+			self._nodes.append(SceneNode())
+		
+		for instance in enemies:
+			loc = instance.getLocation().getExactLayerCoordinates()
+			
+			objectName = instance.getObject().getId()
+			print objectName
+			
+			enemy = Ship(self._model, 'enemy', self._layer, False)
+			enemy.instance = instance
+			
+			nodeindex = int(loc.x * xscale)
+			self._nodes[nodeindex].spaceobjects.append(enemy)
+
+	def getObjectsInNode(self, node):
+		return self._nodes[node].instances
+
+	def getObjectsInRange(self, rangeL, rangeR):
+		objects = list()
+		
+		for i in range(rangeL, rangeR):
+			objects.extend(self._nodes[i].spaceobjects)
+			
+		return objects
+	
+	def checkCollision(self, instance1, instance2):
+		pos1 = instnace1.getLocation().getExactLayerCoordinates()
+		pos2 = instnace2.getLocation().getExactLayerCoordinates()
+		
+		
+	
 	def attachCamera(self, cam):
 		self._camera = cam
 		self._camera.setLocation(self._player.location)
@@ -56,6 +111,18 @@
 		exactloc.x += timedelta * 0.001
 		loc.setExactLayerCoordinates(exactloc)
 		self._camera.setLocation(loc)
+		
+		topleft = self._camera.toMapCoordinates(fife.ScreenPoint(0,0))
+		bottomright = self._camera.toMapCoordinates(fife.ScreenPoint(1024,768))
+
+		leftnode = int(topleft.x)
+		rightnode = int(bottomright.x) + 1
+		
+		if leftnode < 0:
+			leftnode = 0
+		if rightnode > self._maxnodes:
+			rightnode = self._maxnodes
+		collisionlist = self.getObjectsInRange(leftnode, rightnode)
 
 		self._player.update(timedelta, keystate, self._camera)