This repository has been archived on 2019-12-07. You can view files and clone it, but cannot push or open issues or pull requests.
dungeon-gunner/source/abstract/Character.cpp

266 lines
5.5 KiB
C++
Raw Normal View History

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 <Thor/Vectors.hpp>
#include "../items/Weapon.h"
#include "../sprites/Corpse.h"
#include "../util/Log.h"
#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"
const float Character::VISION_DISTANCE = 500.0f;
const float Character::POINT_REACHED_DISTANCE = 1.0f;
2012-10-01 09:02:44 +00:00
/**
* Saves pointer to this instance in static var for think().
*/
Character::Character(const sf::Vector2f& position, Category category,
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),
mMaxHealth(config.get("health", 100)),
mCurrentHealth(mMaxHealth),
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),
mLastPosition(getPosition()),
mFaction((Faction) config.get("faction", 1)) {
2012-10-01 09:02:44 +00:00
}
Character::~Character() {
}
/**
* 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();
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
/**
* 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.
*
2013-03-09 15:25:04 +00:00
* @param elapsed Amount of time to simulate.
*/
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);
move();
}
/**
* 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() {
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
}
/**
* 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
}
/**
2013-04-18 10:45:37 +00:00
* Set a destination to be walked to. Call move() to actually
* perform the movement.
*
2013-04-18 10:45:37 +00:00
* @param destination An absolute point to move towards. Set to current
* position to stop movement.
* @return True if a path was found.
*/
bool
2012-12-22 14:10:26 +00:00
Character::setDestination(const sf::Vector2f& destination) {
2013-04-28 16:11:39 +00:00
mPath = mPathfinder.getPath(getPosition(), destination, getRadius());
return !mPath.empty();
}
/**
* Move towards a destination. Call setDestination() for setting the destination.
* This is automatically called from onThink().
*/
void
Character::move() {
if (mPath.empty())
return;
mLastPosition = getPosition();
setSpeed(mPath.back() - getPosition(), mMovementSpeed);
setDirection(mPath.back() - getPosition());
if (thor::length(mPath.back() - getPosition()) < POINT_REACHED_DISTANCE) {
mPath.pop_back();
if (mPath.empty())
setSpeed(sf::Vector2f(), 0);
}
}
/**
* 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 {
return mWorld.raycast(getPosition(), target);
}
/**
* Calls World::getCharacters with current position.
*/
std::vector<std::shared_ptr<Character> >
Character::getCharacters() const {
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;
}
int
Character::getMagazineAmmo() const {
2013-06-25 20:00:40 +00:00
return mActiveWeapon->getMagazineAmmo();
}
int
Character::getTotalAmmo() const {
2013-06-25 20:00:40 +00:00
return mActiveWeapon->getTotalAmmo();
}
2013-07-06 11:38:15 +00:00
std::string
Character::getWeaponName() const {
return mActiveWeapon->getName();
}
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-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-12 22:01:57 +00:00
void
Character::swapGadgets() {
std::swap(mLeftGadget, mRightGadget);
2013-07-06 12:37:16 +00:00
}