SupaLidlGame/Items/Inventory.cs

77 lines
1.8 KiB
C#
Raw Normal View History

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; }
public List<Item> Items { get; private set; } = new List<Item>();
public const int MaxCapacity = 32;
private Item _selectedItem;
public Item SelectedItem
{
get => _selectedItem;
set
{
if (!Items.Contains(value))
{
GD.PrintErr("Tried to equip an item not in the inventory.");
return;
}
if (_selectedItem is not null)
{
_selectedItem.Unequip(Character);
}
_selectedItem = value;
2022-11-19 21:21:12 -08:00
// this is to handle if item was manually unequipped
if (_selectedItem is not null)
{
_selectedItem.Equip(Character);
}
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;
2022-11-13 19:52:09 -08:00
Items.Add(item);
return item;
}
public Item DropItem(Item item)
{
2022-11-19 21:21:12 -08:00
item.CharacterOwner = null;
item.Visible = true;
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>();
2022-11-18 13:53:51 -08:00
foreach (Node child in GetChildren())
{
if (child is Item item)
{
2022-11-19 21:21:12 -08:00
AddItem(item);
2022-11-18 13:53:51 -08:00
}
}
2022-11-13 19:52:09 -08:00
base._Ready();
}
}
}