godot_grid_inventory/Code/InventoryController.cs

144 lines
3.9 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
2024-01-11 21:00:48 -05:00
using Godot;
using GodotGridInventory.Code.Grid;
2024-01-14 00:30:09 -05:00
using GodotGridInventory.Code.UI;
using InventoryGrid = GodotGridInventory.Code.Grid.InventoryGrid;
2024-01-11 21:00:48 -05:00
namespace GodotGridInventory.Code;
2024-01-14 00:30:09 -05:00
public partial class InventoryController : Control
2024-01-11 21:00:48 -05:00
{
2024-01-14 00:30:09 -05:00
public static int PLAYER_INVENTORY_ID = 0;
#region Private Variables
private readonly ItemDatabase _itemDatabase = ItemDatabase.Instance;
2024-01-11 21:00:48 -05:00
2024-01-14 00:30:09 -05:00
private int _inventoryIdCounter = 0;
#endregion
[Export] public float CellSize { get; set; } = 25;
#region Public Methods
public List<InventoryModel> GetInventoriesLoaded()
{
2024-01-14 00:30:09 -05:00
var result = _inventories.Values.ToList();
GD.Print($"Getting list of all _inventoriesLoaded loaded into memory ({result.Count}).");
return result;
}
2024-01-14 00:30:09 -05:00
public bool UpdateInventory(int id, InventoryModel inventoryModel)
{
var inventory = _inventories[id];
if(inventory == null)
{
GD.PrintErr($"Inventory not found with id {id}.");
return false;
}
_inventories[id] = inventoryModel;
return true;
}
public InventoryModel GetInventory(int id)
{
var inventory = _inventories[id];
if(inventory == null)
{
GD.PrintErr($"Inventory not found with id {id}.");
return null;
}
var result = _inventories[id];
return result;
}
public void AddInventory(string name, Vector2 size, string[] items)
{
var inventoryGrid = new InventoryGrid(this, size);
foreach (var item in items)
{
inventoryGrid.AddItem(item);
}
_inventories.Add(_inventoryIdCounter, new InventoryModel()
{
Id = _inventoryIdCounter,
Name = name,
Items = items.Select( item=> _itemDatabase.GetItemConfiguration(item)).ToList(),
Cells = inventoryGrid.GridCells,
Open = false,
Grid = inventoryGrid,
GridInterface = null,
GridWidth = (int)Math.Round(size.X),
GridHeight = (int)Math.Round(size.Y)
});
_inventoryIdCounter++;
}
public bool RemoveInventory(int Id)
{
var inventory = _inventories[Id];
if(inventory == null)
{
GD.PrintErr($"Inventory not found with id {Id}.");
return false;
}
_inventories.Remove(Id);
GD.Print($"Inventory {inventory.Name} with id {inventory.Id} removed.");
return true;
}
public bool OpenInventory(int Id)
{
var inventory = _inventories[Id];
if(inventory == null)
{
GD.PrintErr($"Inventory not found with id {Id}.");
return false;
}
if (inventory.Open == true)
{
GD.PrintErr($"Inventory with {Id} is already open.");
return false;
}
_inventories[Id].Open = true;
GD.Print($"Opened inventory {inventory.Name} with id {inventory.Id}.");
return true;
}
public bool CloseInventory(int Id)
{
if(!_inventories.ContainsKey(Id))
{
GD.PrintErr("Inventory not found with id " + Id + ".");
return false;
}
var inventory = _inventories[Id];
if (inventory.Open == false)
{
GD.PrintErr($"Inventory with {Id} is already closed.");
return false;
}
_inventories[Id].Open = false;
_inventories.Remove(Id);
GD.Print("Closed inventory " + inventory.Name + " with id " + inventory.Id + ".");
return true;
}
2024-01-14 00:30:09 -05:00
#endregion
private Dictionary<int,InventoryModel> _inventories { get; set; } = new Dictionary<int,InventoryModel>();
public override void _Ready()
{
2024-01-14 00:30:09 -05:00
base._Ready();
}
2024-01-14 00:30:09 -05:00
2024-01-11 21:00:48 -05:00
}