88 lines
2.7 KiB
C#
88 lines
2.7 KiB
C#
using UnityEngine;
|
|
|
|
public class PlayerInteraction : MonoBehaviour
|
|
{
|
|
[Header("--- 설정 ---")]
|
|
[SerializeField] private float interactRange = 3f;
|
|
[SerializeField] private LayerMask itemLayer;
|
|
[SerializeField] private Transform handSlot;
|
|
[SerializeField] private Stats playerStats;
|
|
|
|
private EquippableItem _currentWeapon;
|
|
public EquippableItem CurrentWeapon => _currentWeapon;
|
|
|
|
public void TryInteract()
|
|
{
|
|
Collider[] hits = Physics.OverlapSphere(transform.position, interactRange, itemLayer);
|
|
foreach (var hit in hits)
|
|
{
|
|
// 🏹 [새로 추가] 화살 아이템 습득
|
|
if (hit.TryGetComponent<ArrowItem>(out var arrowItem))
|
|
{
|
|
PickupArrow(arrowItem);
|
|
break;
|
|
}
|
|
|
|
// 기존 무기 습득
|
|
if (hit.TryGetComponent<EquippableItem>(out var item))
|
|
{
|
|
if (playerStats.Strength >= item.Config.RequiredStrength)
|
|
{
|
|
EquipWeapon(item);
|
|
break;
|
|
}
|
|
else { CinemachineShake.Instance?.ShakeNoNo(); continue; }
|
|
}
|
|
|
|
// 기존 포션/제단
|
|
if (hit.TryGetComponent<HealthPotion>(out var potion)) { potion.Use(GetComponent<PlayerHealth>()); break; }
|
|
if (hit.TryGetComponent<HealthAltar>(out var altar)) { altar.Use(GetComponent<PlayerHealth>()); break; }
|
|
}
|
|
}
|
|
|
|
// 🏹 [새 함수] 화살 습득 처리
|
|
private void PickupArrow(ArrowItem arrowItem)
|
|
{
|
|
if (arrowItem == null) return;
|
|
|
|
// PlayerAttack 찾아서 화살 교체
|
|
PlayerAttack playerAttack = GetComponent<PlayerAttack>();
|
|
if (playerAttack != null)
|
|
{
|
|
arrowItem.Pickup(playerAttack);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning("PlayerAttack 컴포넌트를 찾을 수 없습니다!");
|
|
}
|
|
}
|
|
|
|
private void EquipWeapon(EquippableItem item)
|
|
{
|
|
if (_currentWeapon != null) _currentWeapon.OnDropped(transform.forward);
|
|
_currentWeapon = item;
|
|
_currentWeapon.OnPickedUp(handSlot);
|
|
|
|
if (playerStats != null)
|
|
{
|
|
playerStats.weaponDamage = item.Config.BaseDamage;
|
|
playerStats.UpdateWeaponWeight(item.Config.RequiredStrength);
|
|
}
|
|
|
|
// ⭐ 무기 장착 즉시 UI 갱신
|
|
FindObjectOfType<PlayerStatsUI>()?.UpdateStatTexts();
|
|
}
|
|
|
|
public void ClearCurrentWeapon()
|
|
{
|
|
_currentWeapon = null;
|
|
if (playerStats != null)
|
|
{
|
|
playerStats.weaponDamage = 0;
|
|
playerStats.ResetWeight();
|
|
|
|
// ⭐ 무기 제거 즉시 UI 갱신
|
|
FindObjectOfType<PlayerStatsUI>()?.UpdateStatTexts();
|
|
}
|
|
}
|
|
} |