Mercurial > fife-parpg
changeset 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 |
files | demos/shooter/scripts/common/baseobject.py demos/shooter/scripts/scene.py demos/shooter/scripts/ships/player.py demos/shooter/scripts/weapons.py |
diffstat | 4 files changed, 83 insertions(+), 40 deletions(-) [+] |
line wrap: on
line diff
--- a/demos/shooter/scripts/common/baseobject.py Thu Apr 01 17:03:34 2010 +0000 +++ b/demos/shooter/scripts/common/baseobject.py Thu Apr 01 18:44:01 2010 +0000 @@ -23,6 +23,7 @@ from fife import fife from scripts.common.helpers import normalize +from scripts.common.helpers import Rect class SpaceObject(object): def __init__(self, model, name, layer, findInstance=True): @@ -34,36 +35,44 @@ self._velocity = fife.DoublePoint(0,0) self._maxvelocity = 1 self._timedelta = 0 - + self._boundingBox = Rect(0,0,0,0) + self._running = False + if findInstance: self._instance = self._layer.getInstance(self._name) else: self._instnace = None def start(self): - pass + if self._instance: + self._running = True def update(self, timedelta): - self._timedelta = timedelta + if self._running: + self._timedelta = timedelta - shiploc = self.location - exactloc = shiploc.getExactLayerCoordinates() + shiploc = self.location + exactloc = shiploc.getExactLayerCoordinates() - exactloc.x += self._velocity.x - exactloc.y += self._velocity.y + exactloc.x += self._velocity.x + exactloc.y += self._velocity.y + + self._boundingBox.x = exactloc.x + self._boundingBox.w/2 + self._boundingBox.y = exactloc.y + self._boundingBox.h/2 - shiploc.setExactLayerCoordinates(exactloc) - self.location = shiploc + shiploc.setExactLayerCoordinates(exactloc) + self.location = shiploc def stop(self): - pass + self._running = False def destroy(self): - pass + self._running = False + self._layer.deleteInstance(self._instance) def applyThrust(self, vector, timedelta): - self._velocity.x += (vector.x * (timedelta/100.0))/self._xscale - self._velocity.y += (vector.y * (timedelta/100.0))/self._yscale + 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) @@ -93,8 +102,11 @@ norm.x *= brakingForce norm.y *= brakingForce - self._velocity.x += (norm.x * (timedelta/100.0))/self._xscale - self._velocity.y += (norm.y * (timedelta/100.0))/self._yscale + self._velocity.x += (norm.x * (timedelta/1000.0))/self._xscale + self._velocity.y += (norm.y * (timedelta/1000.0))/self._yscale + + def _isRunning(self): + return self._running def _getMaxVelocity(self): return self._maxvelocity @@ -116,8 +128,30 @@ def _getVelocity(self): return self._velocity + + def _setVelocity(self, velocity): + self._velocity = velocity + def _getBoundingBox(self): + return self._boundingBox + + def _getW(self): + return self._boundingBox.w + + def _getH(self): + return self._boundingBox.h + + def _setW(self, w): + self._boundingBox.w = w + + def _setH(self, h): + self._boundingBox.h = h + + width = property(_getW, _setW) + height = property(_getH, _setH) + boundingbox = property(_getBoundingBox) location = property(_getLocation,_setLocation) instance = property(_getInstance, _setInstance) - velocity = property(_getVelocity) - maxvelocity = property(_getMaxVelocity, _setMaxVelocity) \ No newline at end of file + velocity = property(_getVelocity, _setVelocity) + maxvelocity = property(_getMaxVelocity, _setMaxVelocity) + running = property(_isRunning) \ No newline at end of file
--- a/demos/shooter/scripts/scene.py Thu Apr 01 17:03:34 2010 +0000 +++ b/demos/shooter/scripts/scene.py Thu Apr 01 18:44:01 2010 +0000 @@ -50,6 +50,8 @@ self._nodes = list() self._player = Player(self._model, 'player', self._layer) + self._player.start() + self._projectiles = list() self._lasttime = 0 @@ -73,6 +75,10 @@ enemy = Ship(self._model, 'enemy', self._layer, False) enemy.instance = instance + enemy.width = 0.5 + enemy.height = 0.5 + enemy.velocity.x = -0.013 + enemy.start() nodeindex = int(loc.x * xscale) self._nodes[nodeindex].spaceobjects.append(enemy) @@ -115,21 +121,31 @@ topleft = self._camera.toMapCoordinates(fife.ScreenPoint(0,0)) bottomright = self._camera.toMapCoordinates(fife.ScreenPoint(1024,768)) + #which scene nodes to use to update objects leftnode = int(topleft.x) - rightnode = int(bottomright.x) + 1 + rightnode = int(bottomright.x) + 2 + #update other objects on the screen if leftnode < 0: leftnode = 0 if rightnode > self._maxnodes: rightnode = self._maxnodes - collisionlist = self.getObjectsInRange(leftnode, rightnode) + screenlist = self.getObjectsInRange(leftnode, rightnode) + + for cl in screenlist: + cl.update(timedelta) + #update the player self._player.update(timedelta, keystate, self._camera) #update the list of projectiles todelete = list() for p in self._projectiles: - p.update(time) + p.update(timedelta) + for o in screenlist: + if p.boundingbox.intersects(o.boundingbox): + p.destroy() + o.destroy() if not p.running: todelete.append(p) for p in todelete:
--- a/demos/shooter/scripts/ships/player.py Thu Apr 01 17:03:34 2010 +0000 +++ b/demos/shooter/scripts/ships/player.py Thu Apr 01 18:44:01 2010 +0000 @@ -37,20 +37,20 @@ oldpos = self.location if keystate['UP']: - self.applyThrust(fife.DoublePoint(0,-0.0075), timedelta) + self.applyThrust(fife.DoublePoint(0,-0.075), timedelta) key = True if keystate['DOWN']: - self.applyThrust(fife.DoublePoint(0,0.0075), timedelta) + self.applyThrust(fife.DoublePoint(0,0.075), timedelta) key = True if keystate['LEFT']: - self.applyThrust(fife.DoublePoint(-0.0075,0), timedelta) + self.applyThrust(fife.DoublePoint(-0.075,0), timedelta) key = True if keystate['RIGHT']: - self.applyThrust(fife.DoublePoint(0.0075,0), timedelta) + self.applyThrust(fife.DoublePoint(0.075,0), timedelta) key = True if not key and self._velocity.length() > 0: - self.applyBrake(0.0075, timedelta) + self.applyBrake(0.075, timedelta) super(Player, self).update(timedelta)
--- a/demos/shooter/scripts/weapons.py Thu Apr 01 17:03:34 2010 +0000 +++ b/demos/shooter/scripts/weapons.py Thu Apr 01 18:44:01 2010 +0000 @@ -31,9 +31,10 @@ self._layer = layer self._obj = self._model.getObject(self._name, "http://www.fifengine.de/xml/tutorial") - self._running = False + self._ttl = timeToLive self._starttime = 0 + self._totaltime = 0 def create(self, location): self._instance = self._layer.createInstance(self._obj, location.getExactLayerCoordinates(), "bullet") @@ -55,26 +56,16 @@ self._layer.deleteInstance(self._instance) self._running = False - def _isRunning(self): - return self._running - def _getTTL(self): return self._ttl - def update(self, curtime): - if self._running and (curtime - self._starttime) < self._ttl: - projloc = self.location - exactloc = projloc.getExactLayerCoordinates() - - exactloc.x += self._velocity.x - exactloc.y += self._velocity.y - - projloc.setExactLayerCoordinates(exactloc) - self.location = projloc + def update(self, timedelta): + self._totaltime += timedelta + if self._running and (self._totaltime - self._starttime) < self._ttl: + super(Projectile, self).update(timedelta) else: self.destroy() - running = property(_isRunning) ttl = property(_getTTL) class Weapon(object): @@ -89,6 +80,8 @@ def fire(self, curtime): if (curtime - self._lastfired) > self._firerate: pjctl = Projectile(self._model, "bullet1", self._layer, 2000 ) + pjctl.width = 0.05 + pjctl.height = 0.05 pjctl.run(fife.DoublePoint(self._projectileVelocity.x,self._projectileVelocity.y), self._ship.location, curtime) self._lastfired = curtime return pjctl