-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinventory.lua
More file actions
157 lines (145 loc) · 5.03 KB
/
inventory.lua
File metadata and controls
157 lines (145 loc) · 5.03 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
local items = require('items')
local utils = require('utils')
local inventory = {
grid = {},
cellSize = 50,
selectedCell = nil
}
inventory.init = function(self, cellSize)
self.cellSize = cellSize or self.cellSize
for y = 1, 10 do
inventory.grid[y] = {}
for x = 1, 10 do
inventory.grid[y][x] = {
item = nil,
quantity = 0,
}
end
end
end
inventory.draw = function(self)
love.graphics.setColor(1, 1, 1)
for y = 1, #self.grid do
for x = 1, #self.grid[y] do
-- Grid
love.graphics.rectangle('line', (x - 1) * self.cellSize, (y - 1) * self.cellSize, self.cellSize, self.cellSize)
-- Items
local cell = self.grid[y][x]
if cell.item then
local textOffset = 5
love.graphics.print(cell.item.name, (x - 1) * self.cellSize + textOffset, (y - 1) * self.cellSize + textOffset)
love.graphics.print(cell.quantity, (x - 1) * self.cellSize + textOffset, (y - 1) * self.cellSize + textOffset + 17)
end
end
end
if self.selectedCell and self.selectedCell.cell and self.selectedCell.cell.item then
local mx, my = love.mouse.getPosition()
love.graphics.rectangle('line', mx, my, self.cellSize, self.cellSize)
local textOffset = 5
love.graphics.print(self.selectedCell.cell.item.name, mx + textOffset, my + textOffset)
love.graphics.print(self.selectedCell.cell.quantity, mx + textOffset, my + textOffset + 17)
--[[
love.graphics.setColor(1, 1, 0)
love.graphics.rectangle('line', (self.selectedCell.x - 1) * self.cellSize, (self.selectedCell.y - 1) * self.cellSize, self.cellSize, self.cellSize)
]]
end
end
inventory.lookupForAvailableStack = function(self, item)
for y = 1, #self.grid do
for x = 1, #self.grid[y] do
local cell = self.grid[y][x]
if cell.item and cell.item.name == item.name then
local maxQuantity = items[item.name].max
if cell.quantity < maxQuantity then
local leftQuantity = maxQuantity - cell.quantity
return true, x, y, leftQuantity
end
end
end
end
return false
end
inventory.findNextEmptyCell = function(self)
for y = 1, #self.grid do
for x = 1, #self.grid[y] do
local cell = self.grid[y][x]
if cell.item == nil then
return x, y
end
end
end
end
inventory.addItemAtPosition = function(self, item, quantity, x, y)
local desiredCell = self.grid[y][x]
if desiredCell.item == nil then
desiredCell.item = item
desiredCell.quantity = quantity
else
if desiredCell.item.name == item.name then
local leftQuantity = item.max - desiredCell.quantity
if quantity <= leftQuantity then
desiredCell.quantity = desiredCell.quantity + quantity
else
desiredCell.quantity = desiredCell.quantity + leftQuantity
local newStackQuantity = quantity - leftQuantity
self:addItem(item, newStackQuantity)
end
end
end
end
inventory.addItem = function(self, item, quantity)
local stackExists, x, y, leftQuantity = self:lookupForAvailableStack(item)
if stackExists then
local cell = self.grid[y][x]
if quantity <= leftQuantity then
cell.quantity = cell.quantity + quantity
else
cell.quantity = cell.quantity + leftQuantity
local newStackQuantity = quantity - leftQuantity
self:addItem(item, newStackQuantity)
end
else
local x, y = self:findNextEmptyCell()
self.grid[y][x].item = item
if quantity > item.max then
self.grid[y][x].quantity = item.max
self:addItem(item, quantity - item.max)
else
self.grid[y][x].quantity = quantity
end
end
end
inventory.mousepressed = function(self, x, y, button)
local gx = math.floor(x / self.cellSize) + 1
local gy = math.floor(y / self.cellSize) + 1
if gy > #self.grid or gx > #self.grid[1] then
self.selectedCell = nil
return
end
if self.selectedCell then
print('Selected cell should not be existing')
end
self.selectedCell = {
cell = utils.deepcopy(self.grid[gy][gx]),
x = gx,
y = gy,
}
self.grid[gy][gx] = {
item = nil,
quantity = 0
}
end
inventory.mousereleased = function(self, x, y, button)
if not self.selectedCell then
print('Selected cell should be existing')
end
local gx = math.floor(x / self.cellSize) + 1
local gy = math.floor(y / self.cellSize) + 1
if gy > #self.grid or gx > #self.grid[1] then
self.selectedCell = nil
return
end
self:addItemAtPosition(self.selectedCell.cell.item, self.selectedCell.cell.quantity, gx, gy)
self.selectedCell = nil
end
return inventory