#include "damage_area.hpp"
#include "godot_cpp/classes/global_constants.hpp"
#include "utils/godot_macros.h"

namespace godot {
void DamageArea::_bind_methods() {
#define CLASSNAME DamageArea
    GDPROPERTY_HINTED(target_classes, Variant::ARRAY, PROPERTY_HINT_ARRAY_TYPE, "StringName");
}

void DamageArea::_enter_tree() { GDGAMEONLY();
    this->connect("body_entered", callable_mp(this, &DamageArea::body_entered));
    if(this->has_node("AnimationPlayer"))
        this->anim = this->get_node<AnimationPlayer>("AnimationPlayer");
}

void DamageArea::_process(double delta_time) {
    if(this->anim && this->anim->get_current_animation_position() >= this->anim->get_current_animation_length())
        this->queue_free();
}

void DamageArea::body_entered(Node3D *node) {
    if(this->target_classes.has(node->get_class())) {
        node->call("damage");
    }
}

void DamageArea::set_target_classes(Array array) {
    this->target_classes.clear();
    for(int i = 0; i < array.size(); ++i) {
        if(array[i].get_type() == Variant::STRING_NAME && !this->target_classes.has(array[i]))
            this->target_classes.push_back(array[i]);
        else this->target_classes.push_back("");
    }
}

Array DamageArea::get_target_classes() const {
    Array a{};
    for(StringName const &name : this->target_classes)
        a.push_back(name);
    return a;
}
}
