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