//>--------------------------------------------------------------------------
// btTaskManageVulnerability
//---------------------------------------------------------------------------
//>--------------------------------------------------------------------------
// Manages a state of Invulnerability to all damage for the AI Tree owner
//---------------------------------------------------------------------------
//>--------------------------------------------------------------------------
// Brad Lansford - 02-May-2014
// Copyright © 2014 CD Projekt RED
//---------------------------------------------------------------------------

class TaskManageVulnerability extends IBehTreeTask
{
	var vulnerable							: bool;
	var onActivate 							: bool;
	var onDeactivate 						: bool;
	var overrideForThisTask					: bool;
	var onEvent								: bool;
	
	private var m_waitingForEndOfAnimEvent	: bool;
	
	function OnActivate() : EBTNodeStatus
	{
		m_waitingForEndOfAnimEvent = false;
		
		if ( overrideForThisTask )
		{
			SwitchVulnerability( vulnerable );
		}
		else if ( onActivate )
		{			
			SwitchVulnerability( vulnerable );
		}
		return BTNS_Active;
	}
	function OnDeactivate()
	{
		if ( overrideForThisTask )
		{
			SwitchVulnerability( !vulnerable );
		}
		else if ( onDeactivate )
		{
			SwitchVulnerability( vulnerable );
		}
		
		if( m_waitingForEndOfAnimEvent )
		{
			SwitchVulnerability( true );
		}
	}
	//>--------------------------------------------------------------------------
	//---------------------------------------------------------------------------
	function OnAnimEvent( animEventName : name, animEventType : EAnimationEventType, animInfo : SAnimationEventAnimInfo ) : bool
	{		
		if( !onEvent ) return false;
		
		if ( animEventName == 'Invulnerable' )
		{			
			if( animEventType == AET_DurationEnd )
			{
				SwitchVulnerability( true );
			}
			else
			{			
				SwitchVulnerability( false );
				if( animEventType == AET_DurationStart )
					m_waitingForEndOfAnimEvent = true;
			}
		}
		else if ( animEventName == 'Vulnerable' )
		{
			SwitchVulnerability( true );
		}
		
		return true;
	}
	//>--------------------------------------------------------------------------
	//---------------------------------------------------------------------------
	final function SwitchVulnerability( _Vulnerable : bool )
	{
		if( _Vulnerable )
		{
			GetNPC().SetImmortalityMode( AIM_None, AIC_Combat );
		}
		else
		{
			GetNPC().SetImmortalityMode( AIM_Invulnerable, AIC_Combat );
		}
	}
}
class TaskManageVulnerabilityDef extends IBehTreeTaskDefinition
{
	default instanceClass = 'TaskManageVulnerability';

	editable var vulnerable				: bool;
	editable var onActivate 			: bool;
	editable var onDeactivate 			: bool;
	editable var overrideForThisTask	: bool;
	editable var onEvent 				: bool;
	
	hint onEvent 						= "Switch on Invulnerable and Vulnerable events";
};