Scriptname aaaUDSwingShoutScript extends activemagiceffect  

Spell property aaaUDSwingFireShout auto
Spell property aaaUDSwingFrostShout auto
Spell Property AbDragonFrost auto
Projectile property aaaUDSwingFireShoutProjectile auto
Projectile property aaaUDSwingFrostShoutProjectile auto
Activator property FXEmptyActivator auto

Actor selfActor
ObjectReference ShoutRef
ObjectReference TargetBox
ObjectReference StartBox
ObjectReference EndBox

Event OnEffectStart(Actor akTarget, Actor akCaster)
	selfActor = akTarget
	registerForAnimationEvent(selfActor, "SwingShoutEvent")
	registerForAnimationEvent(selfActor, "ShoutEndEvent")
	Debug.SendAnimationEvent(selfActor, "SwingShout")
EndEvent

Event onAnimationEvent(objectReference deliverator, string eventName)
	if eventName == "SwingShoutEvent"
		SwingShout()
	elseif eventName == "ShoutEndEvent"
		AllDelete()
	endif
EndEvent


Function SwingShout()

	Actor Target	= selfActor.GetCombatTarget()
	TargetBox		= selfActor.Placeatme(FXEmptyActivator)	;xmarker selfActor.Placeatme(Game.GetFormFromFile(0x0000f03c ,"UltimateDragons.esp"))
	StartBox		= selfActor.Placeatme(FXEmptyActivator)
	EndBox			= selfActor.Placeatme(FXEmptyActivator)

	float Distance	= selfActor.GetDistance(Target)
	float AngleZ		= selfActor.GetAngleZ() - 30
	float LocX		= Distance * math.sin(AngleZ)		;distance = 380
	float LocY		= Distance * math.cos(AngleZ)
	float LocZ		= Target.Z - selfActor.Z
	float EndAngleZ	= selfActor.GetAngleZ() + 80	;90
	float EndLocX	= 310 * math.sin(EndAngleZ)
	float EndLocY	= 310 * math.cos(EndAngleZ)
	StartBox.MoveToNode(selfActor,"NPC Head MagicNode [Hmag]")
	EndBox.MoveTo(StartBox, EndLocX, EndLocY, 0)
	TargetBox.MoveTo(StartBox, LocX, LocY, LocZ - 40)

	if selfActor.HasSpell(AbDragonFrost)
		aaaUDSwingFrostShout.RemoteCast(StartBox, selfActor,TargetBox)
		ShoutRef = Game.FindClosestReferenceOfTypeFromRef(aaaUDSwingFrostShoutProjectile,selfActor,500)
	else
		aaaUDSwingFireShout.RemoteCast(StartBox, selfActor,TargetBox)
		ShoutRef = Game.FindClosestReferenceOfTypeFromRef(aaaUDSwingFireShoutProjectile, selfActor,500)	
	endif

	Float SAngleZ = ShoutRef.GetAngleZ() + 120
	if SAngleZ >= 360
		SAngleZ = SAngleZ - 360
	endif
	ShoutRef.TranslateTo(EndBox.X, EndBox.Y, EndBox.Z, ShoutRef.GetAngleX(), ShoutRef.GetAngleY(), SAngleZ, 200.0, 18.0)	
EndFunction

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

Event OnHit(ObjectReference akAggressor, Form akSource, Projectile akProjectile, bool abPowerAttack, bool abSneakAttack, bool abBashAttack, bool abHitBlocked)
	if selfActor.GetAnimationVariableBool("IsStaggering")
		AllDelete()
	endif
EndEvent


Function AllDelete()
		StartBox.InterruptCast()
		TargetBox.Delete()
		StartBox.Delete()
		EndBox.Delete()
		if (ShoutRef != none)
			ShoutRef.Delete()
		endIf
EndFunction

