Rope fixing
authorekku
Mon, 08 Dec 2008 22:37:27 +0000
changeset 322 f94a5c192097
parent 321 69ed10f20a9e
child 323 a17043af6995
Rope fixing
src/PhysicsObject.cc
src/PhysicsObject.hh
src/Player.cc
src/Player.hh
src/Rope.cc
src/Rope.hh
--- a/src/PhysicsObject.cc	Mon Dec 08 22:34:28 2008 +0000
+++ b/src/PhysicsObject.cc	Mon Dec 08 22:37:27 2008 +0000
@@ -130,13 +130,7 @@
     // If the object (practically player) has a pivot point add
     // a force towards that
     if (pivot != NULL) {
-        Vector direction(pivot->getPosition()-position);
-        float magnitude = pivot->getPivotForce(this);
-        float length = direction.length();
-        if (length > 0) {
-            direction = direction / length * magnitude;
-            applyForce(direction);
-        }
+        applyForce(getPivotForce());
     }
 
     std::pair<Force, TimeMS> force;
@@ -381,6 +375,10 @@
     this->shape = shape;
 }
 
+PhysicsObject *PhysicsObject::getPivot (void) {
+    return this->pivot;
+}
+
 void PhysicsObject::setPivot (PhysicsObject *pivot) {
     this->pivot = pivot;
 }
@@ -428,10 +426,8 @@
     }
 }
 
-float PhysicsObject::getPivotForce (PhysicsObject *bob) { 
-    (void) bob;
-
-    return 0.0; 
+Vector PhysicsObject::getPivotForce (void) { 
+    return Vector(0,0); 
 }
 
 bool PhysicsObject::collides (const PhysicsObject &obj) {
--- a/src/PhysicsObject.hh	Mon Dec 08 22:34:28 2008 +0000
+++ b/src/PhysicsObject.hh	Mon Dec 08 22:37:27 2008 +0000
@@ -314,6 +314,11 @@
     void setPivot (PhysicsObject *pivot);
 
     /**
+     * Return the pivot object pointer.
+     */
+    PhysicsObject *getPivot (void);
+
+    /**
      * Checks if object collides with other objects
      *
      * @param obj Other PhysicsObject
@@ -327,7 +332,7 @@
      * @param bob Othe object
      * @return Force
      */
-    virtual float getPivotForce (PhysicsObject *bob);
+    virtual Vector getPivotForce (void);
 
     /**
      * Update object in physics simulation.
--- a/src/Player.cc	Mon Dec 08 22:34:28 2008 +0000
+++ b/src/Player.cc	Mon Dec 08 22:37:27 2008 +0000
@@ -293,6 +293,15 @@
     }
 }
 
+Vector Player::getPivotForce () {
+    Vector direction = this->pivot->getPosition() - this->getPosition();
+    float dirLength = direction.length();
+    if (dirLength >= this->rope.getLength())
+        return direction / dirLength * ROPE_FORCE;
+    else
+        return Vector(0,0);
+}
+ 
 void Player::takeDamage (Projectile *source) {
     health -= source->getDamage();
 
--- a/src/Player.hh	Mon Dec 08 22:34:28 2008 +0000
+++ b/src/Player.hh	Mon Dec 08 22:37:27 2008 +0000
@@ -136,6 +136,11 @@
     void takeDamage (Projectile *source);
 
     /**
+     * If the player has a pivot calculate the force it causes.
+     */
+    Vector getPivotForce (void);
+
+    /**
      * Gives player's health in percents from maximum
      */
     float getHealthPercent() const;
--- a/src/Rope.cc	Mon Dec 08 22:34:28 2008 +0000
+++ b/src/Rope.cc	Mon Dec 08 22:37:27 2008 +0000
@@ -8,7 +8,6 @@
 Rope::Rope(Player &player) : 
     PhysicsObject(player.state.world, ROPE_MASS, Vector(0,0), Vector(0,0), ROPE, 0.00, false), 
     player(player), 
-    pivotObject(NULL),
     state(ROPE_FOLDED) 
 {
     // XXX: better shape
@@ -41,15 +40,18 @@
 }
 
 void Rope::onCollision (Vector collisionPoint, PhysicsObject *other) {
+    // Fix the rope to another player if collided with it
     if (other != NULL) {
         if (other->getType() == PLAYER) {
             Player *target = dynamic_cast<Player*>(other);
             if (target == &(this->player))
                 return;
-            pivotObject = target;            
+            this->player.pivot = target;            
         } else if (other->getType() == PROJECTILE) {
             return;
         }
+    } else { // Collided with terrain
+        player.setPivot(this);
     }
 
     // attached to something!
@@ -59,9 +61,6 @@
     // Ropes location will be used as the pivot point, so move the location to the collisionPoint.
     // Currently the position is something like one pixel away from the collisionPoint where there isn't ground.
     setPosition (collisionPoint);
-
-    // set player's pivot
-    player.setPivot(this);
     
     // inform network
     player.handleRopeState(state);
@@ -128,13 +127,6 @@
     this->length = length;
 }
 
-float Rope::getPivotForce (PhysicsObject *bob) {
-    if ((position - bob->getPosition()).length() >= length)
-        return ROPE_FORCE;
-    else
-        return 0;
-}
-
 void Rope::draw (Graphics *g, PixelCoordinate camera) {
     if (state == ROPE_FOLDED)
         return;
@@ -155,9 +147,20 @@
         PhysicsObject::tick(dt); 
     }
     else if (this->state == ROPE_FIXED) {
+
+        this->position = player.getPivot()->getPosition();
+
+        // If players pivot is not this rope but some other player don't do anything 
+        // (though it should be released atleast when the player dies)
+        if (player.getPivot() != this)
+            return;
+
         // If there's not ground on the pivot point anymore, release the rope
         if (!world.collides(position)) { 
-            this->state = ROPE_FLYING;
+            state = ROPE_FLYING;
+            length = ROPE_LENGTH;
+            inAir = true;
+            player.setPivot(NULL);
             player.handleRopeState(state);
         }
     }
--- a/src/Rope.hh	Mon Dec 08 22:34:28 2008 +0000
+++ b/src/Rope.hh	Mon Dec 08 22:37:27 2008 +0000
@@ -29,15 +29,12 @@
     // the owner
     Player &player;
 
-    // possible pivot point (rope is stuck on a player)
-    PhysicsObject *pivotObject;
-
     // How long is the rope in its unstrected state
     float length;
         
     // basic state
     RopeState state;
-        
+
 protected:
     /**
      * Attach the rope, so disable the PhysicsObject and change state
@@ -45,11 +42,6 @@
      */
     virtual void onCollision (Vector collisionPoint, PhysicsObject *other);
 
-    /*
-     * If the rope is currently longer than length, this returns ROPE_FORCE, else 0
-     */
-    virtual float getPivotForce (PhysicsObject *bob);
-
 public:
     Rope(Player &player);