2022-11-13 19:52:09 -08:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using Godot;
|
|
|
|
using SupaLidlGame.Characters;
|
|
|
|
|
|
|
|
namespace SupaLidlGame.Items
|
|
|
|
{
|
|
|
|
public partial class Inventory : Node2D
|
|
|
|
{
|
|
|
|
public Character Character { get; private set; }
|
|
|
|
|
2023-03-26 21:11:38 -07:00
|
|
|
[Export]
|
|
|
|
public Godot.Collections.Array<ItemInfo> Items { get; private set; }
|
|
|
|
|
|
|
|
[Export]
|
|
|
|
public ItemInfo Test { get; set; }
|
2022-11-13 19:52:09 -08:00
|
|
|
|
|
|
|
public const int MaxCapacity = 32;
|
|
|
|
|
2023-03-26 21:11:38 -07:00
|
|
|
private Item _primaryItem;
|
2022-11-13 19:52:09 -08:00
|
|
|
|
2023-03-19 23:36:36 -07:00
|
|
|
private Item _offhandItem;
|
|
|
|
|
2023-03-26 21:11:38 -07:00
|
|
|
public Item PrimaryItem
|
2022-11-13 19:52:09 -08:00
|
|
|
{
|
2023-03-26 21:11:38 -07:00
|
|
|
get => _primaryItem;
|
|
|
|
private set => _primaryItem = value;
|
2023-03-19 23:36:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public Item OffhandItem
|
|
|
|
{
|
2023-03-26 21:11:38 -07:00
|
|
|
get => _offhandItem;
|
|
|
|
private set => _offhandItem = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Inventory()
|
|
|
|
{
|
|
|
|
Items = new Godot.Collections.Array<ItemInfo>();
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool EquipItem(ItemInfo info, bool offhand = false)
|
|
|
|
{
|
|
|
|
if (UnequipItem() == info)
|
|
|
|
{
|
|
|
|
// if the item that was unequipped was our item, then we stop
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
var idx = Items.IndexOf(info);
|
|
|
|
|
|
|
|
if (idx < 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ItemInfo item = Items[idx];
|
|
|
|
|
|
|
|
string name = offhand ? "Offhand" : "Primary";
|
|
|
|
PrimaryItem = item.InstantiateItem(name);
|
|
|
|
PrimaryItem.Equip(Character);
|
|
|
|
PrimaryItem.CharacterOwner = Character;
|
|
|
|
//PrimaryItem.CharacterOwner
|
|
|
|
AddChild(PrimaryItem);
|
|
|
|
return true;
|
2023-03-19 23:36:36 -07:00
|
|
|
}
|
|
|
|
|
2023-03-26 21:11:38 -07:00
|
|
|
public ItemInfo UnequipItem(bool offhand = false)
|
|
|
|
{
|
|
|
|
Item item;
|
|
|
|
if (!offhand)
|
|
|
|
{
|
|
|
|
item = Character.Inventory.GetNode<Item>("Primary");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
item = Character.Inventory.GetNode<Item>("Offhand");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (item is null)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
item.QueueFree();
|
|
|
|
return item.Info;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2023-03-19 23:36:36 -07:00
|
|
|
private bool EquipItem(Item item, ref Item slot)
|
|
|
|
{
|
2023-03-26 21:11:38 -07:00
|
|
|
if (item is not null && item.Info.IsOneHanded)
|
2022-11-13 19:52:09 -08:00
|
|
|
{
|
2023-03-19 23:36:36 -07:00
|
|
|
// we can not equip this if either hand is occupied by
|
|
|
|
// two-handed item
|
|
|
|
|
2023-03-26 21:11:38 -07:00
|
|
|
if (_selectedItem is not null && !_selectedItem.Info.IsOneHanded)
|
2022-11-13 19:52:09 -08:00
|
|
|
{
|
2023-03-19 23:36:36 -07:00
|
|
|
return false;
|
2022-11-13 19:52:09 -08:00
|
|
|
}
|
|
|
|
|
2023-03-26 21:11:38 -07:00
|
|
|
if (_offhandItem is not null && !_offhandItem.Info.IsOneHanded)
|
2022-11-13 19:52:09 -08:00
|
|
|
{
|
2023-03-19 23:36:36 -07:00
|
|
|
return false;
|
2022-11-13 19:52:09 -08:00
|
|
|
}
|
2023-03-19 23:36:36 -07:00
|
|
|
}
|
2022-11-13 19:52:09 -08:00
|
|
|
|
2023-03-19 23:36:36 -07:00
|
|
|
if (!Items.Contains(item))
|
|
|
|
{
|
|
|
|
GD.PrintErr("Tried to equip an item not in the inventory.");
|
|
|
|
return false;
|
|
|
|
}
|
2022-11-13 19:52:09 -08:00
|
|
|
|
2023-03-19 23:36:36 -07:00
|
|
|
if (slot is not null)
|
|
|
|
{
|
|
|
|
slot.Unequip(Character);
|
2022-11-13 19:52:09 -08:00
|
|
|
}
|
2023-03-19 23:36:36 -07:00
|
|
|
|
|
|
|
slot = item;
|
|
|
|
|
|
|
|
if (item is not null)
|
|
|
|
{
|
|
|
|
item.Equip(Character);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2022-11-13 19:52:09 -08:00
|
|
|
}
|
2023-03-26 21:11:38 -07:00
|
|
|
*/
|
2022-11-13 19:52:09 -08:00
|
|
|
|
2023-03-26 21:11:38 -07:00
|
|
|
public ItemInfo AddItem(ItemInfo info)
|
2022-11-13 19:52:09 -08:00
|
|
|
{
|
|
|
|
if (Items.Count >= MaxCapacity)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-03-26 21:11:38 -07:00
|
|
|
info.CharacterOwner = Character;
|
|
|
|
//item.Visible = false;
|
|
|
|
Items.Add(info);
|
|
|
|
return info;
|
2022-11-13 19:52:09 -08:00
|
|
|
}
|
|
|
|
|
2023-03-26 21:11:38 -07:00
|
|
|
public Item DropItem(ItemInfo item)
|
2022-11-13 19:52:09 -08:00
|
|
|
{
|
2022-11-19 21:21:12 -08:00
|
|
|
item.CharacterOwner = null;
|
2023-03-26 21:11:38 -07:00
|
|
|
//item.Visible = true;
|
|
|
|
//var e = SelectedItem = item;
|
2022-11-13 19:52:09 -08:00
|
|
|
throw new System.NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void _Ready()
|
|
|
|
{
|
2022-11-19 21:21:12 -08:00
|
|
|
Character = GetParent<Character>();
|
2023-03-26 21:11:38 -07:00
|
|
|
EnsureChildrenItems();
|
|
|
|
base._Ready();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void EnsureChildrenItems()
|
|
|
|
{
|
2022-11-18 13:53:51 -08:00
|
|
|
foreach (Node child in GetChildren())
|
|
|
|
{
|
|
|
|
if (child is Item item)
|
|
|
|
{
|
2023-03-26 21:11:38 -07:00
|
|
|
GD.Print(item.Info.Count);
|
|
|
|
if (Items.IndexOf(item.Info) < 0)
|
|
|
|
{
|
|
|
|
AddItem(item.Info);
|
|
|
|
PrimaryItem = item;
|
|
|
|
}
|
2022-11-18 13:53:51 -08:00
|
|
|
}
|
|
|
|
}
|
2023-03-26 21:11:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SelectFirstItem()
|
|
|
|
{
|
|
|
|
EnsureChildrenItems();
|
|
|
|
if (Items.Count > 0)
|
|
|
|
{
|
|
|
|
EquipItem(Items[0]);
|
|
|
|
}
|
2022-11-13 19:52:09 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|