瀏覽代碼

Emitter bugfix

ShiroSmith 5 年之前
父節點
當前提交
8f0c931065
共有 1 個文件被更改,包括 50 次插入19 次删除
  1. 50 19
      hrt/prefab/fx/Emitter.hx

+ 50 - 19
hrt/prefab/fx/Emitter.hx

@@ -31,8 +31,9 @@ enum Orientation {
 
 
 enum EmitType {
 enum EmitType {
 	Infinity;
 	Infinity;
-	Burst;
 	Duration;
 	Duration;
+	Burst;
+	BurstDuration;
 }
 }
 
 
 typedef ParamDef = {
 typedef ParamDef = {
@@ -331,8 +332,9 @@ private class ParticleInstance  {
 		if( emitter.useCollision ) {
 		if( emitter.useCollision ) {
 			var worldPos = absPos.getPosition();
 			var worldPos = absPos.getPosition();
 			if( worldPos.z < 0 ) {
 			if( worldPos.z < 0 ) {
-				if( emitter.killOnCollision == 1 || hxd.Math.random() < emitter.killOnCollision )
-					life = lifeTime;
+				if( emitter.killOnCollision == 1 || hxd.Math.random() < emitter.killOnCollision ) {
+					life = lifeTime + 1; // No survivor
+				}
 				else {
 				else {
 					var speedAmount = speedAccumulation.length();
 					var speedAmount = speedAccumulation.length();
 					speedAccumulation.normalize();
 					speedAccumulation.normalize();
@@ -382,6 +384,7 @@ class EmitterObject extends h3d.scene.Object {
 	// OBJECTS
 	// OBJECTS
 	public var particleTemplate : hrt.prefab.Object3D;
 	public var particleTemplate : hrt.prefab.Object3D;
 	public var subEmitterTemplate : Emitter;
 	public var subEmitterTemplate : Emitter;
+	public var trailTemplate : Trail;
 	public var subEmitters : Array<EmitterObject> = [];
 	public var subEmitters : Array<EmitterObject> = [];
 	// LIFE
 	// LIFE
 	public var lifeTime = 2.0;
 	public var lifeTime = 2.0;
@@ -452,10 +455,14 @@ class EmitterObject extends h3d.scene.Object {
 		var p = particles;
 		var p = particles;
 		while(p != null) {
 		while(p != null) {
 			var n = p.next;
 			var n = p.next;
-			disposeInstance(p);
+			disposeInstance(p, false);
 			p = n;
 			p = n;
 		}
 		}
 		particles = null;
 		particles = null;
+		for( s in subEmitters ) {
+			s.remove();
+		}
+		subEmitters = [];
 	}
 	}
 
 
 	override function onRemove() {
 	override function onRemove() {
@@ -480,7 +487,8 @@ class EmitterObject extends h3d.scene.Object {
 	}
 	}
 
 
 	var tmpPos = new h3d.Vector();
 	var tmpPos = new h3d.Vector();
-	function disposeInstance(p: ParticleInstance) {
+	var tmpCtx : hrt.prefab.Context;
+	function disposeInstance(p: ParticleInstance, spawnSubEmitter : Bool) {
 		p.next = pool;
 		p.next = pool;
 		p.dispose();
 		p.dispose();
 		pool = p;
 		pool = p;
@@ -490,11 +498,14 @@ class EmitterObject extends h3d.scene.Object {
 			throw "assert";
 			throw "assert";
 
 
 		// SUB EMITTER
 		// SUB EMITTER
-		if( subEmitterTemplate != null ) {
-			var ctx = new hrt.prefab.Context();
-			ctx.local3d = this.getScene();
-			ctx.shared = new ContextShared();
-			var emitter : EmitterObject = cast subEmitterTemplate.makeInstance(ctx).local3d;
+		if( spawnSubEmitter && subEmitterTemplate != null ) {
+			if( tmpCtx == null ) {
+				tmpCtx = new hrt.prefab.Context();
+				tmpCtx.local3d = this.getScene();
+				tmpCtx.shared = context.shared;
+			}
+			tmpCtx.local3d = this.getScene();
+			var emitter : EmitterObject = cast subEmitterTemplate.makeInstance(tmpCtx).local3d;
 			var pos = p.absPos.getPosition(tmpPos);
 			var pos = p.absPos.getPosition(tmpPos);
 			emitter.setPosition(pos.x, pos.y, pos.z);
 			emitter.setPosition(pos.x, pos.y, pos.z);
 			emitter.isSubEmitter = true;
 			emitter.isSubEmitter = true;
@@ -726,6 +737,25 @@ class EmitterObject extends h3d.scene.Object {
 					doEmit(delta);
 					doEmit(delta);
 					if( isSubEmitter && (parentEmitter == null || parentEmitter.parent == null) )
 					if( isSubEmitter && (parentEmitter == null || parentEmitter.parent == null) )
 						enable = false;
 						enable = false;
+				case Duration:
+					var emitTarget = evaluator.getSum(emitRate, hxd.Math.min(curTime, emitDuration));
+					var delta = hxd.Math.ceil(hxd.Math.min(maxCount - numInstances, emitTarget - emitCount));
+					doEmit(delta);
+					if( isSubEmitter && curTime >= emitDuration )
+						enable = false;
+				case BurstDuration:
+					if( burstDelay > 0 ) {
+						var needBurst = curTime < emitDuration && curTime > nextBurstTime;
+						while( needBurst ) {
+							var delta = hxd.Math.ceil(hxd.Math.min(maxCount - numInstances, burstParticleCount));
+							doEmit(delta);
+							nextBurstTime += burstDelay;
+							totalBurstCount++;
+							needBurst = totalBurstCount < burstCount && curTime > nextBurstTime;
+						}
+					}
+					if( isSubEmitter && curTime >= emitDuration )
+						enable = false;
 				case Burst:
 				case Burst:
 					if( burstDelay > 0 ) {
 					if( burstDelay > 0 ) {
 						var needBurst = totalBurstCount < burstCount && curTime > nextBurstTime;
 						var needBurst = totalBurstCount < burstCount && curTime > nextBurstTime;
@@ -739,12 +769,7 @@ class EmitterObject extends h3d.scene.Object {
 					}
 					}
 					if( isSubEmitter && totalBurstCount == burstCount )
 					if( isSubEmitter && totalBurstCount == burstCount )
 						enable = false;
 						enable = false;
-				case Duration:
-					var emitTarget = evaluator.getSum(emitRate, hxd.Math.min(curTime, emitDuration));
-					var delta = hxd.Math.ceil(hxd.Math.min(maxCount - numInstances, emitTarget - emitCount));
-					doEmit(delta);
-					if( isSubEmitter && curTime >= emitDuration )
-						enable = false;
+				
 			}
 			}
 		}
 		}
 
 
@@ -798,7 +823,7 @@ class EmitterObject extends h3d.scene.Object {
 					prev.next = next;
 					prev.next = next;
 				else
 				else
 					particles = next;
 					particles = next;
-				disposeInstance(p);
+				disposeInstance(p, true);
 			}
 			}
 			else {
 			else {
 				p.update(dt);
 				p.update(dt);
@@ -878,8 +903,8 @@ class Emitter extends Object3D {
 		{ name: "animationLoop", t: PBool, def: true, groupName : "Animation" },
 		{ name: "animationLoop", t: PBool, def: true, groupName : "Animation" },
 		// COLLISION
 		// COLLISION
 		{ name: "useCollision", t: PBool, def: false, groupName : "Ground Collision" },
 		{ name: "useCollision", t: PBool, def: false, groupName : "Ground Collision" },
-		{ name: "elasticity", t: PFloat(0, 1.0), disp: "Elasticity", def : 1.0, groupName : "Collision" },
-		{ name: "killOnCollision", t: PFloat(0, 1.0), disp: "Kill On Collision", def : 0.0, groupName : "Collision" },
+		{ name: "elasticity", t: PFloat(0, 1.0), disp: "Elasticity", def : 1.0, groupName : "Ground Collision" },
+		{ name: "killOnCollision", t: PFloat(0, 1.0), disp: "Kill On Collision", def : 0.0, groupName : "Ground Collision" },
 	];
 	];
 
 
 	public static var instanceParams : Array<ParamDef> = [
 	public static var instanceParams : Array<ParamDef> = [
@@ -1061,6 +1086,9 @@ class Emitter extends Object3D {
 		// SUB-EMITTER
 		// SUB-EMITTER
 		var subEmitterTemplate : Emitter = cast children.find( p -> p.enabled && Std.downcast(p, Emitter) != null && p.to(Object3D).visible);
 		var subEmitterTemplate : Emitter = cast children.find( p -> p.enabled && Std.downcast(p, Emitter) != null && p.to(Object3D).visible);
 		emitterObj.subEmitterTemplate = subEmitterTemplate;
 		emitterObj.subEmitterTemplate = subEmitterTemplate;
+		// TRAIL
+		var trailTemplate : Trail = cast children.find( p -> p.enabled && Std.downcast(p, Trail) != null && p.to(Object3D).visible);
+		emitterObj.trailTemplate = trailTemplate;
 		// RANDOM
 		// RANDOM
 		emitterObj.seedGroup 			= 	getParamVal("seedGroup");
 		emitterObj.seedGroup 			= 	getParamVal("seedGroup");
 		// LIFE	
 		// LIFE	
@@ -1176,6 +1204,9 @@ class Emitter extends Object3D {
 				removeParam("burstDelay");
 				removeParam("burstDelay");
 				removeParam("burstParticleCount");
 				removeParam("burstParticleCount");
 				removeParam("emitDuration");
 				removeParam("emitDuration");
+			case BurstDuration:
+				removeParam("emitRate");
+				removeParam("burstCount");
 			case Burst:
 			case Burst:
 				removeParam("emitDuration");
 				removeParam("emitDuration");
 				removeParam("emitRate");
 				removeParam("emitRate");