Open-Terrarium/crafting.lua

322 lines
10 KiB
Lua
Raw Normal View History

2017-07-24 04:51:33 -04:00
--the crafting library
crafting = {}
crafting_open = false
2017-07-25 02:10:03 -04:00
crafting.craft_size = 3 --number x number
2017-07-24 04:51:33 -04:00
crafting_x = 120
2017-07-25 02:10:03 -04:00
crafting_y = 350
craft_inv_x = 120
craft_inv_y = 50
2017-07-24 04:51:33 -04:00
2017-07-25 02:19:40 -04:00
craft_output_x = 380
craft_output_y = 134
2017-07-24 04:51:33 -04:00
crafting_selection_x = 1
crafting_selection_y = 1
2017-07-25 02:10:03 -04:00
craft_inventory_selection_x = 0
craft_inventory_selection_y = 0
2017-07-25 02:19:40 -04:00
craft_output_selection_x = 0
craft_output_selection_y = 0
2017-07-25 02:10:03 -04:00
crafting.craft_inventory = {}
2017-07-25 02:39:49 -04:00
crafting.held_item = {}
2017-07-25 04:27:17 -04:00
--create output inventory
crafting.output_inventory = {}
crafting.output_inventory[1] = {}
2017-07-25 02:10:03 -04:00
--create crafting inventory
for i = 1,crafting.craft_size*crafting.craft_size do
crafting.craft_inventory[i] = {}
end
--draw entire inventory
2017-07-24 04:51:33 -04:00
function crafting.render_crafting()
if crafting_open == true then
2017-07-25 02:19:40 -04:00
--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
2017-07-25 04:27:17 -04:00
--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
2017-07-25 03:06:47 -04:00
----------------------------------
2017-07-25 02:10:03 -04:00
--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
2017-07-25 03:06:47 -04:00
--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
2017-07-24 04:51:33 -04:00
2017-07-25 03:06:47 -04:00
--------------------------------
2017-07-24 04:51:33 -04:00
--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)
2017-07-25 03:06:47 -04:00
--draw items
2017-07-24 04:51:33 -04:00
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
2017-07-24 05:12:05 -04:00
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
2017-07-24 05:12:05 -04:00
2017-07-26 01:48:23 -04:00
--left click
2017-07-24 05:12:05 -04:00
if crafting_open == true then
if left and old_left_mouse == false then
2017-07-25 04:27:17 -04:00
--the full inventory
2017-07-24 05:12:05 -04:00
if crafting_selection_x > 0 and crafting_selection_y > 0 then
2017-07-26 03:19:00 -04:00
crafting.left_click(crafting_selection_x,crafting_selection_y,inventory,inventory_size,true)
2017-07-24 05:12:05 -04:00
2017-07-25 04:27:17 -04:00
--the crafting inventory
2017-07-25 03:06:47 -04:00
elseif craft_inventory_selection_x > 0 and craft_inventory_selection_y > 0 then
2017-07-26 03:19:00 -04:00
crafting.left_click(craft_inventory_selection_x,craft_inventory_selection_y,crafting.craft_inventory,crafting.craft_size,true)
2017-07-25 04:27:17 -04:00
--only allow grabbing from output
elseif craft_output_selection_x > 0 and craft_output_selection_y > 0 then
2017-07-26 03:19:00 -04:00
crafting.left_click(craft_output_selection_x,craft_output_selection_y,crafting.output_inventory,1,false)
2017-07-26 04:50:01 -04:00
--throw item
else
print("fix this")
throw_item()
end
2017-07-26 01:48:23 -04:00
--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")
2017-07-26 03:55:49 -04:00
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
2017-07-26 03:55:49 -04:00
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
2017-07-26 03:55:49 -04:00
crafting.right_click(craft_output_selection_x,craft_output_selection_y,crafting.output_inventory,1,false)
2017-07-24 05:12:05 -04:00
end
2017-07-24 04:51:33 -04:00
end
2017-07-26 04:45:28 -04:00
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
2017-07-26 04:30:02 -04:00
detect_recipes()
end
2017-07-24 04:51:33 -04:00
end
2017-07-24 05:12:05 -04:00
old_left_mouse = left
old_right_mouse = right
2017-07-24 04:51:33 -04:00
end
2017-07-26 03:19:00 -04:00
--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] = {}
2017-07-26 04:45:28 -04:00
--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
2017-07-26 03:19:00 -04:00
--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
2017-07-26 03:55:49 -04:00
if inventory[i]["count"] + crafting.held_item["count"] <= 64 then
inventory[i]["count"] = inventory[i]["count"] + crafting.held_item["count"]
2017-07-26 03:19:00 -04:00
crafting.held_item = {}
end
--switch
2017-07-26 03:55:49 -04:00
else
2017-07-26 03:19:00 -04:00
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)
2017-07-26 03:55:49 -04:00
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
2017-07-26 03:19:00 -04:00
end
2017-07-26 04:06:03 -04:00
2017-08-04 03:23:26 -04:00
2017-07-26 04:30:02 -04:00
recipe_test = {
2017-07-26 04:37:32 -04:00
recipe = {
nil,nil,nil,
nil,7,nil,
nil,nil,nil,
},
output = 8,
amount = 4,
2017-07-26 04:30:02 -04:00
}
2017-07-26 04:06:03 -04:00
--crafting mechanic
function detect_recipes()
2017-07-26 04:50:01 -04:00
print("add shift clicking")
2017-07-26 04:46:06 -04:00
--print("----------------------")
2017-07-26 04:06:03 -04:00
for i = 1,crafting.craft_size*crafting.craft_size do
2017-07-26 04:37:32 -04:00
if recipe_test["recipe"][i] == crafting.craft_inventory[i]["id"] then
2017-07-26 04:30:02 -04:00
if i == crafting.craft_size*crafting.craft_size then
2017-07-26 04:46:06 -04:00
--print(" real recipe")
2017-07-26 04:37:32 -04:00
crafting.output_inventory[1]["id"] = recipe_test["output"]
crafting.output_inventory[1]["count"] = recipe_test["amount"]
2017-07-26 04:30:02 -04:00
end
end
2017-07-26 04:37:32 -04:00
if recipe_test["recipe"][i] ~= crafting.craft_inventory[i]["id"] then
2017-07-26 04:46:06 -04:00
--print("move onto next")
2017-07-26 04:37:32 -04:00
crafting.output_inventory[1] = {}
2017-07-26 04:30:02 -04:00
return
end
--print(crafting.craft_inventory[i]["id"])
2017-07-26 04:06:03 -04:00
end
2017-07-26 04:30:02 -04:00
2017-07-26 04:06:03 -04:00
end