// Handles Particle Effects.
// Worthless Bums 2012.


// Particle Effect test function.
function tpe ()
{//echo("tpe()");
	// Get active character's indices.
	%xIndex = $playerSquad[$activeCharacter].xIndex;
	%yIndex = $playerSquad[$activeCharacter].yIndex;
	// Create smoke effect on tile.
	createSmokeEffect(%xIndex, %yIndex, 2);
}


// Creates a smoke particle effect at the specified tile location.
function createSmokeEffect (%xIndex, %yIndex, %turns)
{//echo("createSmokeEffect()");
	// The number of turns to stay in effect.
	// NOTE: Should this be a parameter? Probably...
	//%turns = 2;
	
	// Create smoke particle effect.
	%smokeEffect = new t2dParticleEffect()
	{
		effectType = "smoke";
		scenegraph = levelSceneGraph;
		effectFile = "../data/particles/smoke.eff";
		useEffectCollisions = "0";
		effectMode = "INFINITE";
		effectTime = "-1";
		canSaveDynamicFields = "1";
		Position = getTilePositionFromIndices(%xIndex, %yIndex);
		size = "4.000 4.000";
		CollisionMaxIterations = "1";
		//	mountID = "4";
		
		// Track the turns it should stay in effect.
		turns = %turns;
	};
	
	// Add emitters.
	//%smokeEffect.addEmitter("Smoke20");
	//%smokeEffect.addEmitter("Smoke40");
	//%smokeEffect.addEmitter("Smoke60");
	
	// Play effect.
	%smokeEffect.playEffect(false);
	
	// Add smoke effect to the environment effect list.
	$environmentEffects.add(%smokeEffect);
	
	// Mount particle effect to active character.
	//%smokeEffect.mount($playerSquad[$activeCharacter], 0.0, 0.0, 50.0, true, true, true, true);
}

// Creates a fire particle effect on the specific monster.
function createFireEffect (%monsterIndex)
{//echo("createFireEffect()");
	// Grab the monster.
	%monster = $monsterList.getObject(%monsterIndex);
	%position = %monster.getPosition();
	
	// Don't apply another particle effect if one already exists!
	if (isObject(%monster.onFireEffect))
	{
		
		return;
	}
	
	// Create smoke particle effect.
	%fireEffect = new t2dParticleEffect()
	{
		SceneLayer = 9;	// Monster is layer 10.
		scenegraph = levelSceneGraph;
		effectFile = "../data/particles/onFire.eff";
		useEffectCollisions = "0";
		effectMode = "INFINITE";
		effectTime = "-1";
		canSaveDynamicFields = "1";
		Position = %position;
		size = "4.000 4.000";
		CollisionMaxIterations = "1";
		//	mountID = "4";
	};
	
	// Play effect.
	%fireEffect.playEffect(false);
	
	// Mount particle effect to the specified monster.
	%fireEffect.mount(%monster, 0.0, 0.0, 50.0, true, true, true, true);
	
	// Add fire effect to the monster.
	%monster.onFireEffect = %fireEffect;
}

// Creates a fire particle effect on the specified target.
function createFireEffectOnObject (%target)
{//echo("createFireEffectOnObject()");
	// Don't apply another particle effect if one already exists!
	if (isObject(%target.onFireEffect))
	{
		
		return;
	}
	
	// Create smoke particle effect.
	%fireEffect = new t2dParticleEffect()
	{
		SceneLayer = 9;	// Monster is layer 10.
		scenegraph = levelSceneGraph;
		effectFile = "../data/particles/onFire.eff";
		useEffectCollisions = "0";
		effectMode = "INFINITE";
		effectTime = "-1";
		canSaveDynamicFields = "1";
		Position = %position;
		size = "4.000 4.000";
		CollisionMaxIterations = "1";
		//	mountID = "4";
	};
	
	// Play effect.
	%fireEffect.playEffect(false);
	
	// Mount particle effect to the specified target.
	%fireEffect.mount(%target, 0.0, 0.0, 50.0, true, true, true, true);
	
	// Add fire effect to the target.
	%target.onFireEffect = %fireEffect;
}

// Creates a gear explosion particle effect at the specified tile location.
// NOTE: This is a little goofy. Pixel art animation instead?
function createGearExplosionEffect (%xIndex, %yIndex)
{//echo("createGearExplosionEffect()");
return;	// Disabled for now!
	// Create gear explosion particle effect.
	%gearExplosionEffect = new t2dParticleEffect()
	{
		effectType = "gearExplosion";
		scenegraph = levelSceneGraph;
		effectFile = "../data/particles/gearExplosion.eff";
		useEffectCollisions = "0";
		effectMode = "INFINITE";
		effectTime = "-1";
		canSaveDynamicFields = "1";
		Position = getTilePositionFromIndices(%xIndex, %yIndex);
		size = "4.000 4.000";
		CollisionMaxIterations = "1";
	};
	
	// Add emitters.
	//%gearExplosionEffect.addEmitter("Smoke20");
	
	// Play effect.
	%gearExplosionEffect.playEffect(false);
	
	// Add gear explosion effect to the environment effect list.
	//$environmentEffects.add(%gearExplosionEffect);
	
	// Set lifetime.
	%gearExplosionEffect.setLifetime(0.60);
}

