godot_grid_inventory/Code/InventoryController.cs

159 lines
5.1 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;
private Inventory _inventoryUI;
2024-01-14 00:30:09 -05:00
#endregion
[Export] public float CellSize { get; set; } = 25;
[Export] public PackedScene InventoryUIResource { get; set; }
[Export] public PackedScene InventoryGridUIResource { get; set; }
[Export] public PackedScene InventoryGridItemUIResource { get; set; }
public override void _Ready()
{
_inventoryUI = InventoryUIResource.Instantiate() as Inventory;
_inventoryUI.InitializeInventory(this);
AddChild(_inventoryUI);
AddInventory("Player Inventory", new Vector2(10, 10), new string[] {"item_small","item_small","item_small","item_small","item_small","item_small","item_small","item_small","item_small","item_small","item_small","item_small","item_small","item_small","item_small","item_small","item_small"});
base._Ready();
}
2024-01-14 00:30:09 -05:00
#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);
var gridInterface = InventoryGridUIResource.Instantiate() as UI.InventoryGrid;
_inventoryUI.AddChild(gridInterface);
gridInterface.InitializeInventoryGrid(inventoryGrid, this);
var itemGraphics = new List<InventoryGridItem>();
_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 = gridInterface,
GridWidth = (int)Math.Round(size.X),
GridHeight = (int)Math.Round(size.Y)
});
foreach (var item in items)
{
var itemConfig = _itemDatabase.GetItemConfiguration(item);
var itemGraphic = InventoryGridItemUIResource.Instantiate() as InventoryGridItem;
itemGraphic.InitializeItem(itemConfig, this);
gridInterface.AddItem(itemGraphic,false);
itemGraphics.Add(itemGraphic);
}
_inventoryIdCounter++;
}
public bool RemoveInventory(int Id)
{
var inventory = _inventories[Id];
if(inventory == null)
{
GD.PrintErr($"Inventory not found with id {Id}.");
return false;
}
inventory.GridInterface.QueueFree();
_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>();
2024-01-11 21:00:48 -05:00
}