장비 개념 추가 및 Kaykit 애셋 추가
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Unity.Netcode;
|
||||
using UnityEngine;
|
||||
using System;
|
||||
|
||||
public class PlayerInventory : NetworkBehaviour
|
||||
{
|
||||
@@ -9,6 +10,30 @@ public class PlayerInventory : NetworkBehaviour
|
||||
public int slotCount = 5;
|
||||
public float maxWeight = 50f;
|
||||
|
||||
[System.Serializable]
|
||||
public struct DefaultItem
|
||||
{
|
||||
public ItemData item; // 인스펙터에서 아이템 에셋 드래그
|
||||
public int amount; // 초기 개수
|
||||
}
|
||||
|
||||
[Header("Starting Loadout")]
|
||||
[SerializeField] private List<DefaultItem> startingItems; // 기본 지급 아이템 리스트
|
||||
|
||||
// 1. 실제 데이터 저장소 (Private)
|
||||
private NetworkVariable<int> _selectedSlotIndex = new NetworkVariable<int>(0,
|
||||
NetworkVariableReadPermission.Everyone, NetworkVariableWritePermission.Server);
|
||||
|
||||
// 2. 외부에서 읽기 전용으로 값에 접근 (기존 유지)
|
||||
public int SelectedSlotIndex => _selectedSlotIndex.Value;
|
||||
|
||||
// 3. [핵심] 외부에서 이벤트에 함수를 등록(+=)할 수 있도록 델리게이트 노출
|
||||
public NetworkVariable<int>.OnValueChangedDelegate OnSlotChanged
|
||||
{
|
||||
get => _selectedSlotIndex.OnValueChanged;
|
||||
set => _selectedSlotIndex.OnValueChanged = value;
|
||||
}
|
||||
|
||||
public struct InventorySlot : INetworkSerializable, IEquatable<InventorySlot>
|
||||
{
|
||||
public int ItemID; // string에서 int로 변경
|
||||
@@ -39,12 +64,9 @@ public class PlayerInventory : NetworkBehaviour
|
||||
|
||||
public override void OnNetworkSpawn()
|
||||
{
|
||||
// 서버에서만 슬롯 초기화
|
||||
if (IsServer && Slots.Count == 0)
|
||||
if (IsServer)
|
||||
{
|
||||
// 설정된 slotCount만큼 데이터 슬롯 생성
|
||||
for (int i = 0; i < slotCount; i++)
|
||||
Slots.Add(new InventorySlot { ItemID = -1, Amount = 0 });
|
||||
InitializeInventory();
|
||||
}
|
||||
|
||||
// 로컬 플레이어라면 UI 연결
|
||||
@@ -86,4 +108,64 @@ public class PlayerInventory : NetworkBehaviour
|
||||
_currentWeight.Value += addedWeight;
|
||||
}
|
||||
}
|
||||
|
||||
// 슬롯 선택 요청 (최신 RPC 방식 적용)
|
||||
[Rpc(SendTo.Server, InvokePermission = RpcInvokePermission.Everyone)]
|
||||
public void ChangeSelectedSlotRpc(int index)
|
||||
{
|
||||
if (index >= 0 && index < slotCount)
|
||||
{
|
||||
Debug.Log($"<color=green>[PlayerInventory] 슬롯 선택 변경 요청: {index}</color>");
|
||||
_selectedSlotIndex.Value = index;
|
||||
}
|
||||
}
|
||||
|
||||
// 현재 선택된 슬롯의 아이템 데이터를 가져오는 편의 기능
|
||||
public ItemData GetSelectedItemData()
|
||||
{
|
||||
var slot = Slots[SelectedSlotIndex];
|
||||
if (slot.ItemID == -1) return null;
|
||||
return ItemDatabase.Instance.GetItemByID(slot.ItemID);
|
||||
}
|
||||
|
||||
private void InitializeInventory()
|
||||
{
|
||||
// 슬롯을 깨끗하게 비우고 시작 (이미 데이터가 있을 경우를 대비)
|
||||
if (Slots.Count > 0) Slots.Clear();
|
||||
_currentWeight.Value = 0;
|
||||
|
||||
// 전체 슬롯 개수만큼 빈 슬롯(-1) 먼저 생성
|
||||
for (int i = 0; i < slotCount; i++)
|
||||
{
|
||||
Slots.Add(new InventorySlot { ItemID = -1, Amount = 0 });
|
||||
}
|
||||
|
||||
// 설정된 기본 아이템들을 순서대로 채워 넣음
|
||||
for (int i = 0; i < startingItems.Count && i < slotCount; i++)
|
||||
{
|
||||
if (startingItems[i].item != null)
|
||||
{
|
||||
ItemData data = startingItems[i].item;
|
||||
int amount = startingItems[i].amount;
|
||||
|
||||
Slots[i] = new InventorySlot
|
||||
{
|
||||
ItemID = data.itemID,
|
||||
Amount = amount
|
||||
};
|
||||
|
||||
// 초기 무게 합산
|
||||
_currentWeight.Value += data.weight * amount;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// PlayerInventory.cs에 추가
|
||||
public ItemData GetItemDataInSlot(int index)
|
||||
{
|
||||
if (index < 0 || index >= Slots.Count) return null;
|
||||
var slot = Slots[index];
|
||||
if (slot.ItemID == -1) return null;
|
||||
return ItemDatabase.Instance.GetItemByID(slot.ItemID);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user