-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInventory.cs
More file actions
138 lines (117 loc) · 3.88 KB
/
Copy pathInventory.cs
File metadata and controls
138 lines (117 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using Minecraft.Terrain.Block;
using System;
using System.Linq;
namespace Minecraft;
/// <summary>
/// Represents an inventory system with a toolbar of slots.
/// </summary>
public class Inventory
{
/// <summary>Gets the currently selected inventory slot.</summary>
public int SelectedSlotIndex { get; set; } = 1;
/// <summary>Gets or sets the selected slot.</summary>
public ToolbarSlot SelectedSlot { get; set; } = new(1);
// TODO: #90 After first version
// public List<InventoryItem> Items { get; set; } = new();
/// <summary>Gets or sets the toolbar slots.</summary>
public ToolbarSlot[] Toolbar { get; set; } = new ToolbarSlot[10];
// TODO: #90 Use later to optimize inventory logic
// private bool _toolbarSlotsAvailable;
/// <summary>
/// Gets the item within a given inventory slot.
/// </summary>
/// <param name="slotNumber">The index of the slot.</param>
/// <returns>The item within the slot with the index of <paramref name="slotNumber"/>.</returns>
public InventoryItem GetActiveSlotItems(int slotNumber)
=> Toolbar[slotNumber].Items;
/*public void AddItem(BlockId type)
{
var item = Items.FirstOrDefault(x => x.Type == type);
if (item is not null)
item.Amount += 1;
else
Items.Add(new InventoryItem { Type = type, Amount = 1 });
}*/
/// <summary>
/// Adds an item to the toolbar.
/// </summary>
/// <param name="BlockId">The type of the block to add to the toolbar.</param>
public void AddToolbarItem(BlockId BlockId)
{
var slot = Toolbar.FirstOrDefault(i => i.Items.Type == BlockId);
if (slot is not null)
{
slot.Items.Amount += 1;
if (slot.SlotNumber == SelectedSlotIndex)
SelectedSlot = slot;
return;
}
if (SelectedSlot.Items.Type == BlockId.Air)
{
SelectedSlot.Items = new InventoryItem { Type = BlockId, Amount = 1 };
Toolbar[SelectedSlotIndex] = SelectedSlot;
return;
}
for (int i = 0; i < Toolbar.Length; i++)
{
if (Toolbar[i].Items.Type == BlockId.Air)
{
Toolbar[i].Items = new InventoryItem { Type = BlockId, Amount = 1 };
return;
}
}
}
/// <summary>
/// Removes an item from the toolbar.
/// </summary>
public void RemoveToolbarItem() => throw new NotImplementedException();
/// <summary>Initializes the toolbar.</summary>
public void Initialize()
{
for (int i = 0; i < Toolbar.Length; i++)
Toolbar[i] = new(i);
}
/// <summary>
/// Sets the selected slot.
/// </summary>
/// <param name="slotNumber">The index of the slot.</param>
public void SetSelectedSlot(int slotNumber)
{
SelectedSlotIndex = slotNumber;
SelectedSlot = Toolbar[slotNumber];
}
}
/// <summary>
/// Represents a slot in te toolbar.
/// </summary>
public class ToolbarSlot
{
/// <summary>
/// Gets or sets the items within a slot.
/// </summary>
public InventoryItem Items { get; set; } = new();
/// <summary>
/// Gets or sets the number of the slot.
/// </summary>
public int SlotNumber { get; set; }
/// <summary>
/// Initializes a new instance of <see cref="ToolbarSlot" />.
/// </summary>
/// <param name="slotNumber">The index of the slot.</param>
public ToolbarSlot(int slotNumber)
{
SlotNumber = slotNumber;
}
}
/// <summary>
/// Represents an item.
/// </summary>
public class InventoryItem
{
/// <summary>
/// Gets or sets the type of the block in the inventory.
/// </summary>
public BlockId Type { get; set; } = BlockId.Air;
/// <summary>Gets or sets the amount of the block.</summary>
public int Amount { get; set; }
}