Skip to content

Commit 57ff795

Browse files
committed
Documentation improvements and fixes
1 parent 1bb3219 commit 57ff795

File tree

16 files changed

+214
-1384
lines changed

16 files changed

+214
-1384
lines changed

ECS.lua

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.

ECS_concat.lua

Lines changed: 91 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--[[
2-
ECS Lua v2.1.1
2+
ECS Lua v2.1.2
33
44
ECS Lua is a fast and easy to use ECS (Entity Component System) engine for game development.
55
@@ -818,7 +818,7 @@ end
818818
__F__["ECS"] = function()
819819
-- src/ECS.lua
820820
--[[
821-
ECS Lua v2.1.0 [2021-10-15 11:00]
821+
ECS Lua v2.1.2
822822
823823
ECS Lua is a fast and easy to use ECS (Entity Component System) engine for game development.
824824
@@ -1233,12 +1233,12 @@ __F__["EntityRepository"] = function()
12331233
local archetype = entity.archetype
12341234
local storage = self._archetypes[archetype]
12351235
if (storage == nil) then
1236-
storage = { Count = 0, Entities = {} }
1236+
storage = { count = 0, entities = {} }
12371237
self._archetypes[archetype] = storage
12381238
end
12391239

1240-
storage.Entities[entity] = true
1241-
storage.Count = storage.Count + 1
1240+
storage.entities[entity] = true
1241+
storage.count = storage.count + 1
12421242

12431243
self._entitiesArchetype[entity] = archetype
12441244
else
@@ -1259,10 +1259,10 @@ __F__["EntityRepository"] = function()
12591259
self._entitiesArchetype[entity] = nil
12601260

12611261
local storage = self._archetypes[archetypeOld]
1262-
if (storage ~= nil and storage.Entities[entity] == true) then
1263-
storage.Entities[entity] = nil
1264-
storage.Count = storage.Count - 1
1265-
if (storage.Count == 0) then
1262+
if (storage ~= nil and storage.entities[entity] == true) then
1263+
storage.entities[entity] = nil
1264+
storage.count = storage.count - 1
1265+
if (storage.count == 0) then
12661266
self._archetypes[archetypeOld] = nil
12671267
end
12681268
end
@@ -1293,7 +1293,7 @@ __F__["EntityRepository"] = function()
12931293
local chunks = {}
12941294
for archetype, storage in pairs(self._archetypes) do
12951295
if query:Match(archetype) then
1296-
table.insert(chunks, storage.Entities)
1296+
table.insert(chunks, storage.entities)
12971297
end
12981298
end
12991299
return query:Result(chunks), #chunks > 0
@@ -1328,14 +1328,14 @@ __F__["Event"] = function()
13281328
Connection.__index = Connection
13291329

13301330
function Connection.New(event, handler)
1331-
return setmetatable({ _Event = event, _Handler = handler }, Connection)
1331+
return setmetatable({ _event = event, _handler = handler }, Connection)
13321332
end
13331333

13341334
-- Unsubscribe
13351335
function Connection:Disconnect()
1336-
local event = self._Event
1336+
local event = self._event
13371337
if (event and not event.destroyed) then
1338-
local idx = table.find(event._handlers, self._Handler)
1338+
local idx = table.find(event._handlers, self._handler)
13391339
if idx ~= nil then
13401340
table.remove(event._handlers, idx)
13411341
end
@@ -1492,7 +1492,6 @@ __F__["Query"] = function()
14921492
@return QueryResult
14931493
]]
14941494
function Query:Result(chunks)
1495-
self._lastResultChunks = chunks
14961495
return QueryResult.New(chunks, self._clauses)
14971496
end
14981497

@@ -1894,7 +1893,7 @@ __F__["QueryResult"] = function()
18941893
]]
18951894
function QueryResult:AnyMatch(predicate)
18961895
local anyMatch = false
1897-
self:Run(function(value)
1896+
self:ForEach(function(value)
18981897
if predicate(value) then
18991898
anyMatch = true
19001899
end
@@ -1912,7 +1911,7 @@ __F__["QueryResult"] = function()
19121911
]]
19131912
function QueryResult:AllMatch(predicate)
19141913
local allMatch = true
1915-
self:Run(function(value)
1914+
self:ForEach(function(value)
19161915
if (not predicate(value)) then
19171916
allMatch = false
19181917
end
@@ -1935,38 +1934,22 @@ __F__["QueryResult"] = function()
19351934
]]
19361935
function QueryResult:FindAny()
19371936
local out
1938-
self:Run(function(value)
1937+
self:ForEach(function(value)
19391938
out = value
19401939
-- break
19411940
return true
19421941
end)
19431942
return out
19441943
end
19451944

1946-
--[[
1947-
Performs an action for each element of this QueryResult.
1948-
1949-
This is a terminal operation.
1950-
1951-
The behavior of this operation is explicitly nondeterministic. This operation does not guarantee to respect the
1952-
encounter order of the QueryResult.
1953-
1954-
@param action {function(value, count) -> bool} A action to perform on the elements, breaks execution case returns true
1955-
]]
1956-
function QueryResult:ForEach(action)
1957-
self:Run(function(value, count)
1958-
return action(value, count) == true
1959-
end)
1960-
end
1961-
19621945
--[[
19631946
Returns an array containing the elements of this QueryResult.
19641947
19651948
This is a terminal operation.
19661949
]]
19671950
function QueryResult:ToArray()
19681951
local array = {}
1969-
self:Run(function(value)
1952+
self:ForEach(function(value)
19701953
table.insert(array, value)
19711954
end)
19721955
return array
@@ -1981,7 +1964,7 @@ __F__["QueryResult"] = function()
19811964
]]
19821965
function QueryResult:Iterator()
19831966
local thread = coroutine.create(function()
1984-
self:Run(function(value, count)
1967+
self:ForEach(function(value, count)
19851968
-- These will be passed back again next iteration
19861969
coroutine.yield(value, count)
19871970
end)
@@ -1993,13 +1976,17 @@ __F__["QueryResult"] = function()
19931976
end
19941977
end
19951978

1996-
19971979
--[[
1998-
Pipeline this QueryResult, applying callback to each value
1980+
Performs an action for each element of this QueryResult.
1981+
1982+
This is a terminal operation.
1983+
1984+
The behavior of this operation is explicitly nondeterministic. This operation does not guarantee to respect the
1985+
encounter order of the QueryResult.
19991986
2000-
@param callback {function(value, count) -> bool} Break execution case returns true
1987+
@param action {function(value, count) -> bool} A action to perform on the elements, breaks execution case returns true
20011988
]]
2002-
function QueryResult:Run(callback)
1989+
function QueryResult:ForEach(action)
20031990
local count = 1
20041991
local pipeline = self._pipeline
20051992

@@ -2008,13 +1995,14 @@ __F__["QueryResult"] = function()
20081995
-- faster
20091996
for _, entities in ipairs(self.chunks) do
20101997
for entity, _ in pairs(entities) do
2011-
if (callback(entity, count) == true) then
1998+
if (action(entity, count) == true) then
20121999
return
20132000
end
20142001
count = count + 1
20152002
end
20162003
end
20172004
else
2005+
-- Pipeline this QueryResult, applying callback to each value
20182006
for i, entities in ipairs(self.chunks) do
20192007
for entity,_ in pairs(entities) do
20202008
local mustStop = false
@@ -2038,7 +2026,7 @@ __F__["QueryResult"] = function()
20382026
end
20392027

20402028
if itemAccepted then
2041-
if (callback(value, count) == true) then
2029+
if (action(value, count) == true) then
20422030
return
20432031
end
20442032
count = count + 1
@@ -2296,7 +2284,29 @@ __F__["SystemExecutor"] = function()
22962284
local SystemExecutor = {}
22972285
SystemExecutor.__index = SystemExecutor
22982286

2299-
function SystemExecutor.New(world, systems)
2287+
function SystemExecutor.New(world)
2288+
local executor = setmetatable({
2289+
_world = world,
2290+
_onExit = {},
2291+
_onEnter = {},
2292+
_onRemove = {},
2293+
_task = {},
2294+
_render = {},
2295+
_process = {},
2296+
_transform = {},
2297+
_schedulers = {},
2298+
_lastFrameMatchQueries = {},
2299+
_currentFrameMatchQueries = {},
2300+
}, SystemExecutor)
2301+
2302+
world:OnQueryMatch(function(query)
2303+
executor._currentFrameMatchQueries[query] = true
2304+
end)
2305+
2306+
return executor
2307+
end
2308+
2309+
function SystemExecutor:SetSystems(systems)
23002310
local onExit = {}
23012311
local onEnter = {}
23022312
local onRemove = {}
@@ -2348,26 +2358,13 @@ __F__["SystemExecutor"] = function()
23482358
table.sort(updateProcess, orderSystems)
23492359
table.sort(updateTransform, orderSystems)
23502360

2351-
-- tasks = resolveDependecy(systems)
2352-
local executor = setmetatable({
2353-
_world = world,
2354-
_onExit = onExit,
2355-
_onEnter = onEnter,
2356-
_onRemove = onRemove,
2357-
_task = updateTask,
2358-
_render = updateRender,
2359-
_process = updateProcess,
2360-
_transform = updateTransform,
2361-
_schedulers = {},
2362-
_lastFrameMatchQueries = {},
2363-
_currentFrameMatchQueries = {},
2364-
}, SystemExecutor)
2365-
2366-
world:OnQueryMatch(function(query)
2367-
executor._currentFrameMatchQueries[query] = true
2368-
end)
2369-
2370-
return executor
2361+
self._onExit = onExit
2362+
self._onEnter = onEnter
2363+
self._onRemove = onRemove
2364+
self._task = updateTask
2365+
self._render = updateRender
2366+
self._process = updateProcess
2367+
self._transform = updateTransform
23712368
end
23722369

23732370
--[[
@@ -2989,22 +2986,17 @@ __F__["World"] = function()
29892986
]]
29902987
version = 0,
29912988
--[[
2992-
Allows you to define the maximum time that the JobSystem can operate in each step. This value is a percentage
2993-
of the expected time for each frame (see World:SetFrequency(frequency)).
2989+
Allows you to define the maximum time that the JobSystem can operate in each frame.
29942990
2995-
The default value is 0.7
2991+
The default value is 0.011666666666666665 = ((1000/60/1000)*0.7)
29962992
2997-
Ex1. If the world has a frequency set to 30Hz (30 fps), then the JobSystem will try to run a maximum of 0.0077
2998-
seconds in each step, totaling 0.023 seconds of processing per frame. A game that runs at 30fps has 0.0333
2999-
seconds to do all the processing for each frame, including rendering (1000/30/1000)
2993+
A game that runs at 30fps has 0.0333 seconds to do all the processing for each frame, including rendering
30002994
- 30FPS = ((1000/30/1000)*0.7)/3 = 0.007777777777777777
30012995
3002-
Ex2. If the world has the frequency set to 60Hz (60 fps), then the JobSystem will try to run a maximum of 0.0038
3003-
seconds in each step, totaling 0.011 seconds of processing per frame. A game that runs at 60fps has 0.0166
3004-
seconds to do all the processing for each frame, including rendering (1000/60/1000)
2996+
A game that runs at 60fps has 0.0166 seconds to do all the processing for each frame, including rendering
30052997
- 60FPS = ((1000/60/1000)*0.7)/3 = 0.0038888888888888883
30062998
]]
3007-
maxScheduleExecTimePercent = 0.7,
2999+
maxTasksExecTime = 0.013333333333333334,
30083000
_dirty = false, -- True when create/remove entity, add/remove entity component (change archetype)
30093001
_timer = Timer.New(frequency),
30103002
_systems = {}, -- systems in this world
@@ -3017,7 +3009,7 @@ __F__["World"] = function()
30173009
}, World)
30183010

