114 lines
3.7 KiB
C#
114 lines
3.7 KiB
C#
using System.Collections;
|
|
using Sirenix.OdinInspector;
|
|
using UnityEngine;
|
|
|
|
// ReSharper disable once CheckNamespace
|
|
namespace BlueWaterProject
|
|
{
|
|
public class ItemController : MonoBehaviour
|
|
{
|
|
[Title("아이템")]
|
|
[SerializeField] private Item item;
|
|
[SerializeField] private GameObject itemUiPrefab;
|
|
|
|
[Title("자동 파괴")]
|
|
[SerializeField] private bool useAutoDestroy = true;
|
|
[ShowIf("@useAutoDestroy")]
|
|
[SerializeField] private float autoDestroyTime = 30f;
|
|
|
|
[Title("획득")]
|
|
[SerializeField] private bool drawGizmos = true;
|
|
[SerializeField] private float radius = 5f;
|
|
[SerializeField] private float acquisitionTime = 1f;
|
|
[SerializeField] private LayerMask targetLayer;
|
|
|
|
private Collider[] hitColliders = new Collider[1];
|
|
private Collider targetCollider;
|
|
private WaitForSeconds lootCoroutineTime = new(0.5f);
|
|
private AudioSource audioSource;
|
|
private Transform itemsLoot;
|
|
private ItemUiController itemLootUi;
|
|
|
|
private void OnDrawGizmosSelected()
|
|
{
|
|
if (!drawGizmos) return;
|
|
|
|
Gizmos.DrawWireSphere(transform.position, radius);
|
|
}
|
|
|
|
public void Init(Item newItem)
|
|
{
|
|
item = newItem;
|
|
|
|
if (!itemsLoot)
|
|
{
|
|
itemsLoot = UiManager.Inst.OceanUi.MainCanvas.transform.Find("ItemsLoot");
|
|
}
|
|
|
|
var myPos = transform.position;
|
|
var screenPos = CameraManager.Inst.MainCam.WorldToScreenPoint(myPos);
|
|
itemLootUi = Instantiate(itemUiPrefab, screenPos, Quaternion.identity, itemsLoot).GetComponent<ItemUiController>();
|
|
itemLootUi.Init(transform);
|
|
}
|
|
|
|
private void Awake()
|
|
{
|
|
audioSource = transform.parent.Find("Audio").GetComponent<AudioSource>();
|
|
itemsLoot = UiManager.Inst.OceanUi.MainCanvas.transform.Find("ItemsLoot");
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
if (useAutoDestroy)
|
|
{
|
|
Destroy(transform.parent.gameObject, autoDestroyTime);
|
|
Destroy(itemLootUi.gameObject, autoDestroyTime);
|
|
}
|
|
|
|
StartCoroutine(LootCoroutine());
|
|
}
|
|
|
|
private IEnumerator LootCoroutine()
|
|
{
|
|
while (true)
|
|
{
|
|
var maxSize = Physics.OverlapSphereNonAlloc(transform.position, radius, hitColliders, targetLayer);
|
|
if (maxSize > 0)
|
|
{
|
|
targetCollider = hitColliders[0];
|
|
itemLootUi.ItemAcquisition();
|
|
break;
|
|
}
|
|
|
|
yield return lootCoroutineTime;
|
|
}
|
|
|
|
var startPosition = transform.position;
|
|
var elapsedTime = 0f;
|
|
|
|
while (elapsedTime < acquisitionTime)
|
|
{
|
|
elapsedTime += Time.deltaTime;
|
|
var t = elapsedTime / acquisitionTime;
|
|
t = Mathf.SmoothStep(0f, 1f, t);
|
|
|
|
transform.position = Vector3.Lerp(startPosition, targetCollider.transform.position, t);
|
|
yield return null;
|
|
}
|
|
|
|
item.Acquire();
|
|
itemLootUi.gameObject.SetActive(false);
|
|
UiManager.Inst.OceanUi.DropItemGroupController.ShowDropItemInfoUi(item);
|
|
|
|
if (audioSource && audioSource.resource)
|
|
{
|
|
audioSource.Play();
|
|
}
|
|
|
|
yield return new WaitForSeconds(audioSource.clip.length);
|
|
|
|
Destroy(transform.parent.gameObject);
|
|
Destroy(itemLootUi.gameObject);
|
|
}
|
|
}
|
|
} |