Quick <const> and case fixes in ecs and utils
This commit is contained in:
parent
4546902f2f
commit
bf4fbab40f
22
src/ecs.lua
22
src/ecs.lua
|
@ -1,16 +1,16 @@
|
|||
ecs = {}
|
||||
|
||||
local ALL_ENTITIES = {}
|
||||
local allEntities <const> = {}
|
||||
|
||||
---@alias System { callback: fun(delta: number, entity: any, a: any, b: any, c: any, d: any, e: any, any), shapes: {}, keys: string[], entityCache: nil | table<any, boolean> }
|
||||
|
||||
---@type System[]
|
||||
local SYSTEMS = {}
|
||||
local systems <const> = {}
|
||||
|
||||
-- TODO: Add entity to any existing systems
|
||||
function ecs.addEntity(entity)
|
||||
ALL_ENTITIES[entity] = true
|
||||
for _, system in pairs(SYSTEMS) do
|
||||
allEntities[entity] = true
|
||||
for _, system in pairs(systems) do
|
||||
if entityMatchesShapes(entity, system.shapes) then
|
||||
system.entityCache[entity] = true
|
||||
else
|
||||
|
@ -20,17 +20,17 @@ function ecs.addEntity(entity)
|
|||
end
|
||||
|
||||
function ecs.removeEntity(entity)
|
||||
ALL_ENTITIES[entity] = nil
|
||||
for _, system in pairs(SYSTEMS) do
|
||||
allEntities[entity] = nil
|
||||
for _, system in pairs(systems) do
|
||||
system.entityCache[entity] = nil
|
||||
end
|
||||
end
|
||||
|
||||
local PLACEHOLDER = {}
|
||||
local Placeholder = {}
|
||||
---@generic T
|
||||
---@return T
|
||||
function ecs.field()
|
||||
return PLACEHOLDER
|
||||
return Placeholder
|
||||
end
|
||||
|
||||
function allKeysIncluded(entity, filter)
|
||||
|
@ -84,7 +84,7 @@ function tprint(tbl, indent)
|
|||
end
|
||||
|
||||
function addSystem(callback, keys, shapes)
|
||||
SYSTEMS[#SYSTEMS + 1] = {
|
||||
systems[#systems + 1] = {
|
||||
callback = callback,
|
||||
keys = keys,
|
||||
shapes = shapes,
|
||||
|
@ -99,10 +99,10 @@ end
|
|||
|
||||
---@param deltaSeconds number
|
||||
function ecs.update(deltaSeconds)
|
||||
for _, system in pairs(SYSTEMS) do
|
||||
for _, system in pairs(systems) do
|
||||
if not system.entityCache then
|
||||
system.entityCache = {}
|
||||
for entity, _ in pairs(ALL_ENTITIES) do
|
||||
for entity, _ in pairs(allEntities) do
|
||||
if entityMatchesShapes(entity, system.shapes) then
|
||||
system.entityCache[entity] = true
|
||||
end
|
||||
|
|
|
@ -102,20 +102,20 @@ function getNearestOf(array, x, y, extraCondition)
|
|||
return nearest, nearestDistance
|
||||
end
|
||||
|
||||
local NO_VALUE = {}
|
||||
local NoValue <const> = {}
|
||||
|
||||
function buildCache(fetcher)
|
||||
local cacheData = {}
|
||||
return {
|
||||
cacheDate = cacheData,
|
||||
get = function(key)
|
||||
if cacheData[key] == NO_VALUE then
|
||||
if cacheData[key] == NoValue then
|
||||
return nil
|
||||
end
|
||||
if cacheData[key] ~= nil then
|
||||
return cacheData[key]
|
||||
end
|
||||
cacheData[key] = fetcher(key) or NO_VALUE
|
||||
cacheData[key] = fetcher(key) or NoValue
|
||||
return cacheData[key]
|
||||
end,
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue