Mothback/code/entities/particle.lua

133 lines
3.0 KiB
Lua

LoadedObjects.Particles = {}
Particle = class(Entity, {
type = "Particle",
display = Animation:new(animation.particle.simple),
})
function Particle:new(x,y,particle_data)
local o = Entity:new(x,y)
o.pos = {x = x, y = y}
o.speed = particle_data.speed or 0
o.direction = particle_data.direction or 0
o.sprite_rotation = particle_data.sprite_rotation or 0
o.sprite_offset = particle_data.sprite_offset or vector(0,0)
o.sprite_scale = particle_data.sprite_scale or vector(1,1)
o.sprite_tint = particle_data.sprite_tint or {1,1,1}
o.sprite_alpha = particle_data.sprite_alpha or 1
o.sprite_alpha_fade = particle_data.sprite_alpha_fade or false
o.sprite_alpha_base = o.sprite_alpha
o.sprite_flip = particle_data.sprite_flip or vector(1,1)
o.func = particle_data.func or nil
o.time = particle_data.time or nil
if o.time then
if particle_data.time_unit ~= nil
and particle_data.time_unit == "frames" then
o.time = o.time
else
o.time = o.time * game.framerate
end
end
o.timer = 0
o.vel = {
x = o.speed * math.cos(o.direction),
y = o.speed * math.sin(o.direction)
}
if particle_data.light then
local light_data = {}
light_data.radius = particle_data.light
light_data.shine_radius = particle_data.light_shine or nil
light_data.flicker = particle_data.light_flicer or nil
light_data.color = particle_data.light_color or nil
o.light = Light:new(o.pos.x,o.pos.y,light_data)
end
-- animations
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
table.remove(LoadedObjects.Entities,#LoadedObjects.Entities)
table.insert(LoadedObjects.Particles,o)
setmetatable(o, self)
self.__index = self
return o
end
function Particle:kill()
if self.light then
self.light:kill()
end
self.dead = true
end
function Particle:handleAnimation()
self.timer = self.timer + 1
if self.sprite_alpha_fade ~= false then
self.sprite_alpha = self.sprite_alpha_base*(self.time-self.timer)/self.time
end
if self.body then
self.body:animate()
self:draw(self.body)
end
end
function Particle:doLogic()
if self.func then
self:func()
end
if self.time then
if self.timer >= self.time then self:kill() end
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(
self.vel.x
)
-- vertical collision
self:moveY(
self.vel.y
)
-- final position
self:adjustLight()
end
function Particle:debug()
-- draw center CYAN
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
---------------