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"
|
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;
|
2013-06-21 16:00:38 +00:00
|
|
|
const float Character::POINT_REACHED_DISTANCE = 1.0f;
|
2013-07-21 08:52:50 +00:00
|
|
|
const float Character::ITEM_PICKUP_MAX_DISTANCE_SQUARED = 2500.0f;
|
2012-10-01 09:02:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Saves pointer to this instance in static var for think().
|
|
|
|
*/
|
2013-08-07 15:39:43 +00:00
|
|
|
Character::Character(const Vector2f& position, Category category,
|
2013-05-07 22:00:05 +00:00
|
|
|
unsigned short mask, const Yaml& config, World& world,
|
|
|
|
Pathfinder& pathfinder) :
|
|
|
|
Circle(position, category, mask, config),
|
2012-12-22 00:44:36 +00:00
|
|
|
mWorld(world),
|
2013-04-28 16:11:39 +00:00
|
|
|
mPathfinder(pathfinder),
|
2013-06-25 15:50:23 +00:00
|
|
|
mMaxHealth(config.get("health", 100)),
|
2012-10-12 17:34:07 +00:00
|
|
|
mCurrentHealth(mMaxHealth),
|
2013-06-25 15:50:23 +00:00
|
|
|
mMovementSpeed(config.get("speed", 0.0f)),
|
2013-06-25 20:00:40 +00:00
|
|
|
mFirstWeapon(new Weapon(world, *this,
|
|
|
|
Yaml(config.get("first_weapon", std::string())))),
|
|
|
|
mSecondWeapon(new Weapon(world, *this,
|
|
|
|
Yaml(config.get("second_weapon", std::string())))),
|
|
|
|
mActiveWeapon(mFirstWeapon),
|
2013-05-08 17:49:04 +00:00
|
|
|
mLastPosition(getPosition()),
|
2013-06-25 15:50:23 +00:00
|
|
|
mFaction((Faction) config.get("faction", 1)) {
|
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;
|
2013-07-06 19:21:25 +00:00
|
|
|
|
|
|
|
if (mCurrentHealth > mMaxHealth)
|
|
|
|
mCurrentHealth = mMaxHealth;
|
|
|
|
|
2012-10-01 09:02:44 +00:00
|
|
|
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-06-25 20:00:40 +00:00
|
|
|
mActiveWeapon->onThink(elapsed);
|
2013-07-06 19:21:25 +00:00
|
|
|
if (mLeftGadget)
|
|
|
|
mLeftGadget->onThink(elapsed);
|
|
|
|
if (mRightGadget)
|
|
|
|
mRightGadget->onThink(elapsed);
|
2013-04-25 19:57:37 +00:00
|
|
|
move();
|
2012-10-04 17:36:05 +00:00
|
|
|
}
|
|
|
|
|
2013-05-08 17:49:04 +00:00
|
|
|
/**
|
|
|
|
* Returns the faction this character belongs to.
|
|
|
|
*/
|
|
|
|
Character::Faction
|
|
|
|
Character::getFaction() const {
|
|
|
|
return mFaction;
|
|
|
|
}
|
|
|
|
|
2012-10-01 09:02:44 +00:00
|
|
|
/**
|
2013-07-14 19:56:44 +00:00
|
|
|
* Called when health reaches zero. Drops corpse and item.
|
2012-10-01 09:02:44 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
Character::onDeath() {
|
2013-05-02 11:31:26 +00:00
|
|
|
mWorld.insert(std::shared_ptr<Sprite>(new Corpse(getPosition())));
|
2013-07-14 19:56:44 +00:00
|
|
|
|
|
|
|
mWorld.insert(mActiveWeapon);
|
|
|
|
mActiveWeapon->drop(getPosition());
|
|
|
|
|
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-06-25 20:00:40 +00:00
|
|
|
mActiveWeapon->pullTrigger();
|
2012-12-24 00:14:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Release the trigger on the attached weapon.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Character::releaseTrigger() {
|
2013-06-25 20:00:40 +00:00
|
|
|
mActiveWeapon->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
|
2013-08-07 15:39:43 +00:00
|
|
|
Character::setDestination(const Vector2f& destination) {
|
2013-04-28 16:11:39 +00:00
|
|
|
mPath = mPathfinder.getPath(getPosition(), destination, getRadius());
|
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-06-21 16:00:38 +00:00
|
|
|
if (mPath.empty())
|
|
|
|
return;
|
2013-04-27 11:49:11 +00:00
|
|
|
mLastPosition = getPosition();
|
2013-06-21 16:00:38 +00:00
|
|
|
setSpeed(mPath.back() - getPosition(), mMovementSpeed);
|
|
|
|
setDirection(mPath.back() - getPosition());
|
|
|
|
if (thor::length(mPath.back() - getPosition()) < POINT_REACHED_DISTANCE) {
|
|
|
|
mPath.pop_back();
|
|
|
|
if (mPath.empty())
|
2013-08-07 15:39:43 +00:00
|
|
|
setSpeed(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
|
2013-08-07 15:39:43 +00:00
|
|
|
Character::isVisible(const 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 {
|
2013-05-08 17:49:04 +00:00
|
|
|
auto characters = mWorld.getCharacters(getPosition(), VISION_DISTANCE);
|
|
|
|
characters.erase(std::remove_if(characters.begin(), characters.end(),
|
|
|
|
[this](const std::shared_ptr<Character>& c)
|
|
|
|
{return c->getFaction() == getFaction();}), characters.end());
|
|
|
|
return characters;
|
2013-03-29 17:48:49 +00:00
|
|
|
}
|
2013-06-25 15:44:40 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
Character::getMagazineAmmo() const {
|
2013-06-25 20:00:40 +00:00
|
|
|
return mActiveWeapon->getMagazineAmmo();
|
2013-06-25 15:44:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
Character::getTotalAmmo() const {
|
2013-06-25 20:00:40 +00:00
|
|
|
return mActiveWeapon->getTotalAmmo();
|
2013-06-25 15:44:40 +00:00
|
|
|
}
|
|
|
|
|
2013-07-06 11:38:15 +00:00
|
|
|
std::string
|
|
|
|
Character::getWeaponName() const {
|
|
|
|
return mActiveWeapon->getName();
|
|
|
|
}
|
|
|
|
|
2013-06-25 15:44:40 +00:00
|
|
|
void
|
|
|
|
Character::reload() {
|
2013-06-25 20:00:40 +00:00
|
|
|
mActiveWeapon->reload();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Character::toggleWeapon() {
|
|
|
|
mActiveWeapon->cancelReload();
|
|
|
|
mActiveWeapon = (mActiveWeapon == mFirstWeapon)
|
|
|
|
? mSecondWeapon
|
|
|
|
: mFirstWeapon;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Character::selectFirstWeapon() {
|
|
|
|
mActiveWeapon->cancelReload();
|
|
|
|
mActiveWeapon = mFirstWeapon;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Character::selectSecondWeapon() {
|
|
|
|
mActiveWeapon->cancelReload();
|
|
|
|
mActiveWeapon = mSecondWeapon;
|
2013-06-25 15:44:40 +00:00
|
|
|
}
|
2013-07-06 19:21:25 +00:00
|
|
|
|
2013-07-12 22:01:57 +00:00
|
|
|
int
|
|
|
|
Character::getHealth() const {
|
|
|
|
return mCurrentHealth;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-06 19:21:25 +00:00
|
|
|
void
|
|
|
|
Character::setLeftGadget(std::shared_ptr<Gadget> gadget) {
|
|
|
|
mLeftGadget = gadget;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Character::setRightGadget(std::shared_ptr<Gadget> gadget) {
|
|
|
|
mRightGadget = gadget;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Character::useLeftGadget() {
|
|
|
|
mLeftGadget->use(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Character::useRightGadget() {
|
|
|
|
mRightGadget->use(*this);
|
|
|
|
}
|
|
|
|
|
2013-07-10 21:41:24 +00:00
|
|
|
std::string
|
|
|
|
Character::getLeftGadgetName() const {
|
|
|
|
return mLeftGadget->getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
Character::getRightGadgetName() const {
|
|
|
|
return mRightGadget->getName();
|
|
|
|
}
|
|
|
|
|
2013-07-21 08:52:50 +00:00
|
|
|
/**
|
|
|
|
* Picks up the nearest weapon or gadget, and drops the active weapon (or right gadget).
|
|
|
|
*
|
|
|
|
* If no item is nearby, gadgets are swapped instead.
|
|
|
|
*/
|
2013-07-12 22:01:57 +00:00
|
|
|
void
|
2013-07-21 08:52:50 +00:00
|
|
|
Character::pickUpItem() {
|
|
|
|
std::shared_ptr<Item> item = mWorld.getNearestItem(getPosition());
|
|
|
|
if (item != nullptr && thor::squaredLength(item->getPosition() - getPosition()) <=
|
|
|
|
ITEM_PICKUP_MAX_DISTANCE_SQUARED) {
|
|
|
|
std::shared_ptr<Weapon> weapon = std::dynamic_pointer_cast<Weapon>(item);
|
|
|
|
if (weapon != nullptr) {
|
|
|
|
mWorld.insert(mActiveWeapon);
|
|
|
|
mActiveWeapon->drop(getPosition());
|
|
|
|
mActiveWeapon = weapon;
|
|
|
|
mActiveWeapon->setHolder(*this);
|
|
|
|
}
|
|
|
|
std::shared_ptr<Gadget> gadget = std::dynamic_pointer_cast<Gadget>(item);
|
|
|
|
if (gadget != nullptr) {
|
|
|
|
mWorld.insert(mRightGadget);
|
|
|
|
mRightGadget->drop(getPosition());
|
|
|
|
mRightGadget = mLeftGadget;
|
|
|
|
mLeftGadget = gadget;
|
|
|
|
}
|
|
|
|
mWorld.remove(item);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
std::swap(mLeftGadget, mRightGadget);
|
|
|
|
}
|
2013-07-06 12:37:16 +00:00
|
|
|
}
|