working on inventory
This commit is contained in:
103
Assets/jelycho/Code/Items/Inventory.cs
Normal file
103
Assets/jelycho/Code/Items/Inventory.cs
Normal file
@@ -0,0 +1,103 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using RebootKit.Engine.Foundation;
|
||||
|
||||
namespace RebootReality.jelycho.Items {
|
||||
public class Inventory {
|
||||
static readonly Logger s_Logger = new Logger(nameof(Inventory));
|
||||
|
||||
class ItemState {
|
||||
public List<ItemActor> Actors = new List<ItemActor>();
|
||||
}
|
||||
|
||||
readonly ItemState[] m_Items;
|
||||
|
||||
public event Action<ItemActor> OnItemPickedUp = delegate { };
|
||||
public event Action<ItemActor> OnItemDropped = delegate { };
|
||||
public event Action<int> OnSlotUpdated = delegate { };
|
||||
|
||||
public int SlotsCount {
|
||||
get {
|
||||
return m_Items.Length;
|
||||
}
|
||||
}
|
||||
|
||||
public Inventory(int slotsCount) {
|
||||
m_Items = new ItemState[slotsCount];
|
||||
|
||||
for (int i = 0; i < slotsCount; i++) {
|
||||
m_Items[i] = new ItemState();
|
||||
}
|
||||
}
|
||||
|
||||
public bool TryPickup(ItemActor actor) {
|
||||
if (Contains(actor)) {
|
||||
s_Logger.Error($"Item {actor.name} is already in the inventory.");
|
||||
return false;
|
||||
}
|
||||
|
||||
(int slotIndex, ItemState freeItemState) = FindFreeItemState();
|
||||
if (freeItemState == null) {
|
||||
s_Logger.Error("Inventory is full, cannot pick up item.");
|
||||
return false;
|
||||
}
|
||||
|
||||
freeItemState.Actors.Add(actor);
|
||||
OnItemPickedUp?.Invoke(actor);
|
||||
OnSlotUpdated?.Invoke(slotIndex);
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool TryDrop(ItemActor actor) {
|
||||
for (int i = 0; i < m_Items.Length; i++) {
|
||||
if (m_Items[i].Actors.Remove(actor)) {
|
||||
OnItemDropped?.Invoke(actor);
|
||||
OnSlotUpdated?.Invoke(i);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
s_Logger.Error($"Item {actor.name} is not in the inventory.");
|
||||
return false;
|
||||
}
|
||||
|
||||
public int GetQuantity(int slotIndex) {
|
||||
if (slotIndex < 0 || slotIndex >= m_Items.Length) {
|
||||
throw new ArgumentOutOfRangeException(nameof(slotIndex), "Slot index is out of range.");
|
||||
}
|
||||
|
||||
return m_Items[slotIndex].Actors.Count;
|
||||
}
|
||||
|
||||
public ItemActor GetFirstItem(int slotIndex) {
|
||||
if (slotIndex < 0 || slotIndex >= m_Items.Length) {
|
||||
throw new ArgumentOutOfRangeException(nameof(slotIndex), "Slot index is out of range.");
|
||||
}
|
||||
|
||||
if (m_Items[slotIndex].Actors.Count > 0) {
|
||||
return m_Items[slotIndex].Actors[0];
|
||||
}
|
||||
|
||||
s_Logger.Error($"No items in slot {slotIndex}.");
|
||||
return null;
|
||||
}
|
||||
|
||||
public bool Contains(ItemActor actor) {
|
||||
for (int i = 0; i < m_Items.Length; i++) {
|
||||
if (m_Items[i].Actors.Contains(actor)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
(int, ItemState) FindFreeItemState() {
|
||||
for (int i = 0; i < m_Items.Length; i++) {
|
||||
if (m_Items[i].Actors.Count == 0) {
|
||||
return (i, m_Items[i]);
|
||||
}
|
||||
}
|
||||
return (-1, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/jelycho/Code/Items/Inventory.cs.meta
Normal file
3
Assets/jelycho/Code/Items/Inventory.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f84c0e35a7df482c8c7de30104bd05f2
|
||||
timeCreated: 1752181679
|
||||
@@ -1,19 +1,30 @@
|
||||
using RebootKit.Engine.Simulation;
|
||||
using Unity.Netcode;
|
||||
using UnityEngine;
|
||||
using Logger = RebootKit.Engine.Foundation.Logger;
|
||||
|
||||
namespace RebootReality.jelycho.Items {
|
||||
public class ItemActor : Actor {
|
||||
public class ItemActor : Actor, IInteractable {
|
||||
static readonly Logger s_Logger = new Logger(nameof(ItemActor));
|
||||
|
||||
class ItemActorData : IActorData {
|
||||
public string ItemID;
|
||||
public ItemConfig Config;
|
||||
|
||||
public void Serialize(FastBufferWriter writer) {
|
||||
}
|
||||
public void Deserialize(FastBufferReader reader) {
|
||||
}
|
||||
}
|
||||
|
||||
[field: SerializeField] public ItemConfig Config { get; private set; }
|
||||
|
||||
protected override IActorData CreateActorData() {
|
||||
return new ItemActorData();
|
||||
return new ItemActorData {
|
||||
Config = Config
|
||||
};
|
||||
}
|
||||
|
||||
public void Interact() {
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/jelycho/Code/Items/ItemConfig.cs
Normal file
11
Assets/jelycho/Code/Items/ItemConfig.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using System;
|
||||
using Unity.Collections;
|
||||
using UnityEngine;
|
||||
|
||||
namespace RebootReality.jelycho.Items {
|
||||
[Serializable]
|
||||
public struct ItemConfig {
|
||||
public FixedString32Bytes itemID;
|
||||
public Sprite icon;
|
||||
}
|
||||
}
|
||||
3
Assets/jelycho/Code/Items/ItemConfig.cs.meta
Normal file
3
Assets/jelycho/Code/Items/ItemConfig.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7870350a856f43c19d9b830b9eeb7a46
|
||||
timeCreated: 1752164733
|
||||
Reference in New Issue
Block a user