// Creates a Scout shot particle effect at the specified tile location.
function createScoutShotEffect (%srcXIndex, %srcYIndex, %dstXIndex, %dstYIndex, %direction, %range)
{//echo("createScoutShotEffect()");
	// Create shot effect.
	%shotEffect = new t2dParticleEffect()
	{
		SceneLayer = 0;
		graph = 0;
		effectType = "scoutShot";
		scenegraph = levelSceneGraph;
		effectFile = "../data/particles/scoutShot.eff";
		useEffectCollisions = "0";
		effectMode = "INFINITE";
		effectTime = "-1";
		canSaveDynamicFields = "1";
		Position = getTilePositionFromIndices(%srcXIndex, %srcYIndex);
		size = "4.000 4.000";		// Doesn't matter, this is set in the effect file!
		extent = "1.000 1.000";
		CollisionMaxIterations = "1";
	};
	
	// Play effect.
	%shotEffect.playEffect(false);
	// Set particle effect movement speed.
	%speed = 100.0;
	// Get indices' position.
	%position = getTilePositionFromIndices(%srcXIndex, %srcYIndex);
	%xPos = getWord(%position, 0);
	%yPos = getWord(%position, 1);
	// Calculate effective range.
	%effectiveRange = 0;
	if (%dstXIndex == -9999	&&
		%dstYIndex == -9999)
	{
		%effectiveRange = %range;
	}
	else
	{
		%effectiveRange = mRound(VectorDist(%srcXIndex SPC %srcYIndex, %dstXIndex SPC %dstYIndex));
	}
	// Switch by direction to move particle effect in.
	%tileMagnitude = 4.0;
	switch (%direction)
	{
		case $northDirection:	%shotEffect.moveTo(%xPos, %yPos - %effectiveRange * %tileMagnitude, %speed, true, false);
		case $southDirection:	%shotEffect.moveTo(%xPos, %yPos + %effectiveRange * %tileMagnitude, %speed, true, false);
		case $eastDirection:	%shotEffect.moveTo(%xPos + %effectiveRange * %tileMagnitude, %yPos, %speed, true, false);
		case $westDirection:	%shotEffect.moveTo(%xPos - %effectiveRange * %tileMagnitude, %yPos, %speed, true, false);
	}
	// Set lifetime with %effectiveRange.
	%lifetimeModifier = 0.045;
	%shotEffect.setLifetime(%lifetimeModifier * %effectiveRange);
}

// Creates a smoke trail particle effect at the specified tile location.
function createSmokeTrailEffect (%srcXIndex, %srcYIndex, %dstXIndex, %dstYIndex, %direction, %range)
{//echo("createSmokeTrailEffect()");
	// Create shot effect.
	%smokeTrailEffect = new t2dParticleEffect()
	{
		SceneLayer = 0;
		graph = 0;
		effectType = "smokeTrail";
		scenegraph = levelSceneGraph;
		effectFile = "../data/particles/smokeTrail.eff";
		useEffectCollisions = "0";
		effectMode = "INFINITE";
		effectTime = "-1";
		canSaveDynamicFields = "1";
		Position = getTilePositionFromIndices(%srcXIndex, %srcYIndex);
		size = "4.000 4.000";		// Doesn't matter, this is set in the effect file!
		extent = "1.000 1.000";
		CollisionMaxIterations = "1";
	};
	
	// Play effect.
	%smokeTrailEffect.playEffect(false);
	// Set particle effect movement speed.
	%speed = 40.0;//100.0;
	// Get indices' position.
	%position = getTilePositionFromIndices(%srcXIndex, %srcYIndex);
	%xPos = getWord(%position, 0);
	%yPos = getWord(%position, 1);
	// Calculate effective range.
	%effectiveRange = 0;
	if (%dstXIndex == -9999	&&
		%dstYIndex == -9999)
	{
		%effectiveRange = %range;
	}
	else
	{
		%effectiveRange = mRound(VectorDist(%srcXIndex SPC %srcYIndex, %dstXIndex SPC %dstYIndex));
	}
	// Switch by direction to move particle effect in.
	%tileMagnitude = 4.0;
	switch (%direction)
	{
		case $northDirection:	%smokeTrailEffect.moveTo(%xPos, %yPos - %effectiveRange * %tileMagnitude, %speed, true, false);
		case $southDirection:	%smokeTrailEffect.moveTo(%xPos, %yPos + %effectiveRange * %tileMagnitude, %speed, true, false);
		case $eastDirection:	%smokeTrailEffect.moveTo(%xPos + %effectiveRange * %tileMagnitude, %yPos, %speed, true, false);	
		case $westDirection:	%smokeTrailEffect.moveTo(%xPos - %effectiveRange * %tileMagnitude, %yPos, %speed, true, false);
	}
	// Set lifetime with %effectiveRange.
	%lifetimeModifier = 0.2;
	%smokeTrailEffect.setLifetime(%lifetimeModifier * %effectiveRange);
}

