diff --git a/code/entities/cursed_book.lua b/code/entities/cursed_book.lua index ccbb814..6b32109 100644 --- a/code/entities/cursed_book.lua +++ b/code/entities/cursed_book.lua @@ -36,8 +36,6 @@ function CursedBook:new(x,y) light_data.color = nil o.light = Light:new(o.pos.x,o.pos.y,light_data) - o:id() - setmetatable(o, self) self.__index = self return o diff --git a/code/entities/decoration.lua b/code/entities/decoration.lua index 951d139..ab07eee 100644 --- a/code/entities/decoration.lua +++ b/code/entities/decoration.lua @@ -19,9 +19,8 @@ function Decoration:new(x,y,animation,light_data) o.light = Light:new(o.pos.x,o.pos.y,light_data) end - o:id() - setmetatable(o, self) + self.__index = self return o end diff --git a/code/entities/fairy.lua b/code/entities/fairy.lua index e1e88af..5766ba0 100644 --- a/code/entities/fairy.lua +++ b/code/entities/fairy.lua @@ -31,8 +31,6 @@ function Fairy:new(x,y) o.particle_timer = 0 o.particle_time = 5 - o:id() - setmetatable(o, self) self.__index = self return o diff --git a/code/entities/hook_anchor.lua b/code/entities/hook_anchor.lua index 68087e3..0382a9d 100644 --- a/code/entities/hook_anchor.lua +++ b/code/entities/hook_anchor.lua @@ -14,8 +14,6 @@ function HookAnchor:new(x,y,hook_distance) o:centerOffset(o.body) o:createBox(o.body) - o:id() - setmetatable(o, self) self.__index = self return o diff --git a/code/entities/kupo.lua b/code/entities/kupo.lua index b9f101d..e6aaf8b 100644 --- a/code/entities/kupo.lua +++ b/code/entities/kupo.lua @@ -36,8 +36,6 @@ function Kupo:new(x,y) light_data.color = nil o.light = Light:new(o.pos.x,o.pos.y,light_data) - o:id() - setmetatable(o, self) self.__index = self return o diff --git a/code/entities/particle.lua b/code/entities/particle.lua index 9058385..128848c 100644 --- a/code/entities/particle.lua +++ b/code/entities/particle.lua @@ -22,7 +22,7 @@ function Particle:new(x,y,particle_data) o.func = particle_data.func or nil o.time = particle_data.time or nil - if o.time ~= nil then + if o.time then if particle_data.time_unit ~= nil and particle_data.time_unit == "frames" then o.time = o.time @@ -37,7 +37,7 @@ function Particle:new(x,y,particle_data) y = o.speed * math.sin(o.direction) } - if particle_data.light ~= nil then + if particle_data.light then local light_data = {} light_data.radius = particle_data.light light_data.shine_radius = particle_data.light_shine or nil @@ -47,34 +47,24 @@ function Particle:new(x,y,particle_data) end -- animations - if particle_data.animation ~= nil then + if particle_data.animation then o.body = Animation:new(particle_data.animation,particle_data.animation_speed) o:centerOffset(o.body) o:createBox(o.body) end - -- particle id handled differently from other entities + table.remove(LoadedObjects.Entities,#LoadedObjects.Entities) table.insert(LoadedObjects.Particles,o) - o.id = #LoadedObjects.Particles - setmetatable(o, self) self.__index = self return o end function Particle:kill() - if self.light ~= nil then + if self.light then self.light:kill() end - if self.id ~= nil then - for _, e in pairs(LoadedObjects.Particles) do - if e.id > self.id then - e.id = e.id - 1 - end - end - table.remove(LoadedObjects.Particles,self.id) - end - self = nil + self.dead = true end function Particle:handleAnimation() @@ -84,7 +74,7 @@ function Particle:handleAnimation() self.sprite_alpha = self.sprite_alpha_base*(self.time-self.timer)/self.time end - if self.body ~= nil then + if self.body then self.body:animate() self:draw(self.body) end @@ -100,6 +90,15 @@ function Particle:doLogic() end end +function cleanDeadParticles() + for i=1, #LoadedObjects.Particles do + part = LoadedObjects.Particles[i] + if part.kill then + table.remove(LoadedObjects.Particles,i) + end + end +end + function Particle:doPhysics() -- horizontal collision self:moveX( @@ -118,3 +117,16 @@ function Particle:debug() love.graphics.setColor(0,1,1) love.graphics.circle("fill", -Camera.pos.x + self.pos.x, -Camera.pos.y + self.pos.y, 1) end + +--------------- + +function cleanDeadParticles() + for i=1, #LoadedObjects.Particles do + part = LoadedObjects.Particles[i] + if part and part.dead then + table.remove(LoadedObjects.Particles,i) + end + end +end + +--------------- diff --git a/code/entities/player.lua b/code/entities/player.lua index b2ec7e9..9f3a7a5 100644 --- a/code/entities/player.lua +++ b/code/entities/player.lua @@ -80,8 +80,6 @@ function Player:new(x,y) light_data.color = nil o.light = Light:new(o.pos.x,o.pos.y,light_data) - o:id() - setmetatable(o, self) self.__index = self return o diff --git a/code/entity.lua b/code/entity.lua index 83b8dcc..01855ec 100644 --- a/code/entity.lua +++ b/code/entity.lua @@ -24,15 +24,12 @@ function Entity:new(x,y) o.sprite_alpha = 1 o.sprite_flip = { x = 1, y = 1} + table.insert(LoadedObjects.Entities,o) setmetatable(o, self) + self.__index = self return o end -function Entity:id() - table.insert(LoadedObjects.Entities,self) - self.id = #LoadedObjects.Entities -end - function Entity:checkNearest(type,maxdistance) local return_entity = nil local shortest = -1 @@ -64,9 +61,6 @@ function Entity:checkNearest(type,maxdistance) return return_entity end -function Entity:doLogic() -end - function Entity:moveX(amount, func) self.move_remainder.x = self.move_remainder.x + amount local move = math.round(self.move_remainder.x) @@ -130,15 +124,7 @@ function Entity:kill() if self.light ~= nil then self.light:kill() end - if self.id ~= nil then - table.remove(LoadedObjects.Entities,self.id) - for _, e in pairs(LoadedObjects.Entities) do - if e.id > self.id then - e.id = e.id - 1 - end - end - end - self = nil + self.dead = true end function Entity:checkVisionLine(entity,range) @@ -306,6 +292,9 @@ function Entity:debug() end end +function Entity:doLogic() +end + function Entity:doPhysics() end @@ -315,6 +304,19 @@ end function Entity:drawBackground() end +--------------- + +function cleanDeadEntities() + for i=1, #LoadedObjects.Entities do + enty = LoadedObjects.Entities[i] + if enty.dead then + table.remove(LoadedObjects.Entities,i) + end + end +end + +--------------- + require "code/entities/kupo" require "code/entities/arrow" require "code/entities/decoration" diff --git a/code/game.lua b/code/game.lua index 13d4b74..6c2d353 100644 --- a/code/game.lua +++ b/code/game.lua @@ -68,6 +68,10 @@ function stepGame() Demo:startPlayback() end end + + cleanDeadParticles() + cleanDeadEntities() + cleanDeadLights() end function drawGame() diff --git a/code/lights.lua b/code/lights.lua index d0195e9..314268b 100644 --- a/code/lights.lua +++ b/code/lights.lua @@ -1,4 +1,6 @@ -Light = {} +Light = class(nil, { + type = "Light" +}) LoadedObjects.Lights = {} function Light:new(x,y,data) @@ -17,26 +19,12 @@ function Light:new(x,y,data) o.flicker_time = 60/12 o.flicker_timer = 0 - table.insert(LoadedObjects.Lights,o) - o.id = #LoadedObjects.Lights - setmetatable(o, self) self.__index = self + table.insert(LoadedObjects.Lights,o) return o end -function Light:kill() - if self.id ~= nil then - table.remove(LoadedObjects.Lights,self.id) - for _, e in pairs(LoadedObjects.Lights) do - if e.id > self.id then - e.id = e.id - 1 - end - end - end - self = nil -end - function Light:flicker() self.flicker_timer = self.flicker_timer + 1 @@ -82,3 +70,20 @@ function Light:drawShine() love.graphics.setShader() end end + +function Light:kill() + self.dead = true +end + +--------------- + +function cleanDeadLights() + for i=1, #LoadedObjects.Entities do + enty = LoadedObjects.Entities[i] + if enty.dead then + table.remove(LoadedObjects.Entities,i) + end + end +end + +---------------