iPFXemitter.lua
[lua]–Emitter.Lua
–Particle System Emitter Class
module(…, package.seeall)
printDebug(“Loading Emitter.lua”)
require(“iPFXparticle”)
require(“iPFXpaths”)
function new(self,parent,params)
params = params or {}
printDebug(system.getTimer())
printDebug(table.show(params,“New Emitter Params”))
local emitterImg = ‘blank.png’
if params.debug then
emitterImg = ‘crosshair.png’
end
local emitter = display.newImage(emitterImg,50,50)
–print(“LLLLL”,self.x)
emitter.x = params.x or parent.x
emitter.y = params.y or parent.y
emitter.particle = params.particle or {}
–print(table.show(params))
emitter.particles = {}
emitter.particleCount = 0
emitter.maxParticles = params.maxParticles or 500
printDebug("MAXPARTICLES = ",emitter.maxParticles)
emitter.lifeSpan = params.lifeSpan or 3000
emitter.rate = params.rate or 20
emitter.bounds = params.bounds or {y=1,x=1}
emitter.path = iPFXemitterPaths[params.p] or nil
local function followPath()
local j = 0
local t = emitter.lifeSpan/(#emitter.path)
printDebug(t)
for i,v in pairs(emitter.path) do
printDebug(i,v.x,v.y)
transition.to(emitter,{x=emitter.x + v.x,y=emitter.y + v.y,time=t,delay=t*j})
j = j + 1
end
end
if emitter.path then followPath() end
function emitter:getX()
return emitter.x
end
emitter.incParticleCount = function ()
emitter.particleCount = emitter.particleCount + 1
return emitter.particleCount
end
emitter.getParticleCount = function ()
return emitter.particleCount
end
function emitter:emit()
local particleCount = self.getParticleCount()
self.i = self.i+1
if self.maxParticles > self.particleCount then
self.particle.path = self.particle.path or defaultPath
local p = iPFXparticle:new(self,self.particle)
–incParticleCount()
table.insert(self.particles,p)
print(#self.particles)
self.incParticleCount()
end
end
function emitter:removeParticle(particle)
for k,v in ipairs(self.particles) do
if v == particle
then table.remove(self.particles,k)
return true
end
end
end
emitter.lifeTimer = timer.performWithDelay(emitter.lifeSpan,function() emitter:destroy() end,1)
emitter.rateTimer = timer.performWithDelay(emitter.rate,function() emitter:emit() end,(emitter.lifeSpan/emitter.rate))
emitter.i = 0
function emitter:destroy()
timer.cancel(emitter.rateTimer)
printDebug(“Emitter Destroyed”,system.getTimer())
self = nil
end
printDebug(“Emitter Created”,system.getTimer())
emitter:emit()
return emitter
end
function staticEmitter(self,params)
staticEmitter = emitter:new(params)
return staticEmitter
end
[/lua]
iPFXparticle.lua
[lua]–Particle.Lua
–Particle System Particle Class
module(…, package.seeall)
printDebug(“Loading Particle.lua”)
function new(self,parent,params)
local image = params.image or “1.png”
local particle = display.newImage(image,30,30)
particle.parent = parent
particle.onDeath = params.onDeath
–print(table.show(params))
printDebug("Particle Created, Number ",iPFXparticleID, “Particle Count For This Emitter”,parent.getParticleCount())
iPFXparticleID = iPFXparticleID +1
randomize()
particle.alpha = params.alpha or math.random(100)/100
local scale = math.random(100)/100 +.3
particle:scale(scale,scale)
particle.i = 1
particle.x = parent.x
particle.y = parent.y
particle.v = params.v or 5
particle.rotation = params.rotation or math.random(360)
math.randomseed(system.getTimer())
transition.to(particle,{time=300,alpha=0.55})
particle.d = params.d or math.random(360)
particle.fadeStartTime = params.fadeStartTime or 500
–print(params.path)
particle.path = params.path or defaultPath
particle.lifeSpan = params.lifeSpan or 1000
particle.rate = params.rate or 100
transition.to(particle,{delay=particle.fadeStartTime,time=particle.lifeSpan-particle.fadeStartTime,alpha=0})
particle.lifeTimer = timer.performWithDelay(particle.lifeSpan,function() particle:destroy() end,1)
particle.pathTimer = timer.performWithDelay(particle.rate,function() particle:followPath() end,particle.lifeSpan/particle.rate)
function particle:followPath()
local params = self.path(self.x,self.y,self.i,self.d,self.v)
self.i = params.i
–print(‘followPath’,params.x,params.y)
transition.to(self,{time=100,x=params.x,rotation=params.rotation,y=params.y,onComplete=function() end})
–transition.to(self,{time=1000,x=500,y=500})
end
function particle:destroy()
if self.onDeath then
–print(“Callback”)
self.onDeath(self)
end
printDebug(“Particle Destroyed”,system.getTimer())
timer.cancel(self.pathTimer)
decParticleCount()
parent.particleCount = parent.particleCount - 1
parent:removeParticle(self)
self:removeSelf()
self = nil
end
printDebug(“Particle Created”,system.getTimer())
return particle
end
[/lua] [import]uid: 34945 topic_id: 9078 reply_id: 47389[/import]