107 lines
2.7 KiB
C#
107 lines
2.7 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using TMPro;
|
|
|
|
namespace SimpleInventorySystem
|
|
{
|
|
public class LimitStackSlotManager : MonoBehaviour
|
|
{
|
|
public int StackSlotLimit = 0;
|
|
public int Count { get; private set; }
|
|
public LimitStackSlot[] limitStackSlots;
|
|
public bool combinedLimit = true;
|
|
public TextMeshProUGUI capacityText;
|
|
public SlotUI slot;
|
|
|
|
private void Awake()
|
|
{
|
|
foreach (LimitStackSlot ls in limitStackSlots)
|
|
{
|
|
ls.limitStackSlotCapacities = this;
|
|
}
|
|
|
|
//slot = GetComponent<SlotUI>();
|
|
|
|
SetRenderItemUIPropertyValue();
|
|
}
|
|
|
|
private void SetRenderItemUIPropertyValue()
|
|
{
|
|
if (slot != null)
|
|
{
|
|
capacityText.text = Count.ToString() + " / " + StackSlotLimit.ToString();
|
|
}
|
|
}
|
|
|
|
public void SetStackSlotLimit(ItemUI item)
|
|
{
|
|
if(item == null || item.GetComponent<LimitStackSlotCapacity>() == null)
|
|
{
|
|
Debug.LogWarning("LimitStackSlotManager: SetStackSlotLimit(ItemUI item): The passed item should not be null and should have a LimitStackSlotCapacity component.");
|
|
return;
|
|
}
|
|
|
|
StackSlotLimit = item.GetComponent<LimitStackSlotCapacity>().Capacity;
|
|
Debug.Log($"StackSlotLimit: {StackSlotLimit}");
|
|
|
|
SetRenderItemUIPropertyValue();
|
|
}
|
|
|
|
public void UnsetStackSlotLimit()
|
|
{
|
|
StackSlotLimit = 0;
|
|
Count = 0;
|
|
|
|
SetRenderItemUIPropertyValue();
|
|
}
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
foreach (LimitStackSlot ls in limitStackSlots)
|
|
{
|
|
ls.limitStackSlotCapacities = this;
|
|
}
|
|
slot.limitStackSlotManager = this;
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
|
|
}
|
|
|
|
public void Increment(LimitStackSlot ls)
|
|
{
|
|
Count += ls.Weight;
|
|
|
|
SetRenderItemUIPropertyValue();
|
|
}
|
|
public void Decrement(LimitStackSlot ls)
|
|
{
|
|
Count -= ls.Weight;
|
|
|
|
SetRenderItemUIPropertyValue();
|
|
}
|
|
|
|
public bool HasReachedLimit(int weight)
|
|
{
|
|
return Count >= StackSlotLimit || Count + weight > StackSlotLimit;
|
|
}
|
|
|
|
private LimitStackSlot GetLimitStackSlot(SlotUI slot)
|
|
{
|
|
foreach (LimitStackSlot ls in limitStackSlots)
|
|
{
|
|
if(slot == ls.Slot)
|
|
{
|
|
return ls;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|
|
} |