BloodAndGore/Assets/Scripts/Enemies/Enemy.cs

58 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 {
private static Enemy latestDebugHoveredEnemy = null;
public AcceleratedMovement Movement;
[Header("Debug Info")]
public TextMesh BehaviourDisplay;
public string CurrentBehaviour = "Nothing";
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() {
if (Application.isEditor) {
Collider2D Hit = Physics2D.OverlapPoint(Camera.main.ScreenToWorldPoint(Input.mousePosition));
if (Hit != null && Hit.attachedRigidbody == Body) {
latestDebugHoveredEnemy = this;
}
}
bool DisplayDebugInfo = latestDebugHoveredEnemy == this;
BehaviourDisplay.gameObject.SetActive(DisplayDebugInfo);
if (DisplayDebugInfo) {
BehaviourDisplay.text = CurrentBehaviour.Length == 0 ? "Nothing" : 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;
}
}
}