Mothback/data/scripts/entities/particle.lua

83 lines
2.1 KiB
Lua

Particle = Entity:New(x,y)
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 o.direction
o.sprite_rotation = particle_data.sprite_rotation or o.sprite_rotation
o.sprite_offset = particle_data.sprite_offset or o.sprite_offset
o.sprite_scale = particle_data.sprite_scale or o.sprite_scale
o.sprite_tint = particle_data.sprite_tint or o.sprite_tint
o.sprite_alpha = particle_data.sprite_alpha or o.sprite_alpha
o.sprite_alpha_base = o.sprite_alpha
o.sprite_flip = particle_data.sprite_flip or o.sprite_flip
o.animation_active = particle_data.animation_active or false
o.time = 0.5
o.timer = 0
o.vel = {
x = o.speed * math.cos(o.direction),
y = o.speed * math.sin(o.direction)
}
if particle_data.light ~= nil then
o.lightRange = particle_data.light
o.light = CreateLight(o.pos.x,o.pos.y,o.lightRange)
end
-- animations
o.body = Animation:New(particle_data.animation)
o:centerOffset(o.body)
if not o.animation_active then
o.body.speed = 0
end
table.insert(LoadedParticles,o)
o.id = #LoadedParticles
setmetatable(o, self)
self.__index = self
return o
end
function Particle:Kill()
if self.light ~= nil then
KillLight(self.light)
end
if self.id ~= nil then
for _, e in pairs(LoadedParticles) do
if e.id > self.id then
e.id = e.id - 1
end
end
table.remove(LoadedParticles,self.id)
end
self = nil
end
function Particle:HandleAnimation()
self.body:Animate()
self.timer = self.timer + current_dt
self.sprite_alpha = self.sprite_alpha_base*(self.time-self.timer)/self.time
if self.light ~= nil then
self.light.range = self.lightRange * self.sprite_alpha/2
end
if self.sprite_alpha < 0 then self:Kill() end
self:Draw(self.body)
end
function Particle:DoPhysics()
if not self:isCollidingAt(self.pos.x + self.vel.x, self.pos.y, objects.collisions) then
self.pos.x = self.pos.x + self.vel.x
end
if not self:isCollidingAt(self.pos.x, self.pos.y + self.vel.y, objects.collisions) then
self.pos.y = self.pos.y + self.vel.y
end
end