dungeon/src/Actor.cpp

76 lines
1.4 KiB
C++
Raw Permalink Normal View History

2017-09-17 13:43:13 +02:00
#include "Actor.h"
#include "Tilemap.h"
#include "BehaviourTree.h"
2017-09-26 15:49:11 +02:00
int id_counter = 0;
2017-09-24 00:15:28 +02:00
2018-01-09 21:59:05 +01:00
Actor::Actor(vec2i pos) : Entity(pos) {
2017-09-26 15:49:11 +02:00
id = id_counter++;
2017-09-24 00:15:28 +02:00
name = "Actor";
2017-09-26 15:49:11 +02:00
range = 1.5f;
2018-01-09 21:59:05 +01:00
player_controlled = false;
2017-09-26 15:49:11 +02:00
collision = true;
2017-09-23 22:06:36 +02:00
bt = nullptr;
faction = FACTION_NONE;
2017-09-26 15:49:11 +02:00
sprite_id = 1;
2017-09-17 13:43:13 +02:00
}
2018-01-09 21:59:05 +01:00
void Actor::update(Tilemap* map) {
2017-09-17 13:43:13 +02:00
if (!alive) return;
if (!player_controlled && bt != nullptr) {
2018-01-09 21:59:05 +01:00
bt->tick(this, map);
2017-09-17 13:43:13 +02:00
}
2017-09-26 15:49:11 +02:00
if (health < health_max) {
2017-11-16 20:18:13 +01:00
healcounter--;
2017-09-17 13:43:13 +02:00
if (healcounter <= 0) {
2017-11-16 20:18:13 +01:00
heal(1);
2017-09-17 13:43:13 +02:00
healcounter = 4;
}
}
2017-09-26 15:49:11 +02:00
if (health <= 0) {
kill();
}
}
2017-09-17 13:43:13 +02:00
void Actor::damage(int str) {
health -= str;
2018-03-26 22:16:10 +02:00
healcounter = 4;
2017-09-17 13:43:13 +02:00
if (health <= 0) {
2017-09-26 15:49:11 +02:00
kill();
}
}
void Actor::attack(Actor *act) {
if (act) {
vec2i dpos = get_position() - act->get_position();
if (dpos.dist() <= range) {
act->damage(strength);
}
}
}
2018-01-09 21:59:05 +01:00
void Actor::attack(vec2i dpos, Tilemap* map) {
2017-09-26 15:49:11 +02:00
if (dpos.dist() <= range) {
vec2i pos = get_position();
2018-01-09 21:59:05 +01:00
auto acts = map->get_actors(pos.x + dpos.x, pos.y + dpos.y, 0);
2017-09-26 15:49:11 +02:00
for (Entity* ent : acts) {
auto act = (Actor*)ent;
if (act->is_alive() && act->get_actor_faction() != faction) {
2017-09-26 15:49:11 +02:00
act->damage(strength);
break;
}
}
}
}
void Actor::heal(int amount) {
health += amount;
2018-03-26 22:16:10 +02:00
healcounter = 4;
2017-09-26 15:49:11 +02:00
if (health > health_max) {
health = health_max;
2017-09-17 13:43:13 +02:00
}
}
2017-09-23 22:06:36 +02:00
Actor::~Actor() = default;