-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcrafting.lua
321 lines (274 loc) · 10.2 KB
/
crafting.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
--the crafting library
crafting = {}
crafting_open = false
crafting.craft_size = 3 --number x number
crafting_x = 120
crafting_y = 350
craft_inv_x = 120
craft_inv_y = 50
craft_output_x = 380
craft_output_y = 134
crafting_selection_x = 1
crafting_selection_y = 1
craft_inventory_selection_x = 0
craft_inventory_selection_y = 0
craft_output_selection_x = 0
craft_output_selection_y = 0
crafting.craft_inventory = {}
crafting.held_item = {}
--create output inventory
crafting.output_inventory = {}
crafting.output_inventory[1] = {}
--create crafting inventory
for i = 1,crafting.craft_size*crafting.craft_size do
crafting.craft_inventory[i] = {}
end
--draw entire inventory
function crafting.render_crafting()
if crafting_open == true then
--draw output table
love.graphics.draw(inventory_slot, craft_output_x, craft_output_y,0, 1, 1)
--draw output selection
if craft_output_selection_x > 0 and craft_output_selection_y > 0 then
love.graphics.draw(inventory_slot_selection, craft_output_x, craft_output_y,0, 1, 1)
end
--draw output item
if crafting.output_inventory[1]["id"] then
love.graphics.draw(texture_table[crafting.output_inventory[1]["id"]], craft_output_x+10, craft_output_y+10,0, 4, 4)
--love.graphics.draw(texture_table[inventory[i]["id"]], crafting_x+(i*(inv_slot_width))+10, crafting_y,0, 4, 4)
love.graphics.print( crafting.output_inventory[1]["count"], craft_output_x+50, craft_output_y+64, 0, 1, 1)
end
----------------------------------
--draw crafting table
local xstep = 0
local ystep = 0
local nextline = 0
for i = 1,table.getn(crafting.craft_inventory) do
--print(i)
love.graphics.draw(inventory_slot, craft_inv_x+(xstep*84), craft_inv_y+(84*ystep),0, 1, 1)
--move to next step
nextline = nextline + 1
xstep = xstep + 1
if nextline >=crafting.craft_size then
nextline = 0
ystep = ystep + 1
xstep = 0
end
end
--draw craft selection
if craft_inventory_selection_x > 0 and craft_inventory_selection_y > 0 then
love.graphics.draw(inventory_slot_selection, ((craft_inventory_selection_x-1)*84)+craft_inv_x, ((craft_inventory_selection_y-1)*84)+craft_inv_y,0, 1, 1)
end
--draw items
local xstep = 0
local ystep = 0
local nextline = 0
for i = 1,table.getn(crafting.craft_inventory ) 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 crafting.craft_inventory[i]["id"] then
love.graphics.draw(texture_table[crafting.craft_inventory[i]["id"]], craft_inv_x+(xstep*84)+10, craft_inv_y+(84*ystep)+10,0, 4, 4)
--love.graphics.draw(texture_table[inventory[i]["id"]], crafting_x+(i*(inv_slot_width))+10, crafting_y,0, 4, 4)
love.graphics.print( crafting.craft_inventory[i]["count"], craft_inv_x+(xstep*84)+50, craft_inv_y+(84*ystep)+64, 0, 1, 1)
end
--move to next step
nextline = nextline + 1
xstep = xstep + 1
if nextline >=crafting.craft_size then
nextline = 0
ystep = ystep + 1
xstep = 0
end
end
--------------------------------
--draw inventory
local xstep = 0
local ystep = 0
local nextline = 0
for i = 1,table.getn(inventory) do
love.graphics.draw(inventory_slot, crafting_x+(xstep*84), crafting_y+(84*ystep),0, 1, 1)
--move to next step
nextline = nextline + 1
xstep = xstep + 1
if nextline >=inventory_size then
nextline = 0
ystep = ystep + 1
xstep = 0
end
end
--draw selection
if crafting_selection_x > 0 and crafting_selection_y > 0 then
love.graphics.draw(inventory_slot_selection, ((crafting_selection_x-1)*84)+crafting_x, ((crafting_selection_y-1)*84)+crafting_y,0, 1, 1)
end
--love.graphics.rectangle("line", ((crafting_selection_x-1)*84)+crafting_x, ((crafting_selection_y-1)*84)+crafting_y, 84, 84 )
--love.graphics.draw(inventory_slot_selection, crafting_x+(inventory_selection*(inv_slot_width/2)), crafting_y,0, 1/2, 1/2)
--draw items
local xstep = 0
local ystep = 0
local nextline = 0
for i = 1,table.getn(inventory) 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"]], crafting_x+(xstep*84)+10, crafting_y+(84*ystep)+10,0, 4, 4)
--love.graphics.draw(texture_table[inventory[i]["id"]], crafting_x+(i*(inv_slot_width))+10, crafting_y,0, 4, 4)
love.graphics.print( inventory[i]["count"], crafting_x+(xstep*84)+50, crafting_y+(84*ystep)+64, 0, 1, 1)
end
--move to next step
nextline = nextline + 1
xstep = xstep + 1
if nextline >=inventory_size then
nextline = 0
ystep = ystep + 1
xstep = 0
end
end
end
end
old_left_mouse = false
old_selected_slot = 0
selected_slot = 0
function crafting.move_items()
local left = love.mouse.isDown(1)
local right = love.mouse.isDown(2) --split stack in half
--left click
if crafting_open == true then
if left and old_left_mouse == false then
--the full inventory
if crafting_selection_x > 0 and crafting_selection_y > 0 then
crafting.left_click(crafting_selection_x,crafting_selection_y,inventory,inventory_size,true)
--the crafting inventory
elseif craft_inventory_selection_x > 0 and craft_inventory_selection_y > 0 then
crafting.left_click(craft_inventory_selection_x,craft_inventory_selection_y,crafting.craft_inventory,crafting.craft_size,true)
--only allow grabbing from output
elseif craft_output_selection_x > 0 and craft_output_selection_y > 0 then
crafting.left_click(craft_output_selection_x,craft_output_selection_y,crafting.output_inventory,1,false)
--throw item
else
print("fix this")
throw_item()
end
--right click
--place 1 or split stack
elseif right and old_right_mouse == false then
--the full inventory
if crafting_selection_x > 0 and crafting_selection_y > 0 then
--print("full")
crafting.right_click(crafting_selection_x,crafting_selection_y,inventory,inventory_size,true)
--the crafting inventory
elseif craft_inventory_selection_x > 0 and craft_inventory_selection_y > 0 then
crafting.right_click(craft_inventory_selection_x,craft_inventory_selection_y,crafting.craft_inventory,crafting.craft_size,true)
--only allow grabbing from output
elseif craft_output_selection_x > 0 and craft_output_selection_y > 0 then
crafting.right_click(craft_output_selection_x,craft_output_selection_y,crafting.output_inventory,1,false)
end
end
if (left or right) and (old_left_mouse == false) and (old_right_mouse == false) then--and (craft_inventory_selection_x > 0 and craft_inventory_selection_y > 0) then
detect_recipes()
end
end
old_left_mouse = left
old_right_mouse = right
end
--left click mechanic
function crafting.left_click(selectionerx,selectionery,inventory,inventory_width,allow_input)
local i = selectionerx + ((selectionery-1) * inventory_width)
--taking
if not crafting.held_item["id"] then
crafting.held_item["id"] = inventory[i]["id"]
crafting.held_item["count"] = inventory[i]["count"]
inventory[i] = {}
--remove items from craft when taking from output
if inventory == crafting.output_inventory then
--print("test output")
--remove singles from craft
for i = 1,crafting.craft_size*crafting.craft_size do
if crafting.craft_inventory[i]["id"] then
crafting.craft_inventory[i]["count"] = crafting.craft_inventory[i]["count"] - 1
if crafting.craft_inventory[i]["count"] <= 0 then
crafting.craft_inventory[i] = {}
end
end
end
end
--placing
elseif allow_input == true then
--put in blank slot
if not inventory[i]["id"] then
inventory[i]["id"] = crafting.held_item["id"]
inventory[i]["count"] = crafting.held_item["count"]
crafting.held_item = {}
--put into existing
elseif inventory[i]["id"] == crafting.held_item["id"] then
--add into
if inventory[i]["count"] + crafting.held_item["count"] <= 64 then
inventory[i]["count"] = inventory[i]["count"] + crafting.held_item["count"]
crafting.held_item = {}
end
--switch
else
local temp_held = crafting.held_item
crafting.held_item = inventory[i]
inventory[i] = temp_held
end
end
end
--right click mechanic
function crafting.right_click(selectionerx,selectionery,inventory,inventory_width,allow_input)
local i = selectionerx + ((selectionery-1) * inventory_width)
if not crafting.held_item["id"] and math.floor(inventory[i]["count"]/2) > 0 then
crafting.held_item["id"] = inventory[i]["id"]
crafting.held_item["count"] = math.floor(inventory[i]["count"]/2)
inventory[i]["count"] = math.ceil(inventory[i]["count"]/2)
else
--add to blank inventory
if not inventory[i]["id"] and crafting.held_item["count"] - 1 > 0 then
inventory[i]["id"] = crafting.held_item["id"]
inventory[i]["count"] = 1
crafting.held_item["count"] = crafting.held_item["count"] - 1
--last item
elseif not inventory[i]["id"] then
inventory[i]["id"] = crafting.held_item["id"]
inventory[i]["count"] = 1
crafting.held_item = {}
--add to existing stack
elseif inventory[i]["id"] == crafting.held_item["id"] and inventory[i]["count"] + crafting.held_item["count"] <= 64 then
inventory[i]["count"] = inventory[i]["count"] + 1
crafting.held_item["count"] = crafting.held_item["count"] - 1
if crafting.held_item["count"] <= 0 then
crafting.held_item = {}
end
end
end
end
recipe_test = {
recipe = {
nil,nil,nil,
nil,7,nil,
nil,nil,nil,
},
output = 8,
amount = 4,
}
--crafting mechanic
function detect_recipes()
print("add shift clicking")
--print("----------------------")
for i = 1,crafting.craft_size*crafting.craft_size do
if recipe_test["recipe"][i] == crafting.craft_inventory[i]["id"] then
if i == crafting.craft_size*crafting.craft_size then
--print(" real recipe")
crafting.output_inventory[1]["id"] = recipe_test["output"]
crafting.output_inventory[1]["count"] = recipe_test["amount"]
end
end
if recipe_test["recipe"][i] ~= crafting.craft_inventory[i]["id"] then
--print("move onto next")
crafting.output_inventory[1] = {}
return
end
--print(crafting.craft_inventory[i]["id"])
end
end