81 lines
2.5 KiB
C#
81 lines
2.5 KiB
C#
|
using System.Collections;
|
||
|
using BlueWater.Audios;
|
||
|
using BlueWater.Interfaces;
|
||
|
using BlueWater.Maps;
|
||
|
using Sirenix.OdinInspector;
|
||
|
using UnityEngine;
|
||
|
|
||
|
namespace BlueWater
|
||
|
{
|
||
|
public class Rockfall : DamageableProps
|
||
|
{
|
||
|
[Title("컴포넌트")]
|
||
|
[SerializeField]
|
||
|
private Rigidbody _rigidbody;
|
||
|
|
||
|
[SerializeField, Required]
|
||
|
private SphereCollider _sphereCollider;
|
||
|
|
||
|
[Title("충돌 설정")]
|
||
|
[SerializeField]
|
||
|
private LayerMask _targetLayer;
|
||
|
|
||
|
[SerializeField]
|
||
|
private LayerMask _groundLayer;
|
||
|
|
||
|
[SerializeField, Range(0f, 1f)]
|
||
|
private float _checkDistance = 0.1f;
|
||
|
|
||
|
[SerializeField, Range(0, 5)]
|
||
|
private int _attackDamage = 1;
|
||
|
|
||
|
[Title("Ground Crash 설정")]
|
||
|
[SerializeField]
|
||
|
private string _groundCrashSfxName;
|
||
|
|
||
|
[SerializeField]
|
||
|
private ParticleSystem _groundCrashParticle;
|
||
|
|
||
|
private Collider[] _hitColliders = new Collider[4];
|
||
|
private bool _isGrounded;
|
||
|
private bool _isAttacked;
|
||
|
|
||
|
private IEnumerator Start()
|
||
|
{
|
||
|
_sphereCollider.enabled = false;
|
||
|
SpawnLocation = MapManager.Instance.SandMoleMapController.ParticleInstantiateLocation;
|
||
|
while (!_isGrounded)
|
||
|
{
|
||
|
_isGrounded = Physics.Raycast(transform.position, Vector3.down, _checkDistance, _groundLayer);
|
||
|
yield return null;
|
||
|
}
|
||
|
|
||
|
if (_rigidbody)
|
||
|
{
|
||
|
_rigidbody.isKinematic = true;
|
||
|
}
|
||
|
_sphereCollider.enabled = true;
|
||
|
|
||
|
if (!string.IsNullOrEmpty(_groundCrashSfxName))
|
||
|
{
|
||
|
AudioManager.Instance.PlaySfx(_groundCrashSfxName);
|
||
|
}
|
||
|
|
||
|
if (_groundCrashParticle && SpawnLocation)
|
||
|
{
|
||
|
Instantiate(_groundCrashParticle, transform.position, Quaternion.identity, SpawnLocation);
|
||
|
}
|
||
|
|
||
|
var hitCount = Physics.OverlapSphereNonAlloc(_sphereCollider.bounds.center, _sphereCollider.radius,
|
||
|
_hitColliders, _targetLayer, QueryTriggerInteraction.Collide);
|
||
|
for (var i = 0; i < hitCount; i++)
|
||
|
{
|
||
|
var hitCollider = _hitColliders[i];
|
||
|
var iDamageable = hitCollider.GetComponentInParent<IDamageable>();
|
||
|
if (iDamageable == null || !iDamageable.CanDamage()) continue;
|
||
|
|
||
|
iDamageable.TakeDamage(_attackDamage);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|