Bo Dash
Bo Dash

Reputation: 113

How to design a "Dynamic inventory system" for a point and click game?

I have done lots of research on invetory system for point and click game in Lua and corona. I have come across this example,I am doing something similar to this,but I need a dynamic inventory system. I mean if I have 4 slots,and all them are full the fifth object go to next slot,so there will be an arrow to the right so I can click on ;and go to the next page. And imagine there are 5 items,and I have 4 slots,the fifth slot would be on the next page. I use the third item,and third slot would then be empty,so I want the fourth and fifth item automatically move back to third and fourth slot. I have hard time figuring this out. Thanks for advance.

local myInventoryBag={}
local maxItems = 10 -- change this to how many you want

myInventoryBag[5]=3 -- Hammer for instance
myInventoryBag[4]=7 -- A metal Pipe for instance

local function getImageForItem(thisItem)
    local itemNumber = tonumber(thisItem)
    local theImage=""

    if itemNumber==3 then
        theImage="hammer.png"
    elseif itemNumber == 7 then
        theImage="metalpipe.png"
    elseif ... -- for other options
        ...
    else
        return nil
    end

    local image = display.newImage(theImage)
    return image
end

local function displayItems()
    local i
    for i=1,#myInventoryBag do
        local x = 0  -- calculate based on the i
        local y = 0 --  calculate based on the i

        local image = getImageForItem(myInventoryBag[i])

        if image==nil then return end

        image.setReferencePoint(display.TopLeftReferencePoint)
        image.x = x
        image.y = y
    end
end

Upvotes: 0

Views: 2326

Answers (2)

Mud
Mud

Reputation: 29000

local itemImages =
{
    [0] = display.newImage('MISSING_ITEM_IMAGE.PNG'),
    [3] = display.newImage('hammer.png'),
    [7] = display.newImage('metalpipe.png'),
}

function getImageForItem(itemId)
    return itemImages[itemId] or itemImages[0]
end

local myInventoryBag={}
local maxItems = 10 -- change this to how many you want
local visibleItems = 4 -- show this many items at a time (with arrows to scroll to others)

-- show inventory items at index [first,last]
local function displayInventoryItems(first,last)
    local x = 0 -- first item goes here
    local y = 0 -- top of inventory row
    for i=first,last do
        image = getImageForItem(myInventoryBag[i])
        image.x = x
        image.y = y
        x = x + image.width
    end
end

-- show inventory items on a given "page"
local function displayInventoryPage(page)
    page = page or 1 -- default to showing the first page
    if page > maxItems then
        -- error! handle me!
    end
    local first = (page - 1) * visibleItems + 1
    local last = first + visibleItems - 1
    displayInventoryItems(first, last)
end

myInventoryBag[5] = 3 -- Hammer for instance
myInventoryBag[4] = 7 -- A metal Pipe for instance

displayInventoryPage(1)
displayInventoryPage(2)

Upvotes: 3

Darkwater
Darkwater

Reputation: 1386

Basically what you would do is loop through all the inventory slots and check if the slot is empty. If it's empty, place the item in that slot and stop the loop. If it's not, go to the next one.

If you want to remove an item from the inventory, you can simply call table.delete(myInventoryBag, slotToEmpty).

For pages, you'd simply have a page variable. When drawing the inventory slots, just loop from slots (page-1) * 4 + 1 to page * 4.

(Edit: I'd highly recommend using proper indentation, as it will make the code much much more readable.)

Upvotes: 2

Related Questions