106 lines
3.0 KiB
C#
106 lines
3.0 KiB
C#
using Chickensoft.Collections;
|
|
using Chickensoft.GodotNodeInterfaces;
|
|
using Godot;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using static GameJamDungeon.Inventory;
|
|
|
|
namespace GameJamDungeon;
|
|
|
|
public interface IInventory : INode
|
|
{
|
|
public List<IInventoryItem> Items { get; }
|
|
public IAutoProp<Weapon> EquippedWeapon { get; }
|
|
|
|
public IAutoProp<Armor> EquippedArmor { get; }
|
|
|
|
public IAutoProp<Accessory> EquippedAccessory { get; }
|
|
|
|
public bool TryAdd(IInventoryItem inventoryItem);
|
|
|
|
public void Remove(IInventoryItem inventoryItem);
|
|
|
|
public void Equip(IEquipable equipable);
|
|
|
|
public void Unequip(IEquipable equipable);
|
|
|
|
public bool IsEquipped(IEquipable equipable);
|
|
|
|
event Inventory.InventoryAtCapacityEventHandler InventoryAtCapacity;
|
|
}
|
|
|
|
public partial class Inventory : Node, IInventory
|
|
{
|
|
// TODO: Constants class with export
|
|
private const int _maxInventorySize = 20;
|
|
|
|
[Signal]
|
|
public delegate void InventoryAtCapacityEventHandler(string rejectedItemName);
|
|
|
|
public Inventory()
|
|
{
|
|
Items = [];
|
|
}
|
|
|
|
public List<IInventoryItem> Items { get; private set; }
|
|
|
|
public IAutoProp<Weapon> EquippedWeapon => _equippedWeapon;
|
|
private AutoProp<Weapon> _equippedWeapon { get; set; } = new AutoProp<Weapon>(new Weapon());
|
|
|
|
public IAutoProp<Armor> EquippedArmor => _equippedArmor;
|
|
private AutoProp<Armor> _equippedArmor { get; set; } = new AutoProp<Armor>(new Armor());
|
|
|
|
public IAutoProp<Accessory> EquippedAccessory => _equippedAccessory;
|
|
private AutoProp<Accessory> _equippedAccessory { get; set; } = new AutoProp<Accessory>(new Accessory());
|
|
|
|
public bool TryAdd(IInventoryItem inventoryItem)
|
|
{
|
|
if (Items.Count >= _maxInventorySize)
|
|
{
|
|
EmitSignal(SignalName.InventoryAtCapacity, inventoryItem.Info.Name);
|
|
return false;
|
|
}
|
|
|
|
Items.Add(inventoryItem);
|
|
return true;
|
|
}
|
|
|
|
public void Remove(IInventoryItem inventoryItem) => Items.Remove(inventoryItem);
|
|
|
|
public void Equip(IEquipable equipable)
|
|
{
|
|
if (equipable is Weapon weapon)
|
|
_equippedWeapon.OnNext(weapon);
|
|
else if (equipable is Armor armor)
|
|
_equippedArmor.OnNext(armor);
|
|
else if (equipable is Accessory accessory)
|
|
_equippedAccessory.OnNext(accessory);
|
|
else
|
|
throw new NotImplementedException("Item type is not supported.");
|
|
}
|
|
|
|
public void Unequip(IEquipable equipable)
|
|
{
|
|
if (equipable is Weapon weapon)
|
|
_equippedWeapon.OnNext(new Weapon());
|
|
else if (equipable is Armor armor)
|
|
_equippedArmor.OnNext(new Armor());
|
|
else if (equipable is Accessory accessory)
|
|
_equippedAccessory.OnNext(new Accessory());
|
|
else
|
|
throw new NotImplementedException("Item type is not supported.");
|
|
}
|
|
|
|
public bool IsEquipped(IEquipable equipable)
|
|
{
|
|
if (equipable is Weapon weapon)
|
|
return _equippedWeapon.Value.Equals(weapon);
|
|
else if (equipable is Armor armor)
|
|
return _equippedArmor.Value.Equals(armor);
|
|
else if (equipable is Accessory accessory)
|
|
return _equippedAccessory.Value.Equals(accessory);
|
|
else
|
|
throw new NotImplementedException("Item type is not supported.");
|
|
}
|
|
}
|