mirror of
https://github.com/bakpakin/tiny-ecs.git
synced 2026-07-22 07:56:52 -06:00
Remove world.systemIndices to fix possible obscure bugs.
Remove tiny.getSystemIndex(world, system) in favor of a System 'index' field. Add some comments and documentation.
This commit is contained in:
+2
-2
@@ -211,8 +211,8 @@ describe('tiny-ecs:', function()
|
|||||||
world:update(1)
|
world:update(1)
|
||||||
world:setSystemIndex(moveSystem, 2)
|
world:setSystemIndex(moveSystem, 2)
|
||||||
world:update(1)
|
world:update(1)
|
||||||
assert.equals(2, world:getSystemIndex(moveSystem))
|
assert.equals(2, moveSystem.index)
|
||||||
assert.equals(1, world:getSystemIndex(oneTimeSystem))
|
assert.equals(1, oneTimeSystem.index)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it("Sorts Entities in Sorting Systems", function()
|
it("Sorts Entities in Sorting Systems", function()
|
||||||
|
|||||||
@@ -206,12 +206,15 @@ end
|
|||||||
-- * The `active` flag is whether or not the System is updated automatically.
|
-- * The `active` flag is whether or not the System is updated automatically.
|
||||||
-- Inactive Systems should be updated manually or not at all via
|
-- Inactive Systems should be updated manually or not at all via
|
||||||
-- `system:update(dt)`. Defaults to true.
|
-- `system:update(dt)`. Defaults to true.
|
||||||
-- * The 'entities' field is an ordered list of Entities in the System. This
|
-- * The `entities` field is an ordered list of Entities in the System. This
|
||||||
-- list can be used to quickly iterate through all Entities in a System.
|
-- list can be used to quickly iterate through all Entities in a System.
|
||||||
-- * The `interval` field is an optional field that makes Systems update at
|
-- * The `interval` field is an optional field that makes Systems update at
|
||||||
-- certain intervals using buffered time, regardless of World update frequency.
|
-- certain intervals using buffered time, regardless of World update frequency.
|
||||||
-- For example, to make a System update once a second, set the System's interval
|
-- For example, to make a System update once a second, set the System's interval
|
||||||
-- to 1.
|
-- to 1.
|
||||||
|
-- * The `index` field is the System's index in the World. Lower indexed
|
||||||
|
-- Systems are processed before higher indices. The `index` is a read only
|
||||||
|
-- field; to set the `index`, use `tiny.setSystemIndex(world, system)`.
|
||||||
-- * The `indices` field is a table of Entity keys to their indices in the
|
-- * The `indices` field is a table of Entity keys to their indices in the
|
||||||
-- `entities` list. Most Systems can ignore this.
|
-- `entities` list. Most Systems can ignore this.
|
||||||
-- * The `modified` flag is an indicator if the System has been modified in
|
-- * The `modified` flag is an indicator if the System has been modified in
|
||||||
@@ -361,15 +364,11 @@ function tiny.world(...)
|
|||||||
-- Set of Entities
|
-- Set of Entities
|
||||||
entities = {},
|
entities = {},
|
||||||
|
|
||||||
-- Number of Entities in World.
|
-- Number of Entities in World
|
||||||
entityCount = 0,
|
entityCount = 0,
|
||||||
|
|
||||||
-- List of System Data. A data element is a table with 4
|
-- List of Systems
|
||||||
-- keys: system, indices, entities, and active.
|
systems = {}
|
||||||
systems = {},
|
|
||||||
|
|
||||||
-- Table of Systems to System Indices
|
|
||||||
systemIndices = {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tiny_add(ret, ...)
|
tiny_add(ret, ...)
|
||||||
@@ -459,7 +458,6 @@ function tiny_manageSystems(world)
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local systemIndices = world.systemIndices
|
|
||||||
local entities = world.entities
|
local entities = world.entities
|
||||||
local systems = world.systems
|
local systems = world.systems
|
||||||
local system, index, filter
|
local system, index, filter
|
||||||
@@ -468,8 +466,8 @@ function tiny_manageSystems(world)
|
|||||||
-- Remove Systems
|
-- Remove Systems
|
||||||
for i = 1, #s2r do
|
for i = 1, #s2r do
|
||||||
system = s2r[i]
|
system = s2r[i]
|
||||||
index = systemIndices[system]
|
index = system.index
|
||||||
if index then
|
if system.world == world then
|
||||||
onRemove = system.onRemove
|
onRemove = system.onRemove
|
||||||
if onRemove then
|
if onRemove then
|
||||||
entityList = system.entities
|
entityList = system.entities
|
||||||
@@ -477,10 +475,9 @@ function tiny_manageSystems(world)
|
|||||||
onRemove(system, entityList[j])
|
onRemove(system, entityList[j])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
systemIndices[system] = nil
|
|
||||||
tremove(systems, index)
|
tremove(systems, index)
|
||||||
for j = index, #systems do
|
for j = index, #systems do
|
||||||
systemIndices[systems[j]] = j
|
systems[j].index = j
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
s2r[i] = nil
|
s2r[i] = nil
|
||||||
@@ -489,12 +486,13 @@ function tiny_manageSystems(world)
|
|||||||
system.world = nil
|
system.world = nil
|
||||||
system.entities = nil
|
system.entities = nil
|
||||||
system.indices = nil
|
system.indices = nil
|
||||||
|
system.index = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Add Systems
|
-- Add Systems
|
||||||
for i = 1, #s2a do
|
for i = 1, #s2a do
|
||||||
system = s2a[i]
|
system = s2a[i]
|
||||||
if not systemIndices[system] then
|
if systems[system.index] ~= system then
|
||||||
entityList = {}
|
entityList = {}
|
||||||
entityIndices = {}
|
entityIndices = {}
|
||||||
system.entities = entityList
|
system.entities = entityList
|
||||||
@@ -505,7 +503,7 @@ function tiny_manageSystems(world)
|
|||||||
system.modified = true
|
system.modified = true
|
||||||
system.world = world
|
system.world = world
|
||||||
index = #systems + 1
|
index = #systems + 1
|
||||||
systemIndices[system] = index
|
system.index = index
|
||||||
systems[index] = system
|
systems[index] = system
|
||||||
|
|
||||||
-- Try to add Entities
|
-- Try to add Entities
|
||||||
@@ -679,26 +677,18 @@ function tiny.getSystemCount(world)
|
|||||||
return #(world.systems)
|
return #(world.systems)
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Gets the index of a System in the World. Lower indexed Systems are processed
|
|
||||||
-- before higher indexed systems.
|
|
||||||
function tiny.getSystemIndex(world, system)
|
|
||||||
return world.systemIndices[system]
|
|
||||||
end
|
|
||||||
|
|
||||||
--- Sets the index of a System in the World, and returns the old index. Changes
|
--- Sets the index of a System in the World, and returns the old index. Changes
|
||||||
-- the order in which they Systems processed, because lower indexed Systems are
|
-- the order in which they Systems processed, because lower indexed Systems are
|
||||||
-- processed first.
|
-- processed first.
|
||||||
function tiny.setSystemIndex(world, system, index)
|
function tiny.setSystemIndex(world, system, index)
|
||||||
local systemIndices = world.systemIndices
|
local oldIndex = system.index
|
||||||
local oldIndex = systemIndices[system]
|
|
||||||
local systems = world.systems
|
local systems = world.systems
|
||||||
local system = systems[oldIndex]
|
|
||||||
|
|
||||||
tremove(systems, oldIndex)
|
tremove(systems, oldIndex)
|
||||||
tinsert(systems, index, system)
|
tinsert(systems, index, system)
|
||||||
|
|
||||||
for i = oldIndex, index, index >= oldIndex and 1 or -1 do
|
for i = oldIndex, index, index >= oldIndex and 1 or -1 do
|
||||||
systemIndices[systems[i]] = i
|
systems[i].index = i
|
||||||
end
|
end
|
||||||
|
|
||||||
return oldIndex
|
return oldIndex
|
||||||
|
|||||||
Reference in New Issue
Block a user