2024-06-03 18:26:03 +00:00
|
|
|
using System;
|
2024-06-24 15:05:05 +00:00
|
|
|
using BlueWater.Audios;
|
2024-06-03 18:26:03 +00:00
|
|
|
using BlueWater.Interfaces;
|
|
|
|
using Sirenix.OdinInspector;
|
|
|
|
using UnityEngine;
|
2024-06-14 20:36:32 +00:00
|
|
|
using UnityEngine.Serialization;
|
2024-06-03 18:26:03 +00:00
|
|
|
|
|
|
|
namespace BlueWater
|
|
|
|
{
|
2024-06-14 20:36:32 +00:00
|
|
|
public class ProjectileController : MonoBehaviour
|
2024-06-03 18:26:03 +00:00
|
|
|
{
|
2024-06-14 20:36:32 +00:00
|
|
|
[Title("컴포넌트")]
|
|
|
|
[SerializeField, Required]
|
|
|
|
private Rigidbody _rigidbody;
|
|
|
|
|
|
|
|
[SerializeField]
|
|
|
|
private SphereCollider _sphereCollider;
|
2024-06-03 18:26:03 +00:00
|
|
|
|
2024-06-14 20:36:32 +00:00
|
|
|
[field: Title("파티클 설정")]
|
|
|
|
[FormerlySerializedAs("_projectileParticle")]
|
2024-06-03 18:26:03 +00:00
|
|
|
[SerializeField]
|
2024-06-14 20:36:32 +00:00
|
|
|
private GameObject _projectilePrefab;
|
2024-06-03 18:26:03 +00:00
|
|
|
|
|
|
|
[SerializeField]
|
|
|
|
private GameObject _muzzleParticle;
|
|
|
|
|
2024-06-14 20:36:32 +00:00
|
|
|
[field: SerializeField]
|
|
|
|
public GameObject ImpactParticle { get; private set; }
|
|
|
|
|
2024-06-03 18:26:03 +00:00
|
|
|
[Title("충돌체 설정")]
|
2024-06-14 20:36:32 +00:00
|
|
|
[SerializeField, Tooltip("Sphere Collider가 없는 경우, 기본 충돌 크기(반지름)"), ShowIf("@!_sphereCollider")]
|
2024-06-03 18:26:03 +00:00
|
|
|
private float _colliderRadius = 1f;
|
2024-06-14 23:37:41 +00:00
|
|
|
// [SerializeField, Range(0f, 1f), Tooltip("타겟보다 해당 값만큼 떨어진 위치에서 충돌")]
|
|
|
|
// private float _collideOffset;
|
2024-06-03 18:26:03 +00:00
|
|
|
|
|
|
|
[SerializeField]
|
|
|
|
private int _attackDamage;
|
|
|
|
|
|
|
|
[SerializeField]
|
|
|
|
private LayerMask _targetLayer;
|
|
|
|
|
|
|
|
[SerializeField]
|
|
|
|
private bool _useAutoDestroy = true;
|
|
|
|
|
|
|
|
[SerializeField, ShowIf("@_useAutoDestroy")]
|
|
|
|
private float _autoDestroyTime = 10f;
|
2024-06-24 15:05:05 +00:00
|
|
|
|
|
|
|
[SerializeField]
|
|
|
|
private string _awakeSfxName;
|
|
|
|
|
2024-06-14 23:37:41 +00:00
|
|
|
public float SphereRadius { get; private set; }
|
2024-06-03 18:26:03 +00:00
|
|
|
|
|
|
|
private float _detectionDistance;
|
2024-06-14 23:37:41 +00:00
|
|
|
private Collider[] _hitColliders;
|
2024-06-03 18:26:03 +00:00
|
|
|
|
|
|
|
public Action OnHitAction;
|
|
|
|
|
|
|
|
private void OnDrawGizmosSelected()
|
|
|
|
{
|
2024-06-14 23:37:41 +00:00
|
|
|
if (SphereRadius == 0f) return;
|
|
|
|
|
2024-06-03 18:26:03 +00:00
|
|
|
var direction = _rigidbody ? _rigidbody.linearVelocity.normalized : transform.forward;
|
|
|
|
|
|
|
|
Gizmos.color = Color.red;
|
2024-06-14 23:37:41 +00:00
|
|
|
Gizmos.DrawWireSphere(transform.position, SphereRadius);
|
2024-06-03 18:26:03 +00:00
|
|
|
Gizmos.DrawLine(transform.position, transform.position + direction * _detectionDistance);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void Start()
|
|
|
|
{
|
2024-06-24 15:05:05 +00:00
|
|
|
if (!string.IsNullOrEmpty(_awakeSfxName))
|
|
|
|
{
|
|
|
|
AudioManager.Instance.PlaySfx(_awakeSfxName);
|
|
|
|
}
|
|
|
|
|
2024-06-14 23:37:41 +00:00
|
|
|
SphereRadius = _sphereCollider ? _sphereCollider.radius : _colliderRadius;
|
|
|
|
_hitColliders = new Collider[1];
|
|
|
|
|
2024-06-03 18:26:03 +00:00
|
|
|
if (_useAutoDestroy)
|
|
|
|
{
|
|
|
|
Destroy(gameObject, _autoDestroyTime);
|
|
|
|
}
|
2024-06-14 20:36:32 +00:00
|
|
|
|
|
|
|
if (_projectilePrefab)
|
|
|
|
{
|
|
|
|
_projectilePrefab = Instantiate(_projectilePrefab, transform.position, transform.rotation, transform);
|
|
|
|
}
|
2024-06-03 18:26:03 +00:00
|
|
|
|
|
|
|
if (_muzzleParticle)
|
|
|
|
{
|
|
|
|
_muzzleParticle = Instantiate(_muzzleParticle, transform.position, transform.rotation, transform);
|
|
|
|
Destroy(_muzzleParticle, 1.5f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void FixedUpdate()
|
|
|
|
{
|
|
|
|
if (_rigidbody.linearVelocity.magnitude != 0)
|
|
|
|
{
|
|
|
|
transform.rotation = Quaternion.LookRotation(_rigidbody.linearVelocity);
|
|
|
|
}
|
2024-06-14 23:37:41 +00:00
|
|
|
|
|
|
|
//var direction = _rigidbody.linearVelocity.normalized;
|
2024-06-03 18:26:03 +00:00
|
|
|
|
|
|
|
_detectionDistance = _rigidbody.linearVelocity.magnitude * Time.deltaTime;
|
2024-06-14 23:37:41 +00:00
|
|
|
var hitCount = Physics.OverlapSphereNonAlloc(transform.position, SphereRadius, _hitColliders, _targetLayer, QueryTriggerInteraction.Collide);
|
|
|
|
if (hitCount == 0) return;
|
2024-06-03 18:26:03 +00:00
|
|
|
|
2024-06-14 23:37:41 +00:00
|
|
|
// transform.position = raycastHit.point + raycastHit.normal * _collideOffset;
|
|
|
|
var hitCollider = _hitColliders[0];
|
2024-06-25 06:56:28 +00:00
|
|
|
if (hitCollider.gameObject.layer == LayerMask.NameToLayer("Props")
|
|
|
|
&& !hitCollider.CompareTag("DamageableProps"))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-06-03 18:26:03 +00:00
|
|
|
var trailParticles = GetComponentsInChildren<ParticleSystem>();
|
|
|
|
foreach (var element in trailParticles)
|
|
|
|
{
|
|
|
|
if (!element.gameObject.name.Contains("Trail")) continue;
|
|
|
|
|
|
|
|
element.transform.SetParent(null);
|
|
|
|
Destroy(element.gameObject, 2f);
|
|
|
|
}
|
|
|
|
|
2024-06-14 23:37:41 +00:00
|
|
|
var impactParticle = Instantiate(ImpactParticle, transform.position, Quaternion.identity);
|
2024-06-03 18:26:03 +00:00
|
|
|
// TODO : HitBox가 레이어로 설정되어있으도, 부모 객체 Player를 계속 가져오는 버그가 있음
|
2024-06-14 23:37:41 +00:00
|
|
|
var iDamageable = hitCollider.GetComponentInParent<IDamageable>();
|
2024-06-03 18:26:03 +00:00
|
|
|
if (iDamageable != null && iDamageable.CanDamage())
|
|
|
|
{
|
|
|
|
iDamageable.TakeDamage(_attackDamage);
|
|
|
|
OnHitAction?.Invoke();
|
|
|
|
}
|
|
|
|
|
2024-06-14 20:36:32 +00:00
|
|
|
Destroy(_projectilePrefab, 3f);
|
2024-06-03 18:26:03 +00:00
|
|
|
if (impactParticle)
|
|
|
|
{
|
|
|
|
Destroy(impactParticle, 3.5f);
|
|
|
|
}
|
|
|
|
Destroy(gameObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Initialize(int attackDamage, LayerMask targetLayer, Action onHitAction = null)
|
|
|
|
{
|
|
|
|
_attackDamage = attackDamage;
|
|
|
|
_targetLayer = targetLayer;
|
|
|
|
OnHitAction = onHitAction;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void AddForce(Vector3 force, ForceMode forceMode) => _rigidbody.AddForce(force, forceMode);
|
|
|
|
}
|
|
|
|
}
|