mirror of
https://github.com/bakpakin/tiny-ecs.git
synced 2026-07-22 07:56:52 -06:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b1c7e2ba83 | ||
|
|
6b549fcbc1 | ||
|
|
b9d994ad50 | ||
|
|
ce8ca35f7e | ||
|
|
19ae03707f |
@@ -0,0 +1,24 @@
|
||||
package = "tiny-ecs"
|
||||
version = "1.1-6"
|
||||
source = {
|
||||
url = "git://github.com/bakpakin/tiny-ecs",
|
||||
tag = "1.1-6"
|
||||
}
|
||||
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"
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -211,8 +211,8 @@ describe('tiny-ecs:', function()
|
||||
world:update(1)
|
||||
world:setSystemIndex(moveSystem, 2)
|
||||
world:update(1)
|
||||
assert.equals(2, world:getSystemIndex(moveSystem))
|
||||
assert.equals(1, world:getSystemIndex(oneTimeSystem))
|
||||
assert.equals(2, moveSystem.index)
|
||||
assert.equals(1, oneTimeSystem.index)
|
||||
end)
|
||||
|
||||
it("Sorts Entities in Sorting Systems", function()
|
||||
|
||||
@@ -23,7 +23,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
-- @author Calvin Rose
|
||||
-- @license MIT
|
||||
-- @copyright 2015
|
||||
local tiny = { _VERSION = "1.1-5" }
|
||||
local tiny = { _VERSION = "1.1-6" }
|
||||
|
||||
-- Local versions of standard lua functions
|
||||
local tinsert = table.insert
|
||||
@@ -206,12 +206,15 @@ end
|
||||
-- * The `active` flag is whether or not the System is updated automatically.
|
||||
-- Inactive Systems should be updated manually or not at all via
|
||||
-- `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.
|
||||
-- * The `interval` field is an optional field that makes Systems update at
|
||||
-- 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
|
||||
-- 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
|
||||
-- `entities` list. Most Systems can ignore this.
|
||||
-- * The `modified` flag is an indicator if the System has been modified in
|
||||
@@ -342,7 +345,8 @@ end
|
||||
local worldMetaTable
|
||||
|
||||
--- Creates a new World.
|
||||
-- Can optionally add default Systems and Entities.
|
||||
-- Can optionally add default Systems and Entities. Returns the new World along
|
||||
-- with default Entities and Systems.
|
||||
function tiny.world(...)
|
||||
local ret = {
|
||||
|
||||
@@ -361,44 +365,43 @@ function tiny.world(...)
|
||||
-- Set of Entities
|
||||
entities = {},
|
||||
|
||||
-- Number of Entities in World.
|
||||
-- Number of Entities in World
|
||||
entityCount = 0,
|
||||
|
||||
-- List of System Data. A data element is a table with 4
|
||||
-- keys: system, indices, entities, and active.
|
||||
systems = {},
|
||||
|
||||
-- Table of Systems to System Indices
|
||||
systemIndices = {}
|
||||
-- List of Systems
|
||||
systems = {}
|
||||
}
|
||||
|
||||
tiny_add(ret, ...)
|
||||
tiny_manageSystems(ret)
|
||||
tiny_manageEntities(ret)
|
||||
|
||||
return setmetatable(ret, worldMetaTable)
|
||||
return setmetatable(ret, worldMetaTable), ...
|
||||
end
|
||||
|
||||
--- Adds an Entity to the world.
|
||||
-- Also call this on Entities that have changed Components such that they
|
||||
-- match different Filters.
|
||||
-- match different Filters. Returns the Entity.
|
||||
function tiny.addEntity(world, entity)
|
||||
local e2a = world.entitiesToAdd
|
||||
e2a[#e2a + 1] = entity
|
||||
if world.entities[entity] then
|
||||
tiny_removeEntity(world, entity)
|
||||
end
|
||||
return entity
|
||||
end
|
||||
tiny_addEntity = tiny.addEntity
|
||||
|
||||
--- Adds a System to the world.
|
||||
--- Adds a System to the world. Returns the System.
|
||||
function tiny.addSystem(world, system)
|
||||
local s2a = world.systemsToAdd
|
||||
s2a[#s2a + 1] = system
|
||||
return system
|
||||
end
|
||||
tiny_addSystem = tiny.addSystem
|
||||
|
||||
--- Shortcut for adding multiple Entities and Systems to the World.
|
||||
--- Shortcut for adding multiple Entities and Systems to the World. Returns all
|
||||
-- added Entities and Systems.
|
||||
function tiny.add(world, ...)
|
||||
local obj
|
||||
for i = 1, select("#", ...) do
|
||||
@@ -411,24 +414,28 @@ function tiny.add(world, ...)
|
||||
end
|
||||
end
|
||||
end
|
||||
return ...
|
||||
end
|
||||
tiny_add = tiny.add
|
||||
|
||||
--- Removes an Entity to the World.
|
||||
--- Removes an Entity to the World. Returns the Entity.
|
||||
function tiny.removeEntity(world, entity)
|
||||
local e2r = world.entitiesToRemove
|
||||
e2r[#e2r + 1] = entity
|
||||
return entity
|
||||
end
|
||||
tiny_removeEntity = tiny.removeEntity
|
||||
|
||||
--- Removes a System from the world.
|
||||
--- Removes a System from the world. Returns the System.
|
||||
function tiny.removeSystem(world, system)
|
||||
local s2r = world.systemsToRemove
|
||||
s2r[#s2r + 1] = system
|
||||
return system
|
||||
end
|
||||
tiny_removeSystem = tiny.removeSystem
|
||||
|
||||
--- Shortcut for removing multiple Entities and Systems from the World.
|
||||
--- Shortcut for removing multiple Entities and Systems from the World. Returns
|
||||
-- all rmeove Systems and Entities
|
||||
function tiny.remove(world, ...)
|
||||
local obj
|
||||
for i = 1, select("#", ...) do
|
||||
@@ -441,6 +448,7 @@ function tiny.remove(world, ...)
|
||||
end
|
||||
end
|
||||
end
|
||||
return ...
|
||||
end
|
||||
tiny_remove = tiny.remove
|
||||
|
||||
@@ -453,7 +461,6 @@ function tiny_manageSystems(world)
|
||||
return
|
||||
end
|
||||
|
||||
local systemIndices = world.systemIndices
|
||||
local entities = world.entities
|
||||
local systems = world.systems
|
||||
local system, index, filter
|
||||
@@ -462,8 +469,8 @@ function tiny_manageSystems(world)
|
||||
-- Remove Systems
|
||||
for i = 1, #s2r do
|
||||
system = s2r[i]
|
||||
index = systemIndices[system]
|
||||
if index then
|
||||
index = system.index
|
||||
if system.world == world then
|
||||
onRemove = system.onRemove
|
||||
if onRemove then
|
||||
entityList = system.entities
|
||||
@@ -471,10 +478,9 @@ function tiny_manageSystems(world)
|
||||
onRemove(system, entityList[j])
|
||||
end
|
||||
end
|
||||
systemIndices[system] = nil
|
||||
tremove(systems, index)
|
||||
for j = index, #systems do
|
||||
systemIndices[systems[j]] = j
|
||||
systems[j].index = j
|
||||
end
|
||||
end
|
||||
s2r[i] = nil
|
||||
@@ -483,12 +489,13 @@ function tiny_manageSystems(world)
|
||||
system.world = nil
|
||||
system.entities = nil
|
||||
system.indices = nil
|
||||
system.index = nil
|
||||
end
|
||||
|
||||
-- Add Systems
|
||||
for i = 1, #s2a do
|
||||
system = s2a[i]
|
||||
if not systemIndices[system] then
|
||||
if systems[system.index] ~= system then
|
||||
entityList = {}
|
||||
entityIndices = {}
|
||||
system.entities = entityList
|
||||
@@ -499,7 +506,7 @@ function tiny_manageSystems(world)
|
||||
system.modified = true
|
||||
system.world = world
|
||||
index = #systems + 1
|
||||
systemIndices[system] = index
|
||||
system.index = index
|
||||
systems[index] = system
|
||||
|
||||
-- Try to add Entities
|
||||
@@ -673,26 +680,24 @@ function tiny.getSystemCount(world)
|
||||
return #(world.systems)
|
||||
end
|
||||
|
||||
--- Gets the index of a System in the World. Lower indexed Systems are processed
|
||||
-- before higher indexed systems.
|
||||
--- Gets the index of the System in the World.
|
||||
-- A simpler alternative is `system.index`.
|
||||
function tiny.getSystemIndex(world, system)
|
||||
return world.systemIndices[system]
|
||||
return system.index
|
||||
end
|
||||
|
||||
--- 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
|
||||
-- processed first.
|
||||
-- processed first. Returns the old system.index.
|
||||
function tiny.setSystemIndex(world, system, index)
|
||||
local systemIndices = world.systemIndices
|
||||
local oldIndex = systemIndices[system]
|
||||
local oldIndex = system.index
|
||||
local systems = world.systems
|
||||
local system = systems[oldIndex]
|
||||
|
||||
tremove(systems, oldIndex)
|
||||
tinsert(systems, index, system)
|
||||
|
||||
for i = oldIndex, index, index >= oldIndex and 1 or -1 do
|
||||
systemIndices[systems[i]] = i
|
||||
systems[i].index = i
|
||||
end
|
||||
|
||||
return oldIndex
|
||||
|
||||
Reference in New Issue
Block a user