// Facing Handler.
// Worthless Bums 2012.


// Debug function that spits out each squad member's facing.
function printFacing ()
{//echo("printFacing()");
	if ($leaderPersist)
	{
		echo("Leader facing = " @ getFacing($playerSquad[0].facing));
	}
	if ($scoutPersist)
	{
		echo("Scout facing = " @ getFacing($playerSquad[1].facing));
	}
	if ($grenadierPersist)
	{
		echo("Grenadier facing = " @ getFacing($playerSquad[2].facing));
	}
	if ($supportPersist)
	{
		echo("Support facing = " @ getFacing($playerSquad[3].facing));
	}
}
function getFacing (%facing)
{//echo("getFacing()"):
	switch (%facing)
	{
		case $northDirection:	return "South";//"North";
		case $eastDirection:	return "East";
		case $southDirection:	return "North";//"South";
		case $westDirection:	return "West";
	}
}
function getReverseDirection (%direction)
{//echo("getReverseDirection()");
	switch (%direction)
	{
		case $northDirection:	return $southDirection;
		case $eastDirection:	return $westDirection;
		case $southDirection:	return $northDirection;
		case $westDirection:	return $eastDirection;
	}
}
function setSquadMemberIndexFacing (%direction, %unit, %characterIndex)
{//echo("setSquadMemberIndexFacing()");
	%unit.facing = %direction;
	%unit.Animation = "Sprites:" @ getUnitIdleAnimation(%unit);
	// Set weapon sprite properties for facing!
	/*switch (%unit.facing)
	{
		case $northDirection:
			//%weaponSprite.dismount();
			%weaponSprite.setFlipX(1);
			%weaponSprite.SceneLayer = %unit.layer + 1;
			%weaponSprite.group = %unit.group + 1;
			//%weaponSprite.mount(%unit, 0.0, 0.0, 50.0, true, true, true, false);
		case $southDirection:
			//%weaponSprite.dismount();
			%weaponSprite.setFlipX(0);
			%weaponSprite.SceneLayer = %unit.layer;
			%weaponSprite.group = %unit.group;
			//%weaponSprite.mount(%unit, 0.0, 0.0, 50.0, true, true, true, false);
		case $eastDirection:
			//%weaponSprite.dismount();
			%weaponSprite.setFlipX(0);
			%weaponSprite.SceneLayer = %unit.layer;
			%weaponSprite.group = %unit.group;
			//%weaponSprite.mount(%unit, 0.0, 0.0, 50.0, true, true, true, false);
		case $westDirection:
			//%weaponSprite.dismount();
			%weaponSprite.setFlipX(1);
			%weaponSprite.SceneLayer = %unit.layer + 1;
			%weaponSprite.group = %unit.group + 1;
			//%weaponSprite.mount(%unit, 0.0, 0.0, 50.0, true, true, true, false);
	}*/
	mountSightToUnit($playerSquad[%characterIndex]);
}
function enforceSquadFacing ()
{//echo("enforceSquadFacing()");
	if ($leaderPersist)
	{
		setSquadMemberFacing($playerSquad[0].facing, $playerSquad[0]);
	}
	if ($scoutPersist)
	{
		setSquadMemberFacing($playerSquad[1].facing, $playerSquad[1]);
	}
	if ($grenadierPersist)
	{
		setSquadMemberFacing($playerSquad[2].facing, $playerSquad[2]);
	}
	if ($supportPersist)
	{
		setSquadMemberFacing($playerSquad[3].facing, $playerSquad[3]);
	}
}
function setSquadMemberFacing (%direction, %unit)
{//echo("setSquadMemberFacing()");
	%weaponSprite = %unit.weaponSprite;
	%unit.facing = %direction;
	%unit.Animation = "Sprites:" @ getUnitIdleAnimation(%unit);
	if (!isLargeUnit(%unit)	&&
		isObject(%weaponSprite))
	{
		mountSightToUnit($playerSquad[$activeCharacter]);
	}
	autoTargetEnemyUnit($autoTargetDelay);
}
function setMonsterFacing (%direction, %monster)
{//echo("setMonsterFacing()");
	%monster.facing = %direction;
	%monster.animation = "Sprites:" @ %monster.name @ "Idle" @ getDirectionName(%monster.facing) @ "Animation";
}
function getAdjacentFacingMonster (%unit)
{//echo("getAdjacentFacingMonster()");
	%xIndex = %unit.xIndex;
	%yIndex = %unit.yIndex;
	switch (%unit.facing)
	{
		case $northDirection:	%monsterIndex = getMonsterIndexOnTile(%xIndex, %yIndex - 1);
		case $southDirection:	%monsterIndex = getMonsterIndexOnTile(%xIndex, %yIndex + 1);
		case $eastDirection:	%monsterIndex = getMonsterIndexOnTile(%xIndex + 1, %yIndex);
		case $westDirection:	%monsterIndex = getMonsterIndexOnTile(%xIndex - 1, %yIndex);
	}
	return %monsterIndex;
}