class AnimatedDecoration extends Decoration;  

#exec OBJ LOAD FILE=Foundry_anim.ukx

var() 				bool 		bActive;
var 				bool 		bLoop;

var() 				float 		ActivationDelay;
var() 				bool 		bRandomActivationDelay;
var  				bool 		bPendingState; // bActive value, which will be set on timer

var() const 		name 		Animation;
var() const 		bool 		bHiddenWhenInactive;
var(Sound) const 	bool 		bAmbientSoundOnlyWhenActive;


replication
{
    reliable if( bNetInitial && Role == Role_Authority )
        Animation, bHiddenWhenInactive, bAmbientSoundOnlyWhenActive;
		
    reliable if( (bNetInitial || bNetDirty) && Role == Role_Authority )
        bActive, bLoop;
}

function PostBeginPlay()
{
	if ( bActive ) {
		bActive = false;
		DelayedActivation(true);
	}
	else 
		SetActive(false);
}

function DelayedActivation(bool bActivate) 
{
	bPendingState = bActivate;
	
	if ( ActivationDelay > 0 ) {
		if ( bRandomActivationDelay )
			SetTimer(fmax(ActivationDelay*frand(), 0.01), false);
		else
			SetTimer(ActivationDelay*frand(), false);
	}
	else 
		SetActive(bActivate);
}

simulated function Timer()
{
	SetActive(bPendingState);
}


simulated function PostNetReceive()
{
	SetActive(bActive);
}


simulated function SetActive(bool value)
{
	if ( bActive != value ) {
		bActive = value;
		if ( Role == ROLE_Authority )
			NetUpdateTime = Level.TimeSeconds - 1;
	}
	
	if ( Level.NetMode != NM_DedicatedServer ) {
		if ( bActive ) {
			if ( bLoop )
				LoopAnim(Animation);
			else {
				PlayAnim(Animation);
				// set inactive when animation ends
				bPendingState = false;
				SetTimer(GetAnimDuration(Animation), false);
			}
				
			if ( bAmbientSoundOnlyWhenActive )
				AmbientSound = default.AmbientSound;
		}
		else {
			StopAnimating();
			if ( bAmbientSoundOnlyWhenActive )
				AmbientSound = none;
		}
		if ( bHiddenWhenInactive )
			bHidden = !bActive;
	}
}

state() TriggerTurnsOn
{
	function BeginState()
	{
		bLoop = true;
	}

	function Trigger( actor Other, pawn EventInstigator )
	{
		Instigator = EventInstigator;
		DelayedActivation(true);
	}
}

state() TriggerPlayOnce extends TriggerTurnsOn
{
	function BeginState()
	{
		bLoop = false;
	}
}

state() TriggerTurnsOff
{
	function Trigger( actor Other, pawn EventInstigator )
	{
		Instigator = EventInstigator;
		SetActive(false);
	}
}

state() TriggerToggle
{
	function BeginState()
	{
		bLoop = true;
	}

	function Trigger( actor Other, pawn EventInstigator )
	{
		Instigator = EventInstigator;
		if ( bActive )
			SetActive(false);
		else
			DelayedActivation(true);
	}
}

defaultproperties
{
     bLoop=True
     bStatic=False
     bStasis=False
     bAlwaysRelevant=True
     RemoteRole=ROLE_SimulatedProxy
     Mesh=SkeletalMesh'Foundry_anim.Steamengine_Wheel'
     bNetNotify=True
}
