Rename 'instance' to 'context'

This commit is contained in:
Justin van der Leij 2018-11-26 12:58:41 +01:00
parent 89a3a7fa8a
commit 26bd0ef937
8 changed files with 72 additions and 72 deletions

View file

@ -1,11 +1,11 @@
local Concord = require("lib") local Concord = require("src")
local Entity = Concord.entity local Entity = Concord.entity
local Component = Concord.component local Component = Concord.component
local System = Concord.system local System = Concord.system
local Assemblage = Concord.assemblage local Assemblage = Concord.assemblage
local Game = Concord.instance() local Game = Concord.context()
local Legs = Component(function(e, legCount) local Legs = Component(function(e, legCount)
e.legCount = legCount or 0 e.legCount = legCount or 0

View file

@ -1,6 +1,6 @@
local PATH = (...):gsub('%.[^%.]+$', '') local PATH = (...):gsub('%.[^%.]+$', '')
local Concord = require("lib") local Concord = require("src")
local C = require(PATH..".src.components") local C = require(PATH..".src.components")
local S = require(PATH..".src.systems") local S = require(PATH..".src.systems")

View file

@ -1,10 +1,10 @@
local Concord = require("lib") local Concord = require("src")
local Entity = Concord.entity local Entity = Concord.entity
local Component = Concord.component local Component = Concord.component
local System = Concord.system local System = Concord.system
local Game = Concord.instance() local Game = Concord.context()
local Position = Component(function(e, x, y) local Position = Component(function(e, x, y)
e.x = x e.x = x

View file

@ -1,17 +1,17 @@
--- Instance --- Context
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 Instance = {} local Context = {}
Instance.__index = Instance Context.__index = Context
--- Creates a new Instance. --- Creates a new Context.
-- @return The new instance -- @return The new context
function Instance.new() function Context.new()
local instance = setmetatable({ local context = setmetatable({
entities = List(), entities = List(),
systems = List(), systems = List(),
events = {}, events = {},
@ -19,35 +19,35 @@ function Instance.new()
marked = {}, marked = {},
removed = {}, removed = {},
__isInstance = true, __isContext = true,
}, Instance) }, Context)
return instance return context
end end
--- Adds an Entity to the Instance. --- Adds an Entity to the Context.
-- @param e The Entity to add -- @param e The Entity to add
-- @return self -- @return self
function Instance:addEntity(e) function Context:addEntity(e)
if not Type.isEntity(e) then if not Type.isEntity(e) then
error("bad argument #1 to 'Instance:addEntity' (Entity expected, got "..type(e)..")", 2) error("bad argument #1 to 'Context:addEntity' (Entity expected, got "..type(e)..")", 2)
end end
self:onEntityAdded(e) self:onEntityAdded(e)
e.instances:add(self) e.contexts: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 Instance. --- Marks an Entity as removed from the Context.
-- @param e The Entity to mark -- @param e The Entity to mark
-- @return self -- @return self
function Instance:removeEntity(e) function Context:removeEntity(e)
if not Type.isEntity(e) then if not Type.isEntity(e) then
error("bad argument #1 to 'Instance:removeEntity' (Entity expected, got "..type(e)..")", 2) error("bad argument #1 to 'Context: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 Instance:removeEntity(e)
return self return self
end end
function Instance:markEntity(e) function Context:markEntity(e)
if not Type.isEntity(e) then if not Type.isEntity(e) then
error("bad argument #1 to 'Instance:markEntity' (Entity expected, got "..type(e)..")", 2) error("bad argument #1 to 'Context: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 Instance:markEntity(e)
return self return self
end end
--- Checks an Entity against all the systems in the Instance. --- Checks an Entity against all the systems in the Context.
-- @param e The Entity to check -- @param e The Entity to check
-- @return self -- @return self
function Instance:checkEntity(e) function Context:checkEntity(e)
if not Type.isEntity(e) then if not Type.isEntity(e) then
error("bad argument #1 to 'Instance:checkEntity' (Entity expected, got "..type(e)..")", 2) error("bad argument #1 to 'Context: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 Instance:checkEntity(e)
return self return self
end end
--- Completely removes all marked Entities in the Instance. --- Completely removes all marked Entities in the Context.
-- @return self -- @return self
function Instance:flush() function Context: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 Instance:flush()
for i = 1, #marked do for i = 1, #marked do
local e = marked[i] local e = marked[i]
e.instances:apply() e.contexts:apply()
e.instances:checkEntity(e) e.contexts:checkEntity(e)
end end
end end
@ -102,7 +102,7 @@ function Instance:flush()
for i = 1, #removed do for i = 1, #removed do
local e = removed[i] local e = removed[i]
e.instances:remove(self) e.contexts: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 Instance:flush()
return self return self
end end
--- Adds a System to the Instance. --- Adds a System to the Context.
-- @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 Instance:addSystem(system, eventName, callback, enabled) function Context:addSystem(system, eventName, callback, enabled)
if not Type.isSystem(system) then if not Type.isSystem(system) then
error("bad argument #1 to 'Instance:addSystem' (System expected, got "..type(system)..")", 2) error("bad argument #1 to 'Context:addSystem' (System expected, got "..type(system)..")", 2)
end end
if system.__instance and system.__instance ~= self then if system.__context and system.__context ~= self then
error("System already in instance '" ..tostring(system.__instance).."'") error("System already in context '" ..tostring(system.__context).."'")
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.__instance = self system.__context = self
system:addedTo(self) system:addedTo(self)
end end
@ -169,41 +169,41 @@ function Instance:addSystem(system, eventName, callback, enabled)
return self return self
end end
--- Enables a System in the Instance. --- Enables a System in the Context.
-- @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 Instance:enableSystem(system, eventName, callback) function Context:enableSystem(system, eventName, callback)
if not Type.isSystem(system) then if not Type.isSystem(system) then
error("bad argument #1 to 'Instance:enableSystem' (System expected, got "..type(system)..")", 2) error("bad argument #1 to 'Context: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 Instance. --- Disables a System in the Context.
-- @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 Instance:disableSystem(system, eventName, callback) function Context:disableSystem(system, eventName, callback)
if not Type.isSystem(system) then if not Type.isSystem(system) then
error("bad argument #1 to 'Instance:disableSystem' (System expected, got "..type(system)..")", 2) error("bad argument #1 to 'Context: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 Instance. --- Sets a System 'enable' in the Context.
-- @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 Instance:setSystem(system, eventName, callback, enable) function Context:setSystem(system, eventName, callback, enable)
if not Type.isSystem(system) then if not Type.isSystem(system) then
error("bad argument #1 to 'Instance:setSystem' (System expected, got "..type(system)..")", 2) error("bad argument #1 to 'Context:setSystem' (System expected, got "..type(system)..")", 2)
end end
callback = callback or eventName callback = callback or eventName
@ -233,13 +233,13 @@ function Instance:setSystem(system, eventName, callback, enable)
return self return self
end end
--- Emits an Event in the Instance. --- Emits an Event in the Context.
-- @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 Instance:emit(eventName, ...) function Context:emit(eventName, ...)
if not eventName or type(eventName) ~= "string" then if not eventName or type(eventName) ~= "string" then
error("bad argument #1 to 'Instance:emit' (String expected, got "..type(eventName)..")") error("bad argument #1 to 'Context:emit' (String expected, got "..type(eventName)..")")
end end
self:flush() self:flush()
@ -259,9 +259,9 @@ function Instance:emit(eventName, ...)
return self return self
end end
--- Removes all entities from the Instance --- Removes all entities from the Context
-- @return self -- @return self
function Instance:clear() function Context: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 Instance:onEntityAdded(e) -- luacheck: ignore function Context: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 Instance:onEntityRemoved(e) -- luacheck: ignore function Context:onEntityRemoved(e) -- luacheck: ignore
end end
return setmetatable(Instance, { return setmetatable(Context, {
__call = function(_, ...) __call = function(_, ...)
return Instance.new(...) return Context.new(...)
end, end,
}) })

View file

@ -13,7 +13,7 @@ Entity.__index = Entity
function Entity.new() function Entity.new()
local e = setmetatable({ local e = setmetatable({
removed = {}, removed = {},
instances = List(), contexts = 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.instances.size do for i = 1, self.contexts.size do
self.instances:get(i):markEntity(self) self.contexts:get(i):markEntity(self)
end end
end end
@ -106,8 +106,8 @@ end
--- Destroys the Entity. --- Destroys the Entity.
-- @return self -- @return self
function Entity:destroy() function Entity:destroy()
for i = 1, self.instances.size do for i = 1, self.contexts.size do
self.instances:get(i):removeEntity(self) self.contexts:get(i):removeEntity(self)
end end
return self return self

View file

@ -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.instance = require(PATH..".instance") Concord.context = require(PATH..".context")
Concord.assemblage = require(PATH..".assemblage") Concord.assemblage = require(PATH..".assemblage")
return Concord return Concord

View file

@ -11,7 +11,7 @@ System.mt = {
local system = setmetatable({ local system = setmetatable({
__all = {}, __all = {},
__pools = {}, __pools = {},
__instance = nil, __context = nil,
__isSystem = true, __isSystem = true,
}, systemProto) }, systemProto)
@ -129,10 +129,10 @@ function System:clear()
end end
end end
--- Returns the Instance the System is in. --- Returns the Context the System is in.
-- @return The Instance -- @return The Context
function System:getInstance() function System:getContext()
return self.__instance return self.__context
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 Instance. -- Default callback for when the System is added to an Context.
-- @param instance The Instance the System was added to -- @param context The Context the System was added to
function System:addedTo(instance) -- luacheck: ignore function System:addedTo(context) -- 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

@ -14,8 +14,8 @@ function Type.isSystem(t)
return type(t) == "table" and t.__isSystem return type(t) == "table" and t.__isSystem
end end
function Type.isInstance(t) function Type.isContext(t)
return type(t) == "table" and t.__isInstance return type(t) == "table" and t.__isContext
end end
function Type.isAssemblage(t) function Type.isAssemblage(t)