Many changes to Invector inventory system, added WIP UI system, added implementation for max stamina, moving speed, attack speed, attack power, thorns

This commit is contained in:
2025-12-05 15:10:52 +01:00
parent 53fa05e246
commit af7706bfac
21 changed files with 2352 additions and 3868 deletions

View File

@@ -40,20 +40,24 @@ namespace Beyond
[vHelpBox("You can ignore display Attributes using this property")]
public List<bItemAttributes> ignoreAttributes;
public UnityEngine.Events.UnityEvent onInitPickUpItem, onFinishPickUpItem;
// --- FIX 1: Initialize Events inline to prevent NullReferenceException ---
public UnityEngine.Events.UnityEvent onInitPickUpItem = new UnityEngine.Events.UnityEvent();
public UnityEngine.Events.UnityEvent onFinishPickUpItem = new UnityEngine.Events.UnityEvent();
public InputField.OnChangeEvent onChangeName;
public InputField.OnChangeEvent onChangeType;
public InputField.OnChangeEvent onChangeAmount;
public InputField.OnChangeEvent onChangeDescription;
public InputField.OnChangeEvent onChangeAttributes;
public OnChangeEquipmentEvent onEquipItem;
public OnChangeEquipmentEvent onUnequipItem;
public OnSelectEquipArea onSelectEquipArea;
public OnChangeEquipmentEvent onEquipItem = new OnChangeEquipmentEvent();
public OnChangeEquipmentEvent onUnequipItem = new OnChangeEquipmentEvent();
public OnSelectEquipArea onSelectEquipArea = new OnSelectEquipArea();
// ------------------------------------------------------------------------
public event Action OnItemUsed;
public UnityEngine.UI.Toggle.ToggleEvent onSetLockToEquip;
public UnityEngine.UI.Toggle.ToggleEvent onSetLockToEquip = new UnityEngine.UI.Toggle.ToggleEvent();
[HideInInspector]
public bEquipSlot currentSelectedSlot;
@@ -79,10 +83,7 @@ namespace Beyond
public bool isLockedToEquip
{
get
{
return _isLockedToEquip;
}
get { return _isLockedToEquip; }
set
{
if (_isLockedToEquip != value) onSetLockToEquip.Invoke(value);
@@ -91,10 +92,6 @@ namespace Beyond
}
public bool ignoreEquipEvents;
/// <summary>
/// used to ignore <see cref="onEquipItem"/> event. if true, the inventory will just add the equipment to area but dont will send to Equip the item. you will nedd to call <see cref="EquipCurrentSlot"/> to equip the item in the area.
/// </summary>
internal bool isInit;
public void Init()
@@ -107,17 +104,24 @@ namespace Beyond
if (!isInit)
{
isInit = true;
//indexOfEquipedItem = -1;
inventory = GetComponentInParent<bInventory>();
itemManager = Player.Instance.GetComponent<bItemManager>();
//itemManager.onEquipItem
// Dependencies
if (inventory == null) inventory = GetComponentInParent<bInventory>();
if (inventory == null && Player.Instance != null && Player.Instance.ItemManager != null)
inventory = Player.Instance.ItemManager.inventory;
itemManager = Player.Instance != null ? Player.Instance.GetComponent<bItemManager>() : null;
if (equipSlots.Count == 0)
{
var equipSlotsArray = GetComponentsInChildren<bEquipSlot>(true);
equipSlots = equipSlotsArray.vToList();
}
foreach (bEquipSlot slot in equipSlots)
{
slot.SetInventory(this.inventory); // Explicitly set inventory
slot.onSubmitSlotCallBack = OnSubmitSlot;
slot.onSelectSlotCallBack = OnSelectSlot;
slot.onDeselectSlotCallBack = OnDeselect;
@@ -139,28 +143,17 @@ namespace Beyond
public void SetNewItemWindow(bItemWindow newItemPicker)
{
if (itemPicker)
{
DisableItemWindow();
}
if (itemPicker) DisableItemWindow();
itemPicker = newItemPicker;
EnableItemWindow();
}
public void DisableItemWindow()
{
itemPicker.gameObject.SetActive(false);
}
public void EnableItemWindow()
{
itemPicker.gameObject.SetActive(true);
}
public void DisableItemWindow() => itemPicker.gameObject.SetActive(false);
public void EnableItemWindow() => itemPicker.gameObject.SetActive(true);
public void SetEquipmentwindowWithFilter(List<bItemType> types)
{
itemPicker.CreateEquipmentWindow(inventory.items, types, null, OnPickItem);
//itemPicker.ReloadItems();
}
public void SetEquipmentWindowsUsableWithFilter(List<bItemType> types)
@@ -170,6 +163,7 @@ namespace Beyond
private void SetOccupiedIndexes()
{
usedIndexes.Clear();
for (int i = 0; i < equipSlots.Count; i++)
{
if (equipSlots[i].isOcupad())
@@ -179,155 +173,61 @@ namespace Beyond
public bool IsAnyItemEquipped()
{
for (int i = 0; i < equipSlots.Count; i++)
{
if (equipSlots[i].isOcupad())
{
return true;
}
}
for (int i = 0; i < equipSlots.Count; i++) if (equipSlots[i].isOcupad()) return true;
return false;
}
/// <summary>
/// Current Equipped Slot
/// </summary>
public bEquipSlot currentEquippedSlot
{
get
{
return equipSlots[indexOfEquippedItem];
}
}
public bEquipSlot currentEquippedSlot => equipSlots[indexOfEquippedItem];
/// <summary>
/// Item in Current Equipped Slot
/// </summary>
public bItem currentEquippedItem
{
get
{
var validEquipSlots = ValidSlots;
if (validEquipSlots.Count > 0 && indexOfEquippedItem >= 0 && indexOfEquippedItem < validEquipSlots.Count) return validEquipSlots[indexOfEquippedItem].item;
return null;
}
}
/// <summary>
/// All valid slot <seealso cref="vItemSlot.isValid"/>
/// </summary>
public List<bEquipSlot> ValidSlots
{
get { return equipSlots.FindAll(slot => slot.isValid && (!skipEmptySlots || slot.item != null)); }
}
public List<bEquipSlot> ValidSlots => equipSlots.FindAll(slot => slot.isValid && (!skipEmptySlots || slot.item != null));
/// <summary>
/// Check if Item is in Area
/// </summary>
/// <param name="item">item to check</param>
/// <returns></returns>
public bool ContainsItem(vItem item)
{
return ValidSlots.Find(slot => slot.item == item) != null;
}
public bool ContainsItem(vItem item) => ValidSlots.Find(slot => slot.item == item) != null;
private void UseItemInternal(bItem item)
{
if (item.type == bItemType.Gemstones)
if (item.type == bItemType.Gemstones)
{
if (gemableWeapons.Count < 1 || gemableWeapons[selectedGemableItemIndex] == null)
{
if (bItemCollectionDisplay.Instance && item.type == bItemType.Gemstones && item)
{
Player.Instance.PlayFullyChargedSound();
//in menu
if (PopupMenuController.Instance != null)
{
PopupMenuController.Instance.TryToShowPopupMesssage("No proper weapons available");
} //cant use gemstone with no proper weapons available
return;
}
}
else //if (item.type == bItemType.Gemstones)
else
{
bItemAttribute powerAttribute = gemableWeapons[selectedGemableItemIndex].attributes.First(attribute => attribute.name == bItemAttributes.Power);
if (powerAttribute.value >= powerChargesMaxValue)
{
Player.Instance.PlayFullyChargedSound();
//Player.Instance.MeleeManager.onEquipWeapon
if (PopupMenuController.Instance != null)
{
PopupMenuController.Instance.TryToShowPopupMesssage(gemableWeapons[selectedGemableItemIndex].name + " is already fully charged");
} //cant use gemstone with no proper weapons available
return;
}
powerAttribute.value += item.GetItemAttribute(bItemAttributes.Power).value;
if (powerAttribute.value > powerChargesMaxValue)
{
powerAttribute.value = powerChargesMaxValue;
}
if (powerAttribute.value > powerChargesMaxValue) powerAttribute.value = powerChargesMaxValue;
}
}
inventory.OnUseItem(item);
}
public void UseItem()
{
if (itemManager.UsingItem)
{
return;
}
//could move the logic to item manager
if (itemManager.UsingItem) return;
UseItemInternal(itemPicker.currentSelectedSlot.item);
/*
bItem itemBeingUsed = itemPicker.currentSelectedSlot.item;
//using here
if (itemBeingUsed.type == bItemType.Gemstones)
{
bItem item = itemBeingUsed;
if (gemableWeapons.Count < 1 || gemableWeapons[selectedGemableItemIndex] == null)
{
if (bItemCollectionDisplay.Instance && item.type == bItemType.Gemstones && item)
{
Player.Instance.PlayFullyChargedSound();
//in menu
if (PopupMenuController.Instance != null)
{
PopupMenuController.Instance.TryToShowPopupMesssage("No proper weapons available");
} //cant use gemstone with no proper weapons available
return;
}
}
else if (item.type == bItemType.Gemstones)
{
bItemAttribute powerAttribute = gemableWeapons[selectedGemableItemIndex].attributes.First(attribute => attribute.name == bItemAttributes.Power);
if (powerAttribute.value >= powerChargesMaxValue)
{
Player.Instance.PlayFullyChargedSound();
if (PopupMenuController.Instance != null)
{
PopupMenuController.Instance.TryToShowPopupMesssage(gemableWeapons[selectedGemableItemIndex].name + " is already fully charged");
} //cant use gemstone with no proper weapons available
return;
}
powerAttribute.value += item.GetItemAttribute(bItemAttributes.Power).value;
if (powerAttribute.value > powerChargesMaxValue)
{
powerAttribute.value = powerChargesMaxValue;
}
}
}
inventory.OnUseItem(itemBeingUsed);
*/
}
public void UseItem(bEquipSlot equipSlot)
@@ -335,8 +235,6 @@ namespace Beyond
bItem itemBeingUsed = equipSlot.item;
FindGemableWeapons();
UseItemInternal(itemBeingUsed);
//do some nice things here i think
//inventory.OnUseItem(itemBeingUsed);
}
public void UseEquippedItem()
@@ -344,10 +242,6 @@ namespace Beyond
inventory.OnUseItem(currentEquippedItem);
}
/// <summary>
/// Event called from Inventory slot UI on Submit
/// </summary>
/// <param name="slot"></param>
public void OnSubmitSlot(bItemSlot slot)
{
lastSelectedSlot = currentSelectedSlot;
@@ -366,39 +260,24 @@ namespace Beyond
}
}
/// <summary>
/// Event called to cancel Submit action
/// </summary>
public void CancelCurrentSlot()
{
if (currentSelectedSlot == null)
currentSelectedSlot = lastSelectedSlot;
if (currentSelectedSlot != null)
currentSelectedSlot.OnCancel();
if (currentSelectedSlot == null) currentSelectedSlot = lastSelectedSlot;
if (currentSelectedSlot != null) currentSelectedSlot.OnCancel();
onFinishPickUpItem.Invoke();
}
/// <summary>
/// Unequip Item of the Slot
/// </summary>
/// <param name="slot">target slot</param>
public void UnequipItem(bEquipSlot slot)
{
if (slot)
{
bItem item = slot.item;
if (ValidSlots[indexOfEquippedItem].item == item)
lastEquipedItem = item;
if (ValidSlots[indexOfEquippedItem].item == item) lastEquipedItem = item;
slot.RemoveItem();
onUnequipItem.Invoke(this, item);
}
}
/// <summary>
/// Unequip Item if is present in slots
/// </summary>
/// <param name="item"></param>
public void UnequipItem(bItem item)
{
var slot = ValidSlots.Find(_slot => _slot.item == item);
@@ -410,9 +289,6 @@ namespace Beyond
}
}
/// <summary>
/// Unequip <seealso cref="currentEquippedItem"/>
/// </summary>
public void UnequipCurrentItem()
{
if (currentSelectedSlot && currentSelectedSlot.item)
@@ -424,11 +300,6 @@ namespace Beyond
}
}
/// <summary>
/// Event called from inventory UI when select an slot
/// never fires apparently
/// </summary>
/// <param name="slot">target slot</param>
public void OnSelectSlot(bItemSlot slot)
{
if (equipSlots.Contains(slot as bEquipSlot))
@@ -439,22 +310,12 @@ namespace Beyond
CreateFullItemDescription(slot);
}
/// <summary>
/// Event called from inventory UI when unselect an slot
/// </summary>
/// <param name="slot">target slot</param>
public void OnDeselect(bItemSlot slot)
{
if (equipSlots.Contains(slot as bEquipSlot))
{
currentSelectedSlot = null;
}
}
/// <summary>
/// Create item description
/// </summary>
/// <param name="slot">target slot</param>
protected virtual void CreateFullItemDescription(bItemSlot slot)
{
var _name = slot.item ? slot.item.name : "";
@@ -477,43 +338,28 @@ namespace Beyond
if (displayAttributesText) displayAttributesText.text = _attributes;
onChangeAttributes.Invoke(_attributes);
Debug.LogError("happeingng?");
}
/// <summary>
/// Event called from inventory UI to open <see cref="vItemWindow"/> when submit slot
/// </summary>
/// <param name="slot">target slot</param>
// --- FIX 2: Safer OnPickItem implementation ---
public void OnPickItem(bItemSlot slot)
{
/*
//for gemstones it does something different, it switches internal idnex of currently selected sword
if (usedIndexes.Count == 0) SetOccupiedIndexes();
if (slot.item.type == bItemType.Gemstones)
{
SwitchPowerableWeapon();
return;
}
*/
//1 check if any slot has this item, if it does, unequip it
//2 if not fill the first free slot
//3 if no free slots available swap with the oldest one
if (usedIndexes.Count == 0)
{
SetOccupiedIndexes();
}
//1
// 1. Unequip if checked
if (slot.isChecked)
{
bEquipSlot occupiedSlot = equipSlots.Find(eSlot => eSlot.item == slot.item);
usedIndexes.Remove(equipSlots.IndexOf(occupiedSlot));
occupiedSlot.RemoveItem();
onUnequipItem.Invoke(this, slot.item);
onFinishPickUpItem.Invoke();
if (occupiedSlot != null) // Check if slot was actually found
{
usedIndexes.Remove(equipSlots.IndexOf(occupiedSlot));
occupiedSlot.RemoveItem();
onUnequipItem?.Invoke(this, slot.item); // Safe Invoke
}
onFinishPickUpItem?.Invoke(); // Safe Invoke
return;
}
//2
// 2. Fill first free slot
bEquipSlot freeSlot = equipSlots.Find(eslot => !eslot.isOcupad() && eslot.isValid && eslot.itemType.Contains(slot.item.type));
if (freeSlot)
{
@@ -521,76 +367,55 @@ namespace Beyond
freeSlot.AddItem(slot.item);
if (!ignoreEquipEvents)
{
onEquipItem.Invoke(this, slot.item);
onEquipItem?.Invoke(this, slot.item);
}
onFinishPickUpItem.Invoke();
onFinishPickUpItem?.Invoke();
usedIndexes.Add(equipSlots.IndexOf(freeSlot));
return;
}
//3
//get last used index that is of valid type
bEquipSlot slotToBeTaken = equipSlots[0];
for (int i = 0; i < usedIndexes.Count; i++)
// 3. Swap with oldest (FIFO) if full
if (usedIndexes.Count > 0)
{
int tempIndex = usedIndexes[i];
if (equipSlots[tempIndex].itemType.Contains(slot.item.type))
int slotIndexToTake = -1;
// Iterate through used indexes to find a compatible slot
for (int i = 0; i < usedIndexes.Count; i++)
{
slotToBeTaken = equipSlots[tempIndex];
usedIndexes.RemoveAt(i);
usedIndexes.Add(tempIndex);
i = usedIndexes.Count;
int idx = usedIndexes[i];
if (equipSlots[idx].itemType.Contains(slot.item.type))
{
slotIndexToTake = idx;
usedIndexes.RemoveAt(i);
usedIndexes.Add(idx);
break;
}
}
// If we found a compatible slot to swap
if (slotIndexToTake != -1)
{
bEquipSlot slotToBeTaken = equipSlots[slotIndexToTake];
if (slotToBeTaken.isOcupad())
{
onUnequipItem?.Invoke(this, slotToBeTaken.item);
slotToBeTaken.RemoveItem();
}
onPickUpItemCallBack?.Invoke(this, slot);
slotToBeTaken.AddItem(slot.item);
if (!ignoreEquipEvents)
{
onEquipItem?.Invoke(this, slot.item);
}
}
}
// bEquipSlot slotToBeTaken = equipSlots[usedIndexes[0]];
//remove old 1st
slotToBeTaken.RemoveItem();
onUnequipItem.Invoke(this, slot.item);
onFinishPickUpItem.Invoke();
//add new
onPickUpItemCallBack?.Invoke(this, slot);
slotToBeTaken.AddItem(slot.item);
if (!ignoreEquipEvents)
{
onEquipItem.Invoke(this, slot.item);
}
onFinishPickUpItem.Invoke();
return;
if (!currentSelectedSlot)
{
currentSelectedSlot = lastSelectedSlot;
}
if (!currentSelectedSlot)
{
return;
}
if (currentSelectedSlot.item != null && slot.item != currentSelectedSlot.item)
{
currentSelectedSlot.item.isInEquipArea = false;
var item = currentSelectedSlot.item;
if (item == slot.item) lastEquipedItem = item;
currentSelectedSlot.RemoveItem();
onUnequipItem.Invoke(this, item);
}
if (slot.item != currentSelectedSlot.item)
{
if (onPickUpItemCallBack != null)
onPickUpItemCallBack(this, slot);
currentSelectedSlot.AddItem(slot.item);
if (!ignoreEquipEvents) onEquipItem.Invoke(this, currentSelectedSlot.item);
}
currentSelectedSlot.OnCancel();
currentSelectedSlot = null;
lastSelectedSlot = null;
onFinishPickUpItem.Invoke();
onFinishPickUpItem?.Invoke();
}
// ----------------------------------------------
void FindGemableWeapons()
{
@@ -603,7 +428,6 @@ namespace Beyond
{
if (itemSelected.item.type == bItemType.Gemstones)
{
//for gemstones reset selection of gemable weapon, no saving per gem as of now
FindGemableWeapons();
if (gemableWeapons.Count > 0)
{
@@ -627,12 +451,8 @@ namespace Beyond
}
selectedPowerableItem = gemableWeapons[selectedGemableItemIndex];
itemPicker.SetPowerableSwitchSwordImage(selectedPowerableItem);
//updaing ui
}
/// <summary>
/// Equip next slot <seealso cref="currentEquippedItem"/>
/// </summary>
public void NextEquipSlot()
{
if (equipSlots == null || equipSlots.Count == 0) return;
@@ -645,13 +465,10 @@ namespace Beyond
indexOfEquippedItem = 0;
if (currentEquippedItem != null && !ignoreEquipEvents)
onEquipItem.Invoke(this, currentEquippedItem);
onUnequipItem.Invoke(this, lastEquipedItem);
onEquipItem?.Invoke(this, currentEquippedItem);
onUnequipItem?.Invoke(this, lastEquipedItem);
}
/// <summary>
/// Equip previous slot <seealso cref="currentEquippedItem"/>
/// </summary>
public void PreviousEquipSlot()
{
if (equipSlots == null || equipSlots.Count == 0) return;
@@ -665,15 +482,11 @@ namespace Beyond
indexOfEquippedItem = validEquipSlots.Count - 1;
if (currentEquippedItem != null && !ignoreEquipEvents)
onEquipItem.Invoke(this, currentEquippedItem);
onEquipItem?.Invoke(this, currentEquippedItem);
onUnequipItem.Invoke(this, lastEquipedItem);
onUnequipItem?.Invoke(this, lastEquipedItem);
}
/// <summary>
/// Equip slot <see cref="currentEquippedItem"/>
/// </summary>
/// <param name="indexOfSlot">index of target slot</param>
public void SetEquipSlot(int indexOfSlot)
{
if (equipSlots == null || equipSlots.Count == 0) return;
@@ -683,25 +496,20 @@ namespace Beyond
indexOfEquippedItem = indexOfSlot;
if (currentEquippedItem != null && !ignoreEquipEvents)
{
onEquipItem.Invoke(this, currentEquippedItem);
onEquipItem?.Invoke(this, currentEquippedItem);
}
if (currentEquippedItem != lastEquipedItem)
onUnequipItem.Invoke(this, lastEquipedItem);
onUnequipItem?.Invoke(this, lastEquipedItem);
}
}
public void EquipCurrentSlot()
{
if (!currentEquippedSlot || (currentEquippedSlot.item != null && currentEquippedSlot.item.isEquiped)) return;
if (currentEquippedItem) onEquipItem.Invoke(this, currentEquippedItem);
else if (lastEquipedItem) onUnequipItem.Invoke(this, lastEquipedItem);
if (currentEquippedItem) onEquipItem?.Invoke(this, currentEquippedItem);
else if (lastEquipedItem) onUnequipItem?.Invoke(this, lastEquipedItem);
}
/// <summary>
/// Add an item to an slot
/// </summary>
/// <param name="slot">target Slot</param>
/// <param name="item">target Item</param>
public void AddItemToEquipSlot(bItemSlot slot, bItem item, bool autoEquip = false)
{
if (slot is bEquipSlot && equipSlots.Contains(slot as bEquipSlot))
@@ -710,11 +518,6 @@ namespace Beyond
}
}
/// <summary>
/// Add an item to an slot
/// </summary>
/// <param name="indexOfSlot">index of target Slot</param>
/// <param name="item">target Item</param>
public void AddItemToEquipSlot(int indexOfSlot, bItem item, bool autoEquip = false)
{
if (indexOfSlot < equipSlots.Count && item != null && item.canBeUsed)
@@ -732,7 +535,7 @@ namespace Beyond
{
if (currentEquippedItem == slot.item) lastEquipedItem = slot.item;
slot.item.isInEquipArea = false;
onUnequipItem.Invoke(this, slot.item);
onUnequipItem?.Invoke(this, slot.item);
}
item.checkColor = slot.checkColor;
item.isInEquipArea = true;
@@ -740,15 +543,11 @@ namespace Beyond
if (autoEquip)
SetEquipSlot(indexOfSlot);
else if (!ignoreEquipEvents)
onEquipItem.Invoke(this, item);
onEquipItem?.Invoke(this, item);
}
}
}
/// <summary>
/// Remove item of an slot
/// </summary>
/// <param name="slot">target Slot</param>
public void RemoveItemOfEquipSlot(bItemSlot slot)
{
if (slot is bEquipSlot && equipSlots.Contains(slot as bEquipSlot))
@@ -757,10 +556,6 @@ namespace Beyond
}
}
/// <summary>
/// Remove item of an slot
/// </summary>
/// <param name="slot">index of target Slot</param>
public void RemoveItemOfEquipSlot(int indexOfSlot)
{
if (indexOfSlot < equipSlots.Count)
@@ -772,15 +567,11 @@ namespace Beyond
item.isInEquipArea = false;
if (currentEquippedItem == item) lastEquipedItem = currentEquippedItem;
slot.RemoveItem();
onUnequipItem.Invoke(this, item);
onUnequipItem?.Invoke(this, item);
}
}
}
/// <summary>
/// Add item to current equiped slot
/// </summary>
/// <param name="item">target item</param>
public void AddCurrentItem(bItem item)
{
if (indexOfEquippedItem < equipSlots.Count)
@@ -790,22 +581,19 @@ namespace Beyond
{
if (currentEquippedItem == slot.item) lastEquipedItem = slot.item;
slot.item.isInEquipArea = false;
onUnequipItem.Invoke(this, currentSelectedSlot.item);
onUnequipItem?.Invoke(this, currentSelectedSlot.item);
}
slot.AddItem(item);
if (!ignoreEquipEvents) onEquipItem.Invoke(this, item);
if (!ignoreEquipEvents) onEquipItem?.Invoke(this, item);
}
}
/// <summary>
/// Remove current equiped Item
/// </summary>
public void RemoveCurrentItem()
{
if (!currentEquippedItem) return;
lastEquipedItem = currentEquippedItem;
ValidSlots[indexOfEquippedItem].RemoveItem();
onUnequipItem.Invoke(this, lastEquipedItem);
onUnequipItem?.Invoke(this, lastEquipedItem);
}
}
}