OldBlueWater/BlueWater/Assets/02.Scripts/Character/Crewmate/Crewmate.cs
2023-10-17 16:31:10 +09:00

378 lines
13 KiB
C#

using System;
using System.Collections;
using Sirenix.OdinInspector;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.InputSystem;
// ReSharper disable once CheckNamespace
namespace BlueWaterProject
{
[RequireComponent(typeof(PlayerInput))]
public abstract class Crewmate : BaseCharacter, IDamageable, IAnimatorBridge, IAiView, INormalAttack
{
#region Properties and variables
[Title("DrawGizmos")]
[Tooltip("전체 Gizmos 그리기 여부")]
[SerializeField] private bool isDrawGizmos = true;
[ShowIf("@isDrawGizmos")]
[Tooltip("타겟 인식 범위 그리기 여부")]
[SerializeField] private bool isDrawViewRange = true;
[ShowIf("@isDrawGizmos")]
[Tooltip("이동제한 범위 그리기 여부")]
[SerializeField] private bool isDrawDefenseRange = true;
[ShowIf("@isDrawGizmos")]
[Tooltip("타겟과의 상태 그리기 여부\n빨간색 = 공격 범위 밖\n파란색 = 공격 범위 안")]
[SerializeField] private bool isDrawTargetRange = true;
[field: Title("Stat")]
[field: Tooltip("최대 체력 설정")]
[field: SerializeField] public float MaxHp { get; private set; } = 100f;
[field: Tooltip("현재 체력")]
[field: SerializeField] public float CurrentHp { get; private set; }
[field: Tooltip("이동 속도 설정")]
[field: SerializeField] public float MoveSpd { get; private set; } = 5f;
[field: Tooltip("공격력 설정")]
[field: SerializeField] public float Atk { get; private set; } = 10f;
[field: Tooltip("공격 속도(다음 공격 주기)\nAtkCooldown = 2f (2초마다 1번 공격)")]
[field: SerializeField] public float AtkCooldown { get; private set; } = 1f;
[field: Tooltip("공격 사거리 설정")]
[field: SerializeField] public float AtkRange { get; set; } = 1.5f;
[field: Tooltip("이동 제한 범위 설정")]
[field: SerializeField] public float DefenseRange { get; private set; } = 20f;
[field: Tooltip("Idle 상태에서 랜덤으로 이동 여부")]
[field: SerializeField] public bool IsRandomMove { get; set; }
[field: ShowIf("@IsRandomMove")]
[field: Tooltip("Idle 상태에서 이동하는 범위 설정")]
[field: SerializeField] public float RandomMoveRange { get; set; }
[field: Title("Data")]
[field: DisableIf("@true")]
[field: SerializeField] public Vector3 DefensePos { get; set; }
[field: DisableIf("@true")]
[field: SerializeField] public bool IsCombated { get; set; }
[field: DisableIf("@true")]
[field: SerializeField] public bool BeAttackedInIdle { get; set; }
[field: DisableIf("@true")]
[field: SerializeField] public bool UseRigidbody { get; set; }
[DisableIf("@true")]
[SerializeField] private bool beAttacked;
[DisableIf("@true")]
[SerializeField] protected bool isAttacking;
protected bool usedNormalAttackCoroutine;
protected WaitForSeconds waitAtkCooldown;
protected Rigidbody rb;
public Collider MyCollider { get; private set; }
protected Animator myAnimator;
public NavMeshAgent Agent { get; set; }
// Hash
protected static readonly int RunStateHash = Animator.StringToHash("RunState");
protected static readonly int AttackHash = Animator.StringToHash("Attack");
protected static readonly int AttackStateHash = Animator.StringToHash("AttackState");
protected static readonly int NormalStateHash = Animator.StringToHash("NormalState");
// Const
private static readonly WaitForSeconds BeAttackedWaitTime = new(0.3f);
#endregion
#region abstract
protected abstract IEnumerator NormalAttackCoroutine();
#endregion
#region Unity built-in methods
protected override void Awake()
{
base.Awake();
rb = GetComponent<Rigidbody>();
MyCollider = GetComponent<Collider>();
Agent = GetComponent<NavMeshAgent>();
myAnimator = transform.Find("UnitRoot")?.GetComponent<Animator>();
if (myAnimator == null)
{
print("UnitRoot를 찾을 수 없습니다.");
}
}
protected override void Start()
{
base.Start();
TargetLayer = LayerMask.GetMask("Enemy");
waitAtkCooldown = new WaitForSeconds(AtkCooldown);
Agent.updateRotation = false;
SetAgentSpeed(ESpeedType.DEFAULT);
SetCurrentHp(MaxHp);
}
protected override void Update()
{
base.Update();
if (GameManager.Inst.InIslandPlayer && GameManager.Inst.InIslandPlayer.UseRigidbody)
{
if (!UseRigidbody)
{
UseRigidbodyMovement();
}
if (!beAttacked)
{
myAnimator.SetFloat(RunStateHash, 0.5f);
}
}
else if (GameManager.Inst.InIslandPlayer && !GameManager.Inst.InIslandPlayer.UseRigidbody)
{
if (UseRigidbody)
{
UseAgentMovement();
}
if (Agent.velocity.x != 0 || Agent.velocity.z != 0)
{
myAnimator.SetFloat(RunStateHash, 0.5f);
}
else if (!beAttacked)
{
myAnimator.SetFloat(RunStateHash, 0f);
}
}
var localScale = transform.localScale;
if (UseRigidbody)
{
var movement = GameManager.Inst.InIslandPlayer.Rb.velocity * (MoveSpd / GameManager.Inst.InIslandPlayer.MoveSpd);
rb.velocity = new Vector3(movement.x, 0, movement.z);
localScale.x = rb.velocity.x switch
{
> 0 => Mathf.Abs(localScale.x),
< 0 => -Mathf.Abs(localScale.x),
_ => localScale.x
};
}
else
{
if (Agent.velocity.x != 0)
{
localScale.x = Agent.velocity.x switch
{
> 0 => Mathf.Abs(localScale.x),
< 0 => -Mathf.Abs(localScale.x),
_ => localScale.x
};
}
else
{
if (Target)
{
var targetToDistanceX = Target.bounds.center.x - MyCollider.bounds.center.x;
localScale.x = targetToDistanceX switch
{
> 0 => Mathf.Abs(localScale.x),
< 0 => -Mathf.Abs(localScale.x),
_ => localScale.x
};
}
}
}
transform.localScale = localScale;
}
#endregion
#region Interfaces
//IDamageable
public void TakeDamage(float attackerPower, float attackerShieldPenetrationRate = default, Vector3? attackPos = null)
{
IsCombated = true;
var changeHp = Mathf.Max(CurrentHp - attackerPower, 0);
SetCurrentHp(changeHp);
// 죽었는지 체크
if (changeHp == 0f)
{
return;
}
StartCoroutine(nameof(BeAttacked));
}
// IAnimatorBridge
public virtual void AttackTiming()
{
if (!Target) return;
var myCenterPos = MyCollider.bounds.center;
var targetDir = (Target.bounds.center - myCenterPos).normalized;
if (!Physics.Raycast(MyCollider.bounds.center, targetDir, out var hit, AtkRange, TargetLayer)) return;
var iDamageable = hit.transform.GetComponent<IDamageable>();
iDamageable.TakeDamage(Atk);
}
public void SetIsAttacking(int boolValue) => isAttacking = boolValue == 1;
// IAiView
[field: Title("IAiView")]
[field: SerializeField] public float ViewRadius { get; set; } = 15f;
[field: SerializeField] public Collider[] Targets { get; set; } = new Collider[MAX_COLLIDERS];
[field: SerializeField] public Collider Target { get; set; }
[field: SerializeField] public LayerMask TargetLayer { get; set; }
private const int MAX_COLLIDERS = 30;
public void FindNearestTargetInRange(bool targetIsTrigger = true)
{
Array.Clear(Targets, 0, MAX_COLLIDERS);
var myCenterPos = MyCollider.bounds.center;
var numResults = Physics.OverlapSphereNonAlloc(myCenterPos, ViewRadius, Targets, TargetLayer,
targetIsTrigger ? QueryTriggerInteraction.Collide : QueryTriggerInteraction.Ignore);
if (numResults <= 0)
{
SetTarget(null);
return;
}
var nearestDistance = ViewRadius * ViewRadius;
Collider nearestTargetCollider = null;
for (var i = 0; i < numResults; i++)
{
var distanceSqrToTarget = (myCenterPos - Targets[i].bounds.center).sqrMagnitude;
if (distanceSqrToTarget >= nearestDistance) continue;
nearestDistance = distanceSqrToTarget;
nearestTargetCollider = Targets[i];
}
SetTarget(nearestTargetCollider);
}
public void SetTarget(Collider value)
{
Target = value;
if (value != null)
{
IsCombated = true;
BeAttackedInIdle = false;
}
}
public bool IsTargetWithinRange()
{
var attackInRange = Vector3.Distance(MyCollider.bounds.center, Target.bounds.center) <= AtkRange;
return attackInRange;
}
public bool GoOutOfBounds()
{
var defensePosInRange = Vector3.Distance(transform.position, DefensePos) <= DefenseRange;
return !defensePosInRange;
}
public void MoveTarget(Vector3 targetPos, ESpeedType speedType, float stopDistance)
{
if (Vector3.Distance(Agent.destination, targetPos) < 0.1f) return;
SetAgentSpeed(speedType);
Agent.stoppingDistance = stopDistance;
Agent.isStopped = false;
Agent.SetDestination(targetPos);
}
// INormalAttack
public void NormalAttack()
{
StartCoroutine(nameof(NormalAttackCoroutine));
}
public void StopNormalAttackCoroutine() => StopCoroutine(nameof(NormalAttackCoroutine));
public bool GetUsedNormalAttackCoroutine() => usedNormalAttackCoroutine;
#endregion
#region Custom methods
private void UseRigidbodyMovement()
{
UseRigidbody = true;
rb.isKinematic = false;
Agent.enabled = false;
}
private void UseAgentMovement()
{
UseRigidbody = false;
rb.isKinematic = true;
Agent.enabled = true;
if (Target) return;
MoveTarget(GameManager.Inst.InIslandPlayer.transform.position, ESpeedType.DEFAULT, GlobalValue.MAXIMUM_STOP_DISTANCE);
}
private IEnumerator BeAttacked()
{
beAttacked = true;
myAnimator.SetFloat(RunStateHash, 1f);
yield return BeAttackedWaitTime;
beAttacked = false;
}
private void SetAgentSpeed(ESpeedType speedType)
{
switch (speedType)
{
case ESpeedType.NONE:
print("speedType == NONE error");
break;
case ESpeedType.DEFAULT:
Agent.speed = MoveSpd;
break;
case ESpeedType.SLOW:
Agent.speed = MoveSpd * 0.5f;
break;
case ESpeedType.FAST:
Agent.speed = MoveSpd * 2f;
break;
default:
throw new ArgumentOutOfRangeException(nameof(speedType), speedType, null);
}
}
private void SetCurrentHp(float value) => CurrentHp = value;
#endregion
}
}