2024-06-14 09:11:35 +00:00
|
|
|
using System.Collections;
|
|
|
|
using BlueWater.Audios;
|
|
|
|
using BlueWater.Interfaces;
|
|
|
|
using BlueWater.Maps;
|
|
|
|
using Sirenix.OdinInspector;
|
|
|
|
using UnityEngine;
|
2024-06-14 09:34:58 +00:00
|
|
|
using UnityEngine.Rendering.Universal;
|
|
|
|
using UnityEngine.Serialization;
|
2024-06-14 09:11:35 +00:00
|
|
|
|
|
|
|
namespace BlueWater
|
|
|
|
{
|
|
|
|
public class Rockfall : DamageableProps
|
|
|
|
{
|
|
|
|
[Title("컴포넌트")]
|
|
|
|
[SerializeField]
|
|
|
|
private Rigidbody _rigidbody;
|
|
|
|
|
|
|
|
[SerializeField, Required]
|
|
|
|
private SphereCollider _sphereCollider;
|
|
|
|
|
2024-06-14 09:34:58 +00:00
|
|
|
[SerializeField]
|
|
|
|
private DecalProjector _indicator;
|
|
|
|
|
|
|
|
[Title("표시기 설정")]
|
|
|
|
[SerializeField]
|
|
|
|
private bool _isUsingIndicator = true;
|
|
|
|
|
2024-06-14 09:11:35 +00:00
|
|
|
[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;
|
2024-06-14 09:34:58 +00:00
|
|
|
|
|
|
|
// Hashes
|
|
|
|
private static readonly int _fillHash = Shader.PropertyToID("_Fill");
|
2024-06-14 09:11:35 +00:00
|
|
|
|
|
|
|
private IEnumerator Start()
|
|
|
|
{
|
|
|
|
_sphereCollider.enabled = false;
|
|
|
|
SpawnLocation = MapManager.Instance.SandMoleMapController.ParticleInstantiateLocation;
|
2024-06-14 09:34:58 +00:00
|
|
|
BasicSetting();
|
|
|
|
ShowIndicator();
|
|
|
|
|
|
|
|
var startDistance = float.PositiveInfinity;
|
2024-06-14 09:11:35 +00:00
|
|
|
while (!_isGrounded)
|
|
|
|
{
|
2024-06-14 09:34:58 +00:00
|
|
|
if (!Physics.Raycast(transform.position, Vector3.down, out var hit, 10f, _groundLayer)) continue;
|
|
|
|
|
|
|
|
if (float.IsPositiveInfinity(startDistance))
|
|
|
|
{
|
|
|
|
startDistance = hit.distance;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_isUsingIndicator && _indicator)
|
|
|
|
{
|
|
|
|
var fillValue = Mathf.Lerp(1f, 0f, hit.distance / startDistance);
|
|
|
|
_indicator.material.SetFloat(_fillHash, fillValue);
|
|
|
|
}
|
|
|
|
_isGrounded = hit.distance <= _checkDistance;
|
2024-06-14 09:11:35 +00:00
|
|
|
yield return null;
|
|
|
|
}
|
2024-06-14 09:34:58 +00:00
|
|
|
|
|
|
|
_indicator.material.SetFloat(_fillHash, 1f);
|
|
|
|
HideIndicator();
|
2024-06-14 09:11:35 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2024-06-14 09:34:58 +00:00
|
|
|
|
|
|
|
private void BasicSetting()
|
|
|
|
{
|
|
|
|
if (!_isUsingIndicator || !_indicator) return;
|
|
|
|
|
|
|
|
_indicator.scaleMode = DecalScaleMode.InheritFromHierarchy;
|
|
|
|
_indicator.material = new Material(_indicator.material);
|
|
|
|
_indicator.material.SetFloat(_fillHash, 0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void HideIndicator()
|
|
|
|
{
|
|
|
|
if (!_isUsingIndicator || !_indicator) return;
|
|
|
|
|
|
|
|
_indicator.enabled = false;
|
|
|
|
_indicator.material.SetFloat(_fillHash, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void ShowIndicator()
|
|
|
|
{
|
|
|
|
if (!_isUsingIndicator || !_indicator) return;
|
|
|
|
|
|
|
|
_indicator.material.SetFloat(_fillHash, 0);
|
|
|
|
_indicator.enabled = true;
|
|
|
|
}
|
2024-06-14 09:11:35 +00:00
|
|
|
}
|
|
|
|
}
|