dungeon/src/RangedAttackNode.cpp

51 lines
1.2 KiB
C++
Raw Normal View History

2017-09-17 13:43:13 +02:00
#include "RangedAttackNode.h"
#include "BehaviourTree.h"
#include "Actor.h"
#include "Tilemap.h"
#include "FieldOfView.h"
2017-09-17 13:43:13 +02:00
RangedAttackNode::RangedAttackNode(BehaviourTreeNode* parent) : BehaviourTreeNode(parent) {}
RangedAttackNode::~RangedAttackNode() {}
BehaviourTreeStatus RangedAttackNode::tick(BTTick * tick) {
if (children.size() <= 0) {
return BT_ERROR;
}
bool ishero = tick->target->isTypeOf(ACT_HERO);
2017-09-21 22:44:05 +02:00
auto actors = tick->target->map->get_actor_list();
2017-09-17 13:43:13 +02:00
std::vector<Actor*> enemies;
2017-09-17 20:07:38 +02:00
for (auto actor : *actors) {
2017-09-17 13:43:13 +02:00
if (actor == tick->target) continue;
if (actor->isTypeOf(ACT_HERO) != ishero) {
2017-09-21 22:44:05 +02:00
vec2i pos = actor->get_position();
if (line_of_sight(tick->target->map, tick->target->get_position(), pos)) {
2017-09-17 13:43:13 +02:00
enemies.push_back(actor);
}
}
}
if (enemies.size() == 0) {
return BT_FAILED;
}
Actor* lowestHpActor = nullptr;
int lowestHp;
2017-09-17 20:07:38 +02:00
for (Actor* actor : enemies) {
2017-09-17 13:43:13 +02:00
if (lowestHpActor == nullptr || actor->health < lowestHp) {
lowestHpActor = actor;
lowestHp = actor->health;
}
}
lowestHpActor->health -= tick->target->strength;
if (lowestHpActor->health <= 0) {
lowestHpActor->Kill();
}
return BT_SUCCEEDED;
}