Browse Source

make h2d.Particles Drawable

ncannasse 8 years ago
parent
commit
cc1dbf1a57
2 changed files with 19 additions and 3 deletions
  1. 13 2
      h2d/Particles.hx
  2. 6 1
      h2d/SpriteBatch.hx

+ 13 - 2
h2d/Particles.hx

@@ -79,7 +79,10 @@ private class Particle extends h2d.SpriteBatch.BatchElement {
 		y += vy * et;
 		life += et;
 
-		rotation += vr * et;
+		if( group.rotAuto )
+			rotation = Math.atan2(vy, vx) + life * vr + group.rotInit;
+		else
+			rotation += vr * et;
 		scale = scaleX * Math.pow(1 + vs, et);
 
 		var t = life / maxLife;
@@ -160,6 +163,7 @@ class ParticleGroup {
 	public var rotInit(default, set) : Float	= 0;
 	public var rotSpeed(default, set) : Float	= 0;
 	public var rotSpeedRand(default, set):Float = 0;
+	public var rotAuto							= false;
 
 	public var fadeIn : Float					= 0.2;
 	public var fadeOut : Float					= 0.8;
@@ -205,6 +209,7 @@ class ParticleGroup {
 	public function new(p) {
 		this.parts = p;
 		batch = new SpriteBatch(null, p);
+		batch.visible = false;
 		pshader = new ParticleShader();
 		batch.addShader(pshader);
 		batch.hasRotationScale = true;
@@ -246,6 +251,7 @@ class ParticleGroup {
 		var life = g.life * (1 + srand() * g.lifeRand);
 		var delay = rand() * life * (1 - g.emitSync) + g.emitDelay;
 		var speed = g.speed * (1 + srand() * g.speedRand);
+		if( g.life == 0 ) life = 1e10;
 
 		switch( g.emitMode ) {
 		case Point:
@@ -305,7 +311,7 @@ class ParticleGroup {
 }
 
 @:access(h2d.ParticleGroup)
-class Particles extends Sprite {
+class Particles extends Drawable {
 
 	static inline var VERSION = 1;
 
@@ -378,6 +384,11 @@ class Particles extends Sprite {
 			onEnd();
 	}
 
+	override function draw(ctx:RenderContext) {
+		for( g in groups )
+			g.batch.drawWith(ctx, this);
+	}
+
 	public inline function getGroups() {
 		return groups.iterator();
 	}

+ 6 - 1
h2d/SpriteBatch.hx

@@ -294,8 +294,13 @@ class SpriteBatch extends Drawable {
 	}
 
 	override function draw( ctx : RenderContext ) {
+		drawWith(ctx, this);
+	}
+
+	@:allow(h2d)
+	function drawWith( ctx:RenderContext, obj : Drawable ) {
 		if( first == null || buffer == null || buffer.isDisposed() ) return;
-		if( !ctx.beginDrawObject(this, tile.getTexture()) ) return;
+		if( !ctx.beginDrawObject(obj, tile.getTexture()) ) return;
 		ctx.engine.renderQuadBuffer(buffer);
 	}