-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinventory.lua
139 lines (112 loc) · 3.82 KB
/
inventory.lua
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
--the inventory library
inventory_size = 9
inventory_height = 5
inventory = {}
inventory_selection = 1
--function new_inventory()
for i = 1,inventory_size*inventory_height do
inventory[i] = {}
--if i > table.getn(blocks) then
-- inventory[i] = {id = i-table.getn(blocks),name = blocks[i-table.getn(blocks)]["name"],image = blocks[i-table.getn(blocks)]["image"],count = 1}
--else
-- inventory[i] = {id = i,name = blocks[i]["name"],image = blocks[i]["image"],count = 1}
--end
end
--end
--new_inventory()
function inventory_add(item,slot)
--add the item
--for slot,table in pairs(inventory) do
if slot then
local table = inventory[slot]
if table["id"] == item and table["count"] < 64 then
-- print("HERE")
inventory[slot]["count"] = inventory[slot]["count"] + 1
return
end
end
if slot then
--for i = 1,table.getn(inventory) do
local table = inventory[slot]
if not table["id"] then
inventory[slot] = {id = item, name = blocks[item]["name"], image = blocks[item]["image"], count = 1}
return
end
--end
end
for i = 1,table.getn(inventory) do
local table = inventory[i]
if table["id"] == item and table["count"] < 64 then
-- print("HERE")
inventory[i]["count"] = inventory[i]["count"] + 1
return
end
end
--else create new slot
--for slot,table in pairs(inventory) do
for i = 1,table.getn(inventory) do
local table = inventory[i]
if not table["id"] then
inventory[i] = {id = item, name = blocks[item]["name"], image = blocks[item]["image"], count = 1}
return
end
end
end
function inventory_remove(slot,item)
if slot and inventory[slot]["count"] then
inventory[slot]["count"] = inventory[slot]["count"] - 1
if inventory[slot]["count"] <= 0 then
inventory[slot] = {}
end
end
--0for slot,table in pairs(inventory) do
-- if table["id"] == item then
-- print("HERE")
-- inventory[slot]["count"] = inventory[slot]["count"] + 1
-- break
-- end
--end
end
function load_inventory_textures()
inventory_slot = love.graphics.newImage("textures/inventory.png")
inventory_slot_selection = love.graphics.newImage("textures/inventory_selection.png")
inv_slot_width,inv_slot_height = inventory_slot:getDimensions( )
inventory_x = 250
inventory_y = love.graphics.getHeight( ) - (inv_slot_height/2)
end
--scroll through the inventory
function love.wheelmoved(x, y)
y = -y
inventory_selection = inventory_selection + y
if inventory_selection < 1 then
inventory_selection = inventory_selection + inventory_size
elseif inventory_selection > inventory_size then
inventory_selection = inventory_selection - inventory_size
end
end
--render hot bar
function render_inventory()
for i = 1,inventory_size do
love.graphics.draw(inventory_slot, inventory_x+(i*(inv_slot_width/2)), inventory_y,0, 1/2, 1/2)
end
--draw selection
love.graphics.draw(inventory_slot_selection, inventory_x+(inventory_selection*(inv_slot_width/2)), inventory_y,0, 1/2, 1/2)
for i = 1,inventory_size do
--print(inventory[i]["id"])
--texture_table[loaded_chunks[xx][yy][x][y]["block"]]
--love.graphics.draw(texture_table[inventory[i]["id"]], drawx,drawy,0, scale/16, scale/16)
if inventory[i]["id"] then
love.graphics.draw(texture_table[inventory[i]["id"]], inventory_x+(i*(inv_slot_width/2))+5, inventory_y+4,0, 1*2.1, 1*2.1)
love.graphics.print( inventory[i]["count"], inventory_x+(i*(inv_slot_width/2))+22, inventory_y+28, 0, 1, 1)
end
end
end
--throw item
function throw_item()
--print(inventory[inventory_selection]["id"])
if inventory[inventory_selection]["id"] ~= nil then
--do it in this order to avoid nil
entity.create_entity("item",0.4,0.4,nil,chunkx,chunky,player.playerx,player.playery+0.5,math.random(-300,300)/1000,math.random(-150,-240)/1000,inventory[inventory_selection]["id"])
inventory_remove(inventory_selection,inventory[inventory_selection]["id"])
end
end