mirror of
https://github.com/bakpakin/tiny-ecs.git
synced 2024-11-17 04:44:23 +00:00
Make the scope of many local variables smaller.
This commit is contained in:
parent
18270cd60a
commit
5035d2c8a7
@ -1,3 +1,8 @@
|
|||||||
|
local GLOBALS = {}
|
||||||
|
for k, v in pairs(_G) do
|
||||||
|
GLOBALS[k] = v
|
||||||
|
end
|
||||||
|
|
||||||
local tiny = require "tiny"
|
local tiny = require "tiny"
|
||||||
|
|
||||||
local function deep_copy(x)
|
local function deep_copy(x)
|
||||||
@ -230,4 +235,8 @@ describe('tiny-ecs:', function()
|
|||||||
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it("Doesn't pollute the global namespace", function()
|
||||||
|
assert.are.same(_G, GLOBALS)
|
||||||
|
end)
|
||||||
|
|
||||||
end)
|
end)
|
||||||
|
101
tiny.lua
101
tiny.lua
@ -235,20 +235,19 @@ end
|
|||||||
|
|
||||||
-- Update function for all Processing Systems.
|
-- Update function for all Processing Systems.
|
||||||
local function processingSystemUpdate(system, dt)
|
local function processingSystemUpdate(system, dt)
|
||||||
local entities = system.entities
|
|
||||||
local preProcess = system.preProcess
|
local preProcess = system.preProcess
|
||||||
local process = system.process
|
local process = system.process
|
||||||
local postProcess = system.postProcess
|
local postProcess = system.postProcess
|
||||||
local entity
|
|
||||||
|
|
||||||
if preProcess then
|
if preProcess then
|
||||||
preProcess(system, dt)
|
preProcess(system, dt)
|
||||||
end
|
end
|
||||||
|
|
||||||
if process then
|
if process then
|
||||||
|
local entities = system.entities
|
||||||
local len = #entities
|
local len = #entities
|
||||||
for i = 1, len do
|
for i = 1, len do
|
||||||
entity = entities[i]
|
local entity = entities[i]
|
||||||
process(system, entity, dt)
|
process(system, entity, dt)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -408,9 +407,8 @@ tiny_addSystem = tiny.addSystem
|
|||||||
--- Shortcut for adding multiple Entities and Systems to the World. Returns all
|
--- Shortcut for adding multiple Entities and Systems to the World. Returns all
|
||||||
-- added Entities and Systems.
|
-- added Entities and Systems.
|
||||||
function tiny.add(world, ...)
|
function tiny.add(world, ...)
|
||||||
local obj
|
|
||||||
for i = 1, select("#", ...) do
|
for i = 1, select("#", ...) do
|
||||||
obj = select(i, ...)
|
local obj = select(i, ...)
|
||||||
if obj then
|
if obj then
|
||||||
if isSystem(obj) then
|
if isSystem(obj) then
|
||||||
tiny_addSystem(world, obj)
|
tiny_addSystem(world, obj)
|
||||||
@ -442,9 +440,8 @@ tiny_removeSystem = tiny.removeSystem
|
|||||||
--- Shortcut for removing multiple Entities and Systems from the World. Returns
|
--- Shortcut for removing multiple Entities and Systems from the World. Returns
|
||||||
-- all removed Systems and Entities
|
-- all removed Systems and Entities
|
||||||
function tiny.remove(world, ...)
|
function tiny.remove(world, ...)
|
||||||
local obj
|
|
||||||
for i = 1, select("#", ...) do
|
for i = 1, select("#", ...) do
|
||||||
obj = select(i, ...)
|
local obj = select(i, ...)
|
||||||
if obj then
|
if obj then
|
||||||
if isSystem(obj) then
|
if isSystem(obj) then
|
||||||
tiny_removeSystem(world, obj)
|
tiny_removeSystem(world, obj)
|
||||||
@ -471,17 +468,15 @@ function tiny_manageSystems(world)
|
|||||||
|
|
||||||
local entities = world.entities
|
local entities = world.entities
|
||||||
local systems = world.systems
|
local systems = world.systems
|
||||||
local system, index, filter
|
|
||||||
local entityList, entityIndices, entityIndex, onRemove, onAdd
|
|
||||||
|
|
||||||
-- Remove Systems
|
-- Remove Systems
|
||||||
for i = 1, #s2r do
|
for i = 1, #s2r do
|
||||||
system = s2r[i]
|
local system = s2r[i]
|
||||||
index = system.index
|
local index = system.index
|
||||||
if system.world == world then
|
if system.world == world then
|
||||||
onRemove = system.onRemove
|
local onRemove = system.onRemove
|
||||||
if onRemove then
|
if onRemove then
|
||||||
entityList = system.entities
|
local entityList = system.entities
|
||||||
for j = 1, #entityList do
|
for j = 1, #entityList do
|
||||||
onRemove(system, entityList[j])
|
onRemove(system, entityList[j])
|
||||||
end
|
end
|
||||||
@ -502,10 +497,10 @@ function tiny_manageSystems(world)
|
|||||||
|
|
||||||
-- Add Systems
|
-- Add Systems
|
||||||
for i = 1, #s2a do
|
for i = 1, #s2a do
|
||||||
system = s2a[i]
|
local system = s2a[i]
|
||||||
if systems[system.index] ~= system then
|
if systems[system.index] ~= system then
|
||||||
entityList = {}
|
local entityList = {}
|
||||||
entityIndices = {}
|
local entityIndices = {}
|
||||||
system.entities = entityList
|
system.entities = entityList
|
||||||
system.indices = entityIndices
|
system.indices = entityIndices
|
||||||
if system.active == nil then
|
if system.active == nil then
|
||||||
@ -513,17 +508,17 @@ function tiny_manageSystems(world)
|
|||||||
end
|
end
|
||||||
system.modified = true
|
system.modified = true
|
||||||
system.world = world
|
system.world = world
|
||||||
index = #systems + 1
|
local index = #systems + 1
|
||||||
system.index = index
|
system.index = index
|
||||||
systems[index] = system
|
systems[index] = system
|
||||||
|
|
||||||
-- Try to add Entities
|
-- Try to add Entities
|
||||||
onAdd = system.onAdd
|
local onAdd = system.onAdd
|
||||||
filter = system.filter
|
local filter = system.filter
|
||||||
if filter then
|
if filter then
|
||||||
for entity in pairs(entities) do
|
for entity in pairs(entities) do
|
||||||
if filter(system, entity) then
|
if filter(system, entity) then
|
||||||
entityIndex = #entityList + 1
|
local entityIndex = #entityList + 1
|
||||||
entityList[entityIndex] = entity
|
entityList[entityIndex] = entity
|
||||||
entityIndices[entity] = entityIndex
|
entityIndices[entity] = entityIndex
|
||||||
if onAdd then
|
if onAdd then
|
||||||
@ -556,38 +551,36 @@ function tiny_manageEntities(world)
|
|||||||
local entities = world.entities
|
local entities = world.entities
|
||||||
local systems = world.systems
|
local systems = world.systems
|
||||||
local entityCount = world.entityCount
|
local entityCount = world.entityCount
|
||||||
local entity, system, index
|
|
||||||
local onRemove, onAdd, ses, seis, filter, tmpEntity
|
|
||||||
|
|
||||||
-- Change Entities
|
-- Change Entities
|
||||||
for i = 1, #e2c do
|
for i = 1, #e2c do
|
||||||
entity = e2c[i]
|
local entity = e2c[i]
|
||||||
if entities[entity] then
|
if entities[entity] then
|
||||||
for j = 1, #systems do
|
for j = 1, #systems do
|
||||||
system = systems[j]
|
local system = systems[j]
|
||||||
ses = system.entities
|
local ses = system.entities
|
||||||
seis = system.indices
|
local seis = system.indices
|
||||||
index = seis[entity]
|
local index = seis[entity]
|
||||||
filter = system.filter
|
local filter = system.filter
|
||||||
if filter and filter(system, entity) then
|
if filter and filter(system, entity) then
|
||||||
if not index then
|
if not index then
|
||||||
system.modified = true
|
system.modified = true
|
||||||
index = #ses + 1
|
index = #ses + 1
|
||||||
ses[index] = entity
|
ses[index] = entity
|
||||||
seis[entity] = index
|
seis[entity] = index
|
||||||
onAdd = system.onAdd
|
local onAdd = system.onAdd
|
||||||
if onAdd then
|
if onAdd then
|
||||||
onAdd(system, entity)
|
onAdd(system, entity)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
elseif index then
|
elseif index then
|
||||||
system.modified = true
|
system.modified = true
|
||||||
tmpEntity = ses[#ses]
|
local tmpEntity = ses[#ses]
|
||||||
ses[index] = tmpEntity
|
ses[index] = tmpEntity
|
||||||
seis[tmpEntity] = index
|
seis[tmpEntity] = index
|
||||||
seis[entity] = nil
|
seis[entity] = nil
|
||||||
ses[#ses] = nil
|
ses[#ses] = nil
|
||||||
onRemove = system.onRemove
|
local onRemove = system.onRemove
|
||||||
if onRemove then
|
if onRemove then
|
||||||
onRemove(system, entity)
|
onRemove(system, entity)
|
||||||
end
|
end
|
||||||
@ -599,61 +592,54 @@ function tiny_manageEntities(world)
|
|||||||
|
|
||||||
-- Remove Entities
|
-- Remove Entities
|
||||||
for i = 1, #e2r do
|
for i = 1, #e2r do
|
||||||
entity = e2r[i]
|
local entity = e2r[i]
|
||||||
if entities[entity] then
|
if entities[entity] then
|
||||||
entities[entity] = nil
|
entities[entity] = nil
|
||||||
entityCount = entityCount - 1
|
entityCount = entityCount - 1
|
||||||
|
|
||||||
for j = 1, #systems do
|
for j = 1, #systems do
|
||||||
system = systems[j]
|
local system = systems[j]
|
||||||
ses = system.entities
|
local ses = system.entities
|
||||||
seis = system.indices
|
local seis = system.indices
|
||||||
index = seis[entity]
|
local index = seis[entity]
|
||||||
|
|
||||||
if index then
|
if index then
|
||||||
system.modified = true
|
system.modified = true
|
||||||
tmpEntity = ses[#ses]
|
local tmpEntity = ses[#ses]
|
||||||
ses[index] = tmpEntity
|
ses[index] = tmpEntity
|
||||||
seis[tmpEntity] = index
|
seis[tmpEntity] = index
|
||||||
seis[entity] = nil
|
seis[entity] = nil
|
||||||
ses[#ses] = nil
|
ses[#ses] = nil
|
||||||
onRemove = system.onRemove
|
local onRemove = system.onRemove
|
||||||
if onRemove then
|
if onRemove then
|
||||||
onRemove(system, entity)
|
onRemove(system, entity)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
e2r[i] = nil
|
e2r[i] = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Add Entities
|
-- Add Entities
|
||||||
for i = 1, #e2a do
|
for i = 1, #e2a do
|
||||||
entity = e2a[i]
|
local entity = e2a[i]
|
||||||
if not entities[entity] then
|
if not entities[entity] then
|
||||||
entities[entity] = true
|
entities[entity] = true
|
||||||
entityCount = entityCount + 1
|
entityCount = entityCount + 1
|
||||||
|
|
||||||
for j = 1, #systems do
|
for j = 1, #systems do
|
||||||
system = systems[j]
|
local system = systems[j]
|
||||||
ses = system.entities
|
local ses = system.entities
|
||||||
seis = system.indices
|
local seis = system.indices
|
||||||
filter = system.filter
|
local filter = system.filter
|
||||||
if filter and filter(system, entity) then
|
if filter and filter(system, entity) then
|
||||||
system.modified = true
|
system.modified = true
|
||||||
index = #ses + 1
|
local index = #ses + 1
|
||||||
ses[index] = entity
|
ses[index] = entity
|
||||||
seis[entity] = index
|
seis[entity] = index
|
||||||
onAdd = system.onAdd
|
local onAdd = system.onAdd
|
||||||
if onAdd then
|
if onAdd then
|
||||||
onAdd(system, entity)
|
onAdd(system, entity)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
e2a[i] = nil
|
e2a[i] = nil
|
||||||
end
|
end
|
||||||
@ -680,23 +666,22 @@ function tiny.update(world, dt, filter)
|
|||||||
tiny_manageEntities(world)
|
tiny_manageEntities(world)
|
||||||
|
|
||||||
local systems = world.systems
|
local systems = world.systems
|
||||||
local system, update, interval, onModify
|
|
||||||
|
|
||||||
-- Iterate through Systems IN ORDER
|
-- Iterate through Systems IN ORDER
|
||||||
for i = 1, #systems do
|
for i = 1, #systems do
|
||||||
system = systems[i]
|
local system = systems[i]
|
||||||
if system.active and ((not filter) or filter(world, system)) then
|
if system.active and ((not filter) or filter(world, system)) then
|
||||||
|
|
||||||
-- Call the modify callback on Systems that have been modified.
|
-- Call the modify callback on Systems that have been modified.
|
||||||
onModify = system.onModify
|
local onModify = system.onModify
|
||||||
if onModify and system.modified then
|
if onModify and system.modified then
|
||||||
onModify(system, dt)
|
onModify(system, dt)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Update Systems that have an update method (most Systems)
|
-- Update Systems that have an update method (most Systems)
|
||||||
update = system.update
|
local update = system.update
|
||||||
if update then
|
if update then
|
||||||
interval = system.interval
|
local interval = system.interval
|
||||||
if interval then
|
if interval then
|
||||||
local bufferedTime = (system.bufferedTime or 0) + dt
|
local bufferedTime = (system.bufferedTime or 0) + dt
|
||||||
while bufferedTime >= interval do
|
while bufferedTime >= interval do
|
||||||
@ -787,7 +772,7 @@ worldMetaTable = {
|
|||||||
setSystemIndex = tiny.setSystemIndex
|
setSystemIndex = tiny.setSystemIndex
|
||||||
},
|
},
|
||||||
__tostring = function(self)
|
__tostring = function(self)
|
||||||
return "tiny-ecs_World"
|
return "<tiny-ecs_World>"
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user