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"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <assert.h>
|
|
|
|
|
2012-10-13 17:36:33 +00:00
|
|
|
#include <Thor/Vectors.hpp>
|
|
|
|
|
2012-12-20 09:31:32 +00:00
|
|
|
#include "../sprites/Corpse.h"
|
2012-10-13 17:36:33 +00:00
|
|
|
#include "../util/Log.h"
|
2012-10-04 17:24:46 +00:00
|
|
|
|
2012-12-22 14:10:26 +00:00
|
|
|
const std::string Character::KEY_HEALTH = "health";
|
2012-12-20 13:59:05 +00:00
|
|
|
const int Character::DEFAULT_HEALTH = 100;
|
2012-12-22 14:10:26 +00:00
|
|
|
const std::string Character::KEY_SPEED = "speed";
|
2012-12-20 13:59:05 +00:00
|
|
|
const float Character::DEFAULT_SPEED = 100;
|
2012-10-13 17:36:33 +00:00
|
|
|
const float Character::POINT_REACHED_DISTANCE = 1.0f;
|
2012-12-23 23:49:49 +00:00
|
|
|
const std::string Character::KEY_WEAPON = "weapon";
|
|
|
|
const std::string Character::DEFAULT_WEAPON = "weapon.yaml";
|
2012-10-04 17:24:46 +00:00
|
|
|
std::vector<Character*> Character::mCharacterInstances = std::vector<Character*>();
|
2012-10-01 09:02:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Saves pointer to this instance in static var for think().
|
|
|
|
*/
|
2012-12-22 13:56:17 +00:00
|
|
|
Character::Character(World& world, Pathfinder& pathfinder,
|
2012-12-22 12:44:17 +00:00
|
|
|
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),
|
2012-12-22 13:56:17 +00:00
|
|
|
mPathfinder(pathfinder),
|
2012-12-20 13:59:05 +00:00
|
|
|
mMaxHealth(config.get(KEY_HEALTH, DEFAULT_HEALTH)),
|
2012-10-12 17:34:07 +00:00
|
|
|
mCurrentHealth(mMaxHealth),
|
2012-12-20 13:59:05 +00:00
|
|
|
mMovementSpeed(config.get(KEY_SPEED, DEFAULT_SPEED)),
|
2012-12-23 23:49:49 +00:00
|
|
|
mWeapon(world, *this, Yaml(config.get(KEY_WEAPON, DEFAULT_WEAPON))),
|
2012-10-13 17:36:33 +00:00
|
|
|
mStartPathfinding(false) {
|
2012-10-04 17:24:46 +00:00
|
|
|
mCharacterInstances.push_back(this);
|
2012-10-01 09:02:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-12-22 00:44:36 +00:00
|
|
|
* Deletes pointer from static variable mCharacterInstances, inserts body into world.
|
2012-10-01 09:02:44 +00:00
|
|
|
*/
|
|
|
|
Character::~Character() {
|
2012-10-04 17:24:46 +00:00
|
|
|
auto it = std::find(mCharacterInstances.begin(), mCharacterInstances.end(), this);
|
|
|
|
assert(it != mCharacterInstances.end());
|
|
|
|
mCharacterInstances.erase(it);
|
|
|
|
|
2012-12-22 13:56:17 +00:00
|
|
|
mWorld.insert(std::shared_ptr<Sprite>(new Corpse(getPosition(), Yaml("body.yaml"))));
|
2012-10-01 09:02:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calls onThink on all Actor instances.
|
|
|
|
*
|
|
|
|
* @param elapsedTime Amount of time to simulate.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Character::think(float elapsedTime) {
|
2012-10-04 17:24:46 +00:00
|
|
|
for (auto i : mCharacterInstances) {
|
2012-12-24 00:14:22 +00:00
|
|
|
i->mWeapon.think();
|
2012-10-01 09:02:44 +00:00
|
|
|
i->onThink(elapsedTime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
/**
|
|
|
|
* Implement this function for any (regular) AI computations. Default implementation does nothing.
|
|
|
|
*
|
|
|
|
* @param elapsedTime Amount of time to simulate.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Character::onThink(float elapsedTime) {
|
|
|
|
}
|
|
|
|
|
2012-10-01 09:02:44 +00:00
|
|
|
/**
|
2012-10-04 17:30:50 +00:00
|
|
|
* Called when health reaches zero. Default immplementation does nothing.
|
2012-10-01 09:02:44 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
Character::onDeath() {
|
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() {
|
|
|
|
mWeapon.pullTrigger();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Release the trigger on the attached weapon.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Character::releaseTrigger() {
|
|
|
|
mWeapon.releaseTrigger();
|
2012-10-13 16:57:12 +00:00
|
|
|
}
|
2012-10-13 17:36:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a destination to be walked to. Call move() for the actual movement.
|
|
|
|
*
|
|
|
|
* @param destination An absolute point to move towards.
|
|
|
|
* @return True if a path was found.
|
|
|
|
*/
|
|
|
|
bool
|
2012-12-22 14:10:26 +00:00
|
|
|
Character::setDestination(const sf::Vector2f& destination) {
|
2012-12-22 00:44:36 +00:00
|
|
|
mPath = mPathfinder.getPath(*this, destination);
|
2012-10-13 17:36:33 +00:00
|
|
|
// Make sure we found a path.
|
|
|
|
if (mPath.empty()) {
|
|
|
|
LOG_I("No path found to destination.");
|
|
|
|
} else {
|
|
|
|
mStartPathfinding = true;
|
|
|
|
}
|
|
|
|
return mStartPathfinding;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Move towards a destination. Call setDestination() for setting the destination.
|
|
|
|
* This should be called from think().
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Character::move() {
|
|
|
|
if (!mPath.empty()) {
|
|
|
|
if (mStartPathfinding) {
|
|
|
|
setSpeed(*mPath.end() - getPosition(), mMovementSpeed);
|
|
|
|
}
|
|
|
|
if (thor::length(*mPath.end() - getPosition()) < POINT_REACHED_DISTANCE) {
|
|
|
|
// Reached a path node.
|
|
|
|
mPath.pop_back();
|
|
|
|
if (!mPath.empty()) {
|
|
|
|
// Move to next path node.
|
|
|
|
setSpeed(*mPath.end() - getPosition(), mMovementSpeed);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
LOG_I("Reached destination.");
|
2012-12-22 14:10:26 +00:00
|
|
|
setSpeed(sf::Vector2f(), 0);
|
2012-10-13 17:36:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|