Newer
Older
TheVengeance-Project-IADE-Unity2D / Assets / Scripts / NPC / Orc / OrcNPCController.cs
using System.Collections;
using UnityEngine;
using MyCollections.AI.FinitStateMachine;
using System.Collections.Generic;
using JetBrains.Annotations;

public class OrcNPCController : EnemyNPCContoller
{
    [SerializeField] private State currentState;
    [SerializeField] private Vector2 lastDirection = Vector2.right; // Default facing right
    [SerializeField] private GameObject weapon;
    private Animator weaponAnimator;

    [SerializeField] private float deadTimer = 2;
    [SerializeField] private float disabletimer;
    [SerializeField] private AnimationClip attackClip;

    private float attackTimer;
    [SerializeField] private float MaxAttackTimer;

    public Vector2 LastDirection => lastDirection;
    public Animator WeaponAnimator => weaponAnimator;
    public GameObject Weapon => weapon;

    public bool isAttacking;

    protected override void Awake()
    {
        base.Awake();
        weaponAnimator = weapon.GetComponent<Animator>();
        weapon.SetActive(false);
    }

    protected override void Start()
    {
        base.Start();
        disabletimer = deadTimer;
        MaxAttackTimer = attackClip.length;
        attackTimer = MaxAttackTimer;
    }

    protected override void OnEnable()
    {
        base.OnEnable();
        lastDirection = Vector2.right;
        AgentFOV.IsEnabled = true;
        MaxAttackTimer = attackClip.length;
        attackTimer = MaxAttackTimer;
    }

    protected override void OnDisable()
    {
        base.OnDisable();
        disabletimer = deadTimer;
    }

    // Update is called once per frame
    protected override void Update()
    {
        base.Update();
        currentState = fsm.CurrentState();
        Debug.Log("UPDATE THE ANIM CONTROLLER");
    }

    protected override void AnimationController()
    {
        base.AnimationController();

        if (agent.agentVelocity != Vector2.zero)
        {
            lastDirection = agent.agentVelocity.normalized;
        }

        animator.SetBool("IsStopped", agent.agentStop);

        animator.SetFloat("VelocityX", agent.agentVelocity.x);
        animator.SetFloat("VelocityY", agent.agentVelocity.y);

        animator.SetFloat("LookDirectionX", lastDirection.x);
        animator.SetFloat("LookDirectionY", lastDirection.y);

        animator.SetBool("IsDead", health <= 0f);

        if (animator.GetBool("IsDead"))
        {
            AnimatorClipInfo deadClip = animator.GetCurrentAnimatorClipInfo(0)[0];
            float wait = deadClip.clip.length;
            disabletimer -= Time.deltaTime;
            
            if (disabletimer <= 0f)
            {
                gameObject.SetActive(false);
            }
        }

        if (weapon.activeSelf)
        {
            weaponAnimator.SetFloat("LookDirectionX", lastDirection.x);
            weaponAnimator.SetFloat("LookDirectionY", lastDirection.y);
        }

        if (isAttacking)
        {
            weapon.SetActive(true);
            weaponAnimator.enabled = true;
            animator.SetBool("IsAttacking", true);
            attackTimer -= Time.deltaTime;

            if (attackTimer <= 0f)
            {
                weaponAnimator.enabled = false;
                weapon.SetActive(false);
                isAttacking = false;
                attackTimer = MaxAttackTimer;
                animator.SetBool("IsAttacking", false);
            }
        }
    }

    public void OnGUI()
    {
        Rect rect = new Rect(0f, 0f, 300f, 200f); // Adjust size as needed
        GUI.color = Color.red;
        List<string> list = new List<string>();

        if (currentState != null)
        {
            list.Add(currentState.name);

            foreach (Transition t in currentState.GetTransitions())
            {
                list.Add($"Transition: {t.name}");
            }

            // Combine all strings into one string with line breaks
            string combinedText = string.Join("\n", list);

            // Create a GUIStyle to modify the text appearance
            GUIStyle style = new GUIStyle(GUI.skin.box);
            style.fontSize = 25; // Set the font size
            style.alignment = TextAnchor.MiddleCenter; // Center align the text (optional)

            // Display the combined string in the GUI.Box with the custom style
            GUI.Box(rect, combinedText, style);
        }
    }
}