// Corona effect, the corona effect is done by each playerpawn in the game, 
// and as it's only a visual effect for each playerpawn, it don't get called by any other pawn
// Still, this effect can be dangerously resourcefull if used too many times, 
// that's why it's only used in bigger explosions or even nukes
///////////////////////
//ATTENTION: this is not a true dynamic corona, as it doesn't blend color

class CoronaEffect expands Actor;

//configurable in the main corona controller

var float MaxDistance;			//Max distance where the corona is visible
var float LifeTime;			//It's basically the lifespan
var float StartScaleTime, EndScaleTime;
var float FadeInTime, FadeOutTime;
var float EndScaleCoef, StartScaleCoef;  //Scaling multiplier when appearing or dying
var texture CoronaSprite;		//Corona texture
var float MaxCoronaSize, MinCoronaSize;	//Corona scales
var float CGlow;			//Corona scaleglow


//in variables
var bool bReceivedData;
var float CountA, CountB;
var bool bVisible;

replication
{
	reliable if (Role == ROLE_Authority && bNetOwner)
		MaxDistance, LifeTime, StartScaleTime, EndScaleTime, FadeInTime,
	FadeOutTime, EndScaleCoef, StartScaleCoef, CoronaSprite, MaxCoronaSize,
	MinCoronaSize, CGlow;
}

simulated function PostBeginPlay()
{
	SetTimer(0.01, False);
}


simulated function Timer()
{
local Actor A;
local vector HitLoc, HitNorm;
local vector X, Y, Z;

	if (!bReceivedData)
	{
		bReceivedData = True;
		LifeSpan = LifeTime;
		Texture = CoronaSprite;
		ScaleGlow = CGlow;
		SetTimer(0.2, True);

		//Destroy this corona if playerpawn owner is not on this side of the network
		if ((Role < ROLE_Authority && !bNetOwner) || Level.NetMode == NM_DedicatedServer)
			Destroy();
	}

	If (PlayerPawn(Owner) == None)
		Destroy();

	GetAxes(PlayerPawn(Owner).ViewRotation,X,Y,Z);

	if (PlayerPawn(Owner).ViewTarget == None)
	{
		if (!Class'NWMutator'.default.bCoronasCollisionDetail)
			bVisible = FastTrace(PlayerPawn(Owner).Location + PlayerPawn(Owner).Eyeheight * Z, Location);
		else
		{
			bVisible = True;
			ForEach TraceActors(Class'Actor', A, HitLoc, HitNorm, PlayerPawn(Owner).Location + PlayerPawn(Owner).Eyeheight * Z, Location)
			{
				if (((A == Level) || (A.bBlockActors)) && A != PlayerPawn(Owner))
				{
					bVisible = False;
					break;
				}
			}
		}
	}
	else
	{
		
		if (!Class'NWMutator'.default.bCoronasCollisionDetail)
			bVisible = FastTrace(PlayerPawn(Owner).ViewTarget.Location, Location);
		else
		{
			bVisible = True;
			ForEach TraceActors(Class'Actor', A, HitLoc, HitNorm, PlayerPawn(Owner).ViewTarget.Location, Location)
			{
				if (((A == Level) || (A.bBlockActors)) && A != PlayerPawn(Owner).ViewTarget)
				{
					bVisible = False;
					break;
				}
			}
		}
	}
}


simulated function Tick(float DeltaTime)
{
local float SizeCoef, SizeDelta;
local vector VectorREF, VectorDELTA;
local float VLineMax, VLine;

	if (bReceivedData)
	{
		//Distance>Size control + MaxDistance control + corona effect
		if (PlayerPawn(Owner).ViewTarget == None)
		{
			VectorREF = Normal(Location - PlayerPawn(Owner).Location) * 20;
			VectorDELTA = vector(PlayerPawn(Owner).ViewRotation) * 20;
			SizeCoef = VSize(PlayerPawn(Owner).Location - Location);
		}
		else
		{
			VectorREF = Normal(Location - PlayerPawn(Owner).ViewTarget.Location) * 20;
			VectorDELTA = vector(PlayerPawn(Owner).ViewTarget.Rotation) * 20;
			SizeCoef = VSize(PlayerPawn(Owner).ViewTarget.Location - Location);
		}

		bHidden = !bVisible || (SizeCoef > MaxDistance);
		SizeDelta = Abs(MaxCoronaSize - MinCoronaSize);
		DrawScale = Abs(MinCoronaSize) + (SizeCoef * SizeDelta / MaxDistance);

		VLineMax = (2 * (VSize(VectorREF)^2))^ 0.5 ;
		VLine = VSize(VectorDELTA - VectorREF);
		SizeCoef = SizeCoef - (VLine * SizeCoef / VLineMax);
		SpriteProjForward = FMax(0, SizeCoef - 1);


		//Fading and scaling effects control
		if (FadeInTime > 0 && CountA < FadeInTime)
		{
			CountA += DeltaTime;
			ScaleGlow = CountA * CGlow / FadeInTime;
		}

		if (StartScaleTime > 0 && CountB < StartScaleTime)
		{
			CountB += DeltaTime;
			DrawScale *= (CountB * StartScaleCoef / StartScaleTime);
		}

		if (FadeOutTime > 0 && LifeTime > 0 && LifeSpan < FadeOutTime)
			ScaleGlow = LifeSpan * CGlow / FadeOutTime;

		if (EndScaleTime > 0 && LifeSpan < EndScaleTime)
			DrawScale *= (LifeSpan * EndScaleCoef / EndScaleTime);
	}
		
}


defaultproperties
{
	DrawType=DT_Sprite
	Style=STY_Translucent
	bUnlit=True
	Texture=None
	RemoteRole=ROLE_SimulatedProxy
	CollisionHeight=0.000000
	CollisionRadius=0.000000
	Mass=0.000000
	bNetTemporary=False
	LifeSpan=5000.000000
	StartScaleCoef=1.000000
	EndScaleCoef=1.000000
}
