Newer
Older
HierarchicalFSM-Unity3D / Assets / Scripts / NPC / FSM / Conditions / CheckTargetState.cs
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.AI;

[CreateAssetMenu(menuName = "Finite State Machine/Condition/CheckTargetState")]
public class CheckTargetState : Condition
{
    [SerializeField] private IState.TargetState targetState;

    //Find Cover Location
    public override bool CheckCondition(FSM fsm)
    {
        GameObject target = fsm.Controller.AgentFOV.GetTarget();

        if (target != null)
        {
            if (target.TryGetComponent(out IState state))
            {
                if (targetState == state.GetState())
                {
                    return true;
                }

                else
                {
                    return false;
                }
            }

            else
            {
                Debug.LogError($"Component not Found: {nameof(IState)}");
                return false;
            }
        }

        else 
        {
            Debug.LogWarning($"Target Missing!");
            return false; 
        }
    }
}