BloodAndGore/Assets/Scripts/Enemies/Enemy.cs

62 lines
1.7 KiB
C#
Raw Normal View History

2019-08-03 17:36:17 +02:00
using UnityEngine;
using UnityEngine.Animations;
2019-08-03 17:36:17 +02:00
using Saltosion.OneWeapon.AI;
using Saltosion.OneWeapon.Utils;
2019-08-03 17:36:17 +02:00
2019-08-14 16:17:48 +02:00
namespace Saltosion.OneWeapon.Enemies {
2019-08-03 17:36:17 +02:00
[RequireComponent(typeof(Rigidbody2D))]
public class Enemy : MonoBehaviour {
2019-08-04 20:09:00 +02:00
[Header("Debug Info")]
public string CurrentBehavior = "Nothing";
[Header("Graphics")]
public Animator Anim;
2019-08-04 20:09:00 +02:00
[Header("Stats")]
2019-08-03 17:36:17 +02:00
public float MoveSpeed;
2019-08-04 20:09:00 +02:00
[Header("Behaviour")]
2019-08-03 17:36:17 +02:00
public BehaviourNode BehaviourTree;
public AcceleratedMovement Movement;
2019-08-04 20:09:00 +02:00
[HideInInspector]
public bool Attacking;
2019-08-03 17:36:17 +02:00
private Rigidbody2D Body;
private bool MovingToTarget = false;
private Vector2 TargetPosition;
private void Start() {
Body = GetComponent<Rigidbody2D>();
}
private void Update() {
BehaviourTree.Execute(this);
2019-08-04 15:44:44 +02:00
CurrentBehavior = BehaviourTree.GetExecutedName();
2019-08-04 20:09:00 +02:00
if (Attacking) {
Anim.Play("Attack");
2019-08-04 20:09:00 +02:00
} else if (Body.velocity.magnitude > 0.1) {
Anim.Play("Walk");
2019-08-04 20:09:00 +02:00
} else {
Anim.Play("Walk");
2019-08-04 20:09:00 +02:00
}
Anim.SetFloat("Speed", Movement.SpeedPercentage);
2019-08-03 17:36:17 +02:00
}
private void FixedUpdate() {
if (MovingToTarget) {
Movement.Direction = (TargetPosition - Body.position).normalized;
} else {
Movement.Direction = Vector2.zero;
2019-08-03 17:36:17 +02:00
}
}
public void StartMovingTo(Vector2 target) {
TargetPosition = target;
MovingToTarget = true;
}
public void StopMoving() {
MovingToTarget = false;
}
}
}