LoadedObjects.Particles = {} Particle = Entity:new() Particle.type = "Particle" 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 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_fade = particle_data.sprite_alpha_fade or false o.sprite_alpha_base = o.sprite_alpha o.sprite_flip = particle_data.sprite_flip or o.sprite_flip o.time = particle_data.time or nil if o.time ~= nil 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) } o.speed_increase = particle_data.speed_increase or 0 if particle_data.light ~= nil then o.light_range = particle_data.light local flicker = particle_data.light_flicker or nil local color = particle_data.light_color or nil o.light = Light:new(o.pos.x,o.pos.y,o.light_range,flicker,color) end -- animations if particle_data.animation ~= nil 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.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 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 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 ~= nil then self.body:animate() self:draw(self.body) end end function Particle:doLogic() -- adjust speed if self.speed_increase ~= 0 then self.speed = self.speed + self.speed_increase self.vel.x = self.speed * math.cos(self.direction) self.vel.y = self.speed * math.sin(self.direction) end if self.time ~= nil then if self.timer >= self.time then self:kill() 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