02.12.2012 22:44:00 • Categories: Monkey X, 2D Game SDK / Framework, Multiplatform • Tags: Monkey
Monkey: particle class
Import mojoClass Particle Global ParticleList:List<particle> = New List<particle> Field x:Float,y:Float,a:Float = 1,r:Int Field ox:Int,oy:Int Field img:Image Field dx:Float,dy:Float, da:Float Field maxdistance:Int Field speed:Float Field dir:Int Field fade:Int Field z:Int
Method Create:Particle(_x:Int, _y:Int, _img:Image, _dir:Int = 0, _speed:Float = 0, frames:Int = 1, _fade:Int = False, _autorot:Int = False, _z:Int = 0) ParticleList.AddLast(Self) z = _z x = _x y = _y ox = _x oy = _y img = _img speed = _speed dir = _dir dx = Sin(dir) speed dy = -Cos(dir) speed da = 1.0 / frames maxdistance = frames If dir And _autorot r = -dir - 180 End If
If _fade fade = True End If End Method
Method Update() x+=dx y+=dy If fade a-=da
maxdistance-=1 If maxdistance <= 0 Or a <= 0 Destroy() End If
If x > VDeviceWidth() Or x < 0 - img.Width Or y > VDeviceHeight() Or y < 0 - img.Height Destroy() End If End Method Method Draw() SetAlpha a DrawImage img,x,y,r,1,1 SetAlpha 1 End Method
Method SetPAlpha(alpha:Float) a = alpha End Method
Method SetPRotation(rot:Int) r = rot End Method
Method Destroy() ParticleList.Remove(Self) End Method
Method AddX(_x:Int) x+= _x End Method
Method AddY(_y:Int) y+= _y End Method
Method SetDirection(_dir:Int) dir = _dir End Method
Method SetSpeed(_speed:Float) speed = _speed End Method
End
Function ParticleExplosion(_x:Float, _y:Float, image:Image, n:Int, frames:Int, speed_multiplyer:Float = 0, ar:Int = False, _z:Int = 0) Local speed:Float
For Local i:Int = 1 To n Local dir:Int = Rnd(0, 359)
If speed_multiplyer speed = Rnd(0.08speed_multiplyer, 1.5speed_multiplyer) Else speed = Rnd(0.08, 1.5) End If
Local part:Particle = New Particle() part.Create(_x, _y, image, dir, speed, frames - 10 + Rnd((frames*1.40)), True, ar, _z) Next
End Function
Function EmitParticle(_x:Float, _y:Float, image:Image, frames:Int, fade:Int = True, dir:Int = 0, speed:Float = 0, ar:Int = False, _z:Int = 0)
Local part:Particle = New Particle() part.Create(_x, _y, image, dir, speed, frames, fade, ar, _z)
End Function
Function ParticleCount:Int() Return Particle.ParticleList.Count() End Function
Function ClearParticles() Particle.ParticleList.Clear() End Function
Function ClearParticlesZ(z:Int = 0) For Local delp:Particle = Eachin Particle.ParticleList If delp.z = z Particle.ParticleList.Remove(delp) End If Next End Function
Function UpdateParticlesZ(z:Int = 0, forcex:Float = 0, forcey:Float = 0) For Local UpdateP:Particle = Eachin Particle.ParticleList If UpdateP.z = z UpdateP.Update() If forcex UpdateP.x+=forcex If forcey UpdateP.y+=forcey End If Next End Function Function DrawParticlesZ(z:Int = 0) For Local UpdateP:Particle = Eachin Particle.ParticleList If UpdateP.z = z UpdateP.Draw() End If Next End Function