// Creates a fire smoke trail particle effect at the specified tile location.
function createFireSmokeTrailEffect (%srcXIndex, %srcYIndex, %dstXIndex, %dstYIndex, %direction, %range)
{//echo("createFireSmokeTrailEffect()");
	// Create shot effect.
	%smokeTrailEffect = new t2dParticleEffect()
	{
		SceneLayer = 0;
		graph = 0;
		effectType = "fireSmokeTrail";
		scenegraph = levelSceneGraph;
		effectFile = "../data/particles/fireSmokeTrail.eff";
		useEffectCollisions = "0";
		effectMode = "INFINITE";
		effectTime = "-1";
		canSaveDynamicFields = "1";
		Position = getTilePositionFromIndices(%srcXIndex, %srcYIndex);
		size = "4.000 4.000";		// Doesn't matter, this is set in the effect file!
		extent = "1.000 1.000";
		CollisionMaxIterations = "1";
	};
	
	// Play effect.
	%smokeTrailEffect.playEffect(false);
	// Set particle effect movement speed.
	%speed = 40.0;//100.0;
	// Get indices' position.
	%position = getTilePositionFromIndices(%srcXIndex, %srcYIndex);
	%xPos = getWord(%position, 0);
	%yPos = getWord(%position, 1);
	// Calculate effective range.
	%effectiveRange = 0;
	if (%dstXIndex == -9999	&&
		%dstYIndex == -9999)
	{
		%effectiveRange = %range;
	}
	else
	{
		%effectiveRange = mRound(VectorDist(%srcXIndex SPC %srcYIndex, %dstXIndex SPC %dstYIndex));
	}
	// Switch by direction to move particle effect in.
	%tileMagnitude = 4.0;
	switch (%direction)
	{
		case $northDirection:	%smokeTrailEffect.moveTo(%xPos, %yPos - %effectiveRange * %tileMagnitude, %speed, true, false);
		case $southDirection:	%smokeTrailEffect.moveTo(%xPos, %yPos + %effectiveRange * %tileMagnitude, %speed, true, false);
		case $eastDirection:	%smokeTrailEffect.moveTo(%xPos + %effectiveRange * %tileMagnitude, %yPos, %speed, true, false);	
		case $westDirection:	%smokeTrailEffect.moveTo(%xPos - %effectiveRange * %tileMagnitude, %yPos, %speed, true, false);
	}
	// Set lifetime with %effectiveRange.
	%lifetimeModifier = 0.2;//0.045;
	%smokeTrailEffect.setLifetime(%lifetimeModifier * %effectiveRange);
}
// Creates a frozen smoke trail particle effect at the specified tile location.
function createFrozenSmokeTrailEffect (%srcXIndex, %srcYIndex, %dstXIndex, %dstYIndex, %direction, %range)
{//echo("createFrozenSmokeTrailEffect()");
	// Create shot effect.
	%smokeTrailEffect = new t2dParticleEffect()
	{
		SceneLayer = 0;
		graph = 0;
		effectType = "frozenSmokeTrail";
		scenegraph = levelSceneGraph;
		effectFile = "../data/particles/frozenSmokeTrail.eff";
		useEffectCollisions = "0";
		effectMode = "INFINITE";
		effectTime = "-1";
		canSaveDynamicFields = "1";
		Position = getTilePositionFromIndices(%srcXIndex, %srcYIndex);
		size = "4.000 4.000";		// Doesn't matter, this is set in the effect file!
		extent = "1.000 1.000";
		CollisionMaxIterations = "1";
	};
	
	// Play effect.
	%smokeTrailEffect.playEffect(false);
	// Set particle effect movement speed.
	%speed = 40.0;//100.0;
	// Get indices' position.
	%position = getTilePositionFromIndices(%srcXIndex, %srcYIndex);
	%xPos = getWord(%position, 0);
	%yPos = getWord(%position, 1);
	// Calculate effective range.
	%effectiveRange = 0;
	if (%dstXIndex == -9999	&&
		%dstYIndex == -9999)
	{
		%effectiveRange = %range;
	}
	else
	{
		%effectiveRange = mRound(VectorDist(%srcXIndex SPC %srcYIndex, %dstXIndex SPC %dstYIndex));
	}
	// Switch by direction to move particle effect in.
	%tileMagnitude = 4.0;
	switch (%direction)
	{
		case $northDirection:	%smokeTrailEffect.moveTo(%xPos, %yPos - %effectiveRange * %tileMagnitude, %speed, true, false);
		case $southDirection:	%smokeTrailEffect.moveTo(%xPos, %yPos + %effectiveRange * %tileMagnitude, %speed, true, false);
		case $eastDirection:	%smokeTrailEffect.moveTo(%xPos + %effectiveRange * %tileMagnitude, %yPos, %speed, true, false);
		case $westDirection:	%smokeTrailEffect.moveTo(%xPos - %effectiveRange * %tileMagnitude, %yPos, %speed, true, false);
	}
	// Set lifetime with %effectiveRange.
	%lifetimeModifier = 0.2;//0.045;
	%smokeTrailEffect.setLifetime(%lifetimeModifier * %effectiveRange);
}
// Creates a acid smoke trail particle effect at the specified tile location.
function createAcidSmokeTrailEffect (%srcXIndex, %srcYIndex, %dstXIndex, %dstYIndex, %direction, %range)
{//echo("createAcidSmokeTrailEffect()");
	// Create shot effect.
	%smokeTrailEffect = new t2dParticleEffect()
	{
		SceneLayer = 0;
		graph = 0;
		effectType = "acidSmokeTrail";
		scenegraph = levelSceneGraph;
		effectFile = "../data/particles/acidSmokeTrail.eff";
		useEffectCollisions = "0";
		effectMode = "INFINITE";
		effectTime = "-1";
		canSaveDynamicFields = "1";
		Position = getTilePositionFromIndices(%srcXIndex, %srcYIndex);
		size = "4.000 4.000";		// Doesn't matter, this is set in the effect file!
		extent = "1.000 1.000";
		CollisionMaxIterations = "1";
	};
	
	// Play effect.
	%smokeTrailEffect.playEffect(false);
	// Set particle effect movement speed.
	%speed = 40.0;//100.0;
	// Get indices' position.
	%position = getTilePositionFromIndices(%srcXIndex, %srcYIndex);
	%xPos = getWord(%position, 0);
	%yPos = getWord(%position, 1);
	// Calculate effective range.
	%effectiveRange = 0;
	if (%dstXIndex == -9999	&&
		%dstYIndex == -9999)
	{
		%effectiveRange = %range;
	}
	else
	{
		%effectiveRange = mRound(VectorDist(%srcXIndex SPC %srcYIndex, %dstXIndex SPC %dstYIndex));
	}
	// Switch by direction to move particle effect in.
	%tileMagnitude = 4.0;
	switch (%direction)
	{
		case $northDirection:	%smokeTrailEffect.moveTo(%xPos, %yPos - %effectiveRange * %tileMagnitude, %speed, true, false);
		case $southDirection:	%smokeTrailEffect.moveTo(%xPos, %yPos + %effectiveRange * %tileMagnitude, %speed, true, false);
		case $eastDirection:	%smokeTrailEffect.moveTo(%xPos + %effectiveRange * %tileMagnitude, %yPos, %speed, true, false);	
		case $westDirection:	%smokeTrailEffect.moveTo(%xPos - %effectiveRange * %tileMagnitude, %yPos, %speed, true, false);
	}
	// Set lifetime with %effectiveRange.
	%lifetimeModifier = 0.2;
	%smokeTrailEffect.setLifetime(%lifetimeModifier * %effectiveRange);
}

// Creates a steam venting particle effect at the specified tile location.
function createSteamVentingEffect (%xIndex, %yIndex)
{//echo("createSteamVentingEffect()");
	// Create steam venting particle effect.
	%steamVentingEffect = new t2dParticleEffect()
	{
		SceneLayer = $SteamVentingLayer;
		graph = $SteamVentingGraph;
		effectType = "steamVenting";
		scenegraph = levelSceneGraph;
		effectFile = "../data/particles/steamVenting.eff";
		useEffectCollisions = "0";
		effectMode = "INFINITE";
		effectTime = "-1";
		canSaveDynamicFields = "1";
		Position = getTilePositionFromIndices(%xIndex, %yIndex);
		size = "4.000 4.000";
		CollisionMaxIterations = "1";
	};
	
	// Play effect.
	%steamVentingEffect.playEffect(false);
	
	// Add smoke effect to the environment effect list.
	$environmentEffects.add(%steamVentingEffect);
}