BloodAndGore/Assets/Scripts/Enemies/Enemy.cs
2019-08-18 21:32:07 +03:00

60 lines
1.9 KiB
C#

using UnityEngine;
using UnityEngine.Animations;
using Saltosion.OneWeapon.AI;
using Saltosion.OneWeapon.Utils;
namespace Saltosion.OneWeapon.Enemies {
[RequireComponent(typeof(Rigidbody2D), typeof(AcceleratedMovement))]
public class Enemy : MonoBehaviour {
private static Enemy latestDebugHoveredEnemy = null;
[Header("Debug Info")]
public TextMesh BehaviourDisplay;
public string CurrentBehaviour = "Nothing";
public AcceleratedMovement Movement { private set; get; }
private Rigidbody2D Body;
private bool MovingToTarget = false;
private Vector2 TargetPosition;
private void Start() {
Movement = GetComponent<AcceleratedMovement>();
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;
}
}
}