It seemed some things kept getting removed because the _destroy function was the same as their new _destroy when an object that had been used before gets used again.
(Classic, an infinite loop just like my signature )
I'll check the code now and see if it doesn't close, I might be back with more code if it does...
Code: Select all
--class.lua modified from rotlove's class
local BaseClass = {timeMade=-1}
CLASSES = {BaseClase=BaseClass}
POOLS = {}
local function log(_str,file)
--asssert(_str~=5)
local file = file or _LOG_FILE
local str = _str--lume.wordwrap(inspect(_str,1),90)
file:write(str.."\n")
return true
end
local function destroyFunc(self,...)
local notActive = POOLS[self.__class].notActive local te = #notActive
local te = #notActive
--log(" pool at "..self.__id.." is being freed."..#notActive)
--assert(self.__active,self.__id)
self.__active = false
notActive[#notActive+1] = self.__id
assert(#notActive<=self.poolSize,#notActive)
local re = self._destroy(self,...)
assert(#notActive-te==1,self.class)
return re
end
local function oldNew(self,...)
local t = setmetatable({}, self)
t:__init__(...)
local kw = ...
t._kwargs = t._kwargs or t.kwargs or (kw and type(kw)=="table" and kw)
t.timeMade = love.timer.getTime()
if store then
store(t)
end
return t
end
function BaseClass:new(...)
local t = setmetatable({}, self)
t:__init__(...)
local kw = ...
t._kwargs = t._kwargs or t.kwargs or (kw and type(kw)=="table" and kw)
t.timeMade = love.timer.getTime()
if store then
store(t)
end
return t
end
function BaseClass:extend(name, t, tt)
assert(name, "Class must have a name!")
local p = t
if type(t)~="table" then
t = nil
else
p = tt
end
t = t or {}
if type(p)=="number" then
t.poolSize = p
end
t.__index = t
t.__class = name
t.class = name
t.super = self
t.__data = t.__data or t.data or {
x=0,y=0,w=0,h=0,_w=0,_h=0,source="none.png",
active=false,attack=0,life=0,passive=true
}
t.poolSize = t.poolSize or 1000
t.pool = p
local tclass = setmetatable(t, { __call = self.new, __index = self })
tclass.__lt = self.__lt
CLASSES[name] = tclass
t["is"..name] = true
if not p then
return tclass
end
t.__data.map = game.map
--log("pooling "..name)
t = tclass
local poolSize = t.poolSize
local notActive = {}
local pool = {}
if not self.__taggezd then
--so won't get repeatedly added to notActive
--from inheritance though does not work if
--super form is not called
-- t.__tagged = true
local destroy = t.destroy or null
t._destroy = destroy
t._hasD = destroy~=null
function t:destroy(...)
--log(name.." pool at "..self.__id.." is being freed.")
-- if self.isPuff then log(string.format("trying to destroy...%s",#notActive)) end
notActive[#notActive+1] = self.__id
-- if self.isPuff then log("done trying now...") end
return destroy(self,...)
end
end
--populating pool
local pp
local notYet = true
for i = 1, poolSize do
pool[i] = t:new(t.__data)
pool[i].__id = i
pool[i]:destroy()
pool[i].passive = false
pp=i
end
if t._hasD then
t._hasD = nil
t.destroy = t._destroy
end
local nonTotal = #notActive
local function new(self,...)
assert(#notActive>=1,"No space left in pool:size "..nonTotal.."!")
if self.class~=name then
return oldNew(self,...)
end
local r = table.remove(notActive,1)
--log(name.." is making at "..r)
local obj = pool[r]
obj:__init__(...)
obj.__active = true
obj.destroyed = false
obj.dead = false
if not obj.__tagged then
assert(obj.destroy~=destroyFunc,r)
obj._destroy = obj.destroy
obj.destroy = destroyFunc
obj.__tagged = true
end
local kw = ...
obj._kwargs = obj._kwargs or obj.kwargs or (kw and type(kw)=="table" and kw)
obj.timeMade = love.timer.getTime()
if store then
store(obj)
end
return obj
end
t.new = new
POOLS[name] = {pool=pool,notActive=notActive}
return tclass
end
function BaseClass:__init__()
end
function BaseClass.__lt(self,b)
return self.timeMade<b.timeMade
end
function BaseClass:__le(self,b)
return true end
function BaseClass:callSuper(func,...)
if not self.super[func] then
return
end
return self.super[func](self,...)
end
return BaseClass