// Handles Character Interactions.
// Worthless Bums 2012.


// Closes any adjacent door in the facing direction.
function closeDoor ()
{//echo("closeDoor()");
	%actionPointCost = getActionPointCost(12);
	if ($playerSquad[$activeCharacter].currentActionPoints >= %actionPointCost)
	{
		%xIndex = $playerSquad[$activeCharacter].xIndex;
		%yIndex = $playerSquad[$activeCharacter].yIndex;
		switch ($playerSquad[$activeCharacter].facing)
		{
			case $northDirection:	%yIndex--;
			case $southDirection:	%yIndex++;
			case $eastDirection:	%xIndex++;
			case $westDirection:	%xIndex--;
		}
		
		// Check if tile is flagged as an open door.
		if ($mazeRoom[%xIndex, %yIndex].status == -4)
		{
			if (tileIsClearOfUnits(%xIndex, %yIndex))
			{
				$GlobalStats.totalDoorsClosed++;
				$mazeRoom[%xIndex, %yIndex].frame = $mazeRoom[%xIndex, %yIndex].frame - 1;
				$mazeRoom[%xIndex, %yIndex].status = -3;
				$mazeRoom[%xIndex, %yIndex].SceneLayer = $DoorClosedLayer;
				setCurrentActionPoints($activeCharacter, $playerSquad[$activeCharacter].currentActionPoints - %actionPointCost);
				initializeReadout();
				inPlaceActionGrowAndShrink($playerSquad[$activeCharacter]);
				playAudio("Audio:sliding_door_close_01", EffectsAudioVolume.value);
				$fogOfWar.showFogOfWar();
				scheduleMonsterTurn();
				autoTargetEnemyUnit($autoTargetDelay);
				updateEventText($squadColor @ $playerSquad[$activeCharacter].name SPC $friendlyActionColor @ "closes door" @ $defaultColor @ ".");
			}
			else
			{
				updateEventText($whiteColor @ "Can't close door!" @ $defaultColor @ "!");
			}
		}
	}
}

//-----------------------------------------------------------------------------------------------
// Attach behavior for sprites (alternative for player avatars being composite sprites!
//-----------------------------------------------------------------------------------------------
function ObjectBehaviorAttach::onBehaviorAdd ( %this )
{	
	%this.Owner.AttachList = new SimSet();
}

function ObjectBehaviorAttach::onBehaviorRemove ( %this )
{
	%this.Owner.AttachList.delete();
}

function ObjectBehaviorAttach::AttachObject ( %this, %object, %offset )
{
	if( !isObject( %object ) )
	{
		TraceE( "ObjectBehaviorAttach.AttachObject.Failed.BadObject" );
		return false;
	}
	
	if( !%object.isMemberOfClass( "SceneObject" ) )
	{
		TraceE( "ObjectBehaviorAttach.AttachObject.Failed.NotSceneObject" );
		return false;
	}
	
	%offset = ( %offset $= "" ) ? "0 0" : %offset;
	
	%object.AttachOffset = %offset;
	%object.LinearVelocity = %this.Owner.LinearVelocity;
	%object.Position = %this.Owner.Position.x + %offset.x SPC %this.Owner.Position.y + %offset.y;
	%this.Owner.AttachList.add( %object );
	
	return true;
}

function ObjectBehaviorAttach::DetachObject( %this, %object )
{
	%this.Owner.AttachList.remove( %object );
}

function ObjectBehaviorAttach::DetachObjectAll ( %this )
{
	%this.Owner.AttachList.clear();
}

function ObjectBehaviorAttach::MoveTogether ( %this, %pos, %speed )
{
	%speed = ( %speed $= "" ) ? 0 : %speed;
	
    if( %speed == 0 )
    {
        %this.Owner.setPosition( %tarPos );
    }
    else
    {
        %this.Owner.moveTo( %pos, %speed );
    }
		
	for( %index = 0; %index < %this.Owner.AttachList.getCount(); %index++ )
	{
		%object = %this.Owner.AttachList.getObject( %index );
		if( !isObject( %object ) ) continue;
		
		%tarPos = %pos.x + %object.AttachOffset.x SPC %pos.y + %object.AttachOffset.y;
			
		if( %speed == 0 )
		{
			%object.setPosition( %tarPos );
		}
		else
		{
			%object.moveTo( %tarPos, %speed );
		}
	}
}

function ObjectBehaviorAttach::StopMoveTogether ( %this )
{
	%this.Owner.cancelMoveTo();
	
	for( %index = 0; %index < %this.Owner.AttachList.getCount(); %index++ )
	{
		%object = %this.Owner.AttachList.getObject( %index );
		if( !isObject( %object ) ) continue;
		
		%object.LinearVelocity = "0 0";
		%object.cancelMoveTo();
	}
}