2022-11-13 19:52:09 -08:00
|
|
|
using Godot;
|
|
|
|
using SupaLidlGame.Characters;
|
2023-04-16 14:11:17 -07:00
|
|
|
using Godot.Collections;
|
2022-11-13 19:52:09 -08:00
|
|
|
|
2023-06-03 18:21:46 -07:00
|
|
|
namespace SupaLidlGame.Items;
|
|
|
|
|
|
|
|
public partial class Inventory : Node2D
|
2022-11-13 19:52:09 -08:00
|
|
|
{
|
2023-06-03 18:21:46 -07:00
|
|
|
public Character Character { get; private set; }
|
2022-11-13 19:52:09 -08:00
|
|
|
|
2023-06-03 18:21:46 -07:00
|
|
|
[Export]
|
|
|
|
public Array<Item> Items { get; private set; }
|
2023-04-16 14:11:17 -07:00
|
|
|
|
2023-06-03 18:21:46 -07:00
|
|
|
[Export]
|
|
|
|
public Dictionary<string, int> InventoryMap { get; set; }
|
2022-11-13 19:52:09 -08:00
|
|
|
|
2023-06-03 18:21:46 -07:00
|
|
|
public const int MaxCapacity = 32;
|
2022-11-13 19:52:09 -08:00
|
|
|
|
2023-06-03 18:21:46 -07:00
|
|
|
private Item _selectedItem;
|
2022-11-13 19:52:09 -08:00
|
|
|
|
2023-06-03 18:21:46 -07:00
|
|
|
private Item _offhandItem;
|
2023-03-19 23:36:36 -07:00
|
|
|
|
2023-06-03 18:21:46 -07:00
|
|
|
public Item SelectedItem
|
|
|
|
{
|
|
|
|
get => _selectedItem;
|
|
|
|
set => EquipItem(value, ref _selectedItem);
|
|
|
|
}
|
2023-03-19 23:36:36 -07:00
|
|
|
|
2023-06-03 18:21:46 -07:00
|
|
|
public Item OffhandItem
|
|
|
|
{
|
|
|
|
get => _selectedItem;
|
|
|
|
set => EquipItem(value, ref _offhandItem);
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool IsUsingItem => (SelectedItem?.IsUsing ?? false) ||
|
|
|
|
(OffhandItem?.IsUsing ?? false);
|
2023-03-19 23:36:36 -07:00
|
|
|
|
2023-06-03 18:21:46 -07:00
|
|
|
public Inventory()
|
|
|
|
{
|
|
|
|
InventoryMap = new Dictionary<string, int>();
|
|
|
|
InventoryMap.Add("equip_1", 0);
|
|
|
|
InventoryMap.Add("equip_2", 1);
|
|
|
|
InventoryMap.Add("equip_3", 2);
|
|
|
|
}
|
2023-04-16 14:11:17 -07:00
|
|
|
|
2023-06-03 18:21:46 -07:00
|
|
|
public override void _Ready()
|
|
|
|
{
|
|
|
|
if (Items is null)
|
2023-04-16 14:11:17 -07:00
|
|
|
{
|
2023-06-03 18:21:46 -07:00
|
|
|
// instantiating a new array will prevent characters from
|
|
|
|
// sharing inventories
|
|
|
|
Items = new Array<Item>();
|
2023-04-16 14:11:17 -07:00
|
|
|
}
|
2023-06-03 18:21:46 -07:00
|
|
|
Character = GetParent<Character>();
|
|
|
|
foreach (Node child in GetChildren())
|
2023-05-28 12:07:19 -07:00
|
|
|
{
|
2023-06-03 18:21:46 -07:00
|
|
|
if (child is Item item)
|
2023-05-28 12:07:19 -07:00
|
|
|
{
|
2023-06-03 18:21:46 -07:00
|
|
|
GD.Print("Adding item " + item.Name);
|
|
|
|
AddItem(item);
|
2023-05-28 12:07:19 -07:00
|
|
|
}
|
|
|
|
}
|
2023-06-03 18:21:46 -07:00
|
|
|
base._Ready();
|
|
|
|
}
|
2023-05-28 12:07:19 -07:00
|
|
|
|
2023-06-03 18:21:46 -07:00
|
|
|
private bool EquipItem(Item item, ref Item slot)
|
|
|
|
{
|
|
|
|
if (item is not null)
|
2023-03-19 23:36:36 -07:00
|
|
|
{
|
2023-06-03 18:21:46 -07:00
|
|
|
if (item.IsOneHanded)
|
2022-11-13 19:52:09 -08:00
|
|
|
{
|
2023-06-03 18:21:46 -07:00
|
|
|
// we can not equip this if either hand is occupied by
|
|
|
|
// two-handed item
|
|
|
|
|
|
|
|
if (_selectedItem is not null && !_selectedItem.IsOneHanded)
|
2022-11-13 19:52:09 -08:00
|
|
|
{
|
2023-06-03 18:21:46 -07:00
|
|
|
return false;
|
2022-11-13 19:52:09 -08:00
|
|
|
}
|
|
|
|
|
2023-06-03 18:21:46 -07:00
|
|
|
if (_offhandItem is not null && !_offhandItem.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-06-03 18:21:46 -07:00
|
|
|
if (!Items.Contains(item))
|
2023-03-19 23:36:36 -07:00
|
|
|
{
|
2023-06-03 18:21:46 -07:00
|
|
|
GD.PrintErr("Tried to equip an item not in the inventory.");
|
|
|
|
return false;
|
2022-11-13 19:52:09 -08:00
|
|
|
}
|
2023-06-03 18:21:46 -07:00
|
|
|
}
|
2023-03-19 23:36:36 -07:00
|
|
|
|
2023-06-03 18:21:46 -07:00
|
|
|
if (slot is not null)
|
|
|
|
{
|
|
|
|
slot.Unequip(Character);
|
|
|
|
}
|
2023-03-19 23:36:36 -07:00
|
|
|
|
2023-06-03 18:21:46 -07:00
|
|
|
slot = item;
|
2023-03-19 23:36:36 -07:00
|
|
|
|
2023-06-03 18:21:46 -07:00
|
|
|
if (item is not null)
|
|
|
|
{
|
|
|
|
item.Equip(Character);
|
2022-11-13 19:52:09 -08:00
|
|
|
}
|
|
|
|
|
2023-06-03 18:21:46 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Item GetItemByMap(string keymap)
|
|
|
|
{
|
|
|
|
if (InventoryMap.ContainsKey(keymap))
|
2023-04-16 14:11:17 -07:00
|
|
|
{
|
2023-06-03 18:21:46 -07:00
|
|
|
int idx = InventoryMap[keymap];
|
|
|
|
if (idx < Items.Count)
|
2023-04-16 14:11:17 -07:00
|
|
|
{
|
2023-06-03 18:21:46 -07:00
|
|
|
return Items[InventoryMap[keymap]];
|
2023-04-16 14:11:17 -07:00
|
|
|
}
|
|
|
|
}
|
2023-06-03 18:21:46 -07:00
|
|
|
else GD.Print(keymap + " does not exist");
|
|
|
|
return null;
|
|
|
|
}
|
2023-04-16 14:11:17 -07:00
|
|
|
|
2023-06-03 18:21:46 -07:00
|
|
|
public Item AddItem(Item item)
|
|
|
|
{
|
|
|
|
if (Items.Count >= MaxCapacity)
|
2022-11-13 19:52:09 -08:00
|
|
|
{
|
2023-06-03 18:21:46 -07:00
|
|
|
return null;
|
2022-11-13 19:52:09 -08:00
|
|
|
}
|
|
|
|
|
2023-06-03 18:21:46 -07:00
|
|
|
item.CharacterOwner = Character;
|
|
|
|
item.Visible = false;
|
|
|
|
if (!Items.Contains(item))
|
2022-11-13 19:52:09 -08:00
|
|
|
{
|
2023-06-03 18:21:46 -07:00
|
|
|
Items.Add(item);
|
2022-11-13 19:52:09 -08:00
|
|
|
}
|
2023-06-03 18:21:46 -07:00
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Item DropItem(Item item)
|
|
|
|
{
|
|
|
|
item.CharacterOwner = null;
|
|
|
|
item.Visible = true;
|
|
|
|
var e = SelectedItem = item;
|
|
|
|
throw new System.NotImplementedException();
|
2022-11-13 19:52:09 -08:00
|
|
|
}
|
|
|
|
}
|