2012-10-14 16:14:06 +00:00
|
|
|
/*
|
|
|
|
* Player.cpp
|
|
|
|
*
|
|
|
|
* Created on: 21.07.2012
|
|
|
|
* Author: Felix
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Player.h"
|
|
|
|
|
|
|
|
#include <Thor/Vectors.hpp>
|
|
|
|
|
2013-05-02 11:31:26 +00:00
|
|
|
#include "../util/Yaml.h"
|
|
|
|
|
2012-10-14 16:14:06 +00:00
|
|
|
/**
|
|
|
|
* Initializes Sprite.
|
|
|
|
*/
|
2013-04-29 14:49:16 +00:00
|
|
|
Player::Player(World& world, Pathfinder& pathfinder,
|
2013-08-07 15:39:43 +00:00
|
|
|
const Vector2f& position) :
|
2013-06-25 15:50:23 +00:00
|
|
|
Character(position, CATEGORY_ACTOR, MASK_ALL, Yaml("player.yaml"), world,
|
2013-05-07 22:00:05 +00:00
|
|
|
pathfinder),
|
2013-03-30 01:36:06 +00:00
|
|
|
mDirection(0) {
|
2012-10-14 16:14:06 +00:00
|
|
|
}
|
|
|
|
|
2013-08-18 11:38:28 +00:00
|
|
|
Vector2f
|
|
|
|
Player::getCrosshairPosition() const {
|
|
|
|
return mCrosshairPosition;
|
|
|
|
}
|
|
|
|
|
2012-10-14 16:14:06 +00:00
|
|
|
/**
|
|
|
|
* Sets the point where to look and shoot at.
|
|
|
|
*
|
|
|
|
* @param Absolute world coordinates of the crosshair.
|
|
|
|
*/
|
|
|
|
void
|
2013-08-07 15:39:43 +00:00
|
|
|
Player::setCrosshairPosition(const Vector2f& position) {
|
2012-10-14 16:14:06 +00:00
|
|
|
mCrosshairPosition = position - getPosition();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the movement direction. This is destined for input via keys (eg. WASD).
|
|
|
|
* Disables any previous commands via Player::move().
|
|
|
|
*
|
|
|
|
* @param direction The direction to move to.
|
|
|
|
* @param unset False to start movement into the direction, true to stop it.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Player::setDirection(Direction direction, bool unset) {
|
2013-04-04 21:13:08 +00:00
|
|
|
if (unset)
|
2012-12-22 00:14:30 +00:00
|
|
|
mDirection = mDirection & ~direction;
|
2013-04-04 21:13:08 +00:00
|
|
|
else
|
2012-12-22 00:14:30 +00:00
|
|
|
mDirection = mDirection | direction;
|
2013-04-04 21:13:08 +00:00
|
|
|
|
2012-10-14 16:14:06 +00:00
|
|
|
// Convert directions into a vector.
|
2013-08-07 15:39:43 +00:00
|
|
|
Vector2f dirVec(0, 0);
|
2013-04-04 21:54:17 +00:00
|
|
|
if (mDirection & Direction::RIGHT)
|
2012-10-14 16:14:06 +00:00
|
|
|
dirVec.x += 1.0f;
|
2013-04-04 21:13:08 +00:00
|
|
|
if (mDirection & Direction::LEFT)
|
2012-10-14 16:14:06 +00:00
|
|
|
dirVec.x += - 1.0f;
|
2013-04-04 21:13:08 +00:00
|
|
|
if (mDirection & Direction::DOWN)
|
2012-10-14 16:14:06 +00:00
|
|
|
dirVec.y += 1.0f;
|
2013-04-04 21:13:08 +00:00
|
|
|
if (mDirection & Direction::UP)
|
2012-10-14 16:14:06 +00:00
|
|
|
dirVec.y += - 1.0f;
|
|
|
|
setSpeed(dirVec, getMovementSpeed());
|
|
|
|
}
|
|
|
|
|
2013-04-18 10:45:37 +00:00
|
|
|
void
|
2013-08-07 15:39:43 +00:00
|
|
|
Player::setDestination(const Vector2f& destination) {
|
2013-04-18 10:45:37 +00:00
|
|
|
mDirection = 0;
|
|
|
|
Character::setDestination(destination);
|
|
|
|
}
|
|
|
|
|
2012-10-14 16:14:06 +00:00
|
|
|
/**
|
|
|
|
* Check if we arrived at destination, turn towards cursor.
|
|
|
|
*/
|
|
|
|
void
|
2013-03-09 15:25:04 +00:00
|
|
|
Player::onThink(int elapsed) {
|
|
|
|
Character::onThink(elapsed);
|
2013-03-30 01:30:11 +00:00
|
|
|
Sprite::setDirection(mCrosshairPosition);
|
2012-10-14 16:14:06 +00:00
|
|
|
}
|