comparison demos/shooter/scripts/scene.py @ 449:1cf56403347a

Added object bounding boxes. Collision detection now works.
author prock@33b003aa-7bff-0310-803a-e67f0ece8222
date Thu, 01 Apr 2010 18:44:01 +0000
parents 5e2ec84902a7
children ba6817013343
comparison
equal deleted inserted replaced
448:5e2ec84902a7 449:1cf56403347a
48 self._model = engine.getModel() 48 self._model = engine.getModel()
49 self._layer = objectLayer 49 self._layer = objectLayer
50 self._nodes = list() 50 self._nodes = list()
51 51
52 self._player = Player(self._model, 'player', self._layer) 52 self._player = Player(self._model, 'player', self._layer)
53 self._player.start()
54
53 self._projectiles = list() 55 self._projectiles = list()
54 self._lasttime = 0 56 self._lasttime = 0
55 57
56 self._maxnodes = 128 58 self._maxnodes = 128
57 59
71 objectName = instance.getObject().getId() 73 objectName = instance.getObject().getId()
72 print objectName 74 print objectName
73 75
74 enemy = Ship(self._model, 'enemy', self._layer, False) 76 enemy = Ship(self._model, 'enemy', self._layer, False)
75 enemy.instance = instance 77 enemy.instance = instance
78 enemy.width = 0.5
79 enemy.height = 0.5
80 enemy.velocity.x = -0.013
81 enemy.start()
76 82
77 nodeindex = int(loc.x * xscale) 83 nodeindex = int(loc.x * xscale)
78 self._nodes[nodeindex].spaceobjects.append(enemy) 84 self._nodes[nodeindex].spaceobjects.append(enemy)
79 85
80 def getObjectsInNode(self, node): 86 def getObjectsInNode(self, node):
113 self._camera.setLocation(loc) 119 self._camera.setLocation(loc)
114 120
115 topleft = self._camera.toMapCoordinates(fife.ScreenPoint(0,0)) 121 topleft = self._camera.toMapCoordinates(fife.ScreenPoint(0,0))
116 bottomright = self._camera.toMapCoordinates(fife.ScreenPoint(1024,768)) 122 bottomright = self._camera.toMapCoordinates(fife.ScreenPoint(1024,768))
117 123
124 #which scene nodes to use to update objects
118 leftnode = int(topleft.x) 125 leftnode = int(topleft.x)
119 rightnode = int(bottomright.x) + 1 126 rightnode = int(bottomright.x) + 2
120 127
128 #update other objects on the screen
121 if leftnode < 0: 129 if leftnode < 0:
122 leftnode = 0 130 leftnode = 0
123 if rightnode > self._maxnodes: 131 if rightnode > self._maxnodes:
124 rightnode = self._maxnodes 132 rightnode = self._maxnodes
125 collisionlist = self.getObjectsInRange(leftnode, rightnode) 133 screenlist = self.getObjectsInRange(leftnode, rightnode)
134
135 for cl in screenlist:
136 cl.update(timedelta)
126 137
138 #update the player
127 self._player.update(timedelta, keystate, self._camera) 139 self._player.update(timedelta, keystate, self._camera)
128 140
129 #update the list of projectiles 141 #update the list of projectiles
130 todelete = list() 142 todelete = list()
131 for p in self._projectiles: 143 for p in self._projectiles:
132 p.update(time) 144 p.update(timedelta)
145 for o in screenlist:
146 if p.boundingbox.intersects(o.boundingbox):
147 p.destroy()
148 o.destroy()
133 if not p.running: 149 if not p.running:
134 todelete.append(p) 150 todelete.append(p)
135 for p in todelete: 151 for p in todelete:
136 self._projectiles.remove(p) 152 self._projectiles.remove(p)
137 153