175 lines
5.8 KiB
C#

using UnityEngine;
using RR.Common.Interfaces;
using RR.Game2D.Core;
using RR.Game2D.Behaviour.ScriptableObjects;
namespace RR.Game2D.Behaviour
{
[RequireComponent(typeof(Rigidbody2D),
typeof(Collider2D),
typeof(Animator))]
public class EnemyController : MonoBehaviour, IDamageable
{
[SerializeField] private EnemyControllerConfiguration config;
private Health health;
[ReadOnlyInInspector] public float CurrentHealth => health?.CurrentHealth ?? 0;
[HideInInspector] public float MaxHealth => health?.MaxHealth ?? 0;
private Animator animator;
private Rigidbody2D rb;
private Collider2D col;
private GameObject playerObj;
// Аниматор
private static readonly int Speed = Animator.StringToHash("Speed");
void Start()
{
rb = GetComponent<Rigidbody2D>();
col = GetComponent<Collider2D>();
animator = GetComponent<Animator>();
playerObj = GameManager.Instance.PlayerObj;
if (playerObj == null)
{
playerObj = GameObject.FindGameObjectWithTag("Player");
}
health = GetComponent<Health>();
if (health == null)
{
health = gameObject.AddComponent<Health>();
}
health.ResetHealth();
health.SetOwner(this);
health.SetMaxHealth(config.maxHealth);
}
void FixedUpdate()
{
if (!playerObj) return;
// if (rb.linearVelocity.x > 0.0001f) Debug.Log(rb.linearVelocity.x);
Vector3 playerCenter = playerObj.transform.position;
if (PlayerInSight())
{
Vector3 toPlayer = playerCenter - transform.position;
float dist = toPlayer.magnitude + 0.1f;
if (dist > 0)
{
rb.linearVelocity = new Vector2(config.moveSpeed * toPlayer.normalized.x, 0);
}
// if (dist > 0)
// rb.linearVelocity = toPlayer / dist * moveSpeed;
// else
// rb.linearVelocity = Vector2.zero;
}
else
{
rb.linearVelocity = Vector2.MoveTowards(
rb.linearVelocity, Vector2.zero, config.acceleration * Time.fixedDeltaTime);
}
animator.SetFloat(Speed, Mathf.Abs(rb.linearVelocity.magnitude));
}
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.collider.CompareTag("Player"))
{
Rigidbody2D playerRb = collision.collider.GetComponent<Rigidbody2D>();
playerObj.TryGetComponent(out PlayerController playerController);
if (playerRb == null) return;
Vector2 contactNormal = collision.GetContact(0).normal;
if (contactNormal.y < -0.5f)
{
playerRb.linearVelocity = new Vector2(playerRb.linearVelocity.x, 35f);
TakeDamage(MaxHealth);
// TakeDamage(1);
}
else
{
playerController.TakeDamage(1f);
}
}
}
private bool PlayerInSight()
{
if (!playerObj) return false;
Vector3 playerPos = playerObj.transform.position;
Collider2D playerCol = playerObj.GetComponent<Collider2D>();
playerPos.y = playerCol.bounds.center.y;
Vector2 eyePos = new(rb.position.x, col.bounds.center.y);
Vector2 toPlayer = (Vector2)playerPos - eyePos;
float dist = toPlayer.magnitude;
if (dist > config.viewDistance) return false;
Vector2 forward = transform.localEulerAngles.y >= 180f ? Vector2.right : Vector2.left;
float cosHalf = Mathf.Cos(0.5f * config.viewAngle * Mathf.Deg2Rad);
float dot = Vector2.Dot(forward.normalized, toPlayer / Mathf.Max(dist, 1e-6f));
if (dot < cosHalf) return false;
int mask = config.obstacleMask;
mask &= ~(1 << gameObject.layer);
mask &= ~(1 << playerObj.layer);
Vector2 origin = eyePos + forward.normalized * 0.05f;
RaycastHit2D hit = Physics2D.Raycast(origin, toPlayer.normalized, dist, mask);
if (hit.collider != null) return false;
return true;
}
public void TakeDamage(float amount)
{
health.TakeDamage(amount);
}
public void Heal(float amount)
{
health.Heal(amount);
}
public void Die()
{
animator.Play("Death");
// Destroy(gameObject);
}
#if UNITY_EDITOR
private void OnDrawGizmos()
{
if (!playerObj) return;
Vector2 origin = new(rb.position.x, col.bounds.center.y);
Vector2 forward = transform.localEulerAngles.y >= 180f ? Vector2.right : Vector2.left;
float half = config.viewAngle * 0.5f;
Vector2 edgeA = (Quaternion.Euler(0, 0, +half) * forward);
Vector2 edgeB = (Quaternion.Euler(0, 0, -half) * forward);
Gizmos.color = Color.yellow;
Gizmos.DrawLine(origin, origin + edgeA * config.viewDistance);
Gizmos.DrawLine(origin, origin + edgeB * config.viewDistance);
Gizmos.DrawWireSphere(origin, config.viewDistance);
Vector2 toPlayer = (Vector2)playerObj.transform.position - origin;
if (toPlayer.magnitude <= config.viewDistance)
{
Gizmos.color = Color.red;
Gizmos.DrawLine(transform.position, playerObj.transform.position);
}
}
#endif
}
}