Projext/Assets/5.TestScript/PlayerInteraction.cs

61 lines
2.0 KiB
C#
Raw Normal View History

2026-01-29 06:58:38 +00:00
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<EquippableItem>(out var item))
{
if (playerStats.Strength >= item.Config.RequiredStrength)
{
EquipWeapon(item);
2026-01-29 06:58:38 +00:00
break;
}
else { CinemachineShake.Instance?.ShakeNoNo(); continue; }
2026-01-29 06:58:38 +00:00
}
if (hit.TryGetComponent<HealthPotion>(out var potion)) { potion.Use(GetComponent<PlayerHealth>()); break; }
if (hit.TryGetComponent<HealthAltar>(out var altar)) { altar.Use(GetComponent<PlayerHealth>()); break; }
2026-01-29 06:58:38 +00:00
}
}
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();
}
}
2026-01-29 06:58:38 +00:00
}