2023-08-02 07:45:11 +00:00
|
|
|
using System;
|
|
|
|
using UnityEngine;
|
2023-08-08 07:53:35 +00:00
|
|
|
using UnityEngine.AI;
|
2023-08-03 03:38:50 +00:00
|
|
|
using Random = UnityEngine.Random;
|
2023-08-02 07:45:11 +00:00
|
|
|
|
|
|
|
// ReSharper disable once CheckNamespace
|
2023-08-03 05:49:05 +00:00
|
|
|
namespace BlueWaterProject
|
2023-08-02 07:45:11 +00:00
|
|
|
{
|
|
|
|
[Serializable]
|
2023-08-03 03:38:50 +00:00
|
|
|
public abstract class AiController : MonoBehaviour, IDamageable
|
2023-08-02 07:45:11 +00:00
|
|
|
{
|
2023-08-03 03:38:50 +00:00
|
|
|
#region Property and variable
|
2023-08-02 07:45:11 +00:00
|
|
|
|
2023-08-08 07:53:35 +00:00
|
|
|
protected bool isAttacking;
|
|
|
|
|
2023-08-03 05:32:17 +00:00
|
|
|
protected Animator aiAnimator;
|
2023-08-08 07:53:35 +00:00
|
|
|
protected AiMover aiMover;
|
2023-08-03 08:00:14 +00:00
|
|
|
protected FieldOfView fieldOfView;
|
2023-08-08 07:53:35 +00:00
|
|
|
protected NavMeshAgent navMeshAgent;
|
2023-08-03 03:38:50 +00:00
|
|
|
|
2023-08-08 07:53:35 +00:00
|
|
|
public static readonly int SpeedHash = Animator.StringToHash("Speed");
|
|
|
|
public static readonly int AttackHash = Animator.StringToHash("TakeDamage");
|
|
|
|
public static readonly int DamageHash = Animator.StringToHash("TakeDamage");
|
|
|
|
public static readonly int DeathTypeHash = Animator.StringToHash("DeathType");
|
|
|
|
public static readonly int DeathHash = Animator.StringToHash("Death");
|
2023-08-02 07:45:11 +00:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2023-08-03 03:38:50 +00:00
|
|
|
#region abstract function
|
|
|
|
|
2023-08-03 05:32:17 +00:00
|
|
|
protected abstract void Attack();
|
2023-08-03 03:38:50 +00:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2023-08-02 07:45:11 +00:00
|
|
|
#region Unity built-in function
|
|
|
|
|
2023-08-03 08:00:14 +00:00
|
|
|
protected virtual void Awake()
|
2023-08-02 07:45:11 +00:00
|
|
|
{
|
2023-08-03 03:38:50 +00:00
|
|
|
aiAnimator = Utils.GetComponentAndAssert<Animator>(transform);
|
2023-08-08 07:53:35 +00:00
|
|
|
aiMover = Utils.GetComponentAndAssert<AiMover>(transform);
|
2023-08-03 08:00:14 +00:00
|
|
|
fieldOfView = Utils.GetComponentAndAssert<FieldOfView>(transform);
|
2023-08-08 07:53:35 +00:00
|
|
|
navMeshAgent = Utils.GetComponentAndAssert<NavMeshAgent>(transform);
|
2023-08-02 07:45:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void Start()
|
|
|
|
{
|
2023-08-03 03:38:50 +00:00
|
|
|
SetCurrentHp(AiStat.maxHp);
|
|
|
|
}
|
|
|
|
|
2023-08-08 07:53:35 +00:00
|
|
|
private void LateUpdate()
|
|
|
|
{
|
|
|
|
Attack();
|
|
|
|
}
|
|
|
|
|
2023-08-03 03:38:50 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region interface property and function
|
|
|
|
|
|
|
|
[field: SerializeField] public AiStat AiStat { get; set; } = new();
|
|
|
|
|
|
|
|
public void TakeDamage(AiStat attacker, AiStat defender)
|
|
|
|
{
|
|
|
|
// 회피 성공 체크
|
|
|
|
if (Random.Range(0, 100) < defender.avoidanceRate)
|
|
|
|
{
|
|
|
|
// TODO : 회피 처리
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var finalDamage = Utils.CalcDamage(attacker, defender);
|
|
|
|
|
|
|
|
// 방패 막기 체크
|
|
|
|
if (finalDamage == 0f)
|
|
|
|
{
|
|
|
|
// TODO : 방패로 막힘 처리(애니메이션 등)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var changeHp = Mathf.Max(defender.currentHp - finalDamage, 0);
|
|
|
|
SetCurrentHp(changeHp);
|
|
|
|
|
|
|
|
// 죽었는지 체크
|
|
|
|
if (changeHp == 0f)
|
|
|
|
{
|
2023-08-08 07:53:35 +00:00
|
|
|
var randomValue = Random.Range(0, 2);
|
|
|
|
aiAnimator.SetInteger(DeathTypeHash, randomValue);
|
|
|
|
|
2023-08-03 03:38:50 +00:00
|
|
|
// TODO : 죽었을 때 처리(죽는 애니메이션 이후 사라지는 효과 등)
|
|
|
|
aiAnimator.SetTrigger(DeathHash);
|
2023-08-08 07:53:35 +00:00
|
|
|
|
|
|
|
Invoke(nameof(Destroy), 5f);
|
2023-08-03 03:38:50 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-08-02 07:45:11 +00:00
|
|
|
|
2023-08-03 03:38:50 +00:00
|
|
|
aiAnimator.SetTrigger(DamageHash);
|
2023-08-02 07:45:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2023-08-03 08:00:14 +00:00
|
|
|
#region Custom function
|
2023-08-02 07:45:11 +00:00
|
|
|
|
2023-08-08 07:53:35 +00:00
|
|
|
private void Destroy() => Destroy(gameObject);
|
|
|
|
public bool GetIsAttacking() => isAttacking;
|
|
|
|
public FieldOfView GetFieldOfView() => fieldOfView;
|
|
|
|
public NavMeshAgent GetNavMeshAgent() => navMeshAgent;
|
|
|
|
public Animator GetAnimator() => aiAnimator;
|
2023-08-02 07:45:11 +00:00
|
|
|
public void SetCurrentHp(float value) => AiStat.currentHp = value;
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|