Scriptname aaaUD3TimesShoutScript extends activemagiceffect  

Spell property aaaUD3ShoutFireSpell auto
Spell property aaaUD3ShoutFrostSpell auto
Spell Property AbDragonFrost auto
Activator property FXEmptyActivator auto

Actor selfActor
Actor Target 
float Distance
float Degrees
ObjectReference Box
ObjectReference StartBox
Spell akSpell

Event OnEffectStart(Actor akTarget, Actor akCaster)

	selfActor = akTarget
	Target = selfActor.GetCombatTarget()
	registerForAnimationEvent(selfActor, "SoundPlay.NPCDragonKillMove")
	registerForAnimationEvent(selfActor, "ShoutEndEvent")
	Debug.SendAnimationEvent(selfActor, "ThreeTimesShout")
	Box =  selfActor.Placeatme(FXEmptyActivator)	;selfActor.Placeatme(Game.GetFormFromFile(0x0000f03c ,"UltimateDragons.esp"))
	StartBox = selfActor.Placeatme(FXEmptyActivator)

	Distance = 1500 ;selfActor.GetDistance(Target)	;1500
	Degrees = -15

	if   selfActor.HasSpell(AbDragonFrost)
		akSpell = aaaUD3ShoutFrostSpell 		
	else
		akSpell = aaaUD3ShoutFireSpell 
	endif
EndEvent

Event onAnimationEvent(objectReference deliverator, string eventName)
	if eventName == "SoundPlay.NPCDragonKillMove"
		ShoutFire()
	elseif eventName == "ShoutEndEvent"
		AllDelete()
	endif
EndEvent

Function ShoutFire()
		float AngleZ	= selfActor.GetAngleZ() + Degrees
		float LocX	= Distance * math.sin(AngleZ)
		float LocY	= Distance * math.cos(AngleZ)
		float LocZ	= Target.Z - selfActor.Z + 80
		StartBox.MovetoNode(selfActor, "NPC Head MagicNode [Hmag]")
		Box.MoveTo(selfActor, LocX, LocY, LocZ)
		akSpell.RemoteCast(StartBox, selfActor, Box)
		Degrees	+= 15
EndFunction

Event OnEffectFinished(Actor akTarget, Actor akCaster)
	AllDelete()
EndEvent

Function AllDelete()
	if (Box != none)
		Box.Delete()
		StartBox.Delete()
	endif
EndFunction