#include "Player.hh"
#include "Rope.hh"
#include "Engine.hh"
#include <math.h>
Rope::Rope(Player &climber) : PhysicsObject(climber.state.world, PLAYER_MASS, Vector(0,0), Vector(0,0), false), pl(climber), rs(FOLDED) {
std::vector<Vector> shape(4);
shape[0] = Vector(-1, -1);
shape[1] = Vector(-1, 1);
shape[2] = Vector(1, 1);
shape[3] = Vector(1, -1);
setShape(shape);
}
void Rope::shoot(void) {
this->rs = FLYING;
this->length = 100;
this->position = pl.getPosition();
Vector direction = pl.getFacing() ? Vector( cos(pl.getAim()), -sin(pl.getAim()) )
: Vector( -cos(pl.getAim()), -sin(pl.getAim()) );
this->velocity = pl.getVelocity() + direction*500;
this->inAir = true;
enable();
pl.state.addRope(this);
}
void Rope::onCollision() {
this->rs = FIXED;
// stop movement
disable();
// set player's pivot
pl.setPivot(this);
}
void Rope::release (void) {
// TODO make it fly first and fold only
// after it's close to the player
this->rs = FOLDED;
// player doesn't have a pivot anymore
pl.setPivot(NULL);
}
RopeState Rope::getState (void) {
return this->rs;
}
float Rope::getPivotForce (PhysicsObject *bob) {
if ( (this->position - pl.getPosition()).length() >= this->length)
return 2000;
else
return 0;
}
void Rope::draw (CL_GraphicContext *gc) const {
gc->draw_line((int)(this->pl.getPosition().x), (int)(this->pl.getPosition().y)
, (int)(this->position.x), (int)(this->position.y), CL_Color::black);
}