mirror of
https://github.com/Keyslam-Group/Concord.git
synced 2025-09-02 12:24:11 -04:00
Improve internal style
This commit is contained in:
parent
6aeb91d984
commit
f6669b2e63
7 changed files with 89 additions and 55 deletions
|
@ -1,14 +1,16 @@
|
||||||
--- Assemblage
|
--- Assemblage
|
||||||
|
|
||||||
local Assemblage = {}
|
local Assemblage = {}
|
||||||
Assemblage.__index = Assemblage
|
Assemblage.__mt = {
|
||||||
|
__index = Assemblage,
|
||||||
|
}
|
||||||
|
|
||||||
function Assemblage.new(assemble)
|
function Assemblage.new(assemble)
|
||||||
local assemblage = setmetatable({
|
local assemblage = setmetatable({
|
||||||
__assemble = assemble,
|
__assemble = assemble,
|
||||||
|
|
||||||
__isAssemblage = true,
|
__isAssemblage = true,
|
||||||
}, Assemblage)
|
}, Assemblage.__mt)
|
||||||
|
|
||||||
return assemblage
|
return assemblage
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
--- Component
|
--- Component
|
||||||
|
|
||||||
local Component = {}
|
local Component = {}
|
||||||
Component.__index = Component
|
Component.__mt = {
|
||||||
|
__index = Component,
|
||||||
|
}
|
||||||
|
|
||||||
--- Creates a new Component.
|
--- Creates a new Component.
|
||||||
-- @param populate A function that populates the Bag with values
|
-- @param populate A function that populates the Bag with values
|
||||||
|
@ -15,9 +17,11 @@ function Component.new(populate)
|
||||||
__populate = populate,
|
__populate = populate,
|
||||||
|
|
||||||
__isBaseComponent = true,
|
__isBaseComponent = true,
|
||||||
}, Component)
|
}, Component.__mt)
|
||||||
|
|
||||||
baseComponent.__mt = {__index = baseComponent}
|
baseComponent.__mt = {
|
||||||
|
__index = baseComponent
|
||||||
|
}
|
||||||
|
|
||||||
return baseComponent
|
return baseComponent
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,7 +5,9 @@ local PATH = (...):gsub('%.[^%.]+$', '')
|
||||||
local Type = require(PATH..".type")
|
local Type = require(PATH..".type")
|
||||||
|
|
||||||
local Entity = {}
|
local Entity = {}
|
||||||
Entity.__index = Entity
|
Entity.__mt = {
|
||||||
|
__index = Entity,
|
||||||
|
}
|
||||||
|
|
||||||
--- Creates and initializes a new Entity.
|
--- Creates and initializes a new Entity.
|
||||||
-- @return A new Entity
|
-- @return A new Entity
|
||||||
|
@ -19,7 +21,7 @@ function Entity.new(world)
|
||||||
__components = {},
|
__components = {},
|
||||||
|
|
||||||
__isEntity = true,
|
__isEntity = true,
|
||||||
}, Entity)
|
}, Entity.__mt)
|
||||||
|
|
||||||
if (world) then
|
if (world) then
|
||||||
world:addEntity(e)
|
world:addEntity(e)
|
||||||
|
|
52
src/list.lua
52
src/list.lua
|
@ -1,35 +1,22 @@
|
||||||
--- List
|
--- List
|
||||||
|
|
||||||
local List = {}
|
local List = {}
|
||||||
local mt = {__index = List}
|
List.__mt = {
|
||||||
|
__index = List
|
||||||
|
}
|
||||||
|
|
||||||
--- Creates a new List.
|
--- Creates a new List.
|
||||||
-- @return A new list
|
-- @return A new list
|
||||||
function List.new()
|
function List.new()
|
||||||
return setmetatable({
|
return setmetatable({
|
||||||
size = 0,
|
size = 0,
|
||||||
}, mt)
|
}, List.__mt)
|
||||||
end
|
|
||||||
|
|
||||||
--- Clears the List completely.
|
|
||||||
-- @return self
|
|
||||||
function List:clear()
|
|
||||||
for i = 1, self.size do
|
|
||||||
local o = self[i]
|
|
||||||
|
|
||||||
self[o] = nil
|
|
||||||
self[i] = nil
|
|
||||||
end
|
|
||||||
|
|
||||||
self.size = 0
|
|
||||||
|
|
||||||
return self
|
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Adds an object to the List.
|
--- Adds an object to the List.
|
||||||
-- @param obj The object to add
|
-- @param obj The object to add
|
||||||
-- @return self
|
-- @return self
|
||||||
function List:add(obj) -- obj can not be a number and also not the string "size"
|
function List:__add(obj) -- obj can not be a number and also not the string "size"
|
||||||
local size = self.size + 1
|
local size = self.size + 1
|
||||||
|
|
||||||
self[size] = obj
|
self[size] = obj
|
||||||
|
@ -42,7 +29,7 @@ end
|
||||||
--- Removes an object from the List.
|
--- Removes an object from the List.
|
||||||
-- @param obj The object to remove
|
-- @param obj The object to remove
|
||||||
-- @return self
|
-- @return self
|
||||||
function List:remove(obj)
|
function List:__remove(obj)
|
||||||
local index = self[obj]
|
local index = self[obj]
|
||||||
if not index then return end
|
if not index then return end
|
||||||
local size = self.size
|
local size = self.size
|
||||||
|
@ -64,6 +51,21 @@ function List:remove(obj)
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- Clears the List completely.
|
||||||
|
-- @return self
|
||||||
|
function List:__clear()
|
||||||
|
for i = 1, self.size do
|
||||||
|
local o = self[i]
|
||||||
|
|
||||||
|
self[o] = nil
|
||||||
|
self[i] = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
self.size = 0
|
||||||
|
|
||||||
|
return self
|
||||||
|
end
|
||||||
|
|
||||||
--- Gets if the List has the object.
|
--- Gets if the List has the object.
|
||||||
-- @param obj The object to search for
|
-- @param obj The object to search for
|
||||||
-- true if the list has the object, false otherwise
|
-- true if the list has the object, false otherwise
|
||||||
|
@ -71,6 +73,18 @@ function List:has(obj)
|
||||||
return self[obj] and true or false
|
return self[obj] and true or false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function List:get(i)
|
||||||
|
return self[i]
|
||||||
|
end
|
||||||
|
|
||||||
|
function List:indexOf(obj)
|
||||||
|
if (not self[obj]) then
|
||||||
|
error("bad argument #1 to 'List:indexOf' (Object was not in List)", 2)
|
||||||
|
end
|
||||||
|
|
||||||
|
return self[obj]
|
||||||
|
end
|
||||||
|
|
||||||
return setmetatable(List, {
|
return setmetatable(List, {
|
||||||
__call = function()
|
__call = function()
|
||||||
return List.new()
|
return List.new()
|
||||||
|
|
30
src/pool.lua
30
src/pool.lua
|
@ -5,17 +5,19 @@ local PATH = (...):gsub('%.[^%.]+$', '')
|
||||||
local List = require(PATH..".list")
|
local List = require(PATH..".list")
|
||||||
|
|
||||||
local Pool = {}
|
local Pool = {}
|
||||||
Pool.__index = Pool
|
Pool.__mt = {
|
||||||
|
__index = Pool,
|
||||||
|
}
|
||||||
|
|
||||||
--- Creates a new Pool
|
--- Creates a new Pool
|
||||||
-- @param name Identifier for the Pool.
|
-- @param name Identifier for the Pool.
|
||||||
-- @param filter Table containing the required Components
|
-- @param filter Table containing the required Components
|
||||||
-- @return The new Pool
|
-- @return The new Pool
|
||||||
function Pool.new(name, filter)
|
function Pool.new(name, filter)
|
||||||
local pool = setmetatable(List(), Pool)
|
local pool = setmetatable(List(), Pool.__mt)
|
||||||
|
|
||||||
pool.name = name
|
pool.__name = name
|
||||||
pool.filter = filter
|
pool.__filter = filter
|
||||||
|
|
||||||
pool.__isPool = true
|
pool.__isPool = true
|
||||||
|
|
||||||
|
@ -25,8 +27,8 @@ end
|
||||||
--- Checks if an Entity is eligible for the Pool.
|
--- Checks if an Entity is eligible for the Pool.
|
||||||
-- @param e The Entity to check
|
-- @param e The Entity to check
|
||||||
-- @return True if the entity is eligible, false otherwise
|
-- @return True if the entity is eligible, false otherwise
|
||||||
function Pool:eligible(e)
|
function Pool:__eligible(e)
|
||||||
for _, component in ipairs(self.filter) do
|
for _, component in ipairs(self.__filter) do
|
||||||
if not e[component] then
|
if not e[component] then
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
@ -35,16 +37,24 @@ function Pool:eligible(e)
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
function Pool:add(e)
|
function Pool:__add(e)
|
||||||
List.add(self, e)
|
List.__add(self, e)
|
||||||
self:onEntityAdded(e)
|
self:onEntityAdded(e)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Pool:remove(e)
|
function Pool:__remove(e)
|
||||||
List.remove(self, e)
|
List.__remove(self, e)
|
||||||
self:onEntityRemoved(e)
|
self:onEntityRemoved(e)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function Pool:getName()
|
||||||
|
return self.__name
|
||||||
|
end
|
||||||
|
|
||||||
|
function Pool:getFilter()
|
||||||
|
return self.__filter
|
||||||
|
end
|
||||||
|
|
||||||
function Pool:onEntityAdded(e) -- luacheck: ignore
|
function Pool:onEntityAdded(e) -- luacheck: ignore
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -31,8 +31,8 @@ System.mt = {
|
||||||
|
|
||||||
for _, filter in pairs(baseSystem.__filter) do
|
for _, filter in pairs(baseSystem.__filter) do
|
||||||
local pool = system:__buildPool(filter)
|
local pool = system:__buildPool(filter)
|
||||||
if not system[pool.name] then
|
if not system[pool.__name] then
|
||||||
system[pool.name] = pool
|
system[pool.__name] = pool
|
||||||
system.__pools[#system.__pools + 1] = pool
|
system.__pools[#system.__pools + 1] = pool
|
||||||
else
|
else
|
||||||
error("Pool with name '"..pool.name.."' already exists.")
|
error("Pool with name '"..pool.name.."' already exists.")
|
||||||
|
@ -81,12 +81,12 @@ end
|
||||||
function System:__evaluate(e)
|
function System:__evaluate(e)
|
||||||
for _, pool in ipairs(self.__pools) do
|
for _, pool in ipairs(self.__pools) do
|
||||||
local has = pool:has(e)
|
local has = pool:has(e)
|
||||||
local eligible = pool:eligible(e)
|
local eligible = pool:__eligible(e)
|
||||||
|
|
||||||
if not has and eligible then
|
if not has and eligible then
|
||||||
pool:add(e)
|
pool:__add(e)
|
||||||
elseif has and not eligible then
|
elseif has and not eligible then
|
||||||
pool:remove(e)
|
pool:__remove(e)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -98,7 +98,7 @@ end
|
||||||
function System:__remove(e)
|
function System:__remove(e)
|
||||||
for _, pool in ipairs(self.__pools) do
|
for _, pool in ipairs(self.__pools) do
|
||||||
if pool:has(e) then
|
if pool:has(e) then
|
||||||
pool:remove(e)
|
pool:__remove(e)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -107,7 +107,7 @@ end
|
||||||
|
|
||||||
function System:clear()
|
function System:clear()
|
||||||
for i = 1, #self.__pools do
|
for i = 1, #self.__pools do
|
||||||
self.__pools[i]:clear()
|
self.__pools[i]:__clear()
|
||||||
end
|
end
|
||||||
|
|
||||||
return self
|
return self
|
||||||
|
|
|
@ -9,7 +9,9 @@ local Utils = require(PATH..".utils")
|
||||||
local World = {
|
local World = {
|
||||||
ENABLE_OPTIMIZATION = true,
|
ENABLE_OPTIMIZATION = true,
|
||||||
}
|
}
|
||||||
World.__index = World
|
World.__mt = {
|
||||||
|
__index = World,
|
||||||
|
}
|
||||||
|
|
||||||
--- Creates a new World.
|
--- Creates a new World.
|
||||||
-- @return The new World
|
-- @return The new World
|
||||||
|
@ -27,7 +29,7 @@ function World.new()
|
||||||
__systemLookup = {},
|
__systemLookup = {},
|
||||||
|
|
||||||
__isWorld = true,
|
__isWorld = true,
|
||||||
}, World)
|
}, World.__mt)
|
||||||
|
|
||||||
-- Optimization: We deep copy the World class into our instance of a world.
|
-- Optimization: We deep copy the World class into our instance of a world.
|
||||||
-- This grants slightly faster access times at the cost of memory.
|
-- This grants slightly faster access times at the cost of memory.
|
||||||
|
@ -52,7 +54,7 @@ function World:addEntity(e)
|
||||||
end
|
end
|
||||||
|
|
||||||
e.__world = self
|
e.__world = self
|
||||||
self.__added:add(e)
|
self.__added:__add(e)
|
||||||
|
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
@ -65,14 +67,14 @@ function World:removeEntity(e)
|
||||||
error("bad argument #1 to 'World:removeEntity' (Entity expected, got "..type(e)..")", 2)
|
error("bad argument #1 to 'World:removeEntity' (Entity expected, got "..type(e)..")", 2)
|
||||||
end
|
end
|
||||||
|
|
||||||
self.__removed:add(e)
|
self.__removed:__add(e)
|
||||||
|
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
function World:__dirtyEntity(e)
|
function World:__dirtyEntity(e)
|
||||||
if not self.__dirty:has(e) then
|
if not self.__dirty:has(e) then
|
||||||
self.__dirty:add(e)
|
self.__dirty:__add(e)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -95,7 +97,7 @@ function World:__flush()
|
||||||
for i = 1, self.__backAdded.size do
|
for i = 1, self.__backAdded.size do
|
||||||
e = self.__backAdded[i]
|
e = self.__backAdded[i]
|
||||||
|
|
||||||
self.entities:add(e)
|
self.entities:__add(e)
|
||||||
|
|
||||||
for j = 1, self.systems.size do
|
for j = 1, self.systems.size do
|
||||||
self.systems[j]:__evaluate(e)
|
self.systems[j]:__evaluate(e)
|
||||||
|
@ -103,14 +105,14 @@ function World:__flush()
|
||||||
|
|
||||||
self:onEntityAdded(e)
|
self:onEntityAdded(e)
|
||||||
end
|
end
|
||||||
self.__backAdded:clear()
|
self.__backAdded:__clear()
|
||||||
|
|
||||||
-- Process removed entities
|
-- Process removed entities
|
||||||
for i = 1, self.__backRemoved.size do
|
for i = 1, self.__backRemoved.size do
|
||||||
e = self.__backRemoved[i]
|
e = self.__backRemoved[i]
|
||||||
|
|
||||||
e.__world = nil
|
e.__world = nil
|
||||||
self.entities:remove(e)
|
self.entities:__remove(e)
|
||||||
|
|
||||||
for j = 1, self.systems.size do
|
for j = 1, self.systems.size do
|
||||||
self.systems[j]:__remove(e)
|
self.systems[j]:__remove(e)
|
||||||
|
@ -118,7 +120,7 @@ function World:__flush()
|
||||||
|
|
||||||
self:onEntityRemoved(e)
|
self:onEntityRemoved(e)
|
||||||
end
|
end
|
||||||
self.__backRemoved:clear()
|
self.__backRemoved:__clear()
|
||||||
|
|
||||||
-- Process dirty entities
|
-- Process dirty entities
|
||||||
for i = 1, self.__backDirty.size do
|
for i = 1, self.__backDirty.size do
|
||||||
|
@ -128,7 +130,7 @@ function World:__flush()
|
||||||
self.systems[j]:__evaluate(e)
|
self.systems[j]:__evaluate(e)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
self.__backDirty:clear()
|
self.__backDirty:__clear()
|
||||||
|
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
@ -148,7 +150,7 @@ function World:addSystem(baseSystem)
|
||||||
local system = baseSystem(self)
|
local system = baseSystem(self)
|
||||||
|
|
||||||
self.__systemLookup[baseSystem] = system
|
self.__systemLookup[baseSystem] = system
|
||||||
self.systems:add(system)
|
self.systems:__add(system)
|
||||||
|
|
||||||
for callbackName, callback in pairs(baseSystem) do
|
for callbackName, callback in pairs(baseSystem) do
|
||||||
-- Skip callback if its blacklisted
|
-- Skip callback if its blacklisted
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue