using UnityEngine; using UnityEngine.Animations; using Saltosion.OneWeapon.AI; namespace Saltosion.OneWeapon.Enemies { [RequireComponent(typeof(Rigidbody2D))] public class Enemy : MonoBehaviour { [Header("Debug Info")] public string CurrentBehavior = "Nothing"; [Header("Graphics")] public Animator Anim; [Header("Stats")] public float MoveSpeed; [Header("Behaviour")] public BehaviourNode BehaviourTree; [HideInInspector] public bool Attacking; private Rigidbody2D Body; private bool MovingToTarget = false; private Vector2 TargetPosition; private void Start() { Body = GetComponent(); } private void Update() { BehaviourTree.Execute(this); CurrentBehavior = BehaviourTree.GetExecutedName(); if (Attacking) { Anim.Play("CrabAttack"); } else if (Body.velocity.magnitude > 0.1) { Anim.Play("CrabWalk"); Anim.SetFloat("Speed", 1); } else { Anim.Play("CrabWalk"); Anim.SetFloat("Speed", 0); } } private void FixedUpdate() { if (MovingToTarget) { Body.velocity = (TargetPosition - Body.position).normalized * MoveSpeed; } } public void StartMovingTo(Vector2 target) { TargetPosition = target; MovingToTarget = true; } public void StopMoving() { MovingToTarget = false; } } }