Quick <const> and case fixes in ecs and utils

This commit is contained in:
Sage Vaillancourt 2025-02-01 17:47:46 -05:00
parent 4546902f2f
commit bf4fbab40f
2 changed files with 14 additions and 14 deletions

View File

@ -1,16 +1,16 @@
ecs = {} 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> } ---@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[] ---@type System[]
local SYSTEMS = {} local systems <const> = {}
-- TODO: Add entity to any existing systems -- TODO: Add entity to any existing systems
function ecs.addEntity(entity) function ecs.addEntity(entity)
ALL_ENTITIES[entity] = true allEntities[entity] = true
for _, system in pairs(SYSTEMS) do for _, system in pairs(systems) do
if entityMatchesShapes(entity, system.shapes) then if entityMatchesShapes(entity, system.shapes) then
system.entityCache[entity] = true system.entityCache[entity] = true
else else
@ -20,17 +20,17 @@ function ecs.addEntity(entity)
end end
function ecs.removeEntity(entity) function ecs.removeEntity(entity)
ALL_ENTITIES[entity] = nil allEntities[entity] = nil
for _, system in pairs(SYSTEMS) do for _, system in pairs(systems) do
system.entityCache[entity] = nil system.entityCache[entity] = nil
end end
end end
local PLACEHOLDER = {} local Placeholder = {}
---@generic T ---@generic T
---@return T ---@return T
function ecs.field() function ecs.field()
return PLACEHOLDER return Placeholder
end end
function allKeysIncluded(entity, filter) function allKeysIncluded(entity, filter)
@ -84,7 +84,7 @@ function tprint(tbl, indent)
end end
function addSystem(callback, keys, shapes) function addSystem(callback, keys, shapes)
SYSTEMS[#SYSTEMS + 1] = { systems[#systems + 1] = {
callback = callback, callback = callback,
keys = keys, keys = keys,
shapes = shapes, shapes = shapes,
@ -99,10 +99,10 @@ end
---@param deltaSeconds number ---@param deltaSeconds number
function ecs.update(deltaSeconds) function ecs.update(deltaSeconds)
for _, system in pairs(SYSTEMS) do for _, system in pairs(systems) do
if not system.entityCache then if not system.entityCache then
system.entityCache = {} system.entityCache = {}
for entity, _ in pairs(ALL_ENTITIES) do for entity, _ in pairs(allEntities) do
if entityMatchesShapes(entity, system.shapes) then if entityMatchesShapes(entity, system.shapes) then
system.entityCache[entity] = true system.entityCache[entity] = true
end end

View File

@ -102,20 +102,20 @@ function getNearestOf(array, x, y, extraCondition)
return nearest, nearestDistance return nearest, nearestDistance
end end
local NO_VALUE = {} local NoValue <const> = {}
function buildCache(fetcher) function buildCache(fetcher)
local cacheData = {} local cacheData = {}
return { return {
cacheDate = cacheData, cacheDate = cacheData,
get = function(key) get = function(key)
if cacheData[key] == NO_VALUE then if cacheData[key] == NoValue then
return nil return nil
end end
if cacheData[key] ~= nil then if cacheData[key] ~= nil then
return cacheData[key] return cacheData[key]
end end
cacheData[key] = fetcher(key) or NO_VALUE cacheData[key] = fetcher(key) or NoValue
return cacheData[key] return cacheData[key]
end, end,
} }