Files
WhiteMan_Unity2D/Assets/02_Scripts/Combat/PlayerWeaponInventory.cs
DESKTOP-VVOCIJO\PC 4a0b07701e 2026-05-19 무기추가
진행중인 사항 -
InputManager + .inputactions: WeaponSlot1/2/3 액션 추가, 키 1/2/3 매핑
PlayerController 통합:
Player에 PlayerWeaponInventory 컴포넌트 자동 부착
OnWeaponChanged 구독 → idle/walk State 이름 동적 교체
OnPunchInput 분기: 무장 시 weapon.AttackRootNode 사용
WeaponSlot 입력 핸들러 3개 추가 (EquipUnarmed / EquipSlot(0) / EquipSlot(1))
2026-05-19 18:05:05 +09:00

79 lines
2.7 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
// ============================================================================
// PlayerWeaponInventory
// ----------------------------------------------------------------------------
// 플레이어가 보유한 무기 목록 + 현재 장착 무기 관리.
// PlayerController가 OnWeaponChanged 이벤트를 구독해서 애니메이션/공격 콤보 교체.
//
// 슬롯 매핑 (외부 코드의 EquipSlot 인덱스):
// -1 → 맨손 (CurrentWeapon == null)
// 0 → 첫 번째로 픽업한 무기
// 1 → 두 번째로 픽업한 무기
// ...
//
// 입력 키 매핑 (PlayerController에서 처리):
// Key 1 → EquipUnarmed()
// Key 2 → EquipSlot(0)
// Key 3 → EquipSlot(1)
// ============================================================================
public class PlayerWeaponInventory : MonoBehaviour
{
private readonly List<WeaponData> _weapons = new();
private int _currentIndex = -1; // -1 = 맨손
// 무기 교체 시 발화. null이면 맨손.
public event Action<WeaponData> OnWeaponChanged;
public WeaponData CurrentWeapon =>
_currentIndex >= 0 && _currentIndex < _weapons.Count
? _weapons[_currentIndex]
: null;
public bool IsArmed => CurrentWeapon != null;
public int Count => _weapons.Count;
// 무기 추가. 이미 보유 중이면 false 반환 (중복 픽업 방지).
// 첫 픽업 시 자동 장착할지는 호출자가 OnWeaponChanged를 보고 결정.
public bool Pickup(WeaponData weapon)
{
if (weapon == null) return false;
if (_weapons.Contains(weapon)) return false;
_weapons.Add(weapon);
// 첫 픽업이면 자동으로 장착해주면 사용자 편의 ↑.
if (_weapons.Count == 1 && _currentIndex == -1)
{
_currentIndex = 0;
OnWeaponChanged?.Invoke(_weapons[0]);
}
return true;
}
// 맨손 상태로 전환.
public void EquipUnarmed()
{
if (_currentIndex == -1) return;
_currentIndex = -1;
OnWeaponChanged?.Invoke(null);
}
// 슬롯 번호로 직접 장착. 슬롯이 비어있으면 무시.
public void EquipSlot(int slotIndex)
{
if (slotIndex < 0 || slotIndex >= _weapons.Count) return;
if (_currentIndex == slotIndex) return;
_currentIndex = slotIndex;
OnWeaponChanged?.Invoke(_weapons[slotIndex]);
}
// 디버그용: 슬롯에 해당 무기가 있는지 확인.
public bool HasWeaponInSlot(int slotIndex)
{
return slotIndex >= 0 && slotIndex < _weapons.Count && _weapons[slotIndex] != null;
}
}