Time for action — creating the particle emitter class

Just like with the Enemy class, we will create the emitter class in one batch:

  1. Add a new class to our script, called ParticleEmitter. Again, this one isn't extended from anything.
    Class ParticleEmitter
    
  2. Add a field for the emitter position.
    Field xp:Float
    Field yp:Float
    
  3. Now, insert a field for the time that is left for the emitter to create particles.
    Field timeLeft:Float
    
  4. The last field in the data section is the kind field. Some particles will be colored, and so we need to tell the emitter to do that.
    Field kind:Int
    
  5. The New constructor method will have the x and y positions as parameters. Also it will take the time factor and the kind flag as values.
    Method New(x:Float, y:Float, time:Int, knd:Int)
    
  6. Set the calls fields with these values.
    timeLeft = time
    xp = x
    yp = y
    kind = knd
    
  7. Close the method. Even in STRICT mode, our New method doesn't need a return value:
    End
    
  8. Insert the Update method. The parameter will be a delta value, which will later be subtracted from the time that is left:
    Method Update:Bool(delta:Float)
    
  9. Subtract the delta from timeLeft. Then, check whether timeLeft is still greater than 0, which means that the emitter has to create a particle:
    timeLeft -= delta
    If timeLeft > 0 Then
    
  10. Next, create a local image object that will be taken from the sprite atlas. Its position is the one from the emitter class:
    Local obj:ftObject = g.eng.CreateImage(g.atlas,64,32,32,32,xp,yp)
    
  11. Set the scale randomly and also its spin factor:
    obj.SetScale(Rnd(10,100)/100)
    obj.SetSpin(Rnd(-20,20))
    
  12. Get a random vector with a random distance and angle:
    Local vec:Float[] = obj.GetVector(Rnd(20,40),Rnd(360))
    
  13. If the kind flag is not 0, set the color randomly:
    If kind <> 0 Then obj.SetColor(Rnd(255),Rnd(255),Rnd(255))
    
  14. Assign the object to the game layer:
    obj.SetLayer(g.layerGame)
    
  15. Now, create a position transition to the previously determined vector and a random running time. Here, the transition ID is set so we can remove the particle once it reaches its target position:
    obj.CreateTransPos(vec[0], vec[1], Rnd(1000,3000),False,g.tidParticle)
    
  16. If no time is left, return FALSE so we can remove the particle emitter:
    Else
    Return False
    Endif
    
  17. Return TRUE so we know the emitter is still active, and close the method and the class:
    Return True
    End
    End
    

What just happened?

We created a class that lets us create an emitter with its own constructor method; we also created an update method that creates new particles, as long the emitter is active.

The last helper method

To spawn a particle emitter easily in the game, we will create a specialized method for it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.61.30