Last active
August 28, 2020 10:17
-
-
Save AustinEast/97e23e8f157fc43e451a24107a886c65 to your computer and use it in GitHub Desktop.
re-implementation of zerolib's ParticleEmitter.hx for HaxeFlixel, without zerolib
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package; | |
import flixel.FlxSprite; | |
import flixel.group.FlxGroup; | |
import flixel.math.FlxPoint; | |
/** | |
* A particle emitter class. | |
* | |
* Originally written for zerolib by @01010111 | |
* https://github.com/01010111/zerolib-flixel/blob/master/zero/flixel/ec/ParticleEmitter.hx | |
*/ | |
class ParticleEmitter extends FlxTypedGroup<Particle> | |
{ | |
var new_particle:Void -> Particle; | |
/** | |
* Creates a new particle emitter | |
* @param new_particle a function that returns the desired Particle | |
*/ | |
public function new(new_particle:Void -> Particle) | |
{ | |
super(); | |
this.new_particle = new_particle; | |
} | |
/** | |
* Fires a particle with given options. If none are available, it will create a new particle using the function passed in new() | |
* @param options | |
*/ | |
public function fire(options:FireOptions) | |
{ | |
while (getFirstAvailable() == null) add(new_particle()); | |
getFirstAvailable().fire(options); | |
} | |
} | |
/** | |
* A particle class | |
*/ | |
class Particle extends FlxSprite | |
{ | |
/** | |
* Creates a new particle | |
*/ | |
public function new() | |
{ | |
super(); | |
exists = false; | |
} | |
/** | |
* Fires this particle with given options | |
* @param options | |
*/ | |
public function fire(options:FireOptions) | |
{ | |
reset(options.position.x, options.position.y); | |
if (options.acceleration != null) acceleration.copyFrom(options.acceleration); | |
if (options.velocity != null) velocity.copyFrom(options.velocity); | |
if (options.animation != null) animation.play(options.animation, true); | |
} | |
} | |
typedef FireOptions = | |
{ | |
position:FlxPoint, | |
?velocity:FlxPoint, | |
?acceleration:FlxPoint, | |
?animation:String, | |
?util_amount:Float, | |
?util_color:Int, | |
?util_int:Int, | |
?util_bool:Bool | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment