53 lines
1.1 KiB
Lua
53 lines
1.1 KiB
Lua
---@class
|
|
Ingredients = {}
|
|
|
|
local ingredientCache = {}
|
|
|
|
local _ingredientCacheIndex = 1
|
|
local maxCache = 80
|
|
for i = 1, maxCache do
|
|
ingredientCache[i] = {}
|
|
end
|
|
|
|
local newestIngredient = {}
|
|
function Ingredients.newestIngredient()
|
|
-- assert(newestIngredient.position ~= nil, "All ingredients are implicitly required to have a position component.")
|
|
return newestIngredient
|
|
end
|
|
|
|
function Ingredients.cacheSize()
|
|
return maxCache
|
|
end
|
|
|
|
function Ingredients.clearCache(world)
|
|
for _, o in ipairs(ingredientCache) do
|
|
for key in pairs(o) do
|
|
o[key] = nil
|
|
end
|
|
world:addEntity(o)
|
|
end
|
|
world:addEntity(Ingredients.getFirst())
|
|
end
|
|
|
|
function Ingredients.nextInCache()
|
|
local index = _ingredientCacheIndex
|
|
_ingredientCacheIndex = _ingredientCacheIndex + 1
|
|
if _ingredientCacheIndex >= maxCache then
|
|
_ingredientCacheIndex = 1
|
|
end
|
|
|
|
local o = ingredientCache[index]
|
|
for key in pairs(o) do
|
|
o[key] = nil
|
|
end
|
|
|
|
newestIngredient = o
|
|
return o
|
|
end
|
|
|
|
function Ingredients.getFirst()
|
|
local ret = Ingredients.nextInCache()
|
|
ret.position = { x = 0, y = 0 }
|
|
return ret
|
|
end
|