#include "enemy_body.h"
#include "macros.h"
#include "npc_unit.h"

void EnemyBody::_bind_methods() {
	ClassDB::bind_method(D_METHOD("set_movement_direction", "direction"), &self_type::set_movement_direction);
}

void EnemyBody::ready() {
	this->fsm = cast_to<StateMachine>(get_node(NodePath("%StateMachine")));
	this->nav = cast_to<NavigationAgent3D>(get_node(NodePath("%NavigationAgent3D")));
}

void EnemyBody::physics_process(double delta) {
	GETSET(velocity, {
		velocity = Vector3{ this->movement_direction.x * this->movement_speed, velocity.y, this->movement_direction.y * this->movement_speed };
	});
	if (!this->movement_direction.is_zero_approx()) {
		look_at(get_global_position() + Vector3{ this->movement_direction.x, 0.f, this->movement_direction.y });
	}
	move_and_slide();
}

void EnemyBody::_notification(int what) {
	if (Engine::get_singleton()->is_editor_hint()) {
		return;
	}
	switch (what) {
		default:
			return;
		case NOTIFICATION_READY:
			set_physics_process(true);
			ready();
			return;
		case NOTIFICATION_PHYSICS_PROCESS:
			physics_process(get_physics_process_delta_time());
			return;
	}
}

void EnemyBody::set_movement_direction(Vector2 direction) {
	this->movement_direction = direction;
}

void EnemyBody::set_unit(NpcUnit *unit) {
	this->unit = unit;
}

NpcUnit *EnemyBody::get_unit() const {
	return this->unit;
}

NavigationAgent3D *EnemyBody::get_nav() const {
	return this->nav;
}

void EnemyBody::set_movement_speed(float value) {
	this->movement_speed = value;
}

float EnemyBody::get_movement_speed() const {
	return this->movement_speed;
}

void EnemyBody::set_unit_offset(Vector2 offset) {
	this->unit_offset = offset;
}

Vector2 EnemyBody::get_unit_offset() const {
	return this->unit_offset;
}

Vector3 EnemyBody::get_unit_offset_3d() const {
	return { this->unit_offset.x, 0, this->unit_offset.y };
}
