mirror of
https://github.com/D4M13N-D3V/godot_grid_inventory.git
synced 2025-03-14 08:14:55 +00:00
started C# refactor
This commit is contained in:
parent
6a3c366cd9
commit
d3c207e8f2
13
.idea/.idea.Godot Grid Inventory/.idea/.gitignore
generated
vendored
Normal file
13
.idea/.idea.Godot Grid Inventory/.idea/.gitignore
generated
vendored
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Rider ignored files
|
||||||
|
/modules.xml
|
||||||
|
/projectSettingsUpdater.xml
|
||||||
|
/contentModel.xml
|
||||||
|
/.idea.Godot Grid Inventory.iml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
1
.idea/.idea.Godot Grid Inventory/.idea/.name
generated
Normal file
1
.idea/.idea.Godot Grid Inventory/.idea/.name
generated
Normal file
@ -0,0 +1 @@
|
|||||||
|
Godot Grid Inventory
|
6
.idea/.idea.Godot Grid Inventory/.idea/encodings.xml
generated
Normal file
6
.idea/.idea.Godot Grid Inventory/.idea/encodings.xml
generated
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="Encoding" addBOMForNewFiles="with BOM under Windows, with no BOM otherwise" />
|
||||||
|
<component name="Encoding" addBOMForNewFiles="with BOM under Windows, with no BOM otherwise" />
|
||||||
|
<component name="Encoding" addBOMForNewFiles="with BOM under Windows, with no BOM otherwise" />
|
||||||
|
</project>
|
8
.idea/.idea.Godot Grid Inventory/.idea/indexLayout.xml
generated
Normal file
8
.idea/.idea.Godot Grid Inventory/.idea/indexLayout.xml
generated
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="UserContentModel">
|
||||||
|
<attachedFolders />
|
||||||
|
<explicitIncludes />
|
||||||
|
<explicitExcludes />
|
||||||
|
</component>
|
||||||
|
</project>
|
6
.idea/.idea.Godot Grid Inventory/.idea/vcs.xml
generated
Normal file
6
.idea/.idea.Godot Grid Inventory/.idea/vcs.xml
generated
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
126
.idea/.idea.Grid Inventory System/.idea/workspace.xml
generated
Normal file
126
.idea/.idea.Grid Inventory System/.idea/workspace.xml
generated
Normal file
@ -0,0 +1,126 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="AutoImportSettings">
|
||||||
|
<option name="autoReloadType" value="SELECTIVE" />
|
||||||
|
</component>
|
||||||
|
<component name="ChangeListManager">
|
||||||
|
<list default="true" id="b9d65472-d9a7-4eff-9052-6bfc4feb9ff9" name="Changes" comment="">
|
||||||
|
<change beforePath="$PROJECT_DIR$/debug.gd" beforeDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/grid_inventory_system/debug/debug_ui.tscn" beforeDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/grid_inventory_system/debug/hat.png" beforeDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/grid_inventory_system/debug/hat.png.import" beforeDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/grid_inventory_system/debug/shirt.png" beforeDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/grid_inventory_system/debug/shirt.png.import" beforeDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/grid_inventory_system/items/body.tres" beforeDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/grid_inventory_system/items/head.tres" beforeDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/grid_inventory_system/items/large_debug_item.tres" beforeDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/grid_inventory_system/items/medium_debug_item.tres" beforeDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/grid_inventory_system/items/small_debug_item.tres" beforeDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/grid_inventory_system/items/weapon.tres" beforeDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/grid_inventory_system/scenes/ContextMenu.tscn" beforeDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/grid_inventory_system/scenes/Inventory.tscn" beforeDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/grid_inventory_system/scenes/InventorySlot.tscn" beforeDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/grid_inventory_system/scenes/Item.tscn" beforeDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/grid_inventory_system/scripts/ContextMenu.gd" beforeDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/grid_inventory_system/scripts/InventoryController.gd" beforeDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/grid_inventory_system/scripts/InventoryGrid.gd" beforeDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/grid_inventory_system/scripts/InventorySlot.gd" beforeDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/grid_inventory_system/scripts/Item.gd" beforeDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/grid_inventory_system/scripts/ItemConfiguration.gd" beforeDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/grid_inventory_system/scripts/ItemDB.gd" beforeDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/grid_inventory_system/scripts/ItemSlot.gd" beforeDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/icon.svg.import" beforeDir="false" afterPath="$PROJECT_DIR$/icon.svg.import" afterDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/project.godot" beforeDir="false" afterPath="$PROJECT_DIR$/project.godot" afterDir="false" />
|
||||||
|
</list>
|
||||||
|
<option name="SHOW_DIALOG" value="false" />
|
||||||
|
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||||
|
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
|
||||||
|
<option name="LAST_RESOLUTION" value="IGNORE" />
|
||||||
|
</component>
|
||||||
|
<component name="Git.Settings">
|
||||||
|
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
|
||||||
|
</component>
|
||||||
|
<component name="MarkdownSettingsMigration">
|
||||||
|
<option name="stateVersion" value="1" />
|
||||||
|
</component>
|
||||||
|
<component name="ProjectColorInfo">{
|
||||||
|
"associatedIndex": 0
|
||||||
|
}</component>
|
||||||
|
<component name="ProjectId" id="2amytOwRHFk0dOiEf8NQEHEAn2s" />
|
||||||
|
<component name="ProjectViewState">
|
||||||
|
<option name="hideEmptyMiddlePackages" value="true" />
|
||||||
|
<option name="showLibraryContents" value="true" />
|
||||||
|
</component>
|
||||||
|
<component name="PropertiesComponent"><![CDATA[{
|
||||||
|
"keyToString": {
|
||||||
|
".NET Executable.Editor.executor": "Run",
|
||||||
|
".NET Executable.Player.executor": "Run",
|
||||||
|
"RunOnceActivity.OpenProjectViewOnStart": "true",
|
||||||
|
"RunOnceActivity.ShowReadmeOnStart": "true",
|
||||||
|
"git-widget-placeholder": "9-setup-project-for-c-development-and-refactor-existing-systems",
|
||||||
|
"last_opened_file_path": "/home/damienostler/Documents/GoDot/godot_grid_inventory",
|
||||||
|
"node.js.detected.package.eslint": "true",
|
||||||
|
"node.js.detected.package.tslint": "true",
|
||||||
|
"node.js.selected.package.eslint": "(autodetect)",
|
||||||
|
"node.js.selected.package.tslint": "(autodetect)",
|
||||||
|
"nodejs_package_manager_path": "npm",
|
||||||
|
"settings.editor.selected.configurable": "preferences.pluginManager",
|
||||||
|
"vue.rearranger.settings.migration": "true"
|
||||||
|
},
|
||||||
|
"keyToStringList": {
|
||||||
|
"rider.external.source.directories": [
|
||||||
|
"/home/damienostler/.config/JetBrains/Rider2023.3/resharper-host/DecompilerCache",
|
||||||
|
"/home/damienostler/.config/JetBrains/Rider2023.3/resharper-host/SourcesCache",
|
||||||
|
"/home/damienostler/.local/share/Symbols/src"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}]]></component>
|
||||||
|
<component name="RunManager" selected=".NET Executable.Player">
|
||||||
|
<configuration name="Editor" type="RunExe" factoryName=".NET Executable">
|
||||||
|
<option name="EXE_PATH" value="$USER_HOME$/Desktop/Godot_v4.2.1-stable_mono_linux_x86_64/Godot_v4.2.1-stable_mono_linux.x86_64" />
|
||||||
|
<option name="PROGRAM_PARAMETERS" value="--path "$PROJECT_DIR$" --editor" />
|
||||||
|
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
|
||||||
|
<option name="PASS_PARENT_ENVS" value="1" />
|
||||||
|
<option name="USE_EXTERNAL_CONSOLE" value="0" />
|
||||||
|
<option name="USE_MONO" value="0" />
|
||||||
|
<option name="RUNTIME_ARGUMENTS" value="" />
|
||||||
|
<option name="RUNTIME_TYPE" value="coreclr" />
|
||||||
|
<method v="2">
|
||||||
|
<option name="Build Solution" enabled="true" />
|
||||||
|
</method>
|
||||||
|
</configuration>
|
||||||
|
<configuration name="Player" type="RunExe" factoryName=".NET Executable">
|
||||||
|
<option name="EXE_PATH" value="$USER_HOME$/Desktop/Godot_v4.2.1-stable_mono_linux_x86_64/Godot_v4.2.1-stable_mono_linux.x86_64" />
|
||||||
|
<option name="PROGRAM_PARAMETERS" value="--path "$PROJECT_DIR$"" />
|
||||||
|
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
|
||||||
|
<option name="PASS_PARENT_ENVS" value="1" />
|
||||||
|
<option name="USE_EXTERNAL_CONSOLE" value="0" />
|
||||||
|
<option name="USE_MONO" value="0" />
|
||||||
|
<option name="RUNTIME_ARGUMENTS" value="" />
|
||||||
|
<option name="RUNTIME_TYPE" value="coreclr" />
|
||||||
|
<method v="2">
|
||||||
|
<option name="Build Solution" enabled="true" />
|
||||||
|
</method>
|
||||||
|
</configuration>
|
||||||
|
</component>
|
||||||
|
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
|
||||||
|
<component name="TaskManager">
|
||||||
|
<task active="true" id="Default" summary="Default task">
|
||||||
|
<changelist id="b9d65472-d9a7-4eff-9052-6bfc4feb9ff9" name="Changes" comment="" />
|
||||||
|
<created>1704938262746</created>
|
||||||
|
<option name="number" value="Default" />
|
||||||
|
<option name="presentableId" value="Default" />
|
||||||
|
<updated>1704938262746</updated>
|
||||||
|
<workItem from="1704938264449" duration="187000" />
|
||||||
|
<workItem from="1704938510358" duration="299000" />
|
||||||
|
<workItem from="1704938819194" duration="3296000" />
|
||||||
|
</task>
|
||||||
|
<servers />
|
||||||
|
</component>
|
||||||
|
<component name="TypeScriptGeneratedFilesManager">
|
||||||
|
<option name="version" value="3" />
|
||||||
|
</component>
|
||||||
|
<component name="VcsManagerConfiguration">
|
||||||
|
<option name="CLEAR_INITIAL_COMMIT_MESSAGE" value="true" />
|
||||||
|
</component>
|
||||||
|
</project>
|
13
.idea/.idea.godot_grid_inventory.dir/.idea/.gitignore
generated
vendored
Normal file
13
.idea/.idea.godot_grid_inventory.dir/.idea/.gitignore
generated
vendored
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Rider ignored files
|
||||||
|
/contentModel.xml
|
||||||
|
/projectSettingsUpdater.xml
|
||||||
|
/.idea.godot_grid_inventory.iml
|
||||||
|
/modules.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
4
.idea/.idea.godot_grid_inventory.dir/.idea/encodings.xml
generated
Normal file
4
.idea/.idea.godot_grid_inventory.dir/.idea/encodings.xml
generated
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="Encoding" addBOMForNewFiles="with BOM under Windows, with no BOM otherwise" />
|
||||||
|
</project>
|
8
.idea/.idea.godot_grid_inventory.dir/.idea/indexLayout.xml
generated
Normal file
8
.idea/.idea.godot_grid_inventory.dir/.idea/indexLayout.xml
generated
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="UserContentModel">
|
||||||
|
<attachedFolders />
|
||||||
|
<explicitIncludes />
|
||||||
|
<explicitExcludes />
|
||||||
|
</component>
|
||||||
|
</project>
|
6
.idea/.idea.godot_grid_inventory.dir/.idea/vcs.xml
generated
Normal file
6
.idea/.idea.godot_grid_inventory.dir/.idea/vcs.xml
generated
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
7
Code/Grid/Enums/EnumInventoryGridCellState.cs
Normal file
7
Code/Grid/Enums/EnumInventoryGridCellState.cs
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
namespace GodotGridInventory.Code.Grid.Enums;
|
||||||
|
|
||||||
|
public enum EnumInventoryGridCellState
|
||||||
|
{
|
||||||
|
Available,
|
||||||
|
Unavailable
|
||||||
|
}
|
21
Code/Grid/InventoryCell.cs
Normal file
21
Code/Grid/InventoryCell.cs
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
using Godot;
|
||||||
|
using GodotGridInventory.Code.Grid.Enums;
|
||||||
|
|
||||||
|
namespace GodotGridInventory.Code.Grid;
|
||||||
|
|
||||||
|
public class InventoryCell
|
||||||
|
{
|
||||||
|
private readonly InventoryGrid _parentGrid;
|
||||||
|
public Vector2 Position { get; set; }
|
||||||
|
public EnumInventoryGridCellState State { get; set; }
|
||||||
|
public ItemConfiguration? Item { get; set; } = null;
|
||||||
|
|
||||||
|
|
||||||
|
public InventoryCell(Vector2 position, InventoryGrid parentGrid)
|
||||||
|
{
|
||||||
|
_parentGrid = parentGrid;
|
||||||
|
State = EnumInventoryGridCellState.Available;
|
||||||
|
Item = null;
|
||||||
|
Position = position;
|
||||||
|
}
|
||||||
|
}
|
166
Code/Grid/InventoryGrid.cs
Normal file
166
Code/Grid/InventoryGrid.cs
Normal file
@ -0,0 +1,166 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using Godot;
|
||||||
|
using GodotGridInventory.Code.Grid.Enums;
|
||||||
|
|
||||||
|
namespace GodotGridInventory.Code.Grid;
|
||||||
|
|
||||||
|
public class InventoryGrid
|
||||||
|
{
|
||||||
|
private readonly int _gridWidth;
|
||||||
|
private readonly int _gridHeight;
|
||||||
|
|
||||||
|
private readonly List<InventoryCell> _gridCells = new();
|
||||||
|
|
||||||
|
public InventoryGrid(int width, int height)
|
||||||
|
{
|
||||||
|
_gridWidth = width;
|
||||||
|
_gridHeight = height;
|
||||||
|
InitializeGrid();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void InitializeGrid()
|
||||||
|
{
|
||||||
|
for(var x = 0; x < _gridWidth; x++)
|
||||||
|
{
|
||||||
|
for(var y = 0; y < _gridHeight; y++)
|
||||||
|
{
|
||||||
|
var cellPosition = new Vector2(x, y);
|
||||||
|
var cell = new InventoryCell(cellPosition, this);
|
||||||
|
_gridCells.Add(cell);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#region Private Methods
|
||||||
|
/// <summary>
|
||||||
|
/// Sets the item of a cell at a given position.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="position">The position of the cell to update.</param>
|
||||||
|
/// <param name="item">The item to set the cell to.</param>
|
||||||
|
private void SetCellItem(Vector2 position, ItemConfiguration item)
|
||||||
|
{
|
||||||
|
var cell = _gridCells.FirstOrDefault(cell => cell.Position == position);
|
||||||
|
if(cell != null)
|
||||||
|
{
|
||||||
|
cell.Item = item;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Clears the item of a cell at a given position.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="position">The position of the cell to update.</param>
|
||||||
|
private void ClearCellItem(Vector2 position)
|
||||||
|
{
|
||||||
|
var cell = _gridCells.FirstOrDefault(cell => cell.Position == position);
|
||||||
|
if(cell != null)
|
||||||
|
{
|
||||||
|
cell.Item = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sets the state of a cell at a given position in inventory grid space.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="position">The position of the cell to set.</param>
|
||||||
|
/// <param name="state">The state to set the cell</param>
|
||||||
|
private void SetCellState(Vector2 position, EnumInventoryGridCellState state)
|
||||||
|
{
|
||||||
|
var cell = _gridCells.FirstOrDefault(cell => cell.Position == position);
|
||||||
|
if(cell != null)
|
||||||
|
{
|
||||||
|
cell.State = state;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sets the state of a selection of cells at a given position and size in inventory grid space.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="position">The position of the cells to set from the top left..</param>
|
||||||
|
/// <param name="size">The size of the area to set starting at the top left.</param>
|
||||||
|
/// <param name="state">The state to set the cells</param>
|
||||||
|
private void SetCellAreaState(Vector2 position, Vector2 size, EnumInventoryGridCellState state)
|
||||||
|
{
|
||||||
|
foreach (var cell in _gridCells.Where(cell => cell.Position.X >= position.X && cell.Position.X < position.X + size.X && cell.Position.Y >= position.Y && cell.Position.Y < position.Y + size.Y))
|
||||||
|
{
|
||||||
|
cell.State = state;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the first available cell position that can fit an item of a given size.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="size">The size to check for.</param>
|
||||||
|
/// <returns>The first coordinates available for something of the given size, or null if no space available.</returns>
|
||||||
|
private Vector2? GetAvailableCellPosition(Vector2 size)
|
||||||
|
{
|
||||||
|
var availableCells = _gridCells.Where(cell => cell.State == EnumInventoryGridCellState.Available);
|
||||||
|
var cell = availableCells.FirstOrDefault();
|
||||||
|
if(cell != null)
|
||||||
|
{
|
||||||
|
return cell.Position;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int x = 0; x <= _gridWidth - size.X; x++)
|
||||||
|
{
|
||||||
|
for (int y = 0; y <= _gridHeight - size.Y; y++)
|
||||||
|
{
|
||||||
|
// Check if the area is available
|
||||||
|
if (IsSpaceAvailable(new Vector2(x, y), size))
|
||||||
|
{
|
||||||
|
return new Vector2(x,y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// This checks all of the cells in a space to see if all of them are available.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="position">The top left position of the item in the inventories grid space.</param>
|
||||||
|
/// <param name="size">The size of the item in inventory grid space.</param>
|
||||||
|
/// <returns>A List of cells that are available for the item to be placed in.</returns>
|
||||||
|
private bool IsSpaceAvailable(Vector2 position, Vector2 size)
|
||||||
|
{
|
||||||
|
var cells = _gridCells.Where( cell => cell.Position.X >= position.X && cell.Position.X < position.X + size.X && cell.Position.Y >= position.Y && cell.Position.Y < position.Y + size.Y);
|
||||||
|
return cells.All(cell => cell.State == EnumInventoryGridCellState.Available);
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Public Methods
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds an item to the inventory grid.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="itemId">The ID of the item to add.</param>
|
||||||
|
/// <param name="position">The position to add the item at in inventory grid space. If null then will insert at first available place.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public bool AddItem(string itemId, Vector2? position)
|
||||||
|
{
|
||||||
|
var item = ItemDatabase.Instance.GetItemConfiguration(itemId);
|
||||||
|
if(item == null)
|
||||||
|
{
|
||||||
|
GD.PrintErr("Attempted to add item with id " + itemId + ", which does not exist.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(position.HasValue==false)
|
||||||
|
{
|
||||||
|
position = GetAvailableCellPosition(item.Size);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (position.HasValue==false)
|
||||||
|
{
|
||||||
|
GD.PrintErr("Attempted to add item with id " + itemId + ", but there is no space available.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
SetCellAreaState((Vector2)position, item.Size, EnumInventoryGridCellState.Unavailable);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
}
|
8
Code/InventoryController.cs
Normal file
8
Code/InventoryController.cs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
using Godot;
|
||||||
|
|
||||||
|
namespace GodotGridInventory.Code;
|
||||||
|
|
||||||
|
public class InventoryController : Node
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
33
Code/ItemDatabase/ItemDatabase.cs
Normal file
33
Code/ItemDatabase/ItemDatabase.cs
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
using System.Linq;
|
||||||
|
using Godot;
|
||||||
|
using Godot.Collections;
|
||||||
|
|
||||||
|
namespace GodotGridInventory.Code;
|
||||||
|
|
||||||
|
public class ItemDatabase
|
||||||
|
{
|
||||||
|
public static ItemDatabase Instance { get; } = new();
|
||||||
|
private readonly Dictionary<string, ItemConfiguration> _itemDatabase = new();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public ItemConfiguration GetItemConfiguration(string id)
|
||||||
|
{
|
||||||
|
return _itemDatabase[id];
|
||||||
|
}
|
||||||
|
|
||||||
|
private void InitializeItemDatabase()
|
||||||
|
{
|
||||||
|
GD.Print("Initializing the item database.");
|
||||||
|
var items = DirAccess.GetFilesAt(ItemDatabaseConstants.ITEMS_PATH).Where(filePath => filePath.EndsWith(ItemDatabaseConstants.ITEM_EXTENSION));
|
||||||
|
foreach (var item in items)
|
||||||
|
{
|
||||||
|
var itemResource = GD.Load<ItemConfiguration>(item);
|
||||||
|
if (itemResource != null)
|
||||||
|
{
|
||||||
|
_itemDatabase.Add(itemResource.Id, itemResource);
|
||||||
|
GD.Print($"Loaded item {itemResource.Name} with id {itemResource.Id} into the item database.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
7
Code/ItemDatabase/ItemDatabaseConstants.cs
Normal file
7
Code/ItemDatabase/ItemDatabaseConstants.cs
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
namespace GodotGridInventory.Code;
|
||||||
|
|
||||||
|
public class ItemDatabaseConstants
|
||||||
|
{
|
||||||
|
public static string ITEMS_PATH = "res://grid_inventory_system/items";
|
||||||
|
public static string ITEM_EXTENSION = ".tres";
|
||||||
|
}
|
15
Code/Items/Enums/EnumItemActions.cs
Normal file
15
Code/Items/Enums/EnumItemActions.cs
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace GodotGridInventory.Code.Items.Enums;
|
||||||
|
|
||||||
|
[Flags]
|
||||||
|
public enum EnumItemActions
|
||||||
|
{
|
||||||
|
None = 0,
|
||||||
|
Examine = 1 << 1,
|
||||||
|
Equip = 1 << 2,
|
||||||
|
Use = 1 << 3,
|
||||||
|
Open = 1 << 4,
|
||||||
|
Drop = 1 << 5,
|
||||||
|
Destroy = 1 << 6
|
||||||
|
}
|
15
Code/Items/ItemConfiguration.cs
Normal file
15
Code/Items/ItemConfiguration.cs
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
using Godot;
|
||||||
|
using System;
|
||||||
|
using GodotGridInventory.Code.Items.Enums;
|
||||||
|
|
||||||
|
[GlobalClass, Icon("res://icon.svg")]
|
||||||
|
public partial class ItemConfiguration : Resource
|
||||||
|
{
|
||||||
|
[Export] public string Id { get; set; }
|
||||||
|
[Export] public string Name { get; set; }
|
||||||
|
[Export] public string Description { get; set; }
|
||||||
|
[Export] public Vector2 Size { get; set; }
|
||||||
|
[Export] public Texture2D Texture { get; set; }
|
||||||
|
[Export] public EnumItemActions Actions { get; set; }
|
||||||
|
[Export] public string[] Slots { get; set; }
|
||||||
|
}
|
9
Godot Grid Inventory.csproj
Normal file
9
Godot Grid Inventory.csproj
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
<Project Sdk="Godot.NET.Sdk/4.2.1">
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
|
<TargetFramework Condition=" '$(GodotTargetPlatform)' == 'android' ">net7.0</TargetFramework>
|
||||||
|
<TargetFramework Condition=" '$(GodotTargetPlatform)' == 'ios' ">net8.0</TargetFramework>
|
||||||
|
<EnableDynamicLoading>true</EnableDynamicLoading>
|
||||||
|
<RootNamespace>GodotGridInventory</RootNamespace>
|
||||||
|
</PropertyGroup>
|
||||||
|
</Project>
|
19
Godot Grid Inventory.sln
Normal file
19
Godot Grid Inventory.sln
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
|
# Visual Studio 2012
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Godot Grid Inventory", "Godot Grid Inventory.csproj", "{93B1D768-2E58-417C-B620-206485D0BF80}"
|
||||||
|
EndProject
|
||||||
|
Global
|
||||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
ExportDebug|Any CPU = ExportDebug|Any CPU
|
||||||
|
ExportRelease|Any CPU = ExportRelease|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
|
{93B1D768-2E58-417C-B620-206485D0BF80}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{93B1D768-2E58-417C-B620-206485D0BF80}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{93B1D768-2E58-417C-B620-206485D0BF80}.ExportDebug|Any CPU.ActiveCfg = ExportDebug|Any CPU
|
||||||
|
{93B1D768-2E58-417C-B620-206485D0BF80}.ExportDebug|Any CPU.Build.0 = ExportDebug|Any CPU
|
||||||
|
{93B1D768-2E58-417C-B620-206485D0BF80}.ExportRelease|Any CPU.ActiveCfg = ExportRelease|Any CPU
|
||||||
|
{93B1D768-2E58-417C-B620-206485D0BF80}.ExportRelease|Any CPU.Build.0 = ExportRelease|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
EndGlobal
|
14
Resources/Items/debug_item_small.tres
Normal file
14
Resources/Items/debug_item_small.tres
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
[gd_resource type="Resource" script_class="ItemConfiguration" load_steps=3 format=3 uid="uid://c8ge77yvt0q84"]
|
||||||
|
|
||||||
|
[ext_resource type="Script" path="res://Code/Items/ItemConfiguration.cs" id="1_8ae1i"]
|
||||||
|
[ext_resource type="Texture2D" uid="uid://bj3tjdbw5xdlj" path="res://icon.svg" id="1_arnqm"]
|
||||||
|
|
||||||
|
[resource]
|
||||||
|
script = ExtResource("1_8ae1i")
|
||||||
|
Id = "item_small"
|
||||||
|
Name = "Item Small"
|
||||||
|
Description = "A small item."
|
||||||
|
Size = Vector2(3, 3)
|
||||||
|
Texture = ExtResource("1_arnqm")
|
||||||
|
Actions = 0
|
||||||
|
Slots = PackedStringArray()
|
33
debug.gd
33
debug.gd
@ -1,33 +0,0 @@
|
|||||||
extends Node
|
|
||||||
|
|
||||||
|
|
||||||
@export var inventory:InventoryController
|
|
||||||
@export var grid:GridContainer
|
|
||||||
func _ready():
|
|
||||||
var items = ItemDb.ITEMS
|
|
||||||
for item in items:
|
|
||||||
var button = Button.new()
|
|
||||||
grid.add_child(button)
|
|
||||||
button.text = "Spawn "+item
|
|
||||||
button.connect("pressed", func():
|
|
||||||
inventory.pickup_item(item))
|
|
||||||
|
|
||||||
|
|
||||||
func _on_button_3_pressed():
|
|
||||||
$"..".pickup_item("small_debug_item")
|
|
||||||
|
|
||||||
|
|
||||||
func _on_button_2_pressed():
|
|
||||||
$"..".pickup_item("medium_debug_item")
|
|
||||||
|
|
||||||
|
|
||||||
func _on_button_pressed():
|
|
||||||
$"..".pickup_item("large_debug_item")
|
|
||||||
|
|
||||||
|
|
||||||
func _on_button_4_pressed():
|
|
||||||
$"..".pickup_item("shirt")
|
|
||||||
|
|
||||||
|
|
||||||
func _on_button_5_pressed():
|
|
||||||
$"..".pickup_item("hat")
|
|
@ -1,20 +0,0 @@
|
|||||||
[gd_scene load_steps=3 format=3 uid="uid://c2c7v5idagdqr"]
|
|
||||||
|
|
||||||
[ext_resource type="Script" path="res://debug.gd" id="1_qtffx"]
|
|
||||||
|
|
||||||
[sub_resource type="GDScript" id="GDScript_fqjhy"]
|
|
||||||
|
|
||||||
[node name="DEBUG" type="Node" node_paths=PackedStringArray("grid")]
|
|
||||||
script = ExtResource("1_qtffx")
|
|
||||||
grid = NodePath("Items/GridContainer")
|
|
||||||
|
|
||||||
[node name="Items" type="ScrollContainer" parent="."]
|
|
||||||
anchors_preset = 9
|
|
||||||
anchor_bottom = 1.0
|
|
||||||
offset_right = 228.0
|
|
||||||
grow_vertical = 2
|
|
||||||
script = SubResource("GDScript_fqjhy")
|
|
||||||
|
|
||||||
[node name="GridContainer" type="GridContainer" parent="Items"]
|
|
||||||
unique_name_in_owner = true
|
|
||||||
layout_mode = 2
|
|
Binary file not shown.
Before Width: | Height: | Size: 825 KiB |
@ -1,34 +0,0 @@
|
|||||||
[remap]
|
|
||||||
|
|
||||||
importer="texture"
|
|
||||||
type="CompressedTexture2D"
|
|
||||||
uid="uid://ghlfm76i4kve"
|
|
||||||
path="res://.godot/imported/hat.png-706aaac0cb9cc74c2e7d8c97ed023b0c.ctex"
|
|
||||||
metadata={
|
|
||||||
"vram_texture": false
|
|
||||||
}
|
|
||||||
|
|
||||||
[deps]
|
|
||||||
|
|
||||||
source_file="res://grid_inventory_system/debug/hat.png"
|
|
||||||
dest_files=["res://.godot/imported/hat.png-706aaac0cb9cc74c2e7d8c97ed023b0c.ctex"]
|
|
||||||
|
|
||||||
[params]
|
|
||||||
|
|
||||||
compress/mode=3
|
|
||||||
compress/high_quality=false
|
|
||||||
compress/lossy_quality=0.7
|
|
||||||
compress/hdr_compression=1
|
|
||||||
compress/normal_map=0
|
|
||||||
compress/channel_pack=0
|
|
||||||
mipmaps/generate=false
|
|
||||||
mipmaps/limit=-1
|
|
||||||
roughness/mode=0
|
|
||||||
roughness/src_normal=""
|
|
||||||
process/fix_alpha_border=true
|
|
||||||
process/premult_alpha=false
|
|
||||||
process/normal_map_invert_y=false
|
|
||||||
process/hdr_as_srgb=false
|
|
||||||
process/hdr_clamp_exposure=false
|
|
||||||
process/size_limit=0
|
|
||||||
detect_3d/compress_to=1
|
|
Binary file not shown.
Before Width: | Height: | Size: 146 KiB |
@ -1,34 +0,0 @@
|
|||||||
[remap]
|
|
||||||
|
|
||||||
importer="texture"
|
|
||||||
type="CompressedTexture2D"
|
|
||||||
uid="uid://bdfcmsyry3sg3"
|
|
||||||
path="res://.godot/imported/shirt.png-c04d1f8c53dc5f843f267734a8b0f810.ctex"
|
|
||||||
metadata={
|
|
||||||
"vram_texture": false
|
|
||||||
}
|
|
||||||
|
|
||||||
[deps]
|
|
||||||
|
|
||||||
source_file="res://grid_inventory_system/debug/shirt.png"
|
|
||||||
dest_files=["res://.godot/imported/shirt.png-c04d1f8c53dc5f843f267734a8b0f810.ctex"]
|
|
||||||
|
|
||||||
[params]
|
|
||||||
|
|
||||||
compress/mode=3
|
|
||||||
compress/high_quality=false
|
|
||||||
compress/lossy_quality=0.7
|
|
||||||
compress/hdr_compression=1
|
|
||||||
compress/normal_map=0
|
|
||||||
compress/channel_pack=0
|
|
||||||
mipmaps/generate=false
|
|
||||||
mipmaps/limit=-1
|
|
||||||
roughness/mode=0
|
|
||||||
roughness/src_normal=""
|
|
||||||
process/fix_alpha_border=true
|
|
||||||
process/premult_alpha=false
|
|
||||||
process/normal_map_invert_y=false
|
|
||||||
process/hdr_as_srgb=false
|
|
||||||
process/hdr_clamp_exposure=false
|
|
||||||
process/size_limit=0
|
|
||||||
detect_3d/compress_to=1
|
|
@ -1,18 +0,0 @@
|
|||||||
[gd_resource type="Resource" script_class="ItemConfiguration" load_steps=3 format=3 uid="uid://bu1t7ogoct2pm"]
|
|
||||||
|
|
||||||
[ext_resource type="Script" path="res://grid_inventory_system/scripts/ItemConfiguration.gd" id="1_f4bsa"]
|
|
||||||
[ext_resource type="Texture2D" uid="uid://bdfcmsyry3sg3" path="res://grid_inventory_system/debug/shirt.png" id="1_gmlai"]
|
|
||||||
|
|
||||||
[resource]
|
|
||||||
script = ExtResource("1_f4bsa")
|
|
||||||
item_id = "shirt"
|
|
||||||
item_name = "Shirt"
|
|
||||||
item_description = "This is a shirt"
|
|
||||||
item_size = Vector2(256, 256)
|
|
||||||
item_usable = false
|
|
||||||
item_openable = false
|
|
||||||
item_droppable = false
|
|
||||||
item_destroyable = false
|
|
||||||
item_equipment = true
|
|
||||||
item_equipment_slot = "BODY"
|
|
||||||
item_texture = ExtResource("1_gmlai")
|
|
@ -1,18 +0,0 @@
|
|||||||
[gd_resource type="Resource" script_class="ItemConfiguration" load_steps=3 format=3 uid="uid://cauw4i0kveld6"]
|
|
||||||
|
|
||||||
[ext_resource type="Script" path="res://grid_inventory_system/scripts/ItemConfiguration.gd" id="1_hflmv"]
|
|
||||||
[ext_resource type="Texture2D" uid="uid://ghlfm76i4kve" path="res://grid_inventory_system/debug/hat.png" id="1_vm0jo"]
|
|
||||||
|
|
||||||
[resource]
|
|
||||||
script = ExtResource("1_hflmv")
|
|
||||||
item_id = "hat"
|
|
||||||
item_name = "Hat"
|
|
||||||
item_description = "This is a hat"
|
|
||||||
item_size = Vector2(224, 160)
|
|
||||||
item_usable = false
|
|
||||||
item_openable = false
|
|
||||||
item_droppable = false
|
|
||||||
item_destroyable = false
|
|
||||||
item_equipment = true
|
|
||||||
item_equipment_slot = "HEAD"
|
|
||||||
item_texture = ExtResource("1_vm0jo")
|
|
@ -1,18 +0,0 @@
|
|||||||
[gd_resource type="Resource" script_class="ItemConfiguration" load_steps=3 format=3 uid="uid://d2orfaiyhg0sa"]
|
|
||||||
|
|
||||||
[ext_resource type="Texture2D" uid="uid://bdvx58rwnaeev" path="res://icon.svg" id="1_l6twi"]
|
|
||||||
[ext_resource type="Script" path="res://grid_inventory_system/scripts/ItemConfiguration.gd" id="2_40ofh"]
|
|
||||||
|
|
||||||
[resource]
|
|
||||||
script = ExtResource("2_40ofh")
|
|
||||||
item_id = "large_debug_item"
|
|
||||||
item_name = "Debug Item Large"
|
|
||||||
item_description = "A large item for debugging."
|
|
||||||
item_size = Vector2(256, 256)
|
|
||||||
item_usable = true
|
|
||||||
item_openable = false
|
|
||||||
item_droppable = false
|
|
||||||
item_destroyable = true
|
|
||||||
item_equipment = false
|
|
||||||
item_equipment_slot = "NONE"
|
|
||||||
item_texture = ExtResource("1_l6twi")
|
|
@ -1,18 +0,0 @@
|
|||||||
[gd_resource type="Resource" script_class="ItemConfiguration" load_steps=3 format=3 uid="uid://drkcx6hkx0ay8"]
|
|
||||||
|
|
||||||
[ext_resource type="Texture2D" uid="uid://bdvx58rwnaeev" path="res://icon.svg" id="1_jwlst"]
|
|
||||||
[ext_resource type="Script" path="res://grid_inventory_system/scripts/ItemConfiguration.gd" id="2_xdqd2"]
|
|
||||||
|
|
||||||
[resource]
|
|
||||||
script = ExtResource("2_xdqd2")
|
|
||||||
item_id = "medium_debug_item"
|
|
||||||
item_name = "Debug Item Medium"
|
|
||||||
item_description = "A medium item for debugging."
|
|
||||||
item_size = Vector2(128, 128)
|
|
||||||
item_usable = false
|
|
||||||
item_openable = true
|
|
||||||
item_droppable = true
|
|
||||||
item_destroyable = false
|
|
||||||
item_equipment = false
|
|
||||||
item_equipment_slot = "NONE"
|
|
||||||
item_texture = ExtResource("1_jwlst")
|
|
@ -1,18 +0,0 @@
|
|||||||
[gd_resource type="Resource" script_class="ItemConfiguration" load_steps=3 format=3 uid="uid://p3m1yi3kw1q6"]
|
|
||||||
|
|
||||||
[ext_resource type="Texture2D" uid="uid://bdvx58rwnaeev" path="res://icon.svg" id="1_g0vsl"]
|
|
||||||
[ext_resource type="Script" path="res://grid_inventory_system/scripts/ItemConfiguration.gd" id="2_lq0i6"]
|
|
||||||
|
|
||||||
[resource]
|
|
||||||
script = ExtResource("2_lq0i6")
|
|
||||||
item_id = "small_debug_item"
|
|
||||||
item_name = "Debug Item Small"
|
|
||||||
item_description = "A small item for debugging."
|
|
||||||
item_size = Vector2(64, 64)
|
|
||||||
item_usable = false
|
|
||||||
item_openable = false
|
|
||||||
item_droppable = false
|
|
||||||
item_destroyable = true
|
|
||||||
item_equipment = false
|
|
||||||
item_equipment_slot = "NONE"
|
|
||||||
item_texture = ExtResource("1_g0vsl")
|
|
@ -1,13 +0,0 @@
|
|||||||
[gd_resource type="Resource" script_class="ItemConfiguration" load_steps=2 format=3 uid="uid://cvy3nwmcgx5le"]
|
|
||||||
|
|
||||||
[ext_resource type="Script" path="res://grid_inventory_system/scripts/ItemConfiguration.gd" id="1_jvmcf"]
|
|
||||||
|
|
||||||
[resource]
|
|
||||||
script = ExtResource("1_jvmcf")
|
|
||||||
item_id = "item"
|
|
||||||
item_name = "Item Name"
|
|
||||||
item_description = "This is the default item description."
|
|
||||||
item_size = Vector2(3, 3)
|
|
||||||
item_usable = false
|
|
||||||
item_equipment = false
|
|
||||||
item_equipment_slot = "NONE"
|
|
@ -1,50 +0,0 @@
|
|||||||
[gd_scene load_steps=2 format=3 uid="uid://bkmpq37oqwww8"]
|
|
||||||
|
|
||||||
[ext_resource type="Script" path="res://grid_inventory_system/scripts/ContextMenu.gd" id="1_dwa8t"]
|
|
||||||
|
|
||||||
[node name="ContextMenu" type="Panel" node_paths=PackedStringArray("use_button", "open_button", "drop_button", "destroy_button")]
|
|
||||||
custom_minimum_size = Vector2(160, 0)
|
|
||||||
offset_left = 240.0
|
|
||||||
offset_top = 102.0
|
|
||||||
offset_right = 400.0
|
|
||||||
offset_bottom = 210.0
|
|
||||||
script = ExtResource("1_dwa8t")
|
|
||||||
use_button = NodePath("ContextOptions/Use")
|
|
||||||
open_button = NodePath("ContextOptions/Open")
|
|
||||||
drop_button = NodePath("ContextOptions/Drop")
|
|
||||||
destroy_button = NodePath("ContextOptions/Destroy")
|
|
||||||
|
|
||||||
[node name="ContextOptions" type="GridContainer" parent="."]
|
|
||||||
layout_mode = 0
|
|
||||||
offset_left = 5.0
|
|
||||||
offset_top = 4.0
|
|
||||||
offset_right = 95.0
|
|
||||||
offset_bottom = 42.0
|
|
||||||
|
|
||||||
[node name="Use" type="Button" parent="ContextOptions"]
|
|
||||||
custom_minimum_size = Vector2(150, 0)
|
|
||||||
layout_mode = 2
|
|
||||||
size_flags_horizontal = 3
|
|
||||||
theme_override_font_sizes/font_size = 10
|
|
||||||
text = "Use"
|
|
||||||
|
|
||||||
[node name="Open" type="Button" parent="ContextOptions"]
|
|
||||||
custom_minimum_size = Vector2(150, 0)
|
|
||||||
layout_mode = 2
|
|
||||||
size_flags_horizontal = 3
|
|
||||||
theme_override_font_sizes/font_size = 10
|
|
||||||
text = "Open"
|
|
||||||
|
|
||||||
[node name="Drop" type="Button" parent="ContextOptions"]
|
|
||||||
custom_minimum_size = Vector2(150, 0)
|
|
||||||
layout_mode = 2
|
|
||||||
size_flags_horizontal = 3
|
|
||||||
theme_override_font_sizes/font_size = 10
|
|
||||||
text = "Drop"
|
|
||||||
|
|
||||||
[node name="Destroy" type="Button" parent="ContextOptions"]
|
|
||||||
custom_minimum_size = Vector2(150, 0)
|
|
||||||
layout_mode = 2
|
|
||||||
size_flags_horizontal = 3
|
|
||||||
theme_override_font_sizes/font_size = 10
|
|
||||||
text = "Destroy"
|
|
@ -1,129 +0,0 @@
|
|||||||
[gd_scene load_steps=5 format=3 uid="uid://brs6u78mwggbp"]
|
|
||||||
|
|
||||||
[ext_resource type="Script" path="res://grid_inventory_system/scripts/InventoryController.gd" id="1_pdgp8"]
|
|
||||||
[ext_resource type="Script" path="res://grid_inventory_system/scripts/ItemSlot.gd" id="2_7s2pc"]
|
|
||||||
[ext_resource type="Script" path="res://grid_inventory_system/scripts/InventoryGrid.gd" id="3_05dq4"]
|
|
||||||
[ext_resource type="PackedScene" uid="uid://c2c7v5idagdqr" path="res://grid_inventory_system/debug/debug_ui.tscn" id="4_w8gq7"]
|
|
||||||
|
|
||||||
[node name="Inventory" type="Control" node_paths=PackedStringArray("inventory_grid", "inventory_loot_grid", "inventory_background", "inventory_equipment_slots")]
|
|
||||||
layout_mode = 3
|
|
||||||
anchors_preset = 15
|
|
||||||
anchor_right = 1.0
|
|
||||||
anchor_bottom = 1.0
|
|
||||||
grow_horizontal = 2
|
|
||||||
grow_vertical = 2
|
|
||||||
script = ExtResource("1_pdgp8")
|
|
||||||
inventory_grid = NodePath("UI/Background/Items")
|
|
||||||
inventory_loot_grid = NodePath("UI/Background/ColorRect/LootItems")
|
|
||||||
inventory_background = NodePath("UI/Background")
|
|
||||||
inventory_equipment_slots = NodePath("UI/Background/Equipment")
|
|
||||||
|
|
||||||
[node name="UI" type="CanvasLayer" parent="."]
|
|
||||||
|
|
||||||
[node name="Background" type="ColorRect" parent="UI"]
|
|
||||||
anchors_preset = 8
|
|
||||||
anchor_left = 0.5
|
|
||||||
anchor_top = 0.5
|
|
||||||
anchor_right = 0.5
|
|
||||||
anchor_bottom = 0.5
|
|
||||||
offset_left = -473.5
|
|
||||||
offset_top = -462.0
|
|
||||||
offset_right = 473.5
|
|
||||||
offset_bottom = 9.0
|
|
||||||
grow_horizontal = 2
|
|
||||||
grow_vertical = 2
|
|
||||||
color = Color(0.121569, 0.121569, 0.121569, 1)
|
|
||||||
|
|
||||||
[node name="Equipment" type="ColorRect" parent="UI/Background"]
|
|
||||||
layout_mode = 1
|
|
||||||
anchors_preset = 11
|
|
||||||
anchor_left = 1.0
|
|
||||||
anchor_right = 1.0
|
|
||||||
anchor_bottom = 1.0
|
|
||||||
offset_left = -237.0
|
|
||||||
offset_top = 4.0
|
|
||||||
offset_right = -4.0
|
|
||||||
offset_bottom = -4.0
|
|
||||||
grow_horizontal = 0
|
|
||||||
grow_vertical = 2
|
|
||||||
color = Color(0.184314, 0.184314, 0.184314, 1)
|
|
||||||
script = ExtResource("2_7s2pc")
|
|
||||||
|
|
||||||
[node name="HEAD" type="ColorRect" parent="UI/Background/Equipment"]
|
|
||||||
layout_mode = 1
|
|
||||||
anchors_preset = 5
|
|
||||||
anchor_left = 0.5
|
|
||||||
anchor_right = 0.5
|
|
||||||
offset_left = -37.0
|
|
||||||
offset_top = 16.5
|
|
||||||
offset_right = 38.0
|
|
||||||
offset_bottom = 91.5
|
|
||||||
grow_horizontal = 2
|
|
||||||
color = Color(0.278431, 0.278431, 0.278431, 1)
|
|
||||||
|
|
||||||
[node name="BODY" type="ColorRect" parent="UI/Background/Equipment"]
|
|
||||||
layout_mode = 1
|
|
||||||
anchors_preset = 4
|
|
||||||
anchor_top = 0.5
|
|
||||||
anchor_bottom = 0.5
|
|
||||||
offset_left = 32.5
|
|
||||||
offset_top = -121.0
|
|
||||||
offset_right = 202.5
|
|
||||||
offset_bottom = 49.0
|
|
||||||
grow_vertical = 2
|
|
||||||
color = Color(0.278431, 0.278431, 0.278431, 1)
|
|
||||||
|
|
||||||
[node name="WEAPON" type="ColorRect" parent="UI/Background/Equipment"]
|
|
||||||
layout_mode = 1
|
|
||||||
anchors_preset = 4
|
|
||||||
anchor_top = 0.5
|
|
||||||
anchor_bottom = 0.5
|
|
||||||
offset_left = 7.5
|
|
||||||
offset_top = 91.0
|
|
||||||
offset_right = 227.5
|
|
||||||
offset_bottom = 194.0
|
|
||||||
grow_vertical = 2
|
|
||||||
color = Color(0.278431, 0.278431, 0.278431, 1)
|
|
||||||
|
|
||||||
[node name="ColorRect" type="ColorRect" parent="UI/Background"]
|
|
||||||
layout_mode = 0
|
|
||||||
offset_left = -3.0
|
|
||||||
offset_top = 470.0
|
|
||||||
offset_right = 711.0
|
|
||||||
offset_bottom = 717.0
|
|
||||||
color = Color(0.105882, 0.105882, 0.105882, 0.905882)
|
|
||||||
|
|
||||||
[node name="LootItems" type="ColorRect" parent="UI/Background/ColorRect"]
|
|
||||||
layout_mode = 1
|
|
||||||
anchors_preset = 9
|
|
||||||
anchor_bottom = 1.0
|
|
||||||
offset_left = 15.0
|
|
||||||
offset_top = 8.0
|
|
||||||
offset_right = 704.0
|
|
||||||
offset_bottom = -14.0
|
|
||||||
grow_vertical = 2
|
|
||||||
color = Color(0.164706, 0.164706, 0.164706, 1)
|
|
||||||
script = ExtResource("3_05dq4")
|
|
||||||
inventory_item_grid_width = 21
|
|
||||||
inventory_item_grid_height = 7
|
|
||||||
inventory_grid_default_color = Color(1, 1, 1, 1)
|
|
||||||
inventory_grid_used_color = Color(0, 1, 0, 1)
|
|
||||||
|
|
||||||
[node name="Items" type="ColorRect" parent="UI/Background"]
|
|
||||||
layout_mode = 1
|
|
||||||
anchors_preset = 9
|
|
||||||
anchor_bottom = 1.0
|
|
||||||
offset_left = 12.0
|
|
||||||
offset_top = 8.0
|
|
||||||
offset_right = 701.0
|
|
||||||
offset_bottom = -8.0
|
|
||||||
grow_vertical = 2
|
|
||||||
color = Color(0.164706, 0.164706, 0.164706, 1)
|
|
||||||
script = ExtResource("3_05dq4")
|
|
||||||
inventory_item_grid_width = 21
|
|
||||||
inventory_item_grid_height = 14
|
|
||||||
inventory_grid_default_color = Color(1, 1, 1, 1)
|
|
||||||
inventory_grid_used_color = Color(0, 1, 0, 1)
|
|
||||||
|
|
||||||
[node name="DEBUG" parent="." node_paths=PackedStringArray("inventory") instance=ExtResource("4_w8gq7")]
|
|
||||||
inventory = NodePath("..")
|
|
@ -1,22 +0,0 @@
|
|||||||
[gd_scene load_steps=3 format=3 uid="uid://b1nobn0glk4ru"]
|
|
||||||
|
|
||||||
[ext_resource type="Script" path="res://grid_inventory_system/scripts/InventorySlot.gd" id="1_wp4po"]
|
|
||||||
[ext_resource type="Texture2D" uid="uid://bdvx58rwnaeev" path="res://icon.svg" id="2_1w5re"]
|
|
||||||
|
|
||||||
[node name="InventorySlot" type="Control"]
|
|
||||||
layout_mode = 3
|
|
||||||
anchors_preset = 15
|
|
||||||
anchor_right = 1.0
|
|
||||||
anchor_bottom = 1.0
|
|
||||||
offset_right = -1888.0
|
|
||||||
offset_bottom = -1048.0
|
|
||||||
grow_horizontal = 2
|
|
||||||
grow_vertical = 2
|
|
||||||
script = ExtResource("1_wp4po")
|
|
||||||
|
|
||||||
[node name="TextureRect" type="TextureRect" parent="."]
|
|
||||||
layout_mode = 1
|
|
||||||
offset_right = 10.0
|
|
||||||
offset_bottom = 10.0
|
|
||||||
texture = ExtResource("2_1w5re")
|
|
||||||
expand_mode = 1
|
|
@ -1,25 +0,0 @@
|
|||||||
[gd_scene load_steps=3 format=3 uid="uid://dtir5hovd6i3h"]
|
|
||||||
|
|
||||||
[ext_resource type="Script" path="res://grid_inventory_system/scripts/Item.gd" id="1_cs6rn"]
|
|
||||||
[ext_resource type="Texture2D" uid="uid://bdvx58rwnaeev" path="res://icon.svg" id="2_uetec"]
|
|
||||||
|
|
||||||
[node name="Item" type="Control" node_paths=PackedStringArray("graphic")]
|
|
||||||
clip_contents = true
|
|
||||||
layout_mode = 3
|
|
||||||
anchors_preset = 0
|
|
||||||
offset_right = 100.0
|
|
||||||
offset_bottom = 100.0
|
|
||||||
script = ExtResource("1_cs6rn")
|
|
||||||
graphic = NodePath("Graphic")
|
|
||||||
|
|
||||||
[node name="Graphic" type="TextureRect" parent="."]
|
|
||||||
unique_name_in_owner = true
|
|
||||||
clip_contents = true
|
|
||||||
layout_mode = 0
|
|
||||||
offset_right = 40.0
|
|
||||||
offset_bottom = 40.0
|
|
||||||
pivot_offset = Vector2(20, 20)
|
|
||||||
texture = ExtResource("2_uetec")
|
|
||||||
expand_mode = 1
|
|
||||||
|
|
||||||
[node name="Node" type="Node" parent="."]
|
|
@ -1,19 +0,0 @@
|
|||||||
extends Panel
|
|
||||||
class_name ContextMenu
|
|
||||||
|
|
||||||
@export var use_button:Button
|
|
||||||
@export var open_button:Button
|
|
||||||
@export var drop_button:Button
|
|
||||||
@export var destroy_button:Button
|
|
||||||
|
|
||||||
func set_use(disabled):
|
|
||||||
use_button.disabled=disabled
|
|
||||||
|
|
||||||
func set_open(disabled):
|
|
||||||
open_button.disabled=disabled
|
|
||||||
|
|
||||||
func set_drop(disabled):
|
|
||||||
drop_button.disabled=disabled
|
|
||||||
|
|
||||||
func set_destroy(disabled):
|
|
||||||
destroy_button.disabled=disabled
|
|
@ -1,173 +0,0 @@
|
|||||||
extends Control
|
|
||||||
class_name InventoryController
|
|
||||||
const ITEM_BASE = preload("res://grid_inventory_system/scenes/Item.tscn")
|
|
||||||
const CONTEXT_MENU = preload("res://grid_inventory_system/scenes/ContextMenu.tscn")
|
|
||||||
@export var inventory_open_input:String = "inventory_open"
|
|
||||||
@export var inventory_close_input:String = "inventory_close"
|
|
||||||
@export var inventory_use_input:String = "inventory_use"
|
|
||||||
@export var inventory_grab_input:String = "inventory_grab"
|
|
||||||
@export var inventory_rotate_input:String = "inventory_rotate"
|
|
||||||
@export var inventory_grid:Control
|
|
||||||
@export var inventory_loot_grid:Control
|
|
||||||
@export var inventory_background:Control
|
|
||||||
@export var inventory_equipment_slots:Control
|
|
||||||
|
|
||||||
|
|
||||||
var inventory_open = true
|
|
||||||
var inventory_item_dragged = null
|
|
||||||
var inventory_item_cursor_offset = Vector2()
|
|
||||||
var inventory_item_dragged_last_container = null
|
|
||||||
var inventory_item_dragged_last_pos = Vector2()
|
|
||||||
var inventory_context_menu:Control = null
|
|
||||||
var inventory_context_menu_item
|
|
||||||
|
|
||||||
|
|
||||||
signal inventory_item_equipped(item_config,slot)
|
|
||||||
signal inventory_item_unequipped(item_config,slot)
|
|
||||||
signal inventory_item_dropped(item_config)
|
|
||||||
signal inventory_item_grabbed(item_config, x, y, container)
|
|
||||||
signal inventory_item_released(item_config, x, y, container)
|
|
||||||
signal inventory_item_used(item_config, x, y)
|
|
||||||
signal inventory_item_destroyed(item_config, x, y)
|
|
||||||
signal inventory_item_opened(item_config, x, y)
|
|
||||||
|
|
||||||
func _ready():
|
|
||||||
if(inventory_grid==null):
|
|
||||||
printerr("The inventory grid was not assigned to the inventory controller!")
|
|
||||||
if(inventory_background==null):
|
|
||||||
printerr("The inventory background was not assigned to the inventory controller!")
|
|
||||||
if(inventory_grid==null):
|
|
||||||
printerr("The inventory equipment slots was not assigned to the inventory controller!")
|
|
||||||
|
|
||||||
func _process(delta):
|
|
||||||
if(Input.is_action_just_pressed(inventory_open_input) and inventory_open==false):
|
|
||||||
inventory_open = true
|
|
||||||
inventory_background.visible=true
|
|
||||||
|
|
||||||
if(Input.is_action_just_pressed(inventory_close_input) and inventory_open==true):
|
|
||||||
inventory_open = false
|
|
||||||
inventory_background.visible=false
|
|
||||||
|
|
||||||
if(Input.is_action_just_pressed(inventory_rotate_input) and inventory_open==true and inventory_item_dragged != null):
|
|
||||||
inventory_item_dragged.rotate_item(90)
|
|
||||||
|
|
||||||
var cursor_pos = get_global_mouse_position()
|
|
||||||
|
|
||||||
if Input.is_action_just_pressed("inventory_grab"):
|
|
||||||
grab(cursor_pos)
|
|
||||||
|
|
||||||
if Input.is_action_just_released("inventory_grab"):
|
|
||||||
release(cursor_pos)
|
|
||||||
|
|
||||||
if Input.is_action_just_pressed("inventory_use"):
|
|
||||||
try_to_open_context_menu()
|
|
||||||
|
|
||||||
|
|
||||||
if inventory_item_dragged != null:
|
|
||||||
inventory_item_dragged.global_position = cursor_pos + inventory_item_cursor_offset
|
|
||||||
|
|
||||||
func try_to_open_context_menu():
|
|
||||||
var mouse_position = get_global_mouse_position()
|
|
||||||
var c = _get_container_mouse_over()
|
|
||||||
if c != null and c.has_method("grab_item"):
|
|
||||||
var item = c.check_item(mouse_position)
|
|
||||||
if item != null:
|
|
||||||
if(inventory_context_menu!=null):
|
|
||||||
inventory_context_menu.queue_free()
|
|
||||||
show_context_menu(mouse_position,item)
|
|
||||||
else:
|
|
||||||
hide_context_menu()
|
|
||||||
|
|
||||||
|
|
||||||
# Function to show the context menu at a specific position
|
|
||||||
func show_context_menu(mouse_pos: Vector2, item):
|
|
||||||
inventory_context_menu = CONTEXT_MENU.instantiate()
|
|
||||||
inventory_background.add_child(inventory_context_menu)
|
|
||||||
inventory_context_menu.global_position = mouse_pos + Vector2(-20,-15)
|
|
||||||
inventory_context_menu.set_use(not item.item_config.item_usable)
|
|
||||||
inventory_context_menu.set_drop(not item.item_config.item_droppable)
|
|
||||||
inventory_context_menu.set_destroy(not item.item_config.item_destroyable)
|
|
||||||
inventory_context_menu.set_open(not item.item_config.item_openable)
|
|
||||||
inventory_context_menu_item = item
|
|
||||||
# Function to hide the context menu
|
|
||||||
func hide_context_menu():
|
|
||||||
if(inventory_context_menu!=null):
|
|
||||||
inventory_context_menu_item=null
|
|
||||||
inventory_context_menu.queue_free()
|
|
||||||
|
|
||||||
func context_menu_open():
|
|
||||||
print("OPEN!")
|
|
||||||
|
|
||||||
func context_menu_use():
|
|
||||||
print("USED!")
|
|
||||||
|
|
||||||
func context_menu_destroy():
|
|
||||||
print("DESTROYED!")
|
|
||||||
|
|
||||||
func context_menu_drop():
|
|
||||||
print("DROPPED!")
|
|
||||||
|
|
||||||
func grab(cursor_pos):
|
|
||||||
var c = _get_container_mouse_over()
|
|
||||||
if c != null and c.has_method("grab_item"):
|
|
||||||
inventory_item_dragged = c.grab_item(cursor_pos)
|
|
||||||
if inventory_item_dragged != null:
|
|
||||||
add_child(inventory_item_dragged)
|
|
||||||
inventory_item_dragged_last_container = c
|
|
||||||
inventory_item_dragged_last_pos = inventory_item_dragged.global_position
|
|
||||||
inventory_item_cursor_offset = inventory_item_dragged.global_position - cursor_pos
|
|
||||||
if(inventory_context_menu_item==inventory_item_dragged):
|
|
||||||
release(cursor_pos)
|
|
||||||
|
|
||||||
func release(cursor_pos):
|
|
||||||
if inventory_item_dragged == null:
|
|
||||||
return
|
|
||||||
var c = _get_container_mouse_over()
|
|
||||||
if c == null:
|
|
||||||
drop_item()
|
|
||||||
elif c.has_method("insert_item"):
|
|
||||||
if c.insert_item(inventory_item_dragged):
|
|
||||||
inventory_item_dragged = null
|
|
||||||
else:
|
|
||||||
return_item()
|
|
||||||
else:
|
|
||||||
return_item()
|
|
||||||
|
|
||||||
func use(cursor_pos):
|
|
||||||
pass
|
|
||||||
|
|
||||||
func pickup_item(item_id):
|
|
||||||
var item = ITEM_BASE.instantiate()
|
|
||||||
item.set_meta("id", item_id)
|
|
||||||
item.init_item(item_id)
|
|
||||||
if not inventory_grid.insert_item_at_first_available_spot(item):
|
|
||||||
item.queue_free()
|
|
||||||
return false
|
|
||||||
return true
|
|
||||||
|
|
||||||
func drop_item():
|
|
||||||
inventory_item_dropped.emit(inventory_item_dragged.item_config)
|
|
||||||
inventory_item_dragged.queue_free()
|
|
||||||
inventory_item_dragged = null
|
|
||||||
|
|
||||||
# Function to return the dragged item to its original position or container
|
|
||||||
func return_item():
|
|
||||||
inventory_item_dragged.global_position = inventory_item_dragged_last_pos
|
|
||||||
inventory_item_dragged_last_container.insert_item(inventory_item_dragged)
|
|
||||||
inventory_item_dragged = null
|
|
||||||
|
|
||||||
func _get_container_mouse_over():
|
|
||||||
|
|
||||||
if(_is_mouse_ontop_of_control(inventory_grid)==true):
|
|
||||||
return inventory_grid
|
|
||||||
elif(_is_mouse_ontop_of_control(inventory_loot_grid)==true):
|
|
||||||
return inventory_loot_grid
|
|
||||||
elif(_is_mouse_ontop_of_control(inventory_equipment_slots)==true):
|
|
||||||
return inventory_equipment_slots
|
|
||||||
elif(_is_mouse_ontop_of_control(inventory_background)==true):
|
|
||||||
return inventory_background
|
|
||||||
return null
|
|
||||||
|
|
||||||
func _is_mouse_ontop_of_control(c):
|
|
||||||
var cursor_pos = get_global_mouse_position()
|
|
||||||
return c.get_global_rect().has_point(cursor_pos)
|
|
@ -1,149 +0,0 @@
|
|||||||
extends ColorRect
|
|
||||||
class_name InventoryItemGrid
|
|
||||||
|
|
||||||
# Arrays to store inventory items and the grid representation
|
|
||||||
var inventory_item_grid_items = []
|
|
||||||
|
|
||||||
# 2D dictionary to represent the inventory grid, where each cell has information about usage and a corresponding ColorRect node
|
|
||||||
var inventory_item_grid = {}
|
|
||||||
var inventory_item_grid_cell_size = 32
|
|
||||||
@export var inventory_item_grid_width = 0
|
|
||||||
@export var inventory_item_grid_height = 0
|
|
||||||
@export var inventory_grid_default_color: Color
|
|
||||||
@export var inventory_grid_used_color: Color
|
|
||||||
|
|
||||||
# Signals for item-related events
|
|
||||||
signal weapon_equipped(weapon_id, item)
|
|
||||||
signal weapon_unequipped(weapon_id, item)
|
|
||||||
signal item_used(item_id)
|
|
||||||
|
|
||||||
func _ready():
|
|
||||||
# Initialize the inventory grid
|
|
||||||
for x in range(inventory_item_grid_width):
|
|
||||||
inventory_item_grid[x] = {}
|
|
||||||
for y in range(inventory_item_grid_height):
|
|
||||||
inventory_item_grid[x][y] = {}
|
|
||||||
inventory_item_grid[x][y]["used"] = false
|
|
||||||
|
|
||||||
# Create ColorRect nodes for each grid cell and set their default colors
|
|
||||||
for y in range(inventory_item_grid_height):
|
|
||||||
for x in range(inventory_item_grid_width):
|
|
||||||
var color_rect = ColorRect.new()
|
|
||||||
color_rect.global_position = Vector2(x * inventory_item_grid_cell_size, y * inventory_item_grid_cell_size)
|
|
||||||
color_rect.size = Vector2(inventory_item_grid_cell_size - 2, inventory_item_grid_cell_size - 2)
|
|
||||||
add_child(color_rect)
|
|
||||||
color_rect.color = inventory_grid_default_color
|
|
||||||
inventory_item_grid[x][y]["rect"] = color_rect
|
|
||||||
|
|
||||||
# Function to insert an item into the inventory grid
|
|
||||||
func insert_item(item):
|
|
||||||
var item_pos = item.global_position-global_position
|
|
||||||
var g_pos = pos_to_grid_coord(item_pos)
|
|
||||||
var item_size = get_grid_size(item)
|
|
||||||
|
|
||||||
# Check if there is enough space to insert the item
|
|
||||||
if is_grid_space_available(g_pos.x, g_pos.y, item_size.x, item_size.y)==true:
|
|
||||||
set_grid_space(g_pos.x, g_pos.y, item_size.x, item_size.y, true)
|
|
||||||
item.position = Vector2(g_pos.x, g_pos.y) * inventory_item_grid_cell_size
|
|
||||||
var parent = item.get_parent()
|
|
||||||
if(parent!=null):
|
|
||||||
parent.remove_child(item)
|
|
||||||
add_child(item)
|
|
||||||
inventory_item_grid_items.append(item)
|
|
||||||
return true
|
|
||||||
else:
|
|
||||||
return false
|
|
||||||
|
|
||||||
# Function to grab an item from the inventory grid based on a position
|
|
||||||
func grab_item(pos):
|
|
||||||
var item = get_item_under_pos(pos)
|
|
||||||
if item == null:
|
|
||||||
return null
|
|
||||||
|
|
||||||
var item_pos = item.position
|
|
||||||
var g_pos = pos_to_grid_coord(item_pos)
|
|
||||||
var item_size = get_grid_size(item)
|
|
||||||
set_grid_space(g_pos.x, g_pos.y, item_size.x, item_size.y, false)
|
|
||||||
|
|
||||||
inventory_item_grid_items.remove_at(inventory_item_grid_items.find(item))
|
|
||||||
return item
|
|
||||||
|
|
||||||
func check_item(pos):
|
|
||||||
var item = get_item_under_pos(pos)
|
|
||||||
if item == null:
|
|
||||||
return null
|
|
||||||
return item
|
|
||||||
|
|
||||||
# Function to use an item from the inventory grid based on a position
|
|
||||||
func use_item(pos):
|
|
||||||
var item = get_item_under_pos(pos)
|
|
||||||
if item == null:
|
|
||||||
return null
|
|
||||||
|
|
||||||
# Emit signal if the item is usable
|
|
||||||
if item.item_config.item_usable == true:
|
|
||||||
item_used.emit(item.item_config.item_id)
|
|
||||||
item.queue_free()
|
|
||||||
|
|
||||||
var item_size = get_grid_size(item)
|
|
||||||
var item_pos = item.global_position-global_position
|
|
||||||
var g_pos = pos_to_grid_coord(item_pos)
|
|
||||||
inventory_item_grid_items.remove_at(inventory_item_grid_items.find(item))
|
|
||||||
set_grid_space(g_pos.x, g_pos.y, item_size.x, item_size.y, false)
|
|
||||||
|
|
||||||
# Function to convert global position to grid coordinates
|
|
||||||
func pos_to_grid_coord(pos):
|
|
||||||
var results = {}
|
|
||||||
results.x = int(pos.x / inventory_item_grid_cell_size)
|
|
||||||
results.y = int(pos.y / inventory_item_grid_cell_size)
|
|
||||||
return results
|
|
||||||
|
|
||||||
# Function to get the grid size of an item in terms of grid cells
|
|
||||||
func get_grid_size(item):
|
|
||||||
var results = {}
|
|
||||||
var s = item.size
|
|
||||||
results.x = int(clamp(s.x / inventory_item_grid_cell_size, 1.0, 500.0))
|
|
||||||
results.y = int(clamp(s.y / inventory_item_grid_cell_size, 1.0, 500.0))
|
|
||||||
return results
|
|
||||||
|
|
||||||
# Function to check if there is enough space in the grid to place an item
|
|
||||||
func is_grid_space_available(x, y, w, h):
|
|
||||||
if x < 0 or y < 0:
|
|
||||||
return false
|
|
||||||
if x + w > inventory_item_grid_width or y + h > inventory_item_grid_height:
|
|
||||||
return false
|
|
||||||
for i in range(x, x + w):
|
|
||||||
for j in range(y, y + h):
|
|
||||||
if inventory_item_grid[i][j]["used"] == true:
|
|
||||||
return false
|
|
||||||
return true
|
|
||||||
|
|
||||||
# Function to set the usage state of grid cells and update ColorRect colors accordingly
|
|
||||||
func set_grid_space(x, y, w, h, state):
|
|
||||||
for i in range(x, x + w):
|
|
||||||
for j in range(y, y + h):
|
|
||||||
if state == true:
|
|
||||||
inventory_item_grid[i][j]["used"] = true
|
|
||||||
inventory_item_grid[i][j]["rect"].color = inventory_grid_used_color
|
|
||||||
else:
|
|
||||||
inventory_item_grid[i][j]["used"] = false
|
|
||||||
inventory_item_grid[i][j]["rect"].color = inventory_grid_default_color
|
|
||||||
|
|
||||||
# Function to get the item under a given position
|
|
||||||
func get_item_under_pos(pos):
|
|
||||||
for item in inventory_item_grid_items:
|
|
||||||
if item.get_global_rect().has_point(pos):
|
|
||||||
return item
|
|
||||||
return null
|
|
||||||
# Function to insert an item at the first available spot in the grid
|
|
||||||
# Function to insert an item at the first available spot in the grid
|
|
||||||
func insert_item_at_first_available_spot(item):
|
|
||||||
for x in range(inventory_item_grid_width):
|
|
||||||
for y in range(inventory_item_grid_height):
|
|
||||||
if inventory_item_grid[x][y]["used"] == false:
|
|
||||||
item.global_position = global_position+ Vector2(x * inventory_item_grid_cell_size, y * inventory_item_grid_cell_size)
|
|
||||||
if insert_item(item) == true:
|
|
||||||
print("Item inserted at:", item.position)
|
|
||||||
return true
|
|
||||||
return false
|
|
||||||
|
|
@ -1,11 +0,0 @@
|
|||||||
extends Control
|
|
||||||
|
|
||||||
enum slot_state { free, used }
|
|
||||||
|
|
||||||
@export var in_use = false
|
|
||||||
|
|
||||||
func used():
|
|
||||||
in_use = true
|
|
||||||
|
|
||||||
func free():
|
|
||||||
in_use = false
|
|
@ -1,25 +0,0 @@
|
|||||||
extends Control
|
|
||||||
class_name ItemGraphic
|
|
||||||
@export var item_config:ItemConfiguration
|
|
||||||
@export var graphic:TextureRect
|
|
||||||
|
|
||||||
func init_item(item_id):
|
|
||||||
var dbItem = ItemDb.get_item(item_id)
|
|
||||||
item_config = dbItem
|
|
||||||
graphic.set_size(Vector2(dbItem.item_size.x, dbItem.item_size.y))
|
|
||||||
set_size(Vector2(dbItem.item_size.x, dbItem.item_size.y))
|
|
||||||
graphic.texture = dbItem.item_texture
|
|
||||||
graphic.pivot_offset = Vector2(graphic.size.x/2.0, graphic.size.y/2.0)
|
|
||||||
pivot_offset = Vector2(graphic.size.x/2.0, graphic.size.y/2.0)
|
|
||||||
graphic.position = Vector2()
|
|
||||||
print("TEST")
|
|
||||||
|
|
||||||
func rotate_item(amount):
|
|
||||||
graphic.rotation_degrees = graphic.rotation_degrees+amount
|
|
||||||
if(graphic.rotation_degrees==90 or graphic.rotation_degrees==270):
|
|
||||||
set_size(Vector2(item_config.item_size.y, item_config.item_size.x))
|
|
||||||
else:
|
|
||||||
set_size(Vector2(item_config.item_size.x, item_config.item_size.y))
|
|
||||||
|
|
||||||
if(graphic.rotation_degrees==360):
|
|
||||||
graphic.rotation_degrees=0
|
|
@ -1,15 +0,0 @@
|
|||||||
extends Resource
|
|
||||||
class_name ItemConfiguration
|
|
||||||
@export var item_id:String = "item"
|
|
||||||
@export var item_name:String = "Item Name"
|
|
||||||
@export var item_description:String = "This is the default item description."
|
|
||||||
@export var item_size:Vector2 = Vector2(3,3)
|
|
||||||
|
|
||||||
@export var item_usable:bool = false
|
|
||||||
@export var item_openable:bool = false
|
|
||||||
@export var item_droppable:bool = false
|
|
||||||
@export var item_destroyable:bool = false
|
|
||||||
|
|
||||||
@export var item_equipment:bool = false
|
|
||||||
@export var item_equipment_slot:String = "NONE"
|
|
||||||
@export var item_texture:Texture2D
|
|
@ -1,19 +0,0 @@
|
|||||||
extends Node
|
|
||||||
|
|
||||||
# Dictionary to store loaded items with their item_id as the key
|
|
||||||
var ITEMS = {}
|
|
||||||
|
|
||||||
func _ready():
|
|
||||||
# Load items from the "items" directory and populate the ITEMS dictionary
|
|
||||||
for i in DirAccess.get_files_at("res://grid_inventory_system/items"):
|
|
||||||
if(i.ends_with(".tres")):
|
|
||||||
var item = load("res://grid_inventory_system/items/" + i)
|
|
||||||
ITEMS[item.item_id] = item
|
|
||||||
|
|
||||||
# Function to retrieve an item based on its item_id
|
|
||||||
func get_item(item_id):
|
|
||||||
# Check if the item_id exists in the ITEMS dictionary
|
|
||||||
if ITEMS.has(item_id):
|
|
||||||
return ITEMS[item_id]
|
|
||||||
else:
|
|
||||||
return null
|
|
@ -1,72 +0,0 @@
|
|||||||
extends ColorRect
|
|
||||||
class_name ItemSlot
|
|
||||||
|
|
||||||
# Reference to child slots
|
|
||||||
@onready var slots = get_children()
|
|
||||||
|
|
||||||
# Dictionary to store items associated with their respective slots
|
|
||||||
var items = {}
|
|
||||||
|
|
||||||
# Signals for item-related events
|
|
||||||
signal weapon_equipped(weapon_id, item)
|
|
||||||
signal weapon_unequipped(weapon_id, item)
|
|
||||||
|
|
||||||
func _ready():
|
|
||||||
# Initialize the items dictionary with slots
|
|
||||||
for slot in slots:
|
|
||||||
items[slot.name] = null
|
|
||||||
|
|
||||||
# Function to insert an item into the equipment slots
|
|
||||||
func insert_item(item):
|
|
||||||
# Calculate the center position of the item
|
|
||||||
var item_pos = item.global_position + item.size / 2
|
|
||||||
# Get the slot under the item's position
|
|
||||||
var slot = get_slot_under_pos(item_pos)
|
|
||||||
|
|
||||||
# Check if the item can be inserted into the slot
|
|
||||||
if slot == null:
|
|
||||||
return false
|
|
||||||
|
|
||||||
# Retrieve item slot information from the ItemDb
|
|
||||||
var item_slot = ItemDb.get_item(item.get_meta("id")).item_equipment_slot
|
|
||||||
# Check if the item's slot matches the target slot
|
|
||||||
if item_slot != slot.name:
|
|
||||||
return false
|
|
||||||
# Check if the slot is already occupied
|
|
||||||
if items[item_slot] != null:
|
|
||||||
return false
|
|
||||||
|
|
||||||
# Place the item in the slot and emit the weapon_equipped signal
|
|
||||||
items[item_slot] = item
|
|
||||||
item.global_position = slot.global_position + slot.size / 2 - item.size / 2
|
|
||||||
weapon_equipped.emit(item.item_config.item_id, item.item_config)
|
|
||||||
return true
|
|
||||||
|
|
||||||
# Function to grab an item from the equipment slots
|
|
||||||
func grab_item(pos):
|
|
||||||
# Get the item under the specified position
|
|
||||||
var item = get_item_under_pos(pos)
|
|
||||||
if item == null:
|
|
||||||
return null
|
|
||||||
|
|
||||||
# Retrieve item slot information from the ItemDb
|
|
||||||
var item_slot = ItemDb.get_item(item.get_meta("id")).item_equipment_slot
|
|
||||||
# Remove the item from the slot and emit the weapon_unequipped signal
|
|
||||||
items[item_slot] = null
|
|
||||||
weapon_unequipped.emit(item.item_config.item_id, item.item_config)
|
|
||||||
return item
|
|
||||||
|
|
||||||
# Function to get the slot under a specified position
|
|
||||||
func get_slot_under_pos(pos):
|
|
||||||
return get_thing_under_pos(slots, pos)
|
|
||||||
|
|
||||||
# Function to get the item under a specified position
|
|
||||||
func get_item_under_pos(pos):
|
|
||||||
return get_thing_under_pos(items.values(), pos)
|
|
||||||
|
|
||||||
# Generic function to get the object (slot or item) under a specified position
|
|
||||||
func get_thing_under_pos(arr, pos):
|
|
||||||
for thing in arr:
|
|
||||||
if thing != null and thing.get_global_rect().has_point(pos):
|
|
||||||
return thing
|
|
||||||
return null
|
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
importer="texture"
|
importer="texture"
|
||||||
type="CompressedTexture2D"
|
type="CompressedTexture2D"
|
||||||
uid="uid://bdvx58rwnaeev"
|
uid="uid://bj3tjdbw5xdlj"
|
||||||
path="res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.ctex"
|
path="res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.ctex"
|
||||||
metadata={
|
metadata={
|
||||||
"vram_texture": false
|
"vram_texture": false
|
||||||
@ -15,7 +15,7 @@ dest_files=["res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.cte
|
|||||||
|
|
||||||
[params]
|
[params]
|
||||||
|
|
||||||
compress/mode=3
|
compress/mode=0
|
||||||
compress/high_quality=false
|
compress/high_quality=false
|
||||||
compress/lossy_quality=0.7
|
compress/lossy_quality=0.7
|
||||||
compress/hdr_compression=1
|
compress/hdr_compression=1
|
||||||
|
3
main.tscn
Normal file
3
main.tscn
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
[gd_scene format=3 uid="uid://cc8f5p1w5xdyx"]
|
||||||
|
|
||||||
|
[node name="Node" type="Node"]
|
@ -10,53 +10,14 @@ config_version=5
|
|||||||
|
|
||||||
[application]
|
[application]
|
||||||
|
|
||||||
config/name="Grid Inventory System"
|
config/name="Godot Grid Inventory"
|
||||||
run/main_scene="res://grid_inventory_system/scenes/Inventory.tscn"
|
config/features=PackedStringArray("4.2", "C#", "Forward Plus")
|
||||||
config/features=PackedStringArray("4.2", "Forward Plus")
|
|
||||||
config/icon="res://icon.svg"
|
config/icon="res://icon.svg"
|
||||||
|
|
||||||
[autoload]
|
[autoload]
|
||||||
|
|
||||||
ItemDb="*res://grid_inventory_system/scripts/ItemDB.gd"
|
ItemDatabase="*res://Code/ItemDatabase.cs"
|
||||||
|
|
||||||
[display]
|
[dotnet]
|
||||||
|
|
||||||
window/size/viewport_width=1920
|
project/assembly_name="Godot Grid Inventory"
|
||||||
window/size/viewport_height=1080
|
|
||||||
window/stretch/mode="canvas_items"
|
|
||||||
|
|
||||||
[editor]
|
|
||||||
|
|
||||||
export/convert_text_resources_to_binary=false
|
|
||||||
|
|
||||||
[input]
|
|
||||||
|
|
||||||
inventory_open={
|
|
||||||
"deadzone": 0.5,
|
|
||||||
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194306,"key_label":0,"unicode":0,"echo":false,"script":null)
|
|
||||||
]
|
|
||||||
}
|
|
||||||
inventory_close={
|
|
||||||
"deadzone": 0.5,
|
|
||||||
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194306,"key_label":0,"unicode":0,"echo":false,"script":null)
|
|
||||||
]
|
|
||||||
}
|
|
||||||
inventory_use={
|
|
||||||
"deadzone": 0.5,
|
|
||||||
"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":0,"position":Vector2(0, 0),"global_position":Vector2(0, 0),"factor":1.0,"button_index":2,"canceled":false,"pressed":false,"double_click":false,"script":null)
|
|
||||||
]
|
|
||||||
}
|
|
||||||
inventory_grab={
|
|
||||||
"deadzone": 0.5,
|
|
||||||
"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":0,"position":Vector2(0, 0),"global_position":Vector2(0, 0),"factor":1.0,"button_index":1,"canceled":false,"pressed":false,"double_click":false,"script":null)
|
|
||||||
]
|
|
||||||
}
|
|
||||||
inventory_rotate={
|
|
||||||
"deadzone": 0.5,
|
|
||||||
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":82,"key_label":0,"unicode":114,"echo":false,"script":null)
|
|
||||||
]
|
|
||||||
}
|
|
||||||
mouse={
|
|
||||||
"deadzone": 0.5,
|
|
||||||
"events": []
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user