30193011
-- System execution plan
3020-
world._executor = SystemExecutor.New(world, {})
3012+
world._executor = SystemExecutor.New(world)
30213013

30223014
world._onChangeArchetypeEvent:Connect(function(entity, archetypeOld, archetypeNew)
30233015
world:_OnChangeArchetype(entity, archetypeOld, archetypeNew)
@@ -3072,7 +3064,8 @@ __F__["World"] = function()
30723064

30733065
if self._systems[systemClass] == nil then
30743066
self._systems[systemClass] = systemClass.New(self, config)
3075-
self._executor = SystemExecutor.New(self, self._systems)
3067+
3068+
self._executor:SetSystems(self._systems)
30763069
end
30773070
end
30783071
end
@@ -3186,32 +3179,39 @@ __F__["World"] = function()
31863179
]]
31873180
function World:Update(step, now)
31883181

3189-
--[[
3190-
.-------------------------------------.
3191-
|----- process|transform|render ------|
3192-
| | |
3193-
| s:ShouldUpdate() | < |
3194-
| s:Update() | s:OnRemove() |
3195-
| | s:OnExit() |
3196-
| | s:OnEnter() |
3197-
| | >{0...n} |
3198-
| | |
3199-
'-------------------------------------'
3200-
]]
3201-
3182+
32023183
self._timer:Update(
32033184
now, step,
32043185
function(Time)
3186+
--[[
3187+
JobSystem
3188+
.------------------.
3189+
| pipeline |
3190+
|------------------|
3191+
| s:ShouldUpdate() |
3192+
| s:Update() |
3193+
'------------------'
3194+
]]
32053195
if step == "process" then
32063196
self._executor:ScheduleTasks(Time)
32073197
end
32083198
-- run suspended Tasks
3209-
-- 60FPS = ((1000/60/1000)*0.7)/3 = 0.0038888888888888883
3210-
-- 30FPS = ((1000/30/1000)*0.7)/3 = 0.007777777777777777
3211-
local maxScheduleExecTime = (self._timer.Time.DeltaFixed * (self.maxScheduleExecTimePercent or 0.7))/3
3212-
self._executor:ExecTasks(maxScheduleExecTime)
3199+
self._executor:ExecTasks(self.maxTasksExecTime)
32133200
end,
32143201
function(Time)
3202+
--[[
3203+
.------------------.
3204+
| pipeline |
3205+
|------------------|
3206+
| s:ShouldUpdate() |
3207+
| s:Update() |
3208+
| |
3209+
|-- CLEAR ---------|
3210+
| s:OnRemove() |
3211+
| s:OnExit() |
3212+
| s:OnEnter() |
3213+
'------------------'
3214+
]]
32153215
if step == "process" then
32163216
self._executor:ExecProcess(Time)
32173217
elseif step == "transform" then

build.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ local SRC_FILES = {
2323
}
2424

2525
local HEADER = [[
26-
ECS Lua v2.1.1
26+
ECS Lua v2.1.2
2727
2828
ECS Lua is a fast and easy to use ECS (Entity Component System) engine for game development.
2929

0 commit comments

Comments
 (0)