#include "Engine.hh"
#include "Player.hh"
Player::Player(GameState &state, Vector position, bool visible) :
PhysicsObject(state.world, PLAYER_MASS, position, Vector(0, 0)), state(state), visible(visible) {
std::vector<Vector> shape(4);
shape[0] = Vector(0,-9);
shape[1] = Vector(6,0);
shape[2] = Vector(0,9);
shape[3] = Vector(-6,0);
// Initialize the shape of the player (salmiakki shape)
setShape(shape);
collision_elasticity = PLAYER_COLLISION_ELASTICITY;
world.addPlayerObject(this);
}
/**
* shoots the selected weapon.
* TODO: selection and weapon information
*/
void Player::shoot (void) {
// here should be somehow considered which projectile it is
if(!canShoot())
return;
reloadTimer += 0;
Vector unitVectorAim = facingRight ? Vector(std::cos(aim), -std::sin(aim)) :
Vector(-std::cos(aim), -std::sin(aim));
float shotspeed = 100*PHYSICS_TICK_MS;
Vector shotRelativeVelocity = unitVectorAim * shotspeed;
Vector shotVelocity = this->velocity + shotRelativeVelocity;
new Projectile(this->state, this->position, shotVelocity, true);
}
void Player::handleMove (PlayerInput_Move input) {
float fx = 0; // Force in x-direction
float da = 0; // Crosshair angle
// handle left/right
if ((input & INPUT_MOVE_LEFT) && (velocity.x > -PLAYER_MAX_SPEED))
fx -= PLAYER_MOVE_FORCE;
if ((input & INPUT_MOVE_RIGHT) && (velocity.x < PLAYER_MAX_SPEED))
fx += PLAYER_MOVE_FORCE;
if (input & INPUT_MOVE_UP)
da += CROSSHAIR_ANGLE_SPEED;
if (input & INPUT_MOVE_DOWN)
da -= CROSSHAIR_ANGLE_SPEED;
if (input & INPUT_MOVE_JUMP) {
if ((input & INPUT_MOVE_LEFT))
jump(-1);
else if ((input & INPUT_MOVE_RIGHT))
jump(1);
else
jump(0);
}
if (input & INPUT_MOVE_DIG) {
// Should create Projectile which destroys ground, but also should be destroyed then,
// but it doesn't.
// But this now just segfaults
// world.addObject(new Projectile(state, position, true));
handleDig(position, 15);
}
if (input & INPUT_SHOOT) {
this->shoot();
}
// Player facing
if (fx < 0) setFacing(false);
else if (fx > 0) setFacing(true);
this->changeAim(da); // Move crosshair
// Apply force
applyForce(Vector(fx, 0));
}
void Player::handleDig (Vector position, float radius) {
world.removeGround(position, radius);
}
void Player::debugInfo (void) {
Engine::log(DEBUG, "Player.debugInfo") << "In air: " << this->inAir;
}