src/Rope.cc
changeset 252 25054ce94d07
parent 248 e40ef56dc62c
child 254 0c3d58912e1b
equal deleted inserted replaced
251:6a6208e5c7a1 252:25054ce94d07
     1 #include "Player.hh"
     1 #include "Player.hh"
     2 #include "Rope.hh"      
     2 #include "Rope.hh"      
     3 #include "Engine.hh"
     3 #include "Engine.hh"
     4 #include "Graphics.hh"
     4 #include "Graphics.hh"
     5 #include <math.h>
     5 #include <math.h>
       
     6 #include <stdexcept>
     6 
     7 
     7 Rope::Rope(Player &player) : 
     8 Rope::Rope(Player &player) : 
     8     PhysicsObject(player.state.world, ROPE_MASS, Vector(0,0), Vector(0,0), false), player(player), state(ROPE_FOLDED) 
     9     PhysicsObject(player.state.world, ROPE_MASS, Vector(0,0), Vector(0,0), false), player(player), state(ROPE_FOLDED) 
     9 {
    10 {
    10     // XXX: better shape
    11     // XXX: better shape
    34 
    35 
    35     // inform network
    36     // inform network
    36     player.handleRopeState(state);
    37     player.handleRopeState(state);
    37 }
    38 }
    38 
    39 
    39 void Rope::onCollision() {
    40 void Rope::onCollision (Vector collisionPoint) {
    40     // attached to something!
    41     // attached to something!
    41     state = ROPE_FIXED;
    42     state = ROPE_FIXED;
    42 
    43         
    43     // stop movement
    44     // Ropes location will be used as the pivot point, so move the location to the collisionPoint.
    44     disable();
    45     // Currently the position is something like one pixel away from the collisionPoint where there isn't ground.
       
    46     this->position = collisionPoint;
    45 
    47 
    46     // set player's pivot
    48     // set player's pivot
    47     player.setPivot(this);
    49     player.setPivot(this);
    48     
    50     
    49     // inform network
    51     // inform network
    50     player.handleRopeState(state);
    52     player.handleRopeState(state);
    51 }
    53 }
    52 
    54 
    53 void Rope::release (void) {
    55 void Rope::release (void) {
    54     // disable if we're flying
    56     // Remove the rope from the PhysicsWorld
    55     if (state == ROPE_FLYING)
    57     disable();
    56         disable();
       
    57 
    58 
    58     // TODO make it fly first and fold only after it's close to the player
       
    59     state = ROPE_FOLDED;
    59     state = ROPE_FOLDED;
    60     
    60     
    61     // player doesn't have a pivot anymore
    61     // player doesn't have a pivot anymore
    62     player.setPivot(NULL);
    62     player.setPivot(NULL);
    63     
    63     
   129             position.x-cam.x, position.y-cam.y, 
   129             position.x-cam.x, position.y-cam.y, 
   130             CL_Color::black
   130             CL_Color::black
   131     );
   131     );
   132 }
   132 }
   133 
   133 
       
   134 void Rope::tick (TimeMS dt) {
       
   135     if (this->state == ROPE_FLYING) {
       
   136         // super
       
   137         PhysicsObject::tick(dt); 
       
   138     }
       
   139     else if (this->state == ROPE_FIXED) {
       
   140         // If there's not ground on the pivot point anymore, release the rope
       
   141         if (!world.collides(position)) { 
       
   142             release();
       
   143         }
       
   144     }
       
   145     else { // ROPE_FOLDED
       
   146         throw std::logic_error("Rope shouldn't be ticking if it is folded");
       
   147     }
       
   148 }