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/sprites/Enemy.cpp

43 lines
1.0 KiB
C++
Raw Normal View History

2012-10-14 16:14:06 +00:00
/*
* Enemy.cpp
*
* Created on: 10.09.2012
* Author: Felix
*/
#include "Enemy.h"
#include <Thor/Vectors.hpp>
2013-04-28 16:11:39 +00:00
Enemy::Enemy(World& world, TileManager& tileManager, Pathfinder& pathfinder,
const sf::Vector2f& position, const Yaml& config) :
2013-04-28 16:11:39 +00:00
Character(world, tileManager, pathfinder, Data(position, CATEGORY_ACTOR, MASK_ALL),
config) {
}
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();
}
else if (!isMoving())
setDestination(target->getPosition());
}
else
releaseTrigger();
2012-10-14 16:14:06 +00:00
}