BloodAndGore/Assets/Scripts/Enemies/Enemy.cs

59 lines
1.8 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 {
public float MoveSpeed;
public AcceleratedMovement Movement;
[Header("Debug Info")]
public TextMesh BehaviourDisplay;
public string CurrentBehaviour = "Nothing";
[HideInInspector]
public bool Attacking;
private Rigidbody2D Body;
private bool MovingToTarget = false;
private Vector2 TargetPosition;
private void Start() {
Body = GetComponent<Rigidbody2D>();
BehaviourDisplay.GetComponent<MeshRenderer>().sortingLayerName = "Debug Text";
}
private void Update() {
bool DisplayDebugInfo = false;
if (Application.isEditor) {
Collider2D Hit = Physics2D.OverlapPoint(Camera.main.ScreenToWorldPoint(Input.mousePosition));
if (Hit != null && Hit.attachedRigidbody == Body) {
DisplayDebugInfo = true;
}
}
BehaviourDisplay.gameObject.SetActive(DisplayDebugInfo);
if (DisplayDebugInfo) {
BehaviourDisplay.text = CurrentBehaviour;
}
}
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;
}
}
}