cave-story-randomizer/src/item_deck.lua

45 lines
940 B
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
end
local function _filterAny(item) return true end
local function _filterWeapon(item) return item.kind == "weapon" end
function C:getAny()
return self:_getItem(_filterAny)
end
function C:getWeapon()
return self:_getItem(_filterWeapon)
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]
2018-12-14 20:38:35 +00:00
table.remove(self._left, index)
return selected
end
return C