2012-10-14 16:14:06 +00:00
|
|
|
/*
|
|
|
|
* Enemy.cpp
|
|
|
|
*
|
|
|
|
* Created on: 10.09.2012
|
|
|
|
* Author: Felix
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Enemy.h"
|
|
|
|
|
2013-04-05 14:54:51 +00:00
|
|
|
#include <Thor/Vectors.hpp>
|
|
|
|
|
2013-05-02 11:31:26 +00:00
|
|
|
#include "../util/Yaml.h"
|
|
|
|
|
|
|
|
const std::string Enemy::CONFIG = "enemy.yaml";
|
|
|
|
|
2013-04-29 14:49:16 +00:00
|
|
|
Enemy::Enemy(World& world, Pathfinder& pathfinder,
|
2013-05-02 11:31:26 +00:00
|
|
|
const sf::Vector2f& position) :
|
2013-05-07 22:00:05 +00:00
|
|
|
Character(position, CATEGORY_ACTOR, MASK_ALL, Yaml(CONFIG), world,
|
|
|
|
pathfinder) {
|
2013-04-05 14:54:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Enemy::onThink(int elapsed) {
|
|
|
|
Character::onThink(elapsed);
|
|
|
|
|
|
|
|
auto characters = getCharacters();
|
|
|
|
std::shared_ptr<Character> target;
|
|
|
|
float distanceSquared = std::numeric_limits<float>::max();
|
|
|
|
for (auto it : characters) {
|
|
|
|
if (distanceSquared > thor::squaredLength(it->getPosition() - getPosition())) {
|
|
|
|
target = it;
|
|
|
|
distanceSquared = thor::squaredLength(it->getPosition() - getPosition());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (target) {
|
|
|
|
if (isVisible(target->getPosition())) {
|
|
|
|
setDestination(getPosition());
|
|
|
|
setDirection(target->getPosition() - getPosition());
|
|
|
|
pullTrigger();
|
|
|
|
}
|
2013-04-25 19:57:37 +00:00
|
|
|
else if (!isMoving())
|
2013-04-05 14:54:51 +00:00
|
|
|
setDestination(target->getPosition());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
releaseTrigger();
|
2012-10-14 16:14:06 +00:00
|
|
|
}
|