2012-10-14 16:14:06 +00:00
|
|
|
/*
|
|
|
|
* Player.cpp
|
|
|
|
*
|
|
|
|
* Created on: 21.07.2012
|
|
|
|
* Author: Felix
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Player.h"
|
|
|
|
|
2012-12-22 14:10:26 +00:00
|
|
|
#include <string>
|
|
|
|
|
2012-10-14 16:14:06 +00:00
|
|
|
#include <Thor/Vectors.hpp>
|
|
|
|
|
|
|
|
#include "../items/Weapon.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes Sprite.
|
|
|
|
*/
|
2012-12-22 13:56:17 +00:00
|
|
|
Player::Player(World& world, Pathfinder& pathfinder,
|
2012-12-22 14:10:26 +00:00
|
|
|
const sf::Vector2f& position, const Yaml& config) :
|
2012-12-22 13:56:17 +00:00
|
|
|
Character(world, pathfinder,
|
|
|
|
Data(position, 0, CATEGORY_ACTOR, MASK_ALL),
|
2012-12-22 00:44:36 +00:00
|
|
|
config),
|
2012-10-14 16:14:06 +00:00
|
|
|
mDirection(0) {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the point where to look and shoot at.
|
|
|
|
*
|
|
|
|
* @param Absolute world coordinates of the crosshair.
|
|
|
|
*/
|
|
|
|
void
|
2012-12-22 14:10:26 +00:00
|
|
|
Player::setCrosshairPosition(const sf::Vector2f& position) {
|
2012-10-14 16:14:06 +00:00
|
|
|
mCrosshairPosition = position - getPosition();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fires the attached Weapon, emitting a Bullet object.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Player::fire() {
|
|
|
|
Character::fire();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Moves the player to a destination point.
|
|
|
|
* Disables any previous calls to Player::setDirection().
|
|
|
|
*
|
|
|
|
* @param destination Absolute world coordinate of the destination point.
|
2012-09-12 12:21:57 +00:00
|
|
|
*/
|
2012-09-09 20:50:15 +00:00
|
|
|
void
|
2012-12-22 14:10:26 +00:00
|
|
|
Player::move(const sf::Vector2f& destination) {
|
2012-10-13 17:36:33 +00:00
|
|
|
setDestination(destination);
|
2012-10-14 16:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
|
|
|
if (unset) {
|
2012-12-22 00:14:30 +00:00
|
|
|
mDirection = mDirection & ~direction;
|
2012-10-14 16:14:06 +00:00
|
|
|
} else {
|
2012-12-22 00:14:30 +00:00
|
|
|
mDirection = mDirection | direction;
|
2012-10-14 16:14:06 +00:00
|
|
|
}
|
|
|
|
// Convert directions into a vector.
|
2012-12-22 14:10:26 +00:00
|
|
|
sf::Vector2f dirVec(0, 0);
|
2012-12-22 00:14:30 +00:00
|
|
|
if (mDirection & Direction::RIGHT) {
|
2012-10-14 16:14:06 +00:00
|
|
|
dirVec.x += 1.0f;
|
|
|
|
}
|
2012-12-22 00:14:30 +00:00
|
|
|
if (mDirection & Direction::LEFT) {
|
2012-10-14 16:14:06 +00:00
|
|
|
dirVec.x += - 1.0f;
|
|
|
|
}
|
2012-12-22 00:14:30 +00:00
|
|
|
if (mDirection & Direction::DOWN) {
|
2012-10-14 16:14:06 +00:00
|
|
|
dirVec.y += 1.0f;
|
|
|
|
}
|
2012-12-22 00:14:30 +00:00
|
|
|
if (mDirection & Direction::UP) {
|
2012-10-14 16:14:06 +00:00
|
|
|
dirVec.y += - 1.0f;
|
|
|
|
}
|
|
|
|
setSpeed(dirVec, getMovementSpeed());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if we arrived at destination, turn towards cursor.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Player::onThink(float elapsedTime) {
|
|
|
|
if (!mDirection) {
|
|
|
|
// Only use path finding movement if no direct input movement active.
|
|
|
|
Character::move();
|
|
|
|
}
|
|
|
|
// Look towards crosshair.
|
2012-12-22 14:10:26 +00:00
|
|
|
if (mCrosshairPosition != sf::Vector2f()) {
|
2012-12-22 12:44:17 +00:00
|
|
|
setAngle(thor::polarAngle(mCrosshairPosition) + 90);
|
|
|
|
}
|
2012-10-14 16:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop movement if we collide with anything except bullets.
|
|
|
|
*/
|
|
|
|
void
|
2012-12-23 14:50:49 +00:00
|
|
|
Player::onCollide(Sprite& other) {
|
2012-12-22 15:52:43 +00:00
|
|
|
if (other.getCategory() != CATEGORY_PARTICLE) {
|
2012-10-14 16:14:06 +00:00
|
|
|
setDestination(getPosition());
|
|
|
|
}
|
|
|
|
}
|