diff --git a/src/ecs.lua b/src/ecs.lua index fa585fa..9b72e6f 100644 --- a/src/ecs.lua +++ b/src/ecs.lua @@ -1,16 +1,16 @@ ecs = {} -local ALL_ENTITIES = {} +local allEntities = {} ---@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 } ---@type System[] -local SYSTEMS = {} +local systems = {} -- 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 diff --git a/src/utils.lua b/src/utils.lua index 712cc11..45f7f21 100644 --- a/src/utils.lua +++ b/src/utils.lua @@ -102,20 +102,20 @@ function getNearestOf(array, x, y, extraCondition) return nearest, nearestDistance end -local NO_VALUE = {} +local NoValue = {} 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, }