Rename 'context' to 'world'

This commit is contained in:
Justin van der Leij 2018-11-29 22:04:50 +01:00
parent 26bd0ef937
commit 83162ec02c
5 changed files with 69 additions and 72 deletions

View file

@ -4,7 +4,7 @@ local Entity = Concord.entity
local Component = Concord.component local Component = Concord.component
local System = Concord.system local System = Concord.system
local Game = Concord.context() local Game = Concord.world()
local Position = Component(function(e, x, y) local Position = Component(function(e, x, y)
e.x = x e.x = x
@ -48,8 +48,6 @@ function CircleRenderer:flush()
for _, e in ipairs(self.pool.removed) do for _, e in ipairs(self.pool.removed) do
print(tostring(e).. " was removed from my pool D:") print(tostring(e).. " was removed from my pool D:")
end end
self:clear()
end end
function CircleRenderer:draw() function CircleRenderer:draw()

View file

@ -13,7 +13,7 @@ Entity.__index = Entity
function Entity.new() function Entity.new()
local e = setmetatable({ local e = setmetatable({
removed = {}, removed = {},
contexts = List(), worlds = List(),
__isEntity = true, __isEntity = true,
}, Entity) }, Entity)
@ -86,8 +86,8 @@ function Entity:assemble(assemblage, ...)
end end
function Entity:mark() function Entity:mark()
for i = 1, self.contexts.size do for i = 1, self.worlds.size do
self.contexts:get(i):markEntity(self) self.worlds:get(i):markEntity(self)
end end
end end
@ -96,7 +96,6 @@ function Entity:apply()
local component = self.removed[i] local component = self.removed[i]
self[component] = nil self[component] = nil
self.removed[i] = nil self.removed[i] = nil
end end
@ -106,8 +105,8 @@ end
--- Destroys the Entity. --- Destroys the Entity.
-- @return self -- @return self
function Entity:destroy() function Entity:destroy()
for i = 1, self.contexts.size do for i = 1, self.worlds.size do
self.contexts:get(i):removeEntity(self) self.worlds:get(i):removeEntity(self)
end end
return self return self

View file

@ -3,7 +3,7 @@
local PATH = (...):gsub('%.init$', '') local PATH = (...):gsub('%.init$', '')
local Concord = { local Concord = {
_VERSION = "1.0", _VERSION = "2.0 Beta",
_DESCRIPTION = "A feature-complete ECS library", _DESCRIPTION = "A feature-complete ECS library",
_LICENCE = [[ _LICENCE = [[
MIT LICENSE MIT LICENSE
@ -34,7 +34,7 @@ local Concord = {
Concord.entity = require(PATH..".entity") Concord.entity = require(PATH..".entity")
Concord.component = require(PATH..".component") Concord.component = require(PATH..".component")
Concord.system = require(PATH..".system") Concord.system = require(PATH..".system")
Concord.context = require(PATH..".context") Concord.world = require(PATH..".world")
Concord.assemblage = require(PATH..".assemblage") Concord.assemblage = require(PATH..".assemblage")
return Concord return Concord

View file

@ -9,9 +9,9 @@ System.mt = {
__index = System, __index = System,
__call = function(systemProto, ...) __call = function(systemProto, ...)
local system = setmetatable({ local system = setmetatable({
__all = {}, __all = {},
__pools = {}, __pools = {},
__context = nil, __world = nil,
__isSystem = true, __isSystem = true,
}, systemProto) }, systemProto)
@ -129,10 +129,10 @@ function System:clear()
end end
end end
--- Returns the Context the System is in. --- Returns the World the System is in.
-- @return The Context -- @return The world the system is in
function System:getContext() function System:getWorld()
return self.__context return self.__world
end end
--- Default callback for system initialization. --- Default callback for system initialization.
@ -140,9 +140,9 @@ end
function System:init(...) -- luacheck: ignore function System:init(...) -- luacheck: ignore
end end
-- Default callback for when the System is added to an Context. -- Default callback for when the System is added to an World.
-- @param context The Context the System was added to -- @param world The World the System was added to
function System:addedTo(context) -- luacheck: ignore function System:addedTo(World) -- luacheck: ignore
end end
-- Default callback for when a System's callback is enabled. -- Default callback for when a System's callback is enabled.

View file

@ -1,17 +1,17 @@
--- Context --- World
local PATH = (...):gsub('%.[^%.]+$', '') local PATH = (...):gsub('%.[^%.]+$', '')
local Type = require(PATH..".type") local Type = require(PATH..".type")
local List = require(PATH..".list") local List = require(PATH..".list")
local Context = {} local World = {}
Context.__index = Context World.__index = World
--- Creates a new Context. --- Creates a new World.
-- @return The new context -- @return The new World
function Context.new() function World.new()
local context = setmetatable({ local world = setmetatable({
entities = List(), entities = List(),
systems = List(), systems = List(),
events = {}, events = {},
@ -19,35 +19,35 @@ function Context.new()
marked = {}, marked = {},
removed = {}, removed = {},
__isContext = true, __isWorld = true,
}, Context) }, World)
return context return world
end end
--- Adds an Entity to the Context. --- Adds an Entity to the World.
-- @param e The Entity to add -- @param e The Entity to add
-- @return self -- @return self
function Context:addEntity(e) function World:addEntity(e)
if not Type.isEntity(e) then if not Type.isEntity(e) then
error("bad argument #1 to 'Context:addEntity' (Entity expected, got "..type(e)..")", 2) error("bad argument #1 to 'World:addEntity' (Entity expected, got "..type(e)..")", 2)
end end
self:onEntityAdded(e) self:onEntityAdded(e)
e.contexts:add(self) e.worlds:add(self)
self.entities:add(e) self.entities:add(e)
self:checkEntity(e) self:checkEntity(e)
return self return self
end end
--- Marks an Entity as removed from the Context. --- Marks an Entity as removed from the World.
-- @param e The Entity to mark -- @param e The Entity to mark
-- @return self -- @return self
function Context:removeEntity(e) function World:removeEntity(e)
if not Type.isEntity(e) then if not Type.isEntity(e) then
error("bad argument #1 to 'Context:removeEntity' (Entity expected, got "..type(e)..")", 2) error("bad argument #1 to 'World:removeEntity' (Entity expected, got "..type(e)..")", 2)
end end
self.removed[#self.removed + 1] = e self.removed[#self.removed + 1] = e
@ -55,9 +55,9 @@ function Context:removeEntity(e)
return self return self
end end
function Context:markEntity(e) function World:markEntity(e)
if not Type.isEntity(e) then if not Type.isEntity(e) then
error("bad argument #1 to 'Context:markEntity' (Entity expected, got "..type(e)..")", 2) error("bad argument #1 to 'World:markEntity' (Entity expected, got "..type(e)..")", 2)
end end
self.marked[#self.marked + 1] = e self.marked[#self.marked + 1] = e
@ -65,12 +65,12 @@ function Context:markEntity(e)
return self return self
end end
--- Checks an Entity against all the systems in the Context. --- Checks an Entity against all the systems in the World.
-- @param e The Entity to check -- @param e The Entity to check
-- @return self -- @return self
function Context:checkEntity(e) function World:checkEntity(e)
if not Type.isEntity(e) then if not Type.isEntity(e) then
error("bad argument #1 to 'Context:checkEntity' (Entity expected, got "..type(e)..")", 2) error("bad argument #1 to 'World:checkEntity' (Entity expected, got "..type(e)..")", 2)
end end
for i = 1, self.systems.size do for i = 1, self.systems.size do
@ -80,9 +80,9 @@ function Context:checkEntity(e)
return self return self
end end
--- Completely removes all marked Entities in the Context. --- Completely removes all marked Entities in the World.
-- @return self -- @return self
function Context:flush() function World:flush()
while #self.marked > 0 do while #self.marked > 0 do
local marked = self.removed local marked = self.removed
self.removed = {} self.removed = {}
@ -90,8 +90,8 @@ function Context:flush()
for i = 1, #marked do for i = 1, #marked do
local e = marked[i] local e = marked[i]
e.contexts:apply() e.Worlds:apply()
e.contexts:checkEntity(e) e.Worlds:checkEntity(e)
end end
end end
@ -102,7 +102,7 @@ function Context:flush()
for i = 1, #removed do for i = 1, #removed do
local e = removed[i] local e = removed[i]
e.contexts:remove(self) e.worlds:remove(self)
self.entities:remove(e) self.entities:remove(e)
for j = 1, self.systems.size do for j = 1, self.systems.size do
@ -122,24 +122,24 @@ function Context:flush()
return self return self
end end
--- Adds a System to the Context. --- Adds a System to the World.
-- @param system The System to add -- @param system The System to add
-- @param eventName The Event to register to -- @param eventName The Event to register to
-- @param callback The function name to call. Defaults to eventName -- @param callback The function name to call. Defaults to eventName
-- @param enabled If the system is enabled. Defaults to true -- @param enabled If the system is enabled. Defaults to true
-- @return self -- @return self
function Context:addSystem(system, eventName, callback, enabled) function World:addSystem(system, eventName, callback, enabled)
if not Type.isSystem(system) then if not Type.isSystem(system) then
error("bad argument #1 to 'Context:addSystem' (System expected, got "..type(system)..")", 2) error("bad argument #1 to 'World:addSystem' (System expected, got "..type(system)..")", 2)
end end
if system.__context and system.__context ~= self then if system.__World and system.__World ~= self then
error("System already in context '" ..tostring(system.__context).."'") error("System already in World '" ..tostring(system.__World).."'")
end end
if not self.systems:has(system) then if not self.systems:has(system) then
self.systems:add(system) self.systems:add(system)
system.__context = self system.__World = self
system:addedTo(self) system:addedTo(self)
end end
@ -169,41 +169,41 @@ function Context:addSystem(system, eventName, callback, enabled)
return self return self
end end
--- Enables a System in the Context. --- Enables a System in the World.
-- @param system The System to enable -- @param system The System to enable
-- @param eventName The Event it was registered to -- @param eventName The Event it was registered to
-- @param callback The callback it was registered with. Defaults to eventName -- @param callback The callback it was registered with. Defaults to eventName
-- @return self -- @return self
function Context:enableSystem(system, eventName, callback) function World:enableSystem(system, eventName, callback)
if not Type.isSystem(system) then if not Type.isSystem(system) then
error("bad argument #1 to 'Context:enableSystem' (System expected, got "..type(system)..")", 2) error("bad argument #1 to 'World:enableSystem' (System expected, got "..type(system)..")", 2)
end end
return self:setSystem(system, eventName, callback, true) return self:setSystem(system, eventName, callback, true)
end end
--- Disables a System in the Context. --- Disables a System in the World.
-- @param system The System to disable -- @param system The System to disable
-- @param eventName The Event it was registered to -- @param eventName The Event it was registered to
-- @param callback The callback it was registered with. Defaults to eventName -- @param callback The callback it was registered with. Defaults to eventName
-- @return self -- @return self
function Context:disableSystem(system, eventName, callback) function World:disableSystem(system, eventName, callback)
if not Type.isSystem(system) then if not Type.isSystem(system) then
error("bad argument #1 to 'Context:disableSystem' (System expected, got "..type(system)..")", 2) error("bad argument #1 to 'World:disableSystem' (System expected, got "..type(system)..")", 2)
end end
return self:setSystem(system, eventName, callback, false) return self:setSystem(system, eventName, callback, false)
end end
--- Sets a System 'enable' in the Context. --- Sets a System 'enable' in the World.
-- @param system The System to set -- @param system The System to set
-- @param eventName The Event it was registered to -- @param eventName The Event it was registered to
-- @param callback The callback it was registered with. Defaults to eventName -- @param callback The callback it was registered with. Defaults to eventName
-- @param enable The state to set it to -- @param enable The state to set it to
-- @return self -- @return self
function Context:setSystem(system, eventName, callback, enable) function World:setSystem(system, eventName, callback, enable)
if not Type.isSystem(system) then if not Type.isSystem(system) then
error("bad argument #1 to 'Context:setSystem' (System expected, got "..type(system)..")", 2) error("bad argument #1 to 'World:setSystem' (System expected, got "..type(system)..")", 2)
end end
callback = callback or eventName callback = callback or eventName
@ -233,13 +233,13 @@ function Context:setSystem(system, eventName, callback, enable)
return self return self
end end
--- Emits an Event in the Context. --- Emits an Event in the World.
-- @param eventName The Event that should be emitted -- @param eventName The Event that should be emitted
-- @param ... Parameters passed to listeners -- @param ... Parameters passed to listeners
-- @return self -- @return self
function Context:emit(eventName, ...) function World:emit(eventName, ...)
if not eventName or type(eventName) ~= "string" then if not eventName or type(eventName) ~= "string" then
error("bad argument #1 to 'Context:emit' (String expected, got "..type(eventName)..")") error("bad argument #1 to 'World:emit' (String expected, got "..type(eventName)..")")
end end
self:flush() self:flush()
@ -259,9 +259,9 @@ function Context:emit(eventName, ...)
return self return self
end end
--- Removes all entities from the Context --- Removes all entities from the World
-- @return self -- @return self
function Context:clear() function World:clear()
for i = 1, self.entities.size do for i = 1, self.entities.size do
self.entities:get(i):destroy() self.entities:get(i):destroy()
end end
@ -273,16 +273,16 @@ end
--- Default callback for adding an Entity. --- Default callback for adding an Entity.
-- @param e The Entity that was added -- @param e The Entity that was added
function Context:onEntityAdded(e) -- luacheck: ignore function World:onEntityAdded(e) -- luacheck: ignore
end end
--- Default callback for removing an Entity. --- Default callback for removing an Entity.
-- @param e The Entity that was removed -- @param e The Entity that was removed
function Context:onEntityRemoved(e) -- luacheck: ignore function World:onEntityRemoved(e) -- luacheck: ignore
end end
return setmetatable(Context, { return setmetatable(World, {
__call = function(_, ...) __call = function(_, ...)
return Context.new(...) return World.new(...)
end, end,
}) })