Open-Terrarium/inventory.lua

140 lines
3.8 KiB
Lua
Raw Normal View History

2017-06-20 04:16:34 -04:00
--the inventory library
2017-07-19 00:41:56 -04:00
inventory_size = 9
2017-07-24 04:51:33 -04:00
inventory_height = 5
2017-07-19 00:41:56 -04:00
inventory = {}
inventory_selection = 1
2017-07-19 02:57:36 -04:00
--function new_inventory()
2017-07-24 04:51:33 -04:00
for i = 1,inventory_size*inventory_height do
2017-07-19 02:57:36 -04:00
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()
2017-07-19 02:32:24 -04:00
2017-07-24 05:12:05 -04:00
function inventory_add(item,slot)
2017-07-19 02:32:24 -04:00
--add the item
2017-07-19 03:03:54 -04:00
--for slot,table in pairs(inventory) do
2017-07-24 05:12:05 -04:00
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
2017-07-19 03:03:54 -04:00
for i = 1,table.getn(inventory) do
local table = inventory[i]
2017-07-19 04:32:18 -04:00
if table["id"] == item and table["count"] < 64 then
2017-07-19 02:32:24 -04:00
-- print("HERE")
2017-07-19 03:03:54 -04:00
inventory[i]["count"] = inventory[i]["count"] + 1
2017-07-19 02:32:24 -04:00
return
end
end
--else create new slot
2017-07-19 03:03:54 -04:00
--for slot,table in pairs(inventory) do
for i = 1,table.getn(inventory) do
local table = inventory[i]
2017-07-19 02:32:24 -04:00
if not table["id"] then
2017-07-19 03:03:54 -04:00
inventory[i] = {id = item, name = blocks[item]["name"], image = blocks[item]["image"], count = 1}
2017-07-19 02:32:24 -04:00
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
2017-07-19 01:07:18 -04:00
end
2017-07-19 02:32:24 -04:00
--0for slot,table in pairs(inventory) do
-- if table["id"] == item then
-- print("HERE")
-- inventory[slot]["count"] = inventory[slot]["count"] + 1
-- break
-- end
--end
2017-07-19 00:41:56 -04:00
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)
2017-07-19 01:07:18 -04:00
y = -y
2017-07-19 00:41:56 -04:00
inventory_selection = inventory_selection + y
if inventory_selection < 1 then
2017-07-24 04:51:33 -04:00
inventory_selection = inventory_selection + inventory_size
elseif inventory_selection > inventory_size then
inventory_selection = inventory_selection - inventory_size
2017-07-19 00:41:56 -04:00
end
end
2017-07-25 02:10:03 -04:00
--render hot bar
2017-07-19 00:41:56 -04:00
function render_inventory()
2017-07-24 04:51:33 -04:00
for i = 1,inventory_size do
2017-07-19 00:41:56 -04:00
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)
2017-07-24 04:51:33 -04:00
for i = 1,inventory_size do
2017-07-19 01:07:18 -04:00
--print(inventory[i]["id"])
--texture_table[loaded_chunks[xx][yy][x][y]["block"]]
2017-07-19 02:32:24 -04:00
--love.graphics.draw(texture_table[inventory[i]["id"]], drawx,drawy,0, scale/16, scale/16)
if inventory[i]["id"] then
2017-07-19 01:07:18 -04:00
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)
2017-07-19 02:32:24 -04:00
love.graphics.print( inventory[i]["count"], inventory_x+(i*(inv_slot_width/2))+22, inventory_y+28, 0, 1, 1)
2017-07-19 01:07:18 -04:00
end
end
2017-07-19 00:41:56 -04:00
end
2017-07-23 16:53:16 -04:00
2017-07-23 17:25:33 -04:00
--throw item
2017-07-23 16:53:16 -04:00
function throw_item()
2017-07-23 17:25:33 -04:00
--print(inventory[inventory_selection]["id"])
2017-07-23 16:53:16 -04:00
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