cave-story-randomizer/src/item_deck.lua

70 lines
1.5 KiB
Lua
Raw Normal View History

2018-12-14 20:38:35 +00:00
local C = Class:extend()
local ITEM_DATA = require 'database.items'
function C:new()
self._left = {}
for k, v in pairs(ITEM_DATA) do
local item = _.clone(v)
table.insert(self._left, item)
end
self._placed = {}
2018-12-14 20:38:35 +00:00
end
local function _filterAny(item) return true end
function C:getAny()
return self:_getItem(_filterAny)
end
function C:getAnyByAttribute(attribute)
function _filterAnyByAttribute(item, attribute)
for k, v in pairs(item.attributes) do
if v == attribute then return true
end
return false
end
return self:_getItem(_filterAnyByAttribute)
end
function C:placeAny()
return self:place(self:getAny())
end
function C:placeAnyByAttribute(attributes)
return self:place(self:getAnyByAttribute(attributes))
2018-12-14 20:38:35 +00:00
end
function C:_getItem(filterFn)
-- Filter down to only applicable items.
local applicable = {}
local indexMap = {}
for index, item in ipairs(self._left) do
2018-12-14 20:38:35 +00:00
if filterFn(item) then
table.insert(applicable, item)
indexMap[item] = index
2018-12-14 20:38:35 +00:00
end
end
assert(#applicable >= 1, 'No applicable items!')
-- Select an item.
local selected = _.sample(applicable)
local index = indexMap[selected]
return selected, index
end
function C:place(item, index)
2018-12-14 20:38:35 +00:00
table.remove(self._left, index)
table.insert(self._placed, item)
return item
end
2018-12-14 20:38:35 +00:00
function C:getPlacedItems(item)
local placed = {table.unpack(self._placed)} -- clone the table
if(item) then table.insert(placed, item) end
return placed
2018-12-14 20:38:35 +00:00
end
return C