// Handles animations.
// Worthless Bums 2012.


function scheduleObjectMove (%objectToMove, %objectMovePosition, %objectMoveDelay)
{//echo("scheduleObjectMove()");
	schedule(%objectMoveDelay, 0, "moveObjectToPosition", %objectToMove, %objectMovePosition);
}
function moveObjectToPosition (%objectToMove, %objectMovePosition)
{//echo("moveObjectToPosition()");
	%objectToMove.position = %objectMovePosition;
}

//---------------------------------------------------------------------------------------------------

// Test functions!
function createButtonAnimation (%position)
{//echo("createButtonAnimation()");
	%buttonAnimation = new Sprite()
	{
		SceneLayer = 0;
		graph = 0;
		scenegraph = levelSceneGraph;
		Animation = "GreenEnergyBlastAnimation";
		canSaveDynamicFields = "1";
		position = %position;
		size = "4 4";
	};
	%buttonAnimation.dump();
}
function fabb ()
{
	schedule( 5, 0, "setReloadButtonBitmap", "../../../Sprites/1/assets/images/UI/ControlPanel/ReloadButton_h");
	schedule(20, 0, "setReloadButtonBitmap", "../../../Sprites/1/assets/images/UI/ControlPanel/ReloadButton_d");
	schedule(30, 0, "setReloadButtonBitmap", "../../../Sprites/1/assets/images/UI/ControlPanel/ReloadButton_i");
	schedule(40, 0, "setReloadButtonBitmap", "../../../Sprites/1/assets/images/UI/ControlPanel/ReloadButton");
	schedule(45, 0, "fabb");
}
function setReloadButtonBitmap (%bitmap)
{
	ReloadButton.bitmap = %bitmap;
}

//---------------------------------------------------------------------------------------------------

// Unit animations!
function getUnitIdleAnimation (%unit)//echo(getUnitIdleAnimation($playerSquad[$activeCharacter]));
{//echo("getUnitIdleAnimation()");
	if (isLargeUnit(%unit))
	{
		return getCharacterClassName(%unit.characterClass) @ "HulkSuitIdle" @ getDirectionName(%unit.facing);
	}
	else
	{//echo(getCharacterClassName(%unit.characterClass) @ getWeaponProjectileType(%unit.weapon.projectileType) @ "Idle" @ getDirectionName(%unit.facing) @ "Animation");
		return getCharacterClassName(%unit.characterClass) @ getWeaponProjectileType(%unit.weapon.projectileType) @ "Idle" @ getDirectionName(%unit.facing) @ "Animation";
	}
}
function getDirectionName (%direction)
{
	switch (%direction)
	{
		case $northDirection:	return "South";//"North";
		case $southDirection:	return "North";//"South";
		case $eastDirection:	return "East";
		case $westDirection:	return "West";
	}
}
function getWeaponProjectileType (%projectileType)
{
	switch (%projectileType)
	{
		case $weaponProjectileBallisticType:	return "Ballistic";
		case $weaponProjectileLaserType:		return "Laser";
		case $weaponProjectilePlasmaType:		return "Plasma";
		case $weaponProjectileBeamType:			return "Beam";
	}
}

// Fire/Reload animation functions.
function setUnitAnimation (%unit, %animation)
{//echo("setUnitAnimation()");
	%unit.Animation = %animation;
}
function playMarineFireAnimation (%characterIndex)
{//echo("playMarineFireAnimation()");
	if (isEventPending($FireAnimationEvent))
	{
		cancel($FireAnimationEvent);
	}
	$playerSquad[%characterIndex].Animation = "Sprites:" @ getCharacterClassName($playerSquad[%characterIndex].characterClass) @ "Fire" @ getProjectileTypeName($playerSquad[%characterIndex].weapon.projectileType) @ getDirectionName($playerSquad[%characterIndex].facing);
	%idleAnimation = "Sprites:" @ getUnitIdleAnimation($playerSquad[%characterIndex]);
	$FireAnimationEvent = schedule(400, 0, "setUnitAnimation", $playerSquad[%characterIndex], %idleAnimation);
}
function playMarineReloadAnimation (%characterIndex)
{//echo("playMarineReloadAnimation()");
	if (isEventPending($ReloadAnimationEvent))
	{
		cancel($ReloadAnimationEvent);
	}
	$playerSquad[%characterIndex].Animation = "Sprites:" @ getCharacterClassName($playerSquad[%characterIndex].characterClass) @ "Reload" @ getProjectileTypeName($playerSquad[%characterIndex].weapon.projectileType) @ getDirectionName($playerSquad[%characterIndex].facing);
	%idleAnimation = "Sprites:" @ getUnitIdleAnimation($playerSquad[%characterIndex]);
	$ReloadAnimationEvent = schedule(500, 0, "setUnitAnimation", $playerSquad[%characterIndex], %idleAnimation);
}

function createFireProjectileAnimation (%characterIndex, %direction, %projectileType, %classType)
{//echo("createFireProjectileAnimation()");
	%x = getWord($playerSquad[%characterIndex].getPosition(), 0);
	%y = getWord($playerSquad[%characterIndex].getPosition(), 1);
	%offset = 3.5;
	%FlipX = 0;
	%FlipY = 0;
	switch (%direction)
	{
		case $northDirection:
			%y -= %offset;
			%rotation = -90.0;
		case $southDirection:
			%y += %offset;
			%rotation = 90.0;
			%FlipY = 1;
		case $westDirection:
			%x -= %offset;
			%rotation = 0;
			%FlipX = 1;
		case $eastDirection:
			%x += %offset;
			%rotation = 0.0;
	}
	%animation = "Sprites:" @ getCharacterClassName(%classType) @ getProjectileTypeName(%projectileType) @ "Projectile";
	%shotAnimation = new Sprite("Projectile Animation")
	{
		SceneLayer = 0;
		scenegraph = levelSceneGraph;
		Animation = %animation;
		canSaveDynamicFields = "1";
		position = %x SPC %y;			// Place at character location with offset.
		size = "5.125 4.000";
		angle = %rotation;
		FlipX = %FlipX;
		FlipY = %FlipY;
	};
	addObjectToScene(%shotAnimation);
	return %shotAnimation;
}

function playLevelUpAnimation (%characterIndex)
{//echo("playLevelUpAnimation()");echo("%characterIndex =" SPC %characterIndex);
	// Ugh. Do this here? Easiest place but it doesn't really suit this function's purpose!
	if ($playerSquad[%characterIndex].rank < 4)		// 4 is currently max rank!
	{
		$playerSquad[%characterIndex].rank++;
	}
	%x = getWord($playerSquad[%characterIndex].getPosition(), 0);
	%y = getWord($playerSquad[%characterIndex].getPosition(), 1);
	%levelUpAnimation = new Sprite()
	{
		SceneLayer = 0;
		scenegraph = levelSceneGraph;
		Animation = "Sprites:LevelUp";
		canSaveDynamicFields = "1";
		position = %x SPC %y;
		size = "4 4";
		rotation = %rotation;
	};
	addObjectToScene(%levelUpAnimation);
	playAudio("Audio:levelUp", EffectsAudioVolume.value);
	%levelUpAnimation.setLifetime(1.2);
	//$playerSquad[%characterIndex].levelUpAnimation = %levelUpAnimation;
}
function flamethrowerAnimation (%direction)
{//echo("flamethrowerAnimation()");
	%x = getWord($playerSquad[$activeCharacter].getPosition(), 0);
	%y = getWord($playerSquad[$activeCharacter].getPosition(), 1);
	%offset = 3.5;
	%FlipX = 0;
	%FlipY = 0;
	switch (%direction)
	{
		case 0: // Fire Up.
			%y -= %offset;
			%rotation = -90.0;
		case 1:	// Fire Down.
			%y += %offset;
			%rotation = 90.0;
			%FlipY = 1;
		case 2:	// Fire Left.
			%x -= %offset;
			%rotation = 0;//180.0;
			%FlipX = 1;
		case 3:	// Fire Right.
			%x += %offset;
			%rotation = 0.0;
	}
	$flamethrowerAnimation = new Sprite(flamethrowerAnimation)
	{
		SceneLayer = 0;
		scenegraph = levelSceneGraph;
		Animation = "Flamethrower_FramesAnimation";
		canSaveDynamicFields = "1";
		position = %x SPC %y;			// Place at character location with offset.
		size = "5.125 4.000";
		rotation = %rotation;
		FlipX = %FlipX;
		FlipY = %FlipY;
	};
	%animationLifetime = 1.4;
	$flamethrowerAnimation.setLifetime(%animationLifetime);
}
function rifleMuzzleFlash (%direction)
{//echo("rifleMuzzleFlash()");
	%currentCharacter = $activeCharacter;
	if ($monsterTurnOngoing	&&
		$playerSquad[$guardModeCharacter].guardMode)
	{//echo("$guardModeCharacter =" SPC $guardModeCharacter);
		%currentCharacter = $guardModeCharacter;
	}
	%x = getWord($playerSquad[%currentCharacter].getPosition(), 0);
	%y = getWord($playerSquad[%currentCharacter].getPosition(), 1);
	switch ($playerSquad[%currentCharacter].characterClass)
	{
		case 0:
			%Animation = "ShotgunBlastFramesAnimation";
			%xOffset = 3;
			%yOffset = 3;
		case 1:	%Animation = "SniperBlastFramesAnimation";
			%xOffset = 3;
			%yOffset = 3;
		case 2:	%Animation = "LauncherBlastFramesAnimation";
			%xOffset = 3.5;
			%yOffset = 3.5;
		case 3:	%Animation = "MachinegunBlastFramesAnimation";
			%xOffset = 3.5;
			%yOffset = 3.5;
		case 4:	%Animation = "MachinegunBlastFramesAnimation";
			%xOffset = 3.5;
			%yOffset = 3.5;
	}
	switch (%direction)
	{
		case $northDirection:	 // Fire Up.
			%y -= %yOffset;
			%rotation = -90.0;
		case $southDirection:	// Fire Down.
			%y += %yOffset;
			%rotation = 90.0;
		case $eastDirection:	// Fire Right.
			%x += %xOffset;
			%rotation = 0.0;
			// Adjust for weapons held lower!
			if ($playerSquad[%currentCharacter].characterClass == 2)
			{
				%y += 0.77;
			}
			else if ($playerSquad[%currentCharacter].characterClass == 3)
			{
				%y += 0.77;
			}
			else if ($playerSquad[%currentCharacter].characterClass == 4)
			{
				%y += 0.77;
			}
		case $westDirection:	// Fire Left.
			%x -= %xOffset;
			%rotation = 180.0;
			// Adjust for weapons held lower!
			if ($playerSquad[%currentCharacter].characterClass == 2)
			{
				%y += 0.77;
			}
			else if ($playerSquad[%currentCharacter].characterClass == 3)
			{
				%y += 0.77;
			}
			else if ($playerSquad[%currentCharacter].characterClass == 4)
			{
				%y += 0.77;
			}
	}
}
function handleActiveCharacterIndicator ()
{//echo("handleActiveCharacterIndicator()");
	updateMonsterTargetPanelInfo();
	updateInventoryTabInformation();
	if (isObject($indicatorAnimation))
	{
		//$indicatorAnimation.safeDelete();
		$indicatorAnimation.Animation = "Sprites:active" @ getCharacterClassName($playerSquad[$activeCharacter].characterClass) @ "Indicator";
		updateUnitIndicatorPosition($playerSquad[$activeCharacter]);
		updateFirearmReloadReadout();
		return;
	}
	%verticalBuffer = -0.75;
	if (isLargeUnit($playerSquad[$activeCharacter]))
	{
		%verticalBuffer = -0.50;
	}
	$indicatorAnimation = new Sprite("activeIndicatorAnimatedSprite")
	{
		SceneLayer = 1;
		graph = 1;
		scenegraph = levelSceneGraph;
		Animation = "Sprites:active" @ getCharacterClassName($playerSquad[$activeCharacter].characterClass) @ "Indicator";
		canSaveDynamicFields = "1";
		position = getWord(%indicatorPosition, 0) SPC getWord(%indicatorPosition, 1) + %verticalBuffer;
		size = "4.000 4.000";
	};
	addObjectToScene($indicatorAnimation);
	updateUnitIndicatorPosition($playerSquad[$activeCharacter]);
	updateFirearmReloadReadout();
}
function handleGrenadierExplosion (%xIndex, %yIndex)
{//echo("handleGrenadierExplosion()");
	%explosionPosition = getTilePositionFromIndices(%xIndex, %yIndex);
	%grenadierExplosionAnimation = new Sprite()
	{
		SceneLayer = 0;
		scenegraph = levelSceneGraph;
		Animation = "Sprites:Explosion";
		canSaveDynamicFields = "1";
		position = %explosionPosition;
		size = "6.000 6.000";
	};
	addObjectToScene(%grenadierExplosionAnimation);
	%grenadierExplosionAnimation.setLifetime(0.65);
	playExplosionSfx();
}
function handleBloatDeathExplosion (%xIndex, %yIndex)
{//echo("handleBloatDeathExplosion()");
	%explosionPosition = getTilePositionFromIndices(%xIndex, %yIndex);
	%bloatDeathExplosionAnimation = new Sprite()
	{
		SceneLayer = 0;
		scenegraph = levelSceneGraph;
		Animation = "Bloat_Death_Explosion_FramesAnimation";
		canSaveDynamicFields = "1";
		position = %explosionPosition;
		size = "10.000 10.000";
	};
	%bloatDeathExplosionAnimation.setLifetime(1.6);
	playAudio("Audio:squareFormation", EffectsAudioVolume.value);
}
function handleFireball (%xIndex, %yIndex, %x, %y)
{//echo("handleFireball()");
	%explosionPosition = getTilePositionFromIndices(%xIndex, %yIndex);
	%fireballAnimation = new Sprite()
	{
		SceneLayer = 0;
		scenegraph = levelSceneGraph;
		Animation = "Grenadier_Explosion_FramesAnimation";
		canSaveDynamicFields = "1";
		position = %explosionPosition;
		size = "6.000 6.000";
	};
	%targetPosition = getTilePositionFromIndices(%x, %y);
	%xPos = getWord(%targetPosition, 0);
	%yPos = getWord(%targetPosition, 1);
	%fireballAnimation.moveTo(%xPos, %yPos, 40.0, true, false);
	%fireballAnimation.setLifetime(0.65);
	playAudio("Audio:fireball", EffectsAudioVolume.value);
}
function handleGreenEnergyBlast (%xIndex, %yIndex, %x, %y)
{//echo("handleGreenEnergyBlast()");
	%explosionPosition = getTilePositionFromIndices(%xIndex, %yIndex);
	%fireballAnimation = new Sprite()
	{
		SceneLayer = 0;
		scenegraph = levelSceneGraph;
		Animation = "GreenEnergyBlastAnimation";
		canSaveDynamicFields = "1";
		position = %explosionPosition;
		size = "6.000 6.000";
	};
	%targetPosition = getTilePositionFromIndices(%x, %y);
	%xPos = getWord(%targetPosition, 0);
	%yPos = getWord(%targetPosition, 1);
	%fireballAnimation.moveTo(%xPos, %yPos, 40.0, true, false);
	%fireballAnimation.setLifetime(0.65);
	playAudio("Audio:energyBlast", EffectsAudioVolume.value);
}
function handleFrostTrollCharge (%frostTroll)
{//echo("handleFrostTrollCharge()");
	%xIndex = %frostTroll.xIndex;
	%yIndex = %frostTroll.yIndex;
	if (isObject(%chargeAnimation))
	{
		%chargeAnimation.setLifetime(0.0);
		%chargeAnimation.safeDelete();
		%chargeAnimation = null;
	}
	%chargePosition = getTilePositionFromIndices(%xIndex, %yIndex);
	%chargeAnimation = new Sprite()
	{
		SceneLayer = 2;
		scenegraph = levelSceneGraph;
		Animation = "frostTrollChargeFramesAnimation";
		canSaveDynamicFields = "1";
		position = %chargePosition;
		size = "6.000 6.000";
	};
	%chargeAnimation.mount(%frostTroll, 0.0, 0.0, 50.0, true, true, true, true);
	%frostTroll.chargeAnimation = %chargeAnimation;
	playAudio("Audio:fireball", EffectsAudioVolume.value);
}
function handleBloodSplatter (%xIndex, %yIndex)
{//echo("handleBloodSplatter()");
	/*if (isObject(%bloodSplatterAnimation))
	{
		%bloodSplatterAnimation.safeDelete();
	}
	%bloodSplatterPosition = getTilePositionFromIndices(%xIndex, %yIndex);
	%bloodSplatterAnimation = new Sprite()
	{
		SceneLayer = 0;
		scenegraph = levelSceneGraph;
		Animation = "BloodSplatterFramesAnimation";
		canSaveDynamicFields = "1";
		position = %bloodSplatterPosition;
		size = "6.000 6.000";
	};
	%bloodSplatterAnimation.setLifetime(0.65);*/
}
function createSleepAnimation (%monster)
{//echo("handleSleepAnimation()");
	if (isObject(%sleepAnimation))
	{
		%sleepAnimation.safeDelete();
	}
	%sleepPosition = getTilePositionFromIndices(%monster.xIndex, %monster.yIndex);
	%sleepAnimation = new Sprite()
	{
		SceneLayer = 9;		// Monster is layer 9.
		scenegraph = levelSceneGraph;
		Animation = "Sleep_FramesAnimation";
		canSaveDynamicFields = "1";
		position = %sleepPosition;
		size = "4.000 4.000";
	};
	%sleepAnimation.mount(%monster, 0.0, 0.0, 50.0, true, true, true, true);
	%monster.sleepAnimation = %sleepAnimation;
}
function createSquadPowerBashAnimation ()
{//echo("createSquadPowerBashAnimation()");
	%character = $playerSquad[$activeCharacter];
	%position = %character.getPosition();
	%bashAnimation = new Sprite()
	{
		SceneLayer = 0;
		scenegraph = levelSceneGraph;
		Animation = "Bash_Animation_FramesAnimation";		// Not available anymore!
		canSaveDynamicFields = "1";
		position = %position;
		size = "5.000 5.000";
	};
	%bashAnimation.mount(%monster, 0.0, 0.0, 50.0, true, true, true, true);
	%bashAnimation.setLifetime(0.5);
}
function createSquadPowerRadarAnimation ()
{//echo("createSquadPowerRadarAnimation()");
	%character = $playerSquad[$activeCharacter];
	%position = %character.getPosition();
	%radarAnimation = new Sprite()
	{
		SceneLayer = 0;
		scenegraph = levelSceneGraph;
		Animation = "Radar_Animation_FramesAnimation";
		canSaveDynamicFields = "1";
		position = %position;
		size = "12.000 12.000";
	};
	%radarAnimation.mount(%radarAnimation, 0.0, 0.0, 50.0, true, true, true, true);
	%radarAnimation.setLifetime(0.5);
}
function createSquadPowerShieldAnimation ()
{//echo("createSquadPowerShieldAnimation()");
	%character = $playerSquad[$activeCharacter];
	%position = %character.getPosition();
	%shieldAnimation = new Sprite()
	{
		SceneLayer = 0;
		scenegraph = levelSceneGraph;
		Animation = "Sprites:ShieldAnimation";
		canSaveDynamicFields = "1";
		position = %position;
		size = "8.000 8.000";
	};
	addObjectToScene(%shieldAnimation);
	%shieldAnimation.mount(%monster, 0.0, 0.0, 50.0, true, true, true, true);
	%shieldAnimation.mount(%character, 0.0, 0.0, 50.0, true, true, true, true);
}
function createLeaderIdleAnimation ()
{//echo("createLeaderIdleAnimation()");
	%leaderAnimation = new Sprite()
	{
		SceneLayer = 0;
		scenegraph = levelSceneGraph;
		Animation = "LeaderIdle2Animation";
		canSaveDynamicFields = "1";
		position = "0 0";
		size = "4.000 4.000";
	};
	return %leaderAnimation;
}
function createScoutIdleAnimation ()
{//echo("createScoutIdleAnimation()");
	%scoutAnimation = new Sprite()
	{
		SceneLayer = 0;
		scenegraph = levelSceneGraph;
		Animation = "ScoutIdle2Animation";
		canSaveDynamicFields = "1";
		position = "-4000, -4000";
		size = "4.000 4.000";
	};
	return %scoutAnimation;
}
function createElevatorZoneAnimation ()
{//echo("createElevatorZoneAnimation()");
	%elevatorZoneAnimation = new Sprite()
	{
		SceneLayer = $ElevatorZoneLayer;
		graph = $ElevatorZoneGraph;
		scenegraph = levelSceneGraph;
		Animation = "Sprites:ElevatorZone";
		canSaveDynamicFields = "1";
		position = "-9999.00 -9999.00";
		size = "4.000 4.000";
	};
	addObjectToScene(%elevatorZoneAnimation);
	return %elevatorZoneAnimation;
}
function createTargetIndicatorAnimation ()
{//echo("createTargetIndicatorAnimation()");
//return null;	// Disabled for now!
	%targetIndicatorAnimation = new Sprite()
	{
		SceneLayer = 11;
		graph = 11;
		scenegraph = levelSceneGraph;
		Animation = "Sprites:TargetIndicator";
		canSaveDynamicFields = "1";
		position = "-4000.00 -4000.00";
		size = "4.000 4.000";
	};
	addObjectToScene(%targetIndicatorAnimation);
	return %targetIndicatorAnimation;
}
function createFreeIndicatorAnimation ()
{//echo("createFreeIndicatorAnimation()");
	%freeIndicatorAnimation = new Sprite()
	{
		SceneLayer = 11;
		scenegraph = levelSceneGraph;
		Animation = "Sprites:FreeIndicator";
		canSaveDynamicFields = "1";
		position = "-4000.00 -4000.00";
		size = "4.000 4.000";
	};
	addObjectToScene(%freeIndicatorAnimation);
	return %freeIndicatorAnimation;
}
function createMaxRangeIndicatorAnimation ()
{//echo("createMaxRangeIndicatorAnimation()");
	%maxRangeIndicatorAnimation = new Sprite()
	{
		SceneLayer = 11;
		scenegraph = levelSceneGraph;
		Animation = "Sprites:MaxRangeIndicator";
		canSaveDynamicFields = "1";
		position = "-4000.00 -4000.00";//%position;
		size = "4.000 4.000";
	};
	addObjectToScene(%maxRangeIndicatorAnimation);
	return %maxRangeIndicatorAnimation;
}
function createMouseIndicatorAnimation (%cursorType)
{//echo("createMouseIndicatorAnimation()");
	%Animation = "";
	switch (%cursorType)
	{
		case 0:	%Animation = "Sprites:cursorMoveAnimation";
		case 1: %Animation = "Sprites:cursorObstructionAnimation";
		case 2: %Animation = "Sprites:cursorEnemyAnimation";
		case 3: %Animation = "Sprites:cursorInteractAnimation";
	}
	%mouseIndicatorAnimation = new Sprite()
	{
		SceneLayer = 0;//$MouseIndicatorLayer;		// Below shadows on layer 11.
		graph = 0;//$MouseIndicatorGroup;		// Below shadows on graph 11.
		scenegraph = levelSceneGraph;
		Animation = %Animation;
		canSaveDynamicFields = "1";
		position = "-4000.00 -4000.00";
		size = "4.375 5.125";
	};
	addObjectToScene(%mouseIndicatorAnimation);
	return %mouseIndicatorAnimation;
}
function createAutoTargetAnimation (%xIndex, %yIndex)
{//echo("createAutoTargetAnimation()");
	if (isObject($autoTargetAnimation))
	{
		$autoTargetAnimation.safeDelete();
	}
	$autoTargetAnimation = new Sprite()
	{
		SceneLayer = 0;
		graph = 0;
		scenegraph = levelSceneGraph;
		Animation = "Sprites:AutoTarget";
		canSaveDynamicFields = "1";
		position = getTilePositionFromIndices(%xIndex, %yIndex);
		size = "4 4";
		xIndex = %xIndex;
		yIndex = %yIndex;
	};
	addObjectToScene($autoTargetAnimation);
}
function createFleeingCrittersAnimation (%xIndex, %yIndex)
{//echo("createFleeingCrittersAnimation()");
	%fleeingCrittersAnimation = new Sprite()
	{
		SceneLayer = 2;
		scenegraph = levelSceneGraph;
		Animation = "FleeingCrittersAnimation";
		canSaveDynamicFields = "1";
		position = getTilePositionFromIndices(%xIndex, %yIndex);
		size = "4.000 4.000";
	};
}
function createWallLightAnimation (%xIndex, %yIndex)
{//echo("createWallLightAnimation()");
	%wallLightAnimation = new Sprite()
	{
		SceneLayer = 25;// Wall it sits on is layer 26!
		scenegraph = levelSceneGraph;
		Animation = "Wall_Lights_FramesAnimation";
		canSaveDynamicFields = "1";
		position = getTilePositionFromIndices(%xIndex, %yIndex);
		size = "4.000 4.000";
	};
	return %wallLightAnimation;
}
function createSmashedBarrelAnimation (%xIndex, %yIndex)
{//echo("createSmashedBarrelAnimation()");
	%smashedBarrelAnimation = new Sprite()
	{
		SceneLayer = 25;// Wall it sits on is layer 26!
		scenegraph = levelSceneGraph;
		Animation = "barrelSmashFramesAnimation";
		canSaveDynamicFields = "1";
		position = getTilePositionFromIndices(%xIndex, %yIndex);
		size = "4.000 4.000";
	};
}
function createExperienceGainAnimation (%x, %y)
{//echo("createExperienceGainAnimation()");
	%experienceGainAnimation = new Sprite()
	{
		SceneLayer = 0;
		scenegraph = levelSceneGraph;
		Animation = "Sprites:ExperienceGain";
		canSaveDynamicFields = "1";
		position = %x SPC %y;
		size = "4.000 4.000";
	};
	addObjectToScene(%experienceGainAnimation);
	%experienceGainAnimation.setLifetime(0.75);
}
function createParalyzeAnimation (%characterIndex)
{//echo("createParalyzeAnimation()");
	%paralyzeAnimation = new Sprite()
	{
		SceneLayer = 0;
		scenegraph = levelSceneGraph;
		Animation = "ParalyzeFramesAnimation";
		canSaveDynamicFields = "1";
		position = $playerSquad[%characterIndex].getPosition();
		size = "4.000 4.000";
	};
	%paralyzeAnimation.mount($playerSquad[%characterIndex], 0.0, 0.0, 50.0, true, true, true, true);
	playAudio("Audio:paralyze", EffectsAudioVolume.value);
	return %paralyzeAnimation;
}
function createStunAnimation (%monsterIndex)
{//echo("createStunAnimation()");
return;// Nothing yet!
	%paralyzeAnimation = new Sprite()
	{
		SceneLayer = 0;
		scenegraph = levelSceneGraph;
		Animation = "ParalyzeFramesAnimation";
		canSaveDynamicFields = "1";
		position = ($monsterList.getObject(%monsterIndex)).getPosition();
		size = "4.000 4.000";
	};
	%paralyzeAnimation.mount($monsterList.getObject(%monsterIndex), 0.0, 0.0, 50.0, true, true, true, true);
	playAudio("Audio:paralyze", EffectsAudioVolume.value);
	return %paralyzeAnimation;
}
function createPromotionAnimation (%characterIndex)
{//echo("createPromotionAnimation()");
	%promotionAnimation = new Sprite()
	{
		SceneLayer = 0;
		scenegraph = levelSceneGraph;
		Animation = "Sprites:LevelUp";
		canSaveDynamicFields = "1";
		position = $playerSquad[%characterIndex].position;//"4000 4000";
		size = "4.000 4.000";
	};
	//%promotionAnimation.setLifetime(2.0);
	//%promotionAnimation.mount($playerSquad[%characterIndex], 0.0, 0.0, 50.0, true, true, true, true);
	$playerSquad[%characterIndex].promotionAnimation = %promotionAnimation;
	switch ($playerSquad[%characterIndex].characterClass)
	{
		case 0:	$leaderPromotionAnimation = %promotionAnimation;
		case 1:	$scoutromotionAnimation = %promotionAnimation;
		case 2:	$grenadierPromotionAnimation = %promotionAnimation;
		case 3:	$supportPromotionAnimation = %promotionAnimation;
		case 4:	$engineerPromotionAnimation = %promotionAnimation;
	}
	addObjectToScene(%promotionAnimation);
	updateEventText($squadColor @ $playerSquad[%characterIndex].name SPC $friendlyActionColor @ "ranks up" @ $defaultColor @ "!");
	return %promotionAnimation;
}
function createElectricalSparksAnimation (%monster)
{//echo("createElectricalSparksAnimation()");
return;	// Disabled for now!
	%eSparksAnimation = new Sprite()
	{
		SceneLayer = 0;
		scenegraph = levelSceneGraph;
		Animation = "Sprites:ElectricalSparks";
		canSaveDynamicFields = "1";
		position = "4000 4000";
		size = "4.000 4.000";
	};
	%eSparksAnimation.mount(%monster, 0.0, 0.0, 50.0, true, true, true, true);
	%eSparksAnimation.setLifetime(0.2);
}
function createLaserAnimation ()
{//echo("createLaserAnimation()");
	%laserAnimation = new Sprite()
	{
		SceneLayer = $WeaponAttachmentLayer;
		scenegraph = levelSceneGraph;
		Animation = "Sprites:LaserAnimation";
		canSaveDynamicFields = "1";
		position = "4000 4000";
		size = "4.000 4.000";
	};
	return %laserAnimation;
}
function createElevatorArrowAnimation ()
{//echo("createElevatorArrowAnimation()");
	if (isObject($elevatorArrowAnimation))
	{
		$elevatorArrowAnimation.safeDelete();
	}
	$elevatorArrowAnimation = new Sprite()
	{
		SceneLayer = 3;//1;	// Use layer 1 if animation is layered over squad members!
		scenegraph = levelSceneGraph;
		Animation = "Sprites:ElevatorArrow";
		canSaveDynamicFields = "1";
		position = $stairsPositionX SPC $stairsPositionY + 1;
		size = "4.000 4.000";
	};
	addObjectToScene($elevatorArrowAnimation);
	return $elevatorArrowAnimation;
}
function updateElevatorArrowAnimation ()
{//echo("updateElevatorArrowAnimation()");
	if (!isObject($elevatorArrowAnimation))
	{
		createElevatorArrowAnimation();
	}
	if ($elevatorPowered)
	{
		$elevatorArrowAnimation.Animation = "Sprites:ElevatorArrow";
	}
	else
	{
		$elevatorArrowAnimation.Animation = "Sprites:ElevatorUnpoweredArrow";
	}
	return $elevatorArrowAnimation;
}
function createLandmineAnimation (%xIndex, %yIndex)
{//echo("createLandmineAnimation()");
	%SceneLayer = $TrapLayer;
	%group = $TrapGroup;
	%position = getTileShiftedPositionFromIndices(%xIndex, %yIndex);
	if (isObject($mazeLiftedTiles[%xIndex, %yIndex]))
	{
		%SceneLayer = $TrapLiftedLayer;
		%group = $TrapLiftedGroup;
	}
	%landmineAnimation = new Sprite()
	{
		SceneLayer = $TrapLayer;
		graph = %group;
		scenegraph = levelSceneGraph;
		image = "Sprites:Set1TrapFloorActivated";
		canSaveDynamicFields = "1";
		position = %position;
		size = "4.000 4.000";
	};
	addObjectToScene(%landmineAnimation);
	return %landmineAnimation;
}
function createGuardModeIndicator (%characterIndex)
{//echo("createGuardModeIndicator()");
	%guardModeIndicatorAnimation = new Sprite()
	{
		SceneLayer = 0;		// Squad members are layer 3!
		graph = 0;			// Squad members are graph 3!
		scenegraph = levelSceneGraph;
		Animation = "Sprites:GuardModeIndicatorFramesAnimation";
		position = (getWord($playerSquad[%characterIndex].getPosition(), 0) + 4) SPC getWord($playerSquad[%characterIndex].getPosition(), 1);
		size = "4.000 4.000";
		visible = true;
		characterIndex = %characterIndex;
	};
	addObjectToScene(%guardModeIndicatorAnimation);
//%guardModeIndicatorAnimation.mount($playerSquad[%characterIndex], 0.0, 0.0 + ($GlobalVerticalShift / 2), 50.0, true, true, true, true);
	$GuardModeAnimationList.add(%guardModeIndicatorAnimation);
}
function createFrostEffect (%monsterIndex)
{//echo("createFrostEffect()");
	%monster = $monsterList.getObject(%monsterIndex);
	%position = %monster.getPosition();
	%frostAnimation = new Sprite()
	{
		SceneLayer = 0;		// Squad members are layer 3!
		graph = 0;		// Squad members are graph 3!
		scenegraph = levelSceneGraph;
		Animation = "frostTrollChargeFramesAnimation";
		position = "4000 4000";
		size = "4.000 4.000";
		monsterIndex = %monsterIndex;
	};
	%frostAnimation.mount(%monster, 0.0, 0.0 + ($GlobalVerticalShift / 2), 50.0, true, true, true, true);
	%frostAnimation.setLifetime(0.25);
	%roll = getRandom(0, 1);
	if (%roll <= 50)	// Just use a 50% chance to reduce AP for now.
	{
		%monster.currentActionPoints--;
		updateMonsterFloatingActionPointText(%monsterIndex);
		if (%monster.currentActionPoints < 0)
		{
			%monster.currentActionPoints = 0;
		}
		scrollFloatingText(%monster, "-1 AP", "apLoss");
	}
}
function createAcidEffect (%monsterIndex)
{//echo("createAcidEffect()");
	%monster = $monsterList.getObject(%monsterIndex);
	%position = %monster.getPosition();
	%acidAnimation = new Sprite()
	{
		SceneLayer = 0;
		graph = 0;
		scenegraph = levelSceneGraph;
		Animation = "frostTrollChargeFramesAnimation";
		position = "4000 4000";
		size = "4.000 4.000";
		monsterIndex = %monsterIndex;
	};
	%acidAnimation.mount(%monster, 0.0, 0.0 + ($GlobalVerticalShift / 2), 50.0, true, true, true, true);
	%acidAnimation.setLifetime(0.25);
	%roll = getRandom(0, 1);
	if (%roll <= 100)		// Just use a 100% chance to reduce AP for now.
	{
		%monster.armorRating--;
		if (%monster.armorRating < 0)
		{
			%monster.armorRating = 0;
		}
		scrollFloatingText(%monster, "-1 AR", "arLoss");
	}
}
function createStasisAnimation (%monster)
{//echo("createStasisAnimation()");
	%position = %monster.getPosition();
	%stasisAnimation = new Sprite(stasisAnimation)
	{
		SceneLayer = 0;
		scenegraph = levelSceneGraph;
		Animation = "Sprites:ShieldAnimation";
		canSaveDynamicFields = "1";
		position = %position;
		size = "8.000 8.000";
	};
	addObjectToScene(%stasisAnimation);
	//%stasisAnimation.mount(%monster, 0.0, 0.0, 50.0, true, true, true, true);
	%monster.stasisAnimation = %stasisAnimation;
}
function createSteamVentingAnimation (%steamTile)
{//echo("createSteamVentingAnimation()");
	%position = %steamTile.getPosition();
	%steamVentingAnimation = new Sprite()
	{
		SceneLayer = $SteamVentingLayer;
		scenegraph = levelSceneGraph;
		Animation = "Sprites:SteamVenting3";
		canSaveDynamicFields = "1";
		position = getWord(%position, 0) + getRandom(0, 15) * 0.1 SPC getWord(%position, 1) + 2.0;
		size = "4 6";
	};
	%steamTile.steamVentingAnimation = %steamVentingAnimation;
	addObjectToScene(%steamVentingAnimation);
}
function createLaserBeamAnimation (%xIndex, %yIndex, %dstXIndex, %dstYIndex, %direction, %range)
{//echo("createLaserBeamAnimation()");
	%laserBeamAnimation = new Sprite()
	{
		SceneLayer = $ProjectileLayer;
		graph = $ProjectileGraph;
		scenegraph = levelSceneGraph;
		Animation = "Sprites:LaserBeam";
		canSaveDynamicFields = "1";
		position = getTilePositionFromIndices(%xIndex, %yIndex);
		size = "4 2";
		xIndex = %xIndex;
		yIndex = %yIndex;
	};
	%laserBeamAnimation.setLifetime(0.5);
	if (%direction == $northDirection	||
		%direction == $southDirection)
	{
		%laserBeamAnimation.rotation = 90;
	}
	playAudio("Audio:LaserBeam", EffectsAudioVolume.value);
	%moveToPosition = "0 0";
	
	
	// HACK
	// Recalculate %dstXIndex and %dstXIndex because something gets fucked up when firing across open door tiles!
	switch (%direction)
	{
		case $northDirection:
			%dstXIndex = %xIndex;
			%dstYIndex = %yIndex - %range;
			
		case $southDirection:
			%dstXIndex = %xIndex;
			%dstYIndex = %yIndex + %range;
			
		case $eastDirection:
			%dstXIndex = %xIndex + %range;
			%dstYIndex = %yIndex;
			
		case $westDirection:
			%dstXIndex = %xIndex - %range;
			%dstYIndex = %yIndex;
	}
	// END OF HACK
	
	
	addObjectToScene(%laserBeamAnimation);
	%moveToPosition = getTilePositionFromIndices(%dstXIndex, %dstYIndex);
	%laserSpeed = 80.0;
	%laserBeamAnimation.moveTo(getWord(%moveToPosition, 0), getWord(%moveToPosition, 1), %laserSpeed, true, false);
}
function createPlasmaBlastAnimation (%xIndex, %yIndex, %dstXIndex, %dstYIndex, %direction, %range)
{//echo("createPlasmaBlastAnimation()");
	%plasmaBlastAnimation = new Sprite()
	{
		SceneLayer = $ProjectileLayer;
		graph = $ProjectileGraph;
		scenegraph = levelSceneGraph;
		Animation = "PlasmaPulseFramesAnimation";
		canSaveDynamicFields = "1";
		position = getTilePositionFromIndices(%xIndex, %yIndex);
		size = "4 2";
		xIndex = %xIndex;
		yIndex = %yIndex;
	};
	%plasmaBlastAnimation.setLifetime(0.5);
	if (%direction == $northDirection)
	{
		%plasmaBlastAnimation.rotation = -90;
	}
	if (%direction == $southDirection)
	{
		%plasmaBlastAnimation.rotation = 90;
	}
	playAudio("Audio:PlasmaBlast", EffectsAudioVolume.value);
	%moveToPosition = "0 0";
	
	
	// HACK
	// Recalculate %dstXIndex and %dstYIndex because something gets fucked up when firing across open door tiles!
	switch (%direction)
	{
		case $northDirection:
			%dstXIndex = %xIndex;
			%dstYIndex = %yIndex - %range;
			
		case $southDirection:
			%dstXIndex = %xIndex;
			%dstYIndex = %yIndex + %range;
			
		case $eastDirection:
			%dstXIndex = %xIndex + %range;
			%dstYIndex = %yIndex;
			
		case $westDirection:
			%dstXIndex = %xIndex - %range;
			%dstYIndex = %yIndex;
	}
	// END OF HACK
	
	
	%moveToPosition = getTilePositionFromIndices(%dstXIndex, %dstYIndex);
	%laserSpeed = 80.0;
	%plasmaBlastAnimation.moveTo(getWord(%moveToPosition, 0), getWord(%moveToPosition, 1), %laserSpeed, true, false);
}
function createPlasticExplosiveAnimation (%xIndex, %yIndex)
{//echo("createPlasticExplosiveAnimation()");
	%plasticExplosiveAnimation = new Sprite()
	{
		SceneLayer = $PlasticExplosiveLayer;
		scenegraph = levelSceneGraph;
		Animation = "Sprites:PlasticExplosive";
		canSaveDynamicFields = "1";
		position = getTilePositionFromIndices(%xIndex, %yIndex);
		size = "4 4";
		xIndex = %xIndex;
		yIndex = %yIndex;
	};
	addObjectToScene(%plasticExplosiveAnimation);
	$mazeRoom[%xIndex, %yIndex].plasticExplosive = %plasticExplosiveAnimation;
	//%plasticExplosiveAnimation.mount($mazeRoom[%xIndex, %yIndex], 0.0, 0.0, 50.0, true, true, true, true);
}
function createOlivineAnimation (%xIndex, %yIndex)//createOlivineAnimation($playerSquad[0].xIndex, $playerSquad[0].yIndex);
{//echo("createOlivineAnimation()");
	%olivineAnimation = new Sprite()
	{
		SceneLayer = $mazeRoom[%xIndex, %yIndex].layer;
		graph = $mazeRoom[%xIndex, %yIndex].graph;
		scenegraph = levelSceneGraph;
		Animation = "Sprites:OlivineRain";
		canSaveDynamicFields = "1";
		position = getTilePositionFromIndices(%xIndex - 1, %yIndex -1);
		size = "4 4";
	};
	addObjectToScene(%olivineAnimation);
	%olivineAnimation.moveTo(getWord(getTilePositionFromIndices(%xIndex, %yIndex), 0), getWord(getTilePositionFromIndices(%xIndex, %yIndex), 1), 5.0, true, false);
	%olivineAnimation.setLifetime(0.3);
	//playAudio("Audio:olivineRain", EffectsAudioVolume.value);
}
function createMeteorShowerAnimation (%xIndex, %yIndex)//createMeteorShowerAnimation($playerSquad[0].xIndex, $playerSquad[0].yIndex);
{//echo("createMeteorShowerAnimation()");
	%weatherAnimation = new Sprite()
	{
		SceneLayer = $mazeRoom[%xIndex, %yIndex].layer;
		graph = $mazeRoom[%xIndex, %yIndex].graph;
		scenegraph = levelSceneGraph;
		Animation = "Sprites:MeteorShower";
		canSaveDynamicFields = "1";
		position = getTilePositionFromIndices(%xIndex - 1, %yIndex -1);
		size = "4 4";
	};
	addObjectToScene(%weatherAnimation);
	%weatherAnimation.moveTo(getWord(getTilePositionFromIndices(%xIndex, %yIndex), 0), getWord(getTilePositionFromIndices(%xIndex, %yIndex), 1), 5.0, true, false);
	%weatherAnimation.setLifetime(0.3);
	//playAudio("Audio:meteorShower", EffectsAudioVolume.value);
}
function createElevatorDownAnimation (%characterIndex)
{//echo("createElevatorDownAnimation()");
	if (isObject($playerSquad[%characterIndex].elevatorDownAnimation))
	{
		($playerSquad[%characterIndex].elevatorDownAnimation).safeDelete();
	}
	%elevatorDownAnimation = new Sprite()
	{
		SceneLayer = 0;
		graph = 0;
		scenegraph = levelSceneGraph;
		Animation = "Sprites:ElevatorReady";
		canSaveDynamicFields = "1";
		position = getTilePositionFromIndices($playerSquad[%characterIndex].xIndex, $playerSquad[%characterIndex].yIndex);
		size = "4 4";
	};
	addObjectToScene(%elevatorDownAnimation);
	$playerSquad[%characterIndex].elevatorDownAnimation = %elevatorDownAnimation;
	%elevatorDownAnimation.setLifetime(1);
}
function triggerElevatorDownAnimations ()
{
	if ($generateTutorialLevel)
	{
		if ($tutorialSection == 10)
		{
			tutorialMessage11();
		}
	}
	if ($leaderPersist)
	{
		createElevatorDownAnimation(0);
	}
	if ($scoutPersist)
	{
		createElevatorDownAnimation(1);
	}
	if ($grenadierPersist)
	{
		createElevatorDownAnimation(2);
	}
	if ($supportPersist)
	{
		createElevatorDownAnimation(3);
	}
	playAudio("Audio:elevatorReady", EffectsAudioVolume.value);
}
function createThrownGrenadeAnimation (%monster, %targetXIndex, %targetYIndex)
{//echo("createThrownGrenadeAnimation()");
	%thrownGrenadeAnimation = new Sprite()
	{
		SceneLayer = 0;
		graph = 0;
		scenegraph = levelSceneGraph;
		Animation = "ThrownGrenadeAnimationAnimation";
		canSaveDynamicFields = "1";
		position = getTilePositionFromIndices(%monster.xIndex, %monster.yIndex);
		size = "4 4";
	};
	%thrownGrenadeAnimation.setLifetime(0.5);
	%targetPosition = getTilePositionFromIndices(%targetXIndex, %targetYIndex);
	%thrownGrenadeAnimation.moveTo(getWord(%targetPosition, 0), getWord(%targetPosition, 1), $ThrownGrenadeSpeed, true, false);
}
function createSteamCloudAnimation (%monster)//createSteamCloudAnimation($playerSquad[$activeCharacter]);
{//echo("createSteamCloudAnimation()");
return;	// Disabled for now!
	%steamCloudAnimation = new Sprite()
	{
		SceneLayer = 0;
		graph = 0;
		scenegraph = levelSceneGraph;
		Animation = "Sprites:SteamClouds";
		canSaveDynamicFields = "1";
		position = getTilePositionFromIndices(%monster.xIndex, %monster.yIndex);
		size = "4 4";
	};
	// Set randomized position and mirroring.
	if (getRandom(0, 1))	// Upper right corner.
	{
		%steamCloudAnimation.position = getWord(%steamCloudAnimation.position, 0) + 2.5 SPC getWord(%steamCloudAnimation.position, 1) - 2.5;
	}
	else					// Upper left corner.
	{
		%steamCloudAnimation.position = getWord(%steamCloudAnimation.position, 0) - 2.5 SPC getWord(%steamCloudAnimation.position, 1) - 2.5;
		%steamCloudAnimation.setFlipX(1);
	}
	%steamCloudAnimation.setLifetime(0.30);
}
function createCasingEjectionAnimation (%firingObject)
{//echo("createCasingEjectionAnimation()");
return;		// Disabled for now!
	%animation = new Sprite()
	{
		SceneLayer = 0;
		graph = 0;
		scenegraph = levelSceneGraph;
		Animation = "CasingEjectionFramesAnimation";
		canSaveDynamicFields = "1";
		position = getTilePositionFromIndices(%firingObject.xIndex, %firingObject.yIndex);
		size = "4 4";
	};
	switch (%firingObject.facing)
	{
		case $northDirection:
			%animation.SceneLayer = %firingObject.layer + 1;
			%animation.setFlipX(1);
		case $southirection:
		case $eastDirection:
		case $westDirection:
			%animation.SceneLayer = %firingObject.layer + 1;
			%animation.setFlipX(1);
	}
	%animation.setLifetime(0.5);
	schedule(450, 0, "playBulletCasingDropSfx");
}
function createDisruptorAnimation (%xIndex, %yIndex)
{//echo("createDisruptorAnimation()");
	if (isObject($DisruptorList))
	{
		for (%i = 0; %i < $DisruptorList.getCount(); %i++)
		{
			$DisruptorList.getObject(%i).safeDelete();
		}
	}
	else
	{
		$DisruptorList = new SimSet();
	}
	$ElevatorDisruptorXIndex = %xIndex;
	$ElevatorDisruptorYIndex = %yIndex;
	%animation = new Sprite()
	{
		SceneLayer = $DisruptorLayer;
		graph = $DisruptorGraph;
		scenegraph = levelSceneGraph;
		Animation = "Sprites:Disruptor";
		canSaveDynamicFields = "1";
		position = getTilePositionFromIndices(%xIndex, %yIndex);
		size = "4 4";
		xIndex = $ElevatorDisruptorXIndex;
		yIndex = $ElevatorDisruptorYIndex;
	};
	$ElevatorDisruptor = %animation;
	addObjectToScene($ElevatorDisruptor);
	$elevatorPowered = false;
	$DisruptorList.add(%animation);
}
function createDisruptorExplosionAnimation (%xIndex, %yIndex)
{//echo("createDisruptorExplosionAnimation()");
	$ElevatorDisruptorXIndex = -1;
	$ElevatorDisruptorYIndex = -1;
	%animation = new Sprite()
	{
		SceneLayer = $DisruptorLayer;
		graph = $DisruptorGraph;
		scenegraph = levelSceneGraph;
		Animation = "Sprites:DisruptorExplosion";
		canSaveDynamicFields = "1";
		position = getTilePositionFromIndices(%xIndex, %yIndex);
		size = "8 8";
		xIndex = %xIndex;
		yIndex = %yIndex;
	};
	addObjectToScene(%animation);
	%animation.setLifetime(0.4);
	$DisruptorList.safeDelete();	// Eh... what if we need this again later?
}
function createCryoTubeAnimation (%xIndex, %yIndex)
{//echo("createCryoTubeAnimation()");
	%animation = new Sprite()
	{
		SceneLayer = $CryoTubeLayer;
		scenegraph = levelSceneGraph;
		Animation = "Sprites:CryoTube";
		canSaveDynamicFields = "1";
		position = getTilePositionFromIndices(%xIndex, %yIndex);
		size = "4 6";
		xIndex = %xIndex;
		yIndex = %yIndex;
		name = "Alien Merchant";
	};
	addObjectToScene(%animation);
	$CryoTubeList.add(%animation);
}
function createHulkSuitExplosionAnimation (%xIndex, %yIndex)
{//echo("createHulkSuitExplosionAnimation()");
	%animation = new Sprite()
	{
		SceneLayer = 0;
		graph = 0;
		scenegraph = levelSceneGraph;
		Animation = "DisruptorExplosionFramesAnimation1";
		canSaveDynamicFields = "1";
		position = getTilePositionFromIndices(%xIndex, %yIndex);
		size = "8 8";
		xIndex = %xIndex;
		yIndex = %yIndex;
	};
	%animation.setLifetime(0.4);
}
function createPumpAnimation (%xIndex, %yIndex)
{//echo("createPumpAnimation()");
return;	// Disabled for now!
	%animation = new Sprite()
	{
		SceneLayer = $PumpLayer;
		graph = $PumpGraph;
		scenegraph = levelSceneGraph;
		Animation = "Sprites:Pump";
		canSaveDynamicFields = "1";
		position = getTilePositionFromIndices(%xIndex, %yIndex);
		size = "4 4";
		xIndex = %xIndex;
		yIndex = %yIndex;
	};
	addObjectToScene(%animation);
	//%animation.mount($mazeRoom[%xIndex, %yIndex], 0.0, -0.6, 50.0, true, true, true, true);
	$mazeRoom[%xIndex, %yIndex].pump = %animation;
}
function createAutoSwitchAnimation (%xIndex, %yIndex)
{//echo("createAutoSwitchAnimation()");
	%animation = new Sprite()
	{
		SceneLayer = 0;
		graph = 0;
		scenegraph = levelSceneGraph;
		Animation = "Sprites:AutoSwitchPending";
		canSaveDynamicFields = "1";
		position = getTilePositionFromIndices(%xIndex, %yIndex);
		size = "4 4";
		xIndex = %xIndex;
		yIndex = %yIndex;
	};
	addObjectToScene(%animation);
	%animation.setLifetime($autoSwitchSquadMemberDelay / 1000.0);
	$AutoSwitchAnimation = %animation;
}
function createHulkFlamethrowerAnimation (%xIndex, %yIndex, %direction)
{//echo("createHulkFlamethrowerAnimation()");
	%xOffset = 0;
	%yOffset = 0;
	%rotation = 0;
	switch (%direction)
	{
		case $northDirection:
			%xOffset = 0;
			%yOffset = -6;//-4;
			%rotation = -90;
		
		case $southDirection:
			%xOffset = 0;
			%yOffset = 5;//4;
			%rotation = 90;
		
		case $eastDirection:
			%xOffset = 6;//4;
			%yOffset = 0;
			%rotation = 0;
		
		case $westDirection:
			%xOffset = -6;//-4;
			%yOffset = 0;
			%rotation = 180;
	}
	%position = getTilePositionFromIndices(%xIndex, %yIndex);
	%SceneLayer = 0;
	%graph = 0;
	if (%direction == $southDirection)
	{
		%SceneLayer = 5;
		%graph = 5;
	}
	%animation = new Sprite()
	{
		SceneLayer = %SceneLayer;
		scenegraph = levelSceneGraph;
		Animation = "Sprites:HulkFlamethrower";
		canSaveDynamicFields = "1";
		position = (getWord(%position, 0) + %xOffset) SPC (getWord(%position, 1) + %yOffset);
		size = "10 8";
		xIndex = %xIndex;
		yIndex = %yIndex;
	};
	addObjectToScene(%animation);
	%animation.angle = %rotation;
	//%animation.setBlending(true, "MINUS_DST_COLOR", "ONE_MINUS_SRC_ALPHA");
	%animation.setLifetime(0.60);
	playAudio("Audio:flamethrower", EffectsAudioVolume.value);
	schedule(375, 0, "playAudio", "Audio:flamethrower", EffectsAudioVolume.value);
}
function createGrenadierExplosion (%xIndex, %yIndex, %character)
{//echo("createGrenadierExplosion()");
	%explosionPosition = getTilePositionFromIndices(%xIndex, %yIndex);
	%grenadierExplosionAnimation = new Sprite()
	{
		SceneLayer = 0;
		scenegraph = levelSceneGraph;
		Animation = "Sprites:Grenadier" @ getProjectileTypeName(%character.weapon.projectileType) @ "Explosion";
		canSaveDynamicFields = "1";
		position = %explosionPosition;
		size = "6.000 6.000";
	};
	addObjectToScene(%grenadierExplosionAnimation);
	%grenadierExplosionAnimation.setLifetime(0.25);
	playExplosionSfx();
}
function createTargetScanAnimation (%unit)
{//echo("createTargetScanAnimation()");
	%position = getTilePositionFromIndices(%unit.xIndex, %unit.yIndex);
	%animation = new Sprite()
	{
		SceneLayer = 0;
		scenegraph = levelSceneGraph;
		Animation = "Sprites:TargetScan";
		canSaveDynamicFields = "1";
		position = %position;
		size = "6.000 6.000";
	};
	addObjectToScene(%animation);
	%animation.setLifetime(0.75);
}
function createTargetScanCastAnimation (%unit)
{//echo("createTargetScanCastAnimation()");
	%position = %unit.getPosition();
	%radarAnimation = new Sprite()
	{
		SceneLayer = 0;
		scenegraph = levelSceneGraph;
		Animation = "TargetScanCastAnimation";
		canSaveDynamicFields = "1";
		position = %position;
		size = "12.000 12.000";
	};
	%radarAnimation.mount(%radarAnimation, 0.0, 0.0, 50.0, true, true, true, true);
	%radarAnimation.setLifetime(0.75);
}
function createLiftedTileMarkerAnimation (%unit)
{//echo("createLiftedTileMarkerAnimation()");
	%animation = new Sprite()
	{
		SceneLayer = 1;//0;	// 0 puts it on top of fog of war!
		scenegraph = levelSceneGraph;
		Animation = "Sprites:LiftedTileMarker";
		position = "-9999 -9999";
		size = "0.5 0.5";
	};
	%xOffset = 1.25;
	%yOffset = 1.25;
	if (isLargeUnit(%unit))
	{//echo("is large unit!");
		%xOffset = 0.75;
		%yOffset = 0.75;
	}
	%animation.position = getWord(%unit.position, 0) + %xOffset SPC getWord(%unit.position, 1) + %yOffset;
	addObjectToScene(%animation);
	%unit.liftedTileMarker = %animation;
	return %animation;
}

//----------------------------------

// Test functions for Gunship beam attack!
function tba (%direction)//tba($EastDirection);
{//echo("tba()");
	createGunshipBeamAttackAnimation($playerSquad[$activeCharacter].xIndex,
									 $playerSquad[$activeCharacter].yIndex,
									 %direction);
}
function createGunshipBeamAttackAnimation (%xIndex, %yIndex, %direction)
{//echo("createGunshipBeamAttackAnimation()");
	// Gunship firing.
	%animation = new Sprite()
	{
		SceneLayer = 0;
		scenegraph = levelSceneGraph;
		Animation = "Sprites:GunshipFire" @ getDirectionName(%direction) @ "Animation";
		position = getTilePositionFromIndices(%xIndex, %yIndex);
		size = "8 8";
	};
	addObjectToScene(%animation);
	schedule($GunshipBeamDelay, 0, "continueGunshipBeamAttackAnimation", %xIndex, %yIndex, %direction);
}
// End Test Functions for Gunship beam attack!

function fireGunshipBeamAnimation (%unit)
{//echo("fireGunshipBeamAnimation()");
	playAudio("Audio:gunshipBeamAttack", EffectsAudioVolume.value);
	%unit.currentActionPoints = 0;
	%oldAnimation = %unit.Animation;
	%direction = %unit.facing;
	%xIndex = %unit.xIndex;
	%yIndex = %unit.yIndex;
	%position = getTilePositionFromIndices(%xIndex, %yIndex);
	%isLifted = isObject($mazeLiftedTiles[%xIndex, %yIndex]);
	if (%isLifted)
	{
		%position = getWord(%position, 0) SPC getWord(%position, 1) - $LiftedTileVerticalShift + $GunshipBeamHeightAdjustment;
	}
	%unit.Animation = "Sprites:GunshipFire" @ getDirectionName(%direction) @ "Animation";
	schedule($GunshipBeamDelay, 0, "continueGunshipBeamAttackAnimation", %xIndex, %yIndex, %direction, %isLifted);
	schedule(1200, 0, "setUnitAnimation", %unit, %oldAnimation);
}
function setUnitAnimation (%unit, %Animation)
{//echo("setUnitAnimation()");
	%unit.Animation = %Animation;
}
function continueGunshipBeamAttackAnimation (%xIndex, %yIndex, %direction, %isLifted)
{//echo("continueGunshipBeamAttackAnimation()");
	%position = getTilePositionFromIndices(%xIndex, %yIndex);
	%rotation = 0;
	switch (%direction)
	{
		case $northDirection:
			%position = getWord(%position, 0) SPC getWord(%position, 1) - 6.9;
			%rotation = 180;
		case $southDirection:
			%position = getWord(%position, 0) SPC getWord(%position, 1) + 6.9;
			%rotation = 0;
		case $eastDirection:
			%position = getWord(%position, 0) + 8 SPC getWord(%position, 1) + 1.3;
			%rotation = 270;
		case $westDirection:
			%position = getWord(%position, 0) - 8 SPC getWord(%position, 1) + 1.3;
			%rotation = 90;
	}
	if (%isLifted)
	{
		%position = getWord(%position, 0) SPC getWord(%position, 1) - $LiftedTileVerticalShift + $GunshipBeamHeightAdjustment;
	}
	%animation = new Sprite()
	{
		SceneLayer = 0;
		scenegraph = levelSceneGraph;
		Animation = "Sprites:GunshipBeamAnimation";
		position = %position;
		size = "8 8";
		angle = %rotation;
	};
	addObjectToScene(%animation);
	%animation.setLifetime(0.375);
	schedule($GunshipPersistBeamDelay, 0, "persistGunshipBeamAttack", %xIndex, %yIndex, %direction, %isLifted);
}
function persistGunshipBeamAttack (%xIndex, %yIndex, %direction, %isLifted)
{//echo("persistGunshipBeamAttack()");
	// Gunship follow-through beam.
	%position = getTilePositionFromIndices(%xIndex, %yIndex);
	if (%isLifted)
	{
		%position = getWord(%position, 0) SPC getWord(%position, 1) - $LiftedTileVerticalShift + $GunshipBeamHeightAdjustment;
	}
	%rotation = 0;
	%xOffset = 0;
	%yOffset = 0;
	switch (%direction)
	{
		case $northDirection:
			%position = getWord(%position, 0) SPC getWord(%position, 1) - 14.9;
			%rotation = 180;
			%xOffset = 0;
			%yOffset = -8;
		case $southDirection:
			%position = getWord(%position, 0) SPC getWord(%position, 1) + 14.9;
			%rotation = 0;
			%xOffset = 0;
			%yOffset = 8;
		case $eastDirection:
			%position = getWord(%position, 0) + 16 SPC getWord(%position, 1) + 1.3;
			%rotation = 90;
			%xOffset = 8;
			%yOffset = 0;
		case $westDirection:
			%position = getWord(%position, 0) - 16 SPC getWord(%position, 1) + 1.3;
			%rotation = 270;
			%xOffset = -8;
			%yOffset = 0;
	}
	for (%i = 0; %i < 6; %i++)
	{
		%animation = new Sprite()
		{
			SceneLayer = 0;
			scenegraph = levelSceneGraph;
			Animation = "Sprites:GunshipBeamPersist";
			position = getWord(%position, 0) + %xOffset * %i SPC getWord(%position, 1) + %yOffset * %i;
			size = "8 8";
			angle = %rotation;
		};
		addObjectToScene(%animation);
		%animation.setLifetime(0.375);
	}
}
function createGunshipExplosionAnimation (%xIndex, %yIndex)
{//echo("createGunshipExplosionAnimation()");
	%animation = new Sprite()
	{
		SceneLayer = 0;
		scenegraph = levelSceneGraph;
		Animation = "Sprites:GunshipExplosion";
		position = getTilePositionFromIndices(%xIndex, %yIndex);
		size = "7.357 7.357";
	};
	addObjectToScene(%animation);
	%animation.setLifetime(0.4);
}
function createGunshipDeathAnimation (%xIndex, %yIndex)
{//echo("createGunshipDeathAnimation()");
	%animation = new Sprite()
	{
		SceneLayer = $monsterLayer;
		scenegraph = levelSceneGraph;
		Animation = "Sprites:GunshipDeath";
		position = getTilePositionFromIndices(%xIndex, %yIndex);
		size = "8 8";
	};
	addObjectToScene(%animation);
	return %animation;
}
function createMatrixDeathAnimation (%xIndex, %yIndex)
{//echo("createMatrixDeathAnimation()");
	%animation = new Sprite()
	{
		SceneLayer = $monsterLayer;
		scenegraph = levelSceneGraph;
		Animation = "Sprites:MatrixDeath";
		position = getTilePositionFromIndices(%xIndex, %yIndex);
		size = "8 8";
	};
	addObjectToScene(%animation);
	return %animation;
}
function createMatixAoeAnimation (%unit)//createMatixAoeAnimation($MonsterList.getObject(0));
{//echo("createMatixAoeAnimation()");
	%animation = new Sprite()
	{
		SceneLayer = $monsterLayer;
		scenegraph = levelSceneGraph;
		Animation = "Sprites:MatrixAoe";
		position = getTilePositionFromIndices(%unit.xIndex, %unit.yIndex);
		size = "8 8";
	};
	addObjectToScene(%animation);
	%animation.setLifetime(0.3);
	// Need sound effect!
	// Nothing here yet!
}
function createMatixUnitSpawnAnimation (%unit)//createMatixUnitSpawnAnimation($playerSquad[$activeCharacter]);
{//echo("createMatixUnitSpawnAnimation()");
	playAudio("Audio:gunshipBeamAttack", EffectsAudioVolume.value);
	%oldAnimation = %unit.Animation;
	%unit.Animation = "Sprites:MatrixSpawn";
	schedule(450, 0, "setUnitAnimation", %unit, %oldAnimation);
}
function createPatriarchChargeAnimation (%unit)//createMatixUnitSpawnAnimation($playerSquad[$activeCharacter]);
{//echo("createPatriarchChargeAnimation()");
	%oldAnimation = %unit.Animation;
	%unit.Animation = "Sprites:PatriarchCharge" @ getDirectionName(%unit.facing) @"Animation";
	schedule(450, 0, "setUnitAnimation", %unit, %oldAnimation);
}
function createPatriarchRingAnimation (%unit)
{//echo("createPatriarchRingAnimation()");
	%position = getTilePositionFromIndices(%unit.xIndex, %unit.yIndex);
	%animation = new Sprite()
	{
		SceneLayer = $SquadMemberLayer + 1;	// Place behind squad members!
		scenegraph = levelSceneGraph;
		Animation = "Sprites:PatriarchRing";
		position = getWord(%position, 0) SPC getWord(%position, 1) - 0.7;
		size = "4 4";
	};
	addObjectToScene(%animation);
	%animation.setLifetime(0.5);
}
function patriarchArmorOrAmmoLoss (%characterIndex)
{//echo("patriarchArmorOrAmmoLoss()");
	if (getRandom(1, 100) <= $PatriarchArmorLossChance)
	{
		// Armor loss.
		%armorLoss = $PatriarchArmorBreak;
		if (%armorLoss >= $playerSquad[%characterIndex].armor.armorRating)
		{
			$playerSquad[%characterIndex].armor.armorRating = 0;
		}
		else
		{
			$playerSquad[%characterIndex].armor.armorRating -= %armorLoss;
		}
		%dialogue = "What the &#@%?!?";
		switch (getRandom(0, 5))
		{
			case 0:	%dialogue = "What the &#@%?!?";
			case 1:	%dialogue = "Oh shi-!";
			case 2:	%dialogue = "Armor malfunction!";
			case 3:	%dialogue = "WHAT HAPPENED.";
			case 4:	%dialogue = "That didn't feel right.";
			case 5:	%dialogue = "I feel funny.";
		}
		setIndividualDialoguePopup($playerSquad[%characterIndex], %dialogue, "dialogue");
		scrollInfoText($playerSquad[%characterIndex], "-1 Armor!");
	}
	else
	{
		// Ammo loss.
		if (isLargeUnit($playerSquad[%characterIndex]))
		{
			useHulkRangedWeapon(%characterIndex, false);
		}
		else if (weaponHasRequiredAmmo(%characterIndex))
		{
			characterFire(%characterIndex);	// Can cause Hulk Suits to fire!
		}
		$playerSquad[%characterIndex].ammo = 0;
		($playerSquad[%characterIndex].weapon).currentAmmo = 0;
		setNoAmmoDialogue($playerSquad[%characterIndex]);
		updateFirearmReloadReadout();
	}
}
function patriarchRingSquad ()
{//echo("patriarchRingSquad()");
	if ($leaderPersist	&&
		isObject($playerSquad[0]))
	{
		createPatriarchRingAnimation($playerSquad[%characterIndex]);
		patriarchArmorOrAmmoLoss(0);
	}
	if ($scoutPersist	&&
		isObject($playerSquad[1]))
	{
		createPatriarchRingAnimation($playerSquad[1]);
		patriarchArmorOrAmmoLoss(1);
	}
	if ($grenadierPersist	&&
		isObject($playerSquad[2]))
	{
		createPatriarchRingAnimation($playerSquad[2]);
		patriarchArmorOrAmmoLoss(2);
	}
	if ($supportPersist	&&
		isObject($playerSquad[3]))
	{
		createPatriarchRingAnimation($playerSquad[3]);
		patriarchArmorOrAmmoLoss(3);
	}
}
function patriarchChargeAttack (%unit)
{//echo("patriarchChargeAttack()");
	createPatriarchChargeAnimation(%unit);
}
function patriarchAutoKnockBack (%monster, %characterIndex)
{//echo("patriarchAutoKnockBack()");//echo("direction =" SPC getReverseDirection(getDirectionBetweenUnits($playerSquad[%characterIndex], %monster)));
	characterHit(%characterIndex, $PatriarchKnockbackDamage, %monster, %monster.rangeDamageType, "normal");
	knockBackUnit($playerSquad[%characterIndex], getReverseDirection(getDirectionBetweenUnits($playerSquad[%characterIndex], %monster)));
	scrollInfoText($playerSquad[%characterIndex], "Auto-Knockback!");
	$playerSquad[%characterIndex].currentHealth -= $PatriarchKnockbackDamage;
	if ($playerSquad[%characterIndex].currentHealth <= 0)
	{
		$playerSquad[%characterIndex].currentHealth = 0;
		executeSquadMemberDeath(%characterIndex, false, "Patriarch");
	}
}