mirror of
https://github.com/bakpakin/tiny-ecs.git
synced 2026-07-21 23:56:50 -06:00
Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8a3c22e74f | ||
|
|
a0fa0904f6 | ||
|
|
80bb6b8d31 | ||
|
|
83d806011b | ||
|
|
d720d361e6 | ||
|
|
d94b178670 |
@@ -0,0 +1,24 @@
|
||||
package = "tiny-ecs"
|
||||
version = "1.3-3"
|
||||
source = {
|
||||
url = "git://github.com/bakpakin/tiny-ecs",
|
||||
tag = version
|
||||
}
|
||||
description = {
|
||||
summary = "Entity Component System for Lua.",
|
||||
detailed = [[
|
||||
Pure Lua implementation of an easy to use, compact, fast, and flexible
|
||||
Entity Component System. Works well with Object Orientation.
|
||||
]],
|
||||
homepage = "https://github.com/bakpakin/tiny-ecs",
|
||||
license = "MIT"
|
||||
}
|
||||
dependencies = {
|
||||
"lua >= 5.1"
|
||||
}
|
||||
build = {
|
||||
type = "builtin",
|
||||
modules = {
|
||||
tiny = "tiny.lua"
|
||||
}
|
||||
}
|
||||
@@ -41,12 +41,13 @@ local tiny_addSystem
|
||||
local tiny_add
|
||||
local tiny_removeEntity
|
||||
local tiny_removeSystem
|
||||
local tiny_remove
|
||||
|
||||
--- Filter functions.
|
||||
-- A Filter is a function that selects which Entities apply to a System.
|
||||
-- Filters take two parameters, the System and the Entity, and return a boolean
|
||||
-- value indicating if the Entity should be processed by the System.
|
||||
-- value indicating if the Entity should be processed by the System. A truthy
|
||||
-- value includes the entity, while a falsey (nil or false) value excludes the
|
||||
-- entity.
|
||||
--
|
||||
-- Filters must be added to Systems by setting the `filter` field of the System.
|
||||
-- Filter's returned by tiny-ecs's Filter functions are immutable and can be
|
||||
@@ -330,7 +331,7 @@ local function processingSystemUpdate(system, dt)
|
||||
end
|
||||
|
||||
-- Sorts Systems by a function system.sortDelegate(entity1, entity2) on modify.
|
||||
local function sortedSystemOnModify(system, dt)
|
||||
local function sortedSystemOnModify(system)
|
||||
local entities = system.entities
|
||||
local indices = system.indices
|
||||
local sortDelegate = system.sortDelegate
|
||||
@@ -420,9 +421,6 @@ local worldMetaTable
|
||||
function tiny.world(...)
|
||||
local ret = setmetatable({
|
||||
|
||||
-- List of Entities to add
|
||||
entitiesToAdd = {},
|
||||
|
||||
-- List of Entities to remove
|
||||
entitiesToRemove = {},
|
||||
|
||||
@@ -438,14 +436,9 @@ function tiny.world(...)
|
||||
-- Set of Entities
|
||||
entities = {},
|
||||
|
||||
-- List of Entities
|
||||
entityList = {},
|
||||
|
||||
-- Number of Entities in World
|
||||
entityCount = 0,
|
||||
|
||||
-- List of Systems
|
||||
systems = {}
|
||||
|
||||
}, worldMetaTable)
|
||||
|
||||
tiny_add(ret, ...)
|
||||
@@ -459,13 +452,8 @@ end
|
||||
-- Also call this on Entities that have changed Components such that they
|
||||
-- match different Filters. Returns the Entity.
|
||||
function tiny.addEntity(world, entity)
|
||||
if world.entities[entity] then
|
||||
local e2c = world.entitiesToChange
|
||||
e2c[#e2c + 1] = entity
|
||||
else
|
||||
local e2a = world.entitiesToAdd
|
||||
e2a[#e2a + 1] = entity
|
||||
end
|
||||
local e2c = world.entitiesToChange
|
||||
e2c[#e2c + 1] = entity
|
||||
return entity
|
||||
end
|
||||
tiny_addEntity = tiny.addEntity
|
||||
@@ -497,7 +485,7 @@ function tiny.add(world, ...)
|
||||
end
|
||||
tiny_add = tiny.add
|
||||
|
||||
--- Removes an Entity to the World. Returns the Entity.
|
||||
--- Removes an Entity from the World. Returns the Entity.
|
||||
function tiny.removeEntity(world, entity)
|
||||
local e2r = world.entitiesToRemove
|
||||
e2r[#e2r + 1] = entity
|
||||
@@ -529,7 +517,6 @@ function tiny.remove(world, ...)
|
||||
end
|
||||
return ...
|
||||
end
|
||||
tiny_remove = tiny.remove
|
||||
|
||||
-- Adds and removes Systems that have been marked from the World.
|
||||
function tiny_manageSystems(world)
|
||||
@@ -543,7 +530,7 @@ function tiny_manageSystems(world)
|
||||
world.systemsToAdd = {}
|
||||
world.systemsToRemove = {}
|
||||
|
||||
local worldEntityList = world.entityList
|
||||
local worldEntityList = world.entities
|
||||
local systems = world.systems
|
||||
|
||||
-- Remove Systems
|
||||
@@ -623,58 +610,58 @@ end
|
||||
-- Adds, removes, and changes Entities that have been marked.
|
||||
function tiny_manageEntities(world)
|
||||
|
||||
local e2a = world.entitiesToAdd
|
||||
local e2r = world.entitiesToRemove
|
||||
local e2c = world.entitiesToChange
|
||||
|
||||
-- Early exit
|
||||
if #e2a == 0 and #e2r == 0 and #e2c == 0 then
|
||||
if #e2r == 0 and #e2c == 0 then
|
||||
return
|
||||
end
|
||||
|
||||
world.entitiesToChange = {}
|
||||
world.entitiesToAdd = {}
|
||||
world.entitiesToRemove = {}
|
||||
|
||||
local entities = world.entities
|
||||
local entityList = world.entityList
|
||||
local systems = world.systems
|
||||
local entityCount = world.entityCount
|
||||
|
||||
-- Change Entities
|
||||
for i = 1, #e2c do
|
||||
local entity = e2c[i]
|
||||
if entities[entity] then
|
||||
for j = 1, #systems do
|
||||
local system = systems[j]
|
||||
if not system.nocache then
|
||||
local ses = system.entities
|
||||
local seis = system.indices
|
||||
local index = seis[entity]
|
||||
local filter = system.filter
|
||||
if filter and filter(system, entity) then
|
||||
if not index then
|
||||
system.modified = true
|
||||
index = #ses + 1
|
||||
ses[index] = entity
|
||||
seis[entity] = index
|
||||
local onAdd = system.onAdd
|
||||
if onAdd then
|
||||
onAdd(system, entity)
|
||||
end
|
||||
end
|
||||
elseif index then
|
||||
-- Add if needed
|
||||
if not entities[entity] then
|
||||
local index = #entities + 1
|
||||
entities[entity] = index
|
||||
entities[index] = entity
|
||||
end
|
||||
for j = 1, #systems do
|
||||
local system = systems[j]
|
||||
if not system.nocache then
|
||||
local ses = system.entities
|
||||
local seis = system.indices
|
||||
local index = seis[entity]
|
||||
local filter = system.filter
|
||||
if filter and filter(system, entity) then
|
||||
if not index then
|
||||
system.modified = true
|
||||
local tmpEntity = ses[#ses]
|
||||
ses[index] = tmpEntity
|
||||
seis[tmpEntity] = index
|
||||
seis[entity] = nil
|
||||
ses[#ses] = nil
|
||||
local onRemove = system.onRemove
|
||||
if onRemove then
|
||||
onRemove(system, entity)
|
||||
index = #ses + 1
|
||||
ses[index] = entity
|
||||
seis[entity] = index
|
||||
local onAdd = system.onAdd
|
||||
if onAdd then
|
||||
onAdd(system, entity)
|
||||
end
|
||||
end
|
||||
elseif index then
|
||||
system.modified = true
|
||||
local tmpEntity = ses[#ses]
|
||||
ses[index] = tmpEntity
|
||||
seis[tmpEntity] = index
|
||||
seis[entity] = nil
|
||||
ses[#ses] = nil
|
||||
local onRemove = system.onRemove
|
||||
if onRemove then
|
||||
onRemove(system, entity)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -688,12 +675,11 @@ function tiny_manageEntities(world)
|
||||
local listIndex = entities[entity]
|
||||
if listIndex then
|
||||
-- Remove Entity from world state
|
||||
local lastEntity = entityList[#entityList]
|
||||
local lastEntity = entities[#entities]
|
||||
entities[lastEntity] = listIndex
|
||||
entities[entity] = nil
|
||||
entityList[listIndex] = lastEntity
|
||||
entityList[#entityList] = nil
|
||||
entityCount = entityCount - 1
|
||||
entities[listIndex] = lastEntity
|
||||
entities[#entities] = nil
|
||||
-- Remove from cached systems
|
||||
for j = 1, #systems do
|
||||
local system = systems[j]
|
||||
@@ -717,39 +703,6 @@ function tiny_manageEntities(world)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- Add Entities
|
||||
for i = 1, #e2a do
|
||||
local entity = e2a[i]
|
||||
if not entities[entity] then
|
||||
local listIndex = #entityList + 1
|
||||
entities[entity] = listIndex
|
||||
entityList[listIndex] = entity
|
||||
entityCount = entityCount + 1
|
||||
for j = 1, #systems do
|
||||
local system = systems[j]
|
||||
if not system.nocache then
|
||||
local ses = system.entities
|
||||
local seis = system.indices
|
||||
local filter = system.filter
|
||||
if filter and filter(system, entity) then
|
||||
system.modified = true
|
||||
local index = #ses + 1
|
||||
ses[index] = entity
|
||||
seis[entity] = index
|
||||
local onAdd = system.onAdd
|
||||
if onAdd then
|
||||
onAdd(system, entity)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
e2a[i] = nil
|
||||
end
|
||||
|
||||
-- Update Entity count
|
||||
world.entityCount = entityCount
|
||||
end
|
||||
|
||||
--- Manages Entities and Systems marked for deletion or addition. Call this
|
||||
@@ -759,7 +712,7 @@ function tiny.refresh(world)
|
||||
tiny_manageSystems(world)
|
||||
tiny_manageEntities(world)
|
||||
local systems = world.systems
|
||||
for i = 1, #systems do
|
||||
for i = #systems, 1, -1 do
|
||||
local system = systems[i]
|
||||
if system.active then
|
||||
local onModify = system.onModify
|
||||
@@ -785,10 +738,17 @@ function tiny.update(world, dt, filter)
|
||||
-- Iterate through Systems IN REVERSE ORDER
|
||||
for i = #systems, 1, -1 do
|
||||
local system = systems[i]
|
||||
local preWrap = system.preWrap
|
||||
if preWrap and system.active and
|
||||
((not filter) or filter(world, system)) then
|
||||
preWrap(system, dt)
|
||||
if system.active then
|
||||
-- Call the modify callback on Systems that have been modified.
|
||||
local onModify = system.onModify
|
||||
if onModify and system.modified then
|
||||
onModify(system, dt)
|
||||
end
|
||||
local preWrap = system.preWrap
|
||||
if preWrap and
|
||||
((not filter) or filter(world, system)) then
|
||||
preWrap(system, dt)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -797,12 +757,6 @@ function tiny.update(world, dt, filter)
|
||||
local system = systems[i]
|
||||
if system.active and ((not filter) or filter(world, system)) then
|
||||
|
||||
-- Call the modify callback on Systems that have been modified.
|
||||
local onModify = system.onModify
|
||||
if onModify and system.modified then
|
||||
onModify(system, dt)
|
||||
end
|
||||
|
||||
-- Update Systems that have an update method (most Systems)
|
||||
local update = system.update
|
||||
if update then
|
||||
@@ -811,9 +765,7 @@ function tiny.update(world, dt, filter)
|
||||
local bufferedTime = (system.bufferedTime or 0) + dt
|
||||
while bufferedTime >= interval do
|
||||
bufferedTime = bufferedTime - interval
|
||||
if update then
|
||||
update(system, interval)
|
||||
end
|
||||
update(system, interval)
|
||||
end
|
||||
system.bufferedTime = bufferedTime
|
||||
else
|
||||
@@ -839,7 +791,7 @@ end
|
||||
|
||||
--- Removes all Entities from the World.
|
||||
function tiny.clearEntities(world)
|
||||
local el = world.entityList
|
||||
local el = world.entities
|
||||
for i = 1, #el do
|
||||
tiny_removeEntity(world, el[i])
|
||||
end
|
||||
@@ -855,18 +807,12 @@ end
|
||||
|
||||
--- Gets number of Entities in the World.
|
||||
function tiny.getEntityCount(world)
|
||||
return world.entityCount
|
||||
return #world.entities
|
||||
end
|
||||
|
||||
--- Gets number of Systems in World.
|
||||
function tiny.getSystemCount(world)
|
||||
return #(world.systems)
|
||||
end
|
||||
|
||||
--- Gets the index of the System in the World.
|
||||
-- A simpler alternative is `system.index`.
|
||||
function tiny.getSystemIndex(world, system)
|
||||
return system.index
|
||||
return #world.systems
|
||||
end
|
||||
|
||||
--- Sets the index of a System in the World, and returns the old index. Changes
|
||||
@@ -905,10 +851,9 @@ worldMetaTable = {
|
||||
clearSystems = tiny.clearSystems,
|
||||
getEntityCount = tiny.getEntityCount,
|
||||
getSystemCount = tiny.getSystemCount,
|
||||
getSystemIndex = tiny.getSystemIndex,
|
||||
setSystemIndex = tiny.setSystemIndex
|
||||
},
|
||||
__tostring = function(self)
|
||||
__tostring = function()
|
||||
return "<tiny-ecs_World>"
|
||||
end
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user