from hoop import SimObject class ImprSimObject(SimObject): def update(self, interval, world): """update an object's physical state for an interval. """ if self.threshold and self.uDelay: self.uTimer += interval if self.uTimer < self.uDelay: return else: interval = self.uTimer self.uTimer -= self.uDelay radians = toRadians(self.facing) if self.accel: dx = math.cos(radians) * self.accel * interval dy = math.sin(radians) * self.accel * interval print "dx:", dx self.velocityX += dx self.velocityY += dy newPosX = self.posX + (self.velocityX * interval) newPosY = self.posY + (self.velocityY * interval) if self.turnRate: newFacing = self.facing + self.turnRate*interval newFacing = newFacing % 360 else: newFacing = self.facing moveSuccessful = self.tryMove(world, newPosX, newPosY, self.facing) if not moveSuccessful: moveSuccessful = self.tryMove(world, newPosX, 0, self.facing) if not moveSuccessful: moveSuccessful = self.tryMove(world, 0, newPosY, self.facing) if self.life: self.life -= interval if self.life <= 0: self.alive = 0 # calculate the variable delay if self.threshold: value = max( abs(self.velocityX), abs(self.velocityY), abs(self.turnRate) ) if value < self.threshold: self.uDelay = 1.0 - (value / self.threshold) else: self.uDelay = 0 return self.alive def tryMove(self, world, newPosX, newPosY, newFacing): if world.canMove(self, newPosX, newPosY,newFacing): self.posX = newPosX self.posY = newPosY self.facing = newFacing world.move(self, newPosX, newPosY, newFacing) self.graphicsObject.setState(newPosX, newPosY, newFacing) return 1 else: return 0