SupaLidlGame/Items/Inventory.cs

206 lines
4.4 KiB
C#
Raw Normal View History

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, IItemCollection<ItemMetadata>
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]
2023-12-31 05:59:33 -08:00
public Array<Item> Hotbar { get; private set; }
[Export]
public Array<ItemMetadata> 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-07-21 02:54:13 -07:00
[Signal]
public delegate void UsedItemEventHandler(Item item);
[Signal]
public delegate void EquippedItemEventHandler(Item newItem, Item prevItem);
[Signal]
public delegate void ItemAddedEventHandler(ItemMetadata newItemMetadata);
public int Capacity { get; set; } = 30;
public const int HotbarCapacity = 3;
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
public Item SelectedItem
{
get => _selectedItem;
2024-01-01 22:55:53 -08:00
set => EquipItem(value);
}
private int _selectedIndex;
public int SelectedIndex
{
get => _selectedIndex;
set => EquipIndex(value);
2023-06-03 18:21:46 -07:00
}
2023-03-19 23:36:36 -07:00
2023-08-15 00:44:44 -07:00
private int _quickSwitchIndex = -1;
public const int QUICKSWITCH_SIZE = 3;
public int CurrentQuickSwitchIndex
{
get => _quickSwitchIndex;
set
{
const int size = QUICKSWITCH_SIZE;
_quickSwitchIndex = (value % size + size) % size;
}
}
2023-08-10 23:08:41 -07:00
public bool IsUsingItem => SelectedItem?.IsUsing ?? false;
2023-03-19 23:36:36 -07:00
2023-06-03 18:21:46 -07:00
public override void _Ready()
{
2023-12-31 05:59:33 -08:00
if (Hotbar 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
2023-12-31 05:59:33 -08:00
Hotbar = new();
}
if (Items is null)
{
Items = new();
2023-04-16 14:11:17 -07:00
}
2023-12-31 05:59:33 -08:00
2023-06-03 18:21:46 -07:00
Character = GetParent<Character>();
foreach (Node child in GetChildren())
{
2023-06-03 18:21:46 -07:00
if (child is Item item)
{
2023-06-03 18:21:46 -07:00
AddItem(item);
}
}
2023-12-31 05:59:33 -08:00
2023-06-03 18:21:46 -07:00
base._Ready();
}
2024-01-01 22:55:53 -08:00
public virtual bool EquipIndex(int index)
2023-08-15 00:44:44 -07:00
{
2024-01-01 22:55:53 -08:00
if (index >= Hotbar.Count)
2023-08-15 00:44:44 -07:00
{
2024-01-01 22:55:53 -08:00
return false;
2023-08-15 00:44:44 -07:00
}
Item prevItem = _selectedItem;
prevItem?.Unequip(Character);
2024-01-01 22:55:53 -08:00
_selectedIndex = index;
2023-08-15 00:44:44 -07:00
2024-01-01 22:55:53 -08:00
if (index >= 0)
2023-03-19 23:36:36 -07:00
{
2024-01-01 22:55:53 -08:00
_selectedItem = Hotbar[index];
_selectedItem?.Equip(Character);
2023-06-03 18:21:46 -07:00
}
2024-01-01 22:55:53 -08:00
else
2023-06-03 18:21:46 -07:00
{
2024-01-01 22:55:53 -08:00
_selectedItem = null;
2023-06-03 18:21:46 -07:00
}
2023-03-19 23:36:36 -07:00
EmitSignal(SignalName.EquippedItem, prevItem, _selectedItem);
2024-01-01 22:55:53 -08:00
GD.Print($"Inventory: {index} is new selected index.");
return true;
}
2023-03-19 23:36:36 -07:00
2024-01-01 22:55:53 -08:00
protected virtual bool EquipItem(Item item)
{
if (item is null)
2023-06-03 18:21:46 -07:00
{
2024-01-01 22:55:53 -08:00
EquipIndex(-1);
2022-11-13 19:52:09 -08:00
}
2024-01-01 22:55:53 -08:00
int index = Hotbar.IndexOf(item);
if (index < 0)
{
GD.PushWarning("Trying to equip item not in the hot inventory.");
}
return EquipIndex(index);
2023-06-03 18:21:46 -07:00
}
2024-01-01 22:55:53 -08:00
[System.Obsolete]
2023-06-03 18:21:46 -07:00
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];
2023-12-31 05:59:33 -08:00
if (idx < Hotbar.Count)
2023-04-16 14:11:17 -07:00
{
2023-12-31 05:59:33 -08:00
return Hotbar[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
public Item AddToHotbar(ItemMetadata metadata)
2023-12-31 05:59:33 -08:00
{
//AddItemMetadata(metadata);
2023-12-31 05:59:33 -08:00
var item = metadata.Instance.Instantiate<Item>();
AddItem(item);
AddChild(item);
GD.Print("Added " + item.Metadata.Name);
return item;
}
2023-06-03 18:21:46 -07:00
public Item AddItem(Item item)
{
if (Hotbar.Count >= HotbarCapacity)
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;
2023-12-31 05:59:33 -08:00
if (!Hotbar.Contains(item))
2022-11-13 19:52:09 -08:00
{
2023-12-31 05:59:33 -08:00
Hotbar.Add(item);
2022-11-13 19:52:09 -08:00
}
2023-06-03 18:21:46 -07:00
return item;
}
public System.Collections.Generic.IEnumerable<ItemMetadata> GetItems()
{
return Items;
}
public bool Add(ItemMetadata item)
{
if (Items.Count >= Capacity)
{
return false;
}
Items.Add(item);
EmitSignal(SignalName.ItemAdded, item);
return true;
}
public bool Remove(ItemMetadata item)
{
return Items.Remove(item);
}
2023-06-03 18:21:46 -07:00
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
}
}