Some ECS cleanup with ews() rework.

This commit is contained in:
Sage Vaillancourt 2025-01-31 18:35:42 -05:00
parent ff2b1d0180
commit 982b8220a6
1 changed files with 66 additions and 23 deletions

View File

@ -2,19 +2,19 @@ ecs = { }
local ALL_ENTITIES = {} local ALL_ENTITIES = {}
---@alias System { callback: fun(delta: number, a: any, b: any, c: any, d: any, e: any, any), shapes: {}, keys: string[], entityCache: 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 = {}
local PLACEHOLDER = {}
-- 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 ALL_ENTITIES[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
system.entityCache[entity] = nil
end end
end end
end end
@ -26,7 +26,9 @@ function ecs.removeEntity(entity)
end end
end end
---@return any local PLACEHOLDER = {}
---@generic T
---@return T
function ecs.field() function ecs.field()
return PLACEHOLDER return PLACEHOLDER
end end
@ -89,6 +91,11 @@ function addSystem(callback, keys, shapes)
} }
end end
---@return boolean
function is(entity, shape)
return allKeysIncluded(entity, shape)
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
@ -100,9 +107,9 @@ function ecs.update(deltaSeconds)
end end
end end
end end
local keys = system.keys
for entity,_ in pairs(system.entityCache) do for entity,_ in pairs(system.entityCache) do
local keys = system.keys system.callback(deltaSeconds, entity, entity[keys[1]], entity[keys[2]], entity[keys[3]], entity[keys[4]], entity)
system.callback(deltaSeconds, entity[keys[1]], entity[keys[2]], entity[keys[3]], entity[keys[4]], entity)
end end
end end
end end
@ -118,11 +125,11 @@ end
---@generic W ---@generic W
---@generic WKey ---@generic WKey
---@param tShape { [TKey]: T } ---@param tShape { [TKey]: T }
---@param uShape { [UKey]: U } | fun(componentT: T, any) | nil ---@param uShape { [UKey]: U } | fun(entity: any, componentT: T, any) | nil
---@param vShape { [VKey]: V } | fun(componentT: T, componentU: U, any) | nil ---@param vShape { [VKey]: V } | fun(entity: any, componentT: T, componentU: U, any) | nil
---@param wShape { [WKey]: W } | fun(componentT: T, componentU: U, componentV: V, any) | nil ---@param wShape { [WKey]: W } | fun(entity: any, componentT: T, componentU: U, componentV: V, any) | nil
---@param finalFunc fun(componentT: T, componentU: U, componentV: V, componentW: W, any) | nil ---@param finalFunc fun(entity: any, componentT: T, componentU: U, componentV: V, componentW: W, any) | nil
function ecs.entitiesWithShapes(tShape, uShape, vShape, wShape, finalFunc) function ecs.forEntitiesWith(tShape, uShape, vShape, wShape, finalFunc)
local maybeShapes = {tShape, uShape, vShape, wShape, finalFunc} local maybeShapes = {tShape, uShape, vShape, wShape, finalFunc}
local shapes = {} local shapes = {}
local callback local callback
@ -146,23 +153,59 @@ function ecs.entitiesWithShapes(tShape, uShape, vShape, wShape, finalFunc)
end end
local f = ecs.field() local f = ecs.field()
---@type { x: number, y: number }
local XYPair = { x = f, y = f } local XYPair = { x = f, y = f }
local Position = { position = XYPair } local Position = { position = XYPair }
local Target = { target = XYPair }
local Velocity = { velocity = XYPair } local Velocity = { velocity = XYPair }
local someMovable = { function ecs.overlayOnto(entity, value)
position = { x = 0, y = 0 }, for key,v in pairs(value) do
velocity = { x = 2, y = 1 } entity[key] = v
end
ecs.addEntity(entity)
end
local data = {
position = {x = 0, y = 0},
velocity = {x = 1, y = 2},
} }
ecs.addEntity(someMovable) ---@generic T
---@param shape T
---@param entity unknown
---@return T
function ecs.get(shape, entity)
return entity
end
ecs.entitiesWithShapes(Position, Velocity, function (delta, pos, vel) ---@generic T
print("position:") ---@param entity unknown
---@param shape `T`
---@param value `T`
function ecs.set(entity, shape, value)
for key,v in pairs(shape) do
entity[key] = value[v]
end
end
ecs.addEntity(data)
ecs.forEntitiesWith(Position, Velocity, function (delta, e, pos, vel)
pos.x = pos.x + (delta * vel.x)
pos.y = pos.y + (delta * vel.y)
print("position")
tprint(pos, 1) tprint(pos, 1)
pos.x = pos.x + (vel.x * delta) ecs.set(Target, e, {
pos.y = pos.y + (vel.y * delta) --target = { x = 10, y = 10}
end) })
end)
ecs.forEntitiesWith(Target, function (delta, e, pos, vel)
pos.x = pos.x + (delta * vel.x)
pos.y = pos.y + (delta * vel.y)
print("position")
tprint(pos, 1)
ecs.set(e, Target, 'hallo')
end)
ecs.update(1)