ScriptName footprintsFootstepsScriptVampireLord Extends ActiveMagicEffect

ImpactDataSet Property footprintsNPCVampireLordFootWalkImpactSetR Auto
ImpactDataSet Property footprintsNPCVampireLordFootWalkImpactSetL Auto

ImpactDataSet Property footprintsNPCVampireLordFootLStrafeLImpactSet Auto
ImpactDataSet Property footprintsNPCVampireLordFootLStrafeRImpactSet Auto
ImpactDataSet Property footprintsNPCVampireLordFootRStrafeLImpactSet Auto
ImpactDataSet Property footprintsNPCVampireLordFootRStrafeRImpactSet Auto

Spell Property parentSpell Auto

Actor Property playerRef Auto
Actor selfRef

float fDirection
int strafeDir

string boneLeft = "NPC L Calf [LClf]"
string boneRight = "NPC R Calf [RClf]"


Event OnEffectStart( Actor target, Actor caster )
	selfRef = target
	If ( !selfRef )
		GoToState( "dead" )
		return
	EndIf
	
	If ( selfRef != playerRef )
		;Debug.Trace( "Vampire Lord Effect applied to " + selfRef + ". Race: " + selfRef.GetRace() +" on (" + self +")" )
		If ( RegisterForAnimationEvent( selfRef, "FootLeft" ) && RegisterForAnimationEvent( selfRef, "FootRight" ) )
			GoToState( "alive" )
		EndIf
	ElseIf ( selfRef == playerRef )
		If ( RegisterForAnimationEvent( selfRef, "FootLeft" ) && RegisterForAnimationEvent( selfRef, "FootRight" ) \
			&& RegisterForAnimationEvent( selfRef, "JumpUp" ) && RegisterForAnimationEvent( selfRef, "JumpDown" ) )
			GoToState( "alive_player" )
		EndIf
	Else
		GoToState( "dead" )
	EndIf
EndEvent

State alive

	Event OnAnimationEvent( ObjectReference akSelfRef, string e )
		If ( !akSelfRef )
			return
		EndIf
		
		If ( e == "FootLeft" )
			;Debug.Trace( "Foot Left" )
			akSelfRef.PlayImpactEffect( footprintsNPCVampireLordFootWalkImpactSetL, boneLeft, 0, 0, -1, 512 )
		ElseIf ( e == "FootRight" )
			;Debug.Trace( "Foot Right" )
			akSelfRef.PlayImpactEffect( footprintsNPCVampireLordFootWalkImpactSetR, boneRight, 0, 0, -1, 512 )
		EndIf
	EndEvent
	
	Event OnDying( Actor killer )
		GoToState( "dead" )
	EndEvent
	
	Event OnEffectFinish( Actor akTarget, Actor akCaster )
		GoToState( "dead" )
	EndEvent

EndState


State alive_player

	Event OnBeginState()
		RegisterForSingleUpdate( 2.0 )
	EndEvent

	Event OnUpdate()
		strafeDir = StrafeDir()
		RegisterForSingleUpdate( 1.0 )
	EndEvent

	Event OnAnimationEvent( ObjectReference akSelfRef, string e )
		If ( !akSelfRef )
			return
		EndIf
		
		;If ( (akSelfRef as Actor) != playerRef )
		;	Debug.Trace( (akSelfRef as Actor) + " and " + playerRef +" not equal on (" + self +")" )
		;	GoToState( "alive" )
		;	return
		;EndIf
		
		If ( strafeDir == 0 )
			;Debug.Trace( "Vampire Lord footsteps: Not strafing" )
			If ( e == "FootRight" ) 
				akSelfRef.PlayImpactEffect( footprintsNPCVampireLordFootWalkImpactSetR, boneRight, 0, 0, -1, 512 )
			ElseIf ( e == "FootLeft" )
				akSelfRef.PlayImpactEffect( footprintsNPCVampireLordFootWalkImpactSetL, boneLeft, 0, 0, -1, 512 )
			ElseIf ( e == "JumpDown" || e == "JumpUp" )
				akSelfRef.PlayImpactEffect( footprintsNPCVampireLordFootWalkImpactSetR, boneRight, 0, 0, -1, 512 )
				akSelfRef.PlayImpactEffect( footprintsNPCVampireLordFootWalkImpactSetL, boneLeft, 0, 0, -1, 512 )
			EndIf
		ElseIf ( strafeDir == -1 )
			;Debug.Trace( "Vampire Lord footsteps: Strafing Left" )
			If ( e == "FootRight" ) 
				akSelfRef.PlayImpactEffect( footprintsNPCVampireLordFootLStrafeRImpactSet, boneRight, 0, 0, -1, 512 )
			ElseIf ( e == "FootLeft" )
				akSelfRef.PlayImpactEffect( footprintsNPCVampireLordFootLStrafeLImpactSet, boneLeft, 0, 0, -1, 512 )
			ElseIf ( e == "JumpDown" || e == "JumpUp" )
				akSelfRef.PlayImpactEffect( footprintsNPCVampireLordFootLStrafeRImpactSet, boneRight, 0, 0, -1, 512 )
				akSelfRef.PlayImpactEffect( footprintsNPCVampireLordFootLStrafeLImpactSet, boneLeft, 0, 0, -1, 512 )
			EndIf
		ElseIf ( strafeDir == 1 )
			;Debug.Trace( "Vampire Lord footsteps: Strafing Right" )
			If ( e == "FootRight" ) 
				akSelfRef.PlayImpactEffect( footprintsNPCVampireLordFootRStrafeRImpactSet, boneRight, 0, 0, -1, 512 )
			ElseIf ( e == "FootLeft" )
				akSelfRef.PlayImpactEffect( footprintsNPCVampireLordFootRStrafeLImpactSet, boneLeft, 0, 0, -1, 512 )
			ElseIf ( e == "JumpDown" || e == "JumpUp" )
				akSelfRef.PlayImpactEffect( footprintsNPCVampireLordFootRStrafeRImpactSet, boneRight, 0, 0, -1, 512 )
				akSelfRef.PlayImpactEffect( footprintsNPCVampireLordFootRStrafeLImpactSet, boneLeft, 0, 0, -1, 512 )
			EndIf
		EndIf
	EndEvent
	
	Event OnDying( Actor killer )
		; NOTHING!
	EndEvent

EndState

State dead
	Event OnBeginState()
		If ( selfRef && parentSpell )
			selfRef.RemoveSpell( parentSpell )
		EndIf
	EndEvent
EndState

Event OnUpdate()
	; nothing
EndEvent

Event OnDying( Actor killer )
	; nothing
EndEvent
	
Event OnRaceSwitchComplete()
	; nothing
EndEvent

int Function StrafeDir()
	fDirection = selfRef.GetAnimationVariableFloat( "Direction" )
	If ( fDirection == 0.25 )
		; Right
		return 1
	ElseIf ( fDirection == 0.75 )
		; Left
		return -1
	EndIf
	return 0
EndFunction
