Some ECS cleanup with ews() rework.
This commit is contained in:
parent
ff2b1d0180
commit
982b8220a6
src
89
src/ecs.lua
89
src/ecs.lua
|
@ -2,19 +2,19 @@ ecs = { }
|
|||
|
||||
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[]
|
||||
local SYSTEMS = {}
|
||||
|
||||
local PLACEHOLDER = {}
|
||||
|
||||
-- TODO: Add entity to any existing systems
|
||||
function ecs.addEntity(entity)
|
||||
ALL_ENTITIES[entity] = true
|
||||
for _, system in pairs(SYSTEMS) do
|
||||
if entityMatchesShapes(entity, system.shapes) then
|
||||
system.entityCache[entity] = true
|
||||
else
|
||||
system.entityCache[entity] = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -26,7 +26,9 @@ function ecs.removeEntity(entity)
|
|||
end
|
||||
end
|
||||
|
||||
---@return any
|
||||
local PLACEHOLDER = {}
|
||||
---@generic T
|
||||
---@return T
|
||||
function ecs.field()
|
||||
return PLACEHOLDER
|
||||
end
|
||||
|
@ -89,6 +91,11 @@ function addSystem(callback, keys, shapes)
|
|||
}
|
||||
end
|
||||
|
||||
---@return boolean
|
||||
function is(entity, shape)
|
||||
return allKeysIncluded(entity, shape)
|
||||
end
|
||||
|
||||
---@param deltaSeconds number
|
||||
function ecs.update(deltaSeconds)
|
||||
for _,system in pairs(SYSTEMS) do
|
||||
|
@ -100,9 +107,9 @@ function ecs.update(deltaSeconds)
|
|||
end
|
||||
end
|
||||
end
|
||||
local keys = system.keys
|
||||
for entity,_ in pairs(system.entityCache) do
|
||||
local keys = system.keys
|
||||
system.callback(deltaSeconds, entity[keys[1]], entity[keys[2]], entity[keys[3]], entity[keys[4]], entity)
|
||||
system.callback(deltaSeconds, entity, entity[keys[1]], entity[keys[2]], entity[keys[3]], entity[keys[4]], entity)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -118,11 +125,11 @@ end
|
|||
---@generic W
|
||||
---@generic WKey
|
||||
---@param tShape { [TKey]: T }
|
||||
---@param uShape { [UKey]: U } | fun(componentT: T, any) | nil
|
||||
---@param vShape { [VKey]: V } | fun(componentT: T, componentU: U, any) | nil
|
||||
---@param wShape { [WKey]: W } | fun(componentT: T, componentU: U, componentV: V, any) | nil
|
||||
---@param finalFunc fun(componentT: T, componentU: U, componentV: V, componentW: W, any) | nil
|
||||
function ecs.entitiesWithShapes(tShape, uShape, vShape, wShape, finalFunc)
|
||||
---@param uShape { [UKey]: U } | fun(entity: any, componentT: T, any) | nil
|
||||
---@param vShape { [VKey]: V } | fun(entity: any, componentT: T, componentU: U, any) | nil
|
||||
---@param wShape { [WKey]: W } | fun(entity: any, componentT: T, componentU: U, componentV: V, any) | nil
|
||||
---@param finalFunc fun(entity: any, componentT: T, componentU: U, componentV: V, componentW: W, any) | nil
|
||||
function ecs.forEntitiesWith(tShape, uShape, vShape, wShape, finalFunc)
|
||||
local maybeShapes = {tShape, uShape, vShape, wShape, finalFunc}
|
||||
local shapes = {}
|
||||
local callback
|
||||
|
@ -146,23 +153,59 @@ function ecs.entitiesWithShapes(tShape, uShape, vShape, wShape, finalFunc)
|
|||
end
|
||||
|
||||
local f = ecs.field()
|
||||
|
||||
---@type { x: number, y: number }
|
||||
local XYPair = { x = f, y = f }
|
||||
|
||||
local Position = { position = XYPair }
|
||||
local Target = { target = XYPair }
|
||||
local Velocity = { velocity = XYPair }
|
||||
|
||||
local someMovable = {
|
||||
position = { x = 0, y = 0 },
|
||||
velocity = { x = 2, y = 1 }
|
||||
function ecs.overlayOnto(entity, value)
|
||||
for key,v in pairs(value) do
|
||||
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)
|
||||
print("position:")
|
||||
---@generic T
|
||||
---@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)
|
||||
pos.x = pos.x + (vel.x * delta)
|
||||
pos.y = pos.y + (vel.y * delta)
|
||||
end)
|
||||
ecs.set(Target, e, {
|
||||
--target = { x = 10, y = 10}
|
||||
})
|
||||
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)
|
Loading…
Reference in New Issue