LoadedObjects.Particles = {} Particle = {} Particle.type = "Particle" Particle.supertype = Entity.type Particle.display = Animation:new(animation.particle.simple) setmetatable(Particle, Entity) 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 ~= 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) } if particle_data.light ~= nil 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 ~= 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() if self.func then self:func() end if self.time 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