116 lines
3.2 KiB
C#
116 lines
3.2 KiB
C#
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
|
|
[DisallowMultipleComponent]
|
|
public class MemoryPiecePickup : MonoBehaviour
|
|
{
|
|
[Header("Reference")]
|
|
[SerializeField] private MemoryProgressManager memoryProgressManager;
|
|
|
|
[Header("Settings")]
|
|
[SerializeField] private int amount = 1;
|
|
[SerializeField] private bool destroyAfterPickup = false;
|
|
[Tooltip("Destroy After Pickup이 꺼져 있을 때, 획득 후 오브젝트를 비활성화합니다.")]
|
|
[SerializeField] private bool disableAfterPickup = true;
|
|
[SerializeField] private bool autoFindManager = true;
|
|
[SerializeField] private string playerTag = "Player";
|
|
[Tooltip("Player Tag가 비어 있을 때 아무 Collider나 획득을 허용합니다. 테스트용이 아니면 끄는 것을 추천합니다.")]
|
|
[SerializeField] private bool allowPickupWhenPlayerTagEmpty = false;
|
|
|
|
[Header("Events")]
|
|
public UnityEvent onPickedUp = new UnityEvent();
|
|
public UnityEvent onPickupFailed = new UnityEvent();
|
|
|
|
private bool pickedUp;
|
|
|
|
private void Awake()
|
|
{
|
|
if (memoryProgressManager == null && autoFindManager)
|
|
memoryProgressManager = FindFirstObjectByType<MemoryProgressManager>();
|
|
}
|
|
|
|
public void PickUp()
|
|
{
|
|
TryPickUp();
|
|
}
|
|
|
|
public bool TryPickUp()
|
|
{
|
|
if (pickedUp)
|
|
return false;
|
|
|
|
if (memoryProgressManager == null)
|
|
{
|
|
Debug.LogWarning("[MemoryPiecePickup] MemoryProgressManager가 연결되지 않았습니다.", this);
|
|
onPickupFailed?.Invoke();
|
|
return false;
|
|
}
|
|
|
|
int addedAmount = memoryProgressManager.AddMemoryPiece(amount);
|
|
|
|
if (addedAmount <= 0)
|
|
{
|
|
onPickupFailed?.Invoke();
|
|
return false;
|
|
}
|
|
|
|
pickedUp = true;
|
|
onPickedUp?.Invoke();
|
|
|
|
if (destroyAfterPickup)
|
|
Destroy(gameObject);
|
|
else if (disableAfterPickup)
|
|
gameObject.SetActive(false);
|
|
|
|
return true;
|
|
}
|
|
|
|
public void ResetPickup()
|
|
{
|
|
pickedUp = false;
|
|
gameObject.SetActive(true);
|
|
}
|
|
|
|
public void SetMemoryProgressManager(MemoryProgressManager manager)
|
|
{
|
|
memoryProgressManager = manager;
|
|
}
|
|
|
|
private void OnTriggerEnter(Collider other)
|
|
{
|
|
if (pickedUp)
|
|
return;
|
|
|
|
if (IsPlayerCollider(other))
|
|
TryPickUp();
|
|
}
|
|
|
|
private bool IsPlayerCollider(Collider other)
|
|
{
|
|
if (other == null)
|
|
return false;
|
|
|
|
if (string.IsNullOrWhiteSpace(playerTag))
|
|
return allowPickupWhenPlayerTagEmpty;
|
|
|
|
return other.CompareTag(playerTag);
|
|
}
|
|
|
|
#if UNITY_EDITOR
|
|
private void OnValidate()
|
|
{
|
|
amount = Mathf.Max(1, amount);
|
|
|
|
if (string.IsNullOrWhiteSpace(playerTag) && !allowPickupWhenPlayerTagEmpty)
|
|
{
|
|
Debug.LogWarning("MemoryPiecePickup: Player Tag가 비어 있고 Allow Pickup When Player Tag Empty가 꺼져 있습니다. 이 상태에서는 트리거 획득이 작동하지 않습니다.", this);
|
|
}
|
|
|
|
if (destroyAfterPickup && disableAfterPickup)
|
|
{
|
|
Debug.LogWarning("MemoryPiecePickup: Destroy After Pickup이 켜져 있으면 Disable After Pickup은 의미가 없습니다.", this);
|
|
}
|
|
}
|
|
#endif
|
|
}
|