BloodAndGore/Assets/Scripts/Enemies/Enemy.cs

62 lines
1.7 KiB
C#

using UnityEngine;
using UnityEngine.Animations;
using Saltosion.OneWeapon.AI;
using Saltosion.OneWeapon.Utils;
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;
public AcceleratedMovement Movement;
[HideInInspector]
public bool Attacking;
private Rigidbody2D Body;
private bool MovingToTarget = false;
private Vector2 TargetPosition;
private void Start() {
Body = GetComponent<Rigidbody2D>();
}
private void Update() {
BehaviourTree.Execute(this);
CurrentBehavior = BehaviourTree.GetExecutedName();
if (Attacking) {
Anim.Play("Attack");
} else if (Body.velocity.magnitude > 0.1) {
Anim.Play("Walk");
} else {
Anim.Play("Walk");
}
Anim.SetFloat("Speed", Movement.SpeedPercentage);
}
private void FixedUpdate() {
if (MovingToTarget) {
Movement.Direction = (TargetPosition - Body.position).normalized;
} else {
Movement.Direction = Vector2.zero;
}
}
public void StartMovingTo(Vector2 target) {
TargetPosition = target;
MovingToTarget = true;
}
public void StopMoving() {
MovingToTarget = false;
}
}
}