115 lines
3.3 KiB
C#
115 lines
3.3 KiB
C#
|
using System.Collections;
|
||
|
using BlueWater.Players.Combat;
|
||
|
using Sirenix.OdinInspector;
|
||
|
using UnityEngine;
|
||
|
|
||
|
namespace BlueWater.Items
|
||
|
{
|
||
|
public class DropItemController : MonoBehaviour
|
||
|
{
|
||
|
[Title("컴포넌트")]
|
||
|
[SerializeField]
|
||
|
private Rigidbody _rigidbody;
|
||
|
|
||
|
[SerializeField]
|
||
|
private Collider _collider;
|
||
|
|
||
|
[SerializeField]
|
||
|
private SpriteRenderer _spriteRenderer;
|
||
|
|
||
|
[Title("자동 파괴")]
|
||
|
[SerializeField]
|
||
|
private bool _useAutoDestroy = true;
|
||
|
|
||
|
[SerializeField, ShowIf("@_useAutoDestroy")]
|
||
|
private float _autoDestroyTime = 30f;
|
||
|
|
||
|
[Title("획득")]
|
||
|
[SerializeField]
|
||
|
private bool _drawGizmos = true;
|
||
|
|
||
|
[SerializeField]
|
||
|
private float _distance = 5f;
|
||
|
|
||
|
[SerializeField]
|
||
|
private float _acquisitionTime = 1f;
|
||
|
|
||
|
private ItemSlot _itemSlot;
|
||
|
private Collider _targetCollider;
|
||
|
private readonly WaitForSeconds _lootCoroutineTime = new(0.5f);
|
||
|
|
||
|
private void OnDrawGizmosSelected()
|
||
|
{
|
||
|
if (!_drawGizmos) return;
|
||
|
|
||
|
Gizmos.DrawWireSphere(transform.position, _distance);
|
||
|
}
|
||
|
|
||
|
private void Start()
|
||
|
{
|
||
|
if (_useAutoDestroy)
|
||
|
{
|
||
|
Destroy(gameObject, _autoDestroyTime);
|
||
|
}
|
||
|
|
||
|
StartCoroutine(LootCoroutine());
|
||
|
}
|
||
|
|
||
|
public void Initialize(ItemSlot itemSlotValue)
|
||
|
{
|
||
|
_itemSlot = itemSlotValue;
|
||
|
if (_spriteRenderer)
|
||
|
{
|
||
|
_spriteRenderer.sprite = ItemManager.Instance.ItemDictionary[_itemSlot.Idx].Sprite;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private IEnumerator LootCoroutine()
|
||
|
{
|
||
|
while (true)
|
||
|
{
|
||
|
_targetCollider ??= FindAnyObjectByType<CombatPlayer>().GetComponent<Collider>();
|
||
|
if (_targetCollider == null)
|
||
|
{
|
||
|
yield return _lootCoroutineTime;
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
var targetDistance = Vector3.Distance(_targetCollider.transform.position, transform.position);
|
||
|
if (targetDistance <= _distance) break;
|
||
|
|
||
|
yield return _lootCoroutineTime;
|
||
|
}
|
||
|
|
||
|
if (_rigidbody)
|
||
|
{
|
||
|
_rigidbody.isKinematic = true;
|
||
|
_rigidbody.useGravity = false;
|
||
|
}
|
||
|
if (_collider)
|
||
|
{
|
||
|
_collider.enabled = false;
|
||
|
}
|
||
|
|
||
|
var startPosition = transform.position;
|
||
|
var elapsedTime = 0f;
|
||
|
|
||
|
while (elapsedTime < _acquisitionTime)
|
||
|
{
|
||
|
if (_targetCollider == null)
|
||
|
{
|
||
|
yield break;
|
||
|
}
|
||
|
elapsedTime += Time.deltaTime;
|
||
|
var t = elapsedTime / _acquisitionTime;
|
||
|
t = Mathf.SmoothStep(0f, 1f, t);
|
||
|
|
||
|
transform.parent.position = Vector3.Lerp(startPosition, _targetCollider.bounds.center, t);
|
||
|
yield return null;
|
||
|
}
|
||
|
|
||
|
ItemManager.Instance.Acquire(_itemSlot);
|
||
|
Destroy(gameObject);
|
||
|
}
|
||
|
}
|
||
|
}
|