-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInventory.lua
More file actions
116 lines (96 loc) · 3.45 KB
/
Inventory.lua
File metadata and controls
116 lines (96 loc) · 3.45 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
require 'Toolkit'
local function generateEmptyPartsInv()
local partsInv = {}
for _,partType in ipairs(Toolkit.PartTypes) do
partsInv[partType] = {}
end
return partsInv
end
Inventory = { tools = {},
parts = generateEmptyPartsInv() }
local function wrap(index, maxIndex)
if index < 1 then
index = maxIndex
elseif index > maxIndex then
index = 1
end
return index
end
function Inventory:addPart(part)
table.insert(Inventory.parts[part.partType],part)
end
function Inventory:addPartSet(parts)
for _,part in ipairs(parts) do
Inventory:addPart(part)
end
end
function Inventory:addTool(tool)
table.insert(Inventory.tools,tool)
Inventory.currentToolIndex = #Inventory.tools
for _,part in ipairs(tool.parts) do
Inventory:addPart(part)
end
end
function Inventory:getCurrentTool()
return Inventory.tools[Inventory.currentToolIndex]
end
function Inventory:setCurrentTool(tool)
Inventory.tools[Inventory.currentToolIndex] = tool
end
function Inventory:nextTool()
Inventory.currentToolIndex = Inventory.currentToolIndex and wrap(Inventory.currentToolIndex + 1,#Inventory.tools) or 0
return Inventory:getCurrentTool()
end
function Inventory:previousTool()
Inventory.currentToolIndex = Inventory.currentToolIndex and wrap(Inventory.currentToolIndex - 1,#Inventory.tools) or 0
return Inventory:getCurrentTool()
end
function Inventory:throwCurrentTool()
local thrownTool = table.remove(Inventory.tools,Inventory.currentToolIndex)
Inventory.currentToolIndex = Inventory.currentToolIndex - 1
Inventory:nextTool()
return thrownTool
end
function Inventory:discardCurrentTool()
print("Inventory:discardCurrentTool")
table.remove(Inventory.tools,Inventory.currentToolIndex)
Inventory.currentToolIndex = Inventory.currentToolIndex - 1
Inventory:nextTool()
end
function Inventory:draw()
if State.inventoryView then
-- return
end
-- means we dont have any tools at all.
if not Inventory:getCurrentTool() then
love.graphics.setColor(50,0,0,155)
love.graphics.print("ERROR::NO_TOOL",5+Properties.DropShadow,50+Properties.DropShadow)
love.graphics.setColor(255,10,10,255)
love.graphics.print("ERROR::NO_TOOL",5,50)
return
end
love.graphics.setColor(0,0,0,155)
love.graphics.print(tostring(Inventory.currentToolIndex).."::"..Inventory:getCurrentTool().Name,5+Properties.DropShadow,50+Properties.DropShadow)
love.graphics.setColor(255,255,255,255)
love.graphics.print(tostring(Inventory.currentToolIndex).."::"..Inventory:getCurrentTool().Name,5,50)
for toolNr = 2, math.min(#Inventory.tools,7) do
local toolIndex = Inventory.currentToolIndex
local up = false
if toolNr % 2 == 0 then
up = false
toolNr = toolNr / 2
toolIndex = toolIndex + toolNr
else
up = true
toolNr = math.floor(toolNr/2)
toolIndex = toolIndex - toolNr
end
toolIndex = wrap(toolIndex, #Inventory.tools)
local x = 5 + (toolNr*toolNr)
local y = 50 + (up and 15 * toolNr or (-15 * toolNr))
love.graphics.setColor(0,0,0,255/toolNr)
love.graphics.print(tostring(toolIndex).."::"..Inventory.tools[toolIndex].Name,x+Properties.DropShadow*toolNr,y+Properties.DropShadow*toolNr)
love.graphics.setColor(255,255,255,255/toolNr)
love.graphics.print(tostring(toolIndex).."::"..Inventory.tools[toolIndex].Name,x,y)
end
end