SupaLidlGame/Items/Inventory.cs

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