SupaLidlGame/Items/Inventory.cs

171 lines
3.8 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
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);
2023-12-31 05:59:33 -08:00
public const int MaxCapacity = 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;
set => EquipItem(value, ref _selectedItem);
}
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 Inventory()
{
2023-12-09 16:26:44 -08:00
//InventoryMap = new Dictionary<string, int>();
//InventoryMap.Add("equip_1", 0);
//InventoryMap.Add("equip_2", 1);
//InventoryMap.Add("equip_3", 2);
2023-06-03 18:21:46 -07:00
}
2023-04-16 14:11:17 -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
Events.EventBus.Instance.EmitSignal(
Events.EventBus.SignalName.PlayerInventoryUpdate, this);
2023-06-03 18:21:46 -07:00
base._Ready();
}
2023-08-15 00:44:44 -07:00
public bool EquipIndex(int index)
{
2023-12-31 05:59:33 -08:00
if (index < Hotbar.Count)
2023-08-15 00:44:44 -07:00
{
2023-12-31 05:59:33 -08:00
return EquipItem(Hotbar[index], ref _selectedItem);
2023-08-15 00:44:44 -07:00
}
return EquipItem(null, ref _selectedItem);
}
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-12-31 05:59:33 -08:00
if (!Hotbar.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-12-31 05:59:33 -08:00
Events.EventBus.Instance.EmitSignal(
Events.EventBus.SignalName.PlayerInventoryUpdate, this);
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];
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
2023-12-31 05:59:33 -08:00
public Item AddItemToHotbar(ItemMetadata metadata)
{
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)
{
2023-12-31 05:59:33 -08:00
if (Hotbar.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;
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 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
}
}