package electrosphere.server.ai.nodes.actions.move;

import org.joml.Quaterniond;
import org.joml.Vector3d;

import electrosphere.entity.Entity;
import electrosphere.entity.EntityUtils;
import electrosphere.entity.types.camera.CameraEntityUtils;
import electrosphere.entity.types.creature.CreatureUtils;
import electrosphere.server.ai.blackboard.Blackboard;
import electrosphere.server.ai.nodes.AITreeNode;
import electrosphere.server.ai.nodes.actions.combat.MeleeTargetingNode;
import electrosphere.util.math.MathUtils;

/**
 * Faces the target
 */
public class FaceTargetNode implements AITreeNode {

    /**
     * Constructor
     */
    public FaceTargetNode(){
    }

    @Override
    public AITreeNodeResult evaluate(Entity entity, Blackboard blackboard) {
        if(MeleeTargetingNode.hasTarget(blackboard)){
            Entity target = MeleeTargetingNode.getTarget(blackboard);
            Vector3d parentPos = EntityUtils.getPosition(entity);
            Vector3d targetPos = EntityUtils.getPosition(target);
            Quaterniond rotation = MathUtils.calculateRotationFromPointToPoint(parentPos, targetPos);
            EntityUtils.getRotation(entity).set(rotation);
            CreatureUtils.setFacingVector(entity, CameraEntityUtils.getFacingVec(rotation));
            return AITreeNodeResult.SUCCESS;
        }
        return AITreeNodeResult.FAILURE;
    }

}
