src/PhysicsObject.cc
changeset 222 293ddf4c067d
parent 221 fbc5db6fce45
child 223 2fcaf54ed37b
--- a/src/PhysicsObject.cc	Sat Dec 06 16:17:05 2008 +0000
+++ b/src/PhysicsObject.cc	Sat Dec 06 17:51:19 2008 +0000
@@ -1,15 +1,21 @@
-#include "Player.hh"
+
 #include "PhysicsObject.hh"
 #include "Engine.hh"
 
 #include <cmath>
 
-PhysicsObject::PhysicsObject (PhysicsWorld &world, float mass, 
-                              Vector position, Vector velocity)
-    : world(world), position(position), velocity(velocity),
-      mass(mass), inAir(true), aim(0), facingRight(true) {
-    // TODO: Is thir the right way to do this?
-    //world.addPlayerObject(this);
+PhysicsObject::PhysicsObject (PhysicsWorld &world, float mass, Vector position, Vector velocity) :
+    world(world), position(position), velocity(velocity), mass(mass), inAir(true), aim(0), facingRight(true), alive(true)
+{
+    world.addPhysicsObject(this);
+}
+
+PhysicsObject::~PhysicsObject (void) {
+    Engine::log(DEBUG, "PhysicsObject.destructor") << this /* << ": objects.size=" << ((int) world.objects.size()) */;
+
+    // world.objects.remove(this);
+
+    Engine::log(DEBUG, "PhysicsObject.destructor") << this /* << ": objects.size=" << ((int) world.objects.size()) */;
 }
 
 /**
@@ -103,10 +109,6 @@
     return true;
 }
 
-void func1() {
-
-}
-
 /**
  * Updates object speed and position. This function organises force
  * integration and collision detection.
@@ -153,10 +155,9 @@
         }
     }
 
-    if(!possibleLocation(position)) {
-        Engine::log(DEBUG, "great failure") << "great failure";
-        func1();
-    }
+    if (!possibleLocation(position))
+        Engine::log(DEBUG, "PhysicsObject.updatePosition") << "impossible location: " << position;
+
     Vector newPosition;
     Vector velAfterTick;
     // Calculate new position and velocity to the given references
@@ -196,26 +197,28 @@
     }
    
     
-    if(!possibleLocation(reached)) {
-        Engine::log(DEBUG, "PhysicsObject.updatePosition") << "inside ground. diffVec: " << diffVec;
-        func1();
-    }
+    if (!possibleLocation(reached))
+        Engine::log(DEBUG, "PhysicsObject.updatePosition") << "impossible location: " << position << ", diffVec=" << diffVec;
 
     // In case of some float error check the final coordinate
-    if(!collided) {
-        if(!possibleLocation(newPosition)) {
+    if (!collided) {
+        if (!possibleLocation(newPosition)) {
             newPosition = reached;
         } else {
             // This means everything was ok, so no need to do anything
         }
+
+        this->position = newPosition;
+
     } else {
         newPosition = reached;
         this->position = newPosition;
+
+        // the following may delete this object, so it must be the last thing called
         onCollision();
-        //TODO: It should be moved before onCollision, for Shots
+
+        return;
     }
-    this->position = newPosition;
-//    Engine::log(DEBUG, "PhysicsObject.updatePosition") << "Pos: " << this->position;
 }
 
 /**
@@ -304,6 +307,10 @@
     return this->position;
 }
 
+Vector PhysicsObject::getVelocity () {
+    return this->velocity;
+}
+
 bool PhysicsObject::getFacing() {
     return this->facingRight;
 }
@@ -323,6 +330,23 @@
 void PhysicsObject::tick (TimeMS tick_length) {
     this->updatePosition(tick_length);
 }
+    
+void PhysicsObject::destroy (void) {
+    alive = false;
+}
+    
+bool PhysicsObject::isDestroyed (void) {
+    return !alive;
+}
+    
+bool PhysicsObject::removeIfDestroyed (void) {
+    if (!alive) {
+        delete this;
+        return true;
+    } else {
+        return false;
+    }
+}
 
 void PhysicsObject::draw(CL_GraphicContext *gc) {
     CL_Quad player(