2012-10-01 09:02:44 +00:00
|
|
|
/*
|
|
|
|
* Actor.cpp
|
|
|
|
*
|
|
|
|
* Created on: 02.09.2012
|
|
|
|
* Author: Felix
|
2012-09-12 12:21:57 +00:00
|
|
|
*/
|
2012-10-01 09:02:44 +00:00
|
|
|
|
|
|
|
#include "Character.h"
|
|
|
|
|
2012-10-13 17:36:33 +00:00
|
|
|
#include <Thor/Vectors.hpp>
|
|
|
|
|
2013-03-29 16:59:35 +00:00
|
|
|
#include "../items/Weapon.h"
|
2012-12-20 09:31:32 +00:00
|
|
|
#include "../sprites/Corpse.h"
|
2013-04-05 14:54:51 +00:00
|
|
|
#include "../sprites/TileManager.h"
|
2012-10-13 17:36:33 +00:00
|
|
|
#include "../util/Log.h"
|
2013-03-29 16:59:35 +00:00
|
|
|
#include "../util/Yaml.h"
|
2013-03-29 17:34:51 +00:00
|
|
|
#include "../World.h"
|
2013-04-28 16:11:39 +00:00
|
|
|
#include "../Pathfinder.h"
|
2012-10-04 17:24:46 +00:00
|
|
|
|
2013-04-05 14:54:51 +00:00
|
|
|
const float Character::VISION_DISTANCE = 500.0f;
|
2012-10-01 09:02:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Saves pointer to this instance in static var for think().
|
|
|
|
*/
|
2013-04-28 16:11:39 +00:00
|
|
|
Character::Character(World& world, TileManager& tileManager, Pathfinder& pathfinder,
|
|
|
|
const Data& data, const Yaml& config) :
|
2012-12-23 14:50:49 +00:00
|
|
|
Sprite(data, config),
|
2012-12-22 00:44:36 +00:00
|
|
|
mWorld(world),
|
2013-04-05 14:54:51 +00:00
|
|
|
mTileManager(tileManager),
|
2013-04-28 16:11:39 +00:00
|
|
|
mPathfinder(pathfinder),
|
2013-04-27 16:54:04 +00:00
|
|
|
mMaxHealth(config.get(YAML_KEY::HEALTH, YAML_DEFAULT::HEALTH)),
|
2012-10-12 17:34:07 +00:00
|
|
|
mCurrentHealth(mMaxHealth),
|
2013-04-27 16:54:04 +00:00
|
|
|
mMovementSpeed(config.get(YAML_KEY::SPEED, YAML_DEFAULT::SPEED)),
|
2013-04-05 14:54:51 +00:00
|
|
|
mWeapon(new Weapon(world, *this,
|
2013-04-27 16:54:04 +00:00
|
|
|
Yaml(config.get(YAML_KEY::WEAPON, YAML_DEFAULT::WEAPON)))),
|
2013-04-28 16:11:39 +00:00
|
|
|
mLastPosition(getPosition()) {
|
2012-10-01 09:02:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Character::~Character() {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-10-04 17:30:50 +00:00
|
|
|
* Subtracts health from Actor. Calls onDeath() when health reaches zero and marks
|
|
|
|
* object for deletion.
|
2012-10-01 09:02:44 +00:00
|
|
|
*
|
|
|
|
* @param damage Amount of health to subtract.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Character::onDamage(int damage) {
|
|
|
|
mCurrentHealth -= damage;
|
|
|
|
if (mCurrentHealth <= 0) {
|
|
|
|
mCurrentHealth = 0;
|
|
|
|
onDeath();
|
2012-10-04 17:30:50 +00:00
|
|
|
setDelete(true);
|
2012-10-01 09:02:44 +00:00
|
|
|
}
|
2012-09-12 12:21:57 +00:00
|
|
|
}
|
2012-10-01 09:02:44 +00:00
|
|
|
|
2012-10-04 17:36:05 +00:00
|
|
|
/**
|
2013-04-25 19:57:37 +00:00
|
|
|
* Implement this function for any (periodical) AI computations.
|
2013-03-09 15:25:04 +00:00
|
|
|
* If overwritten, this function should always be called from the overwriting function.
|
2012-10-04 17:36:05 +00:00
|
|
|
*
|
2013-03-09 15:25:04 +00:00
|
|
|
* @param elapsed Amount of time to simulate.
|
2012-10-04 17:36:05 +00:00
|
|
|
*/
|
|
|
|
void
|
2013-03-09 15:25:04 +00:00
|
|
|
Character::onThink(int elapsed) {
|
2013-03-29 16:59:35 +00:00
|
|
|
mWeapon->onThink(elapsed);
|
2013-04-25 19:57:37 +00:00
|
|
|
move();
|
2012-10-04 17:36:05 +00:00
|
|
|
}
|
|
|
|
|
2012-10-01 09:02:44 +00:00
|
|
|
/**
|
2013-03-03 20:33:01 +00:00
|
|
|
* Called when health reaches zero. Default implementation drops a corpse at
|
|
|
|
* the current position.
|
2012-10-01 09:02:44 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
Character::onDeath() {
|
2013-03-03 20:33:01 +00:00
|
|
|
mWorld.insert(std::shared_ptr<Sprite>(new Corpse(getPosition(), Yaml("body.yaml"))));
|
2012-09-12 12:21:57 +00:00
|
|
|
}
|
2012-10-13 16:50:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the default movement speed (walking) of the character.
|
|
|
|
*/
|
|
|
|
float
|
|
|
|
Character::getMovementSpeed() const {
|
|
|
|
return mMovementSpeed;
|
|
|
|
}
|
2012-10-13 16:57:12 +00:00
|
|
|
|
|
|
|
/**
|
2012-12-24 00:14:22 +00:00
|
|
|
* Pull the trigger on the attached weapon.
|
2012-10-13 16:57:12 +00:00
|
|
|
*/
|
|
|
|
void
|
2012-12-24 00:14:22 +00:00
|
|
|
Character::pullTrigger() {
|
2013-03-29 16:59:35 +00:00
|
|
|
mWeapon->pullTrigger();
|
2012-12-24 00:14:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Release the trigger on the attached weapon.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Character::releaseTrigger() {
|
2013-03-29 16:59:35 +00:00
|
|
|
mWeapon->releaseTrigger();
|
2012-10-13 16:57:12 +00:00
|
|
|
}
|
2012-10-13 17:36:33 +00:00
|
|
|
|
|
|
|
/**
|
2013-04-18 10:45:37 +00:00
|
|
|
* Set a destination to be walked to. Call move() to actually
|
|
|
|
* perform the movement.
|
2012-10-13 17:36:33 +00:00
|
|
|
*
|
2013-04-18 10:45:37 +00:00
|
|
|
* @param destination An absolute point to move towards. Set to current
|
|
|
|
* position to stop movement.
|
2012-10-13 17:36:33 +00:00
|
|
|
* @return True if a path was found.
|
|
|
|
*/
|
|
|
|
bool
|
2012-12-22 14:10:26 +00:00
|
|
|
Character::setDestination(const sf::Vector2f& destination) {
|
2013-04-18 10:45:37 +00:00
|
|
|
if (destination == getPosition()) {
|
|
|
|
mPath.clear();
|
|
|
|
return true;
|
|
|
|
}
|
2013-04-28 16:11:39 +00:00
|
|
|
mPath = mPathfinder.getPath(getPosition(), destination, getRadius());
|
2013-04-25 19:57:37 +00:00
|
|
|
if (!mPath.empty())
|
2013-03-25 15:58:13 +00:00
|
|
|
setSpeed(mPath.back() - getPosition(), mMovementSpeed);
|
2013-04-27 11:49:11 +00:00
|
|
|
else {
|
|
|
|
setSpeed(sf::Vector2f(), 0);
|
2013-03-25 15:58:13 +00:00
|
|
|
LOG_W("No path found to destination.");
|
2013-04-27 11:49:11 +00:00
|
|
|
}
|
2013-03-25 15:58:13 +00:00
|
|
|
return !mPath.empty();
|
2012-10-13 17:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Move towards a destination. Call setDestination() for setting the destination.
|
2013-04-25 19:57:37 +00:00
|
|
|
* This is automatically called from onThink().
|
2012-10-13 17:36:33 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
Character::move() {
|
2013-04-27 11:49:11 +00:00
|
|
|
sf::Vector2f distanceTraveled = mLastPosition - getPosition();
|
|
|
|
mLastPosition = getPosition();
|
2012-10-13 17:36:33 +00:00
|
|
|
if (!mPath.empty()) {
|
2013-04-27 11:49:11 +00:00
|
|
|
// Point reached (during next time step).
|
|
|
|
if (thor::length(mPath.back() - getPosition()) < thor::length(distanceTraveled)) {
|
2012-10-13 17:36:33 +00:00
|
|
|
mPath.pop_back();
|
2013-03-25 15:58:13 +00:00
|
|
|
(!mPath.empty())
|
|
|
|
? setSpeed(mPath.back() - getPosition(), mMovementSpeed)
|
|
|
|
: setSpeed(sf::Vector2f(), 0);
|
2012-10-13 17:36:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-03-29 17:48:49 +00:00
|
|
|
|
2013-04-05 14:54:51 +00:00
|
|
|
/**
|
|
|
|
* Returns true if the character is currently moving.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
Character::isMoving() const {
|
|
|
|
return !mPath.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests if a target is visible from the current position.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
Character::isVisible(const sf::Vector2f& target) const {
|
2013-04-29 14:10:22 +00:00
|
|
|
return mWorld.raycast(getPosition(), target);
|
2013-04-05 14:54:51 +00:00
|
|
|
}
|
|
|
|
|
2013-03-29 17:48:49 +00:00
|
|
|
/**
|
|
|
|
* Calls World::getCharacters with current position.
|
|
|
|
*/
|
|
|
|
std::vector<std::shared_ptr<Character> >
|
2013-04-05 14:54:51 +00:00
|
|
|
Character::getCharacters() const {
|
|
|
|
return mWorld.getCharacters(getPosition(), VISION_DISTANCE);
|
2013-03-29 17:48:49 +00:00
|
|
|
}
|