//-----------------------------------------------------------
//
//  - UnrealRider -  UR_ShieldGenerator
//  Written/tested by Erwan Allain (zall1@hotmail.com)
//
//  Shield Generator maintaining the shield enabled around the flag
//
//-----------------------------------------------------------
class UR_ShieldGenerator extends UTGameObjective
  placeable;

var(ShieldGenerator) string shieldTagName;
var(ShieldGenerator) int disableThreshold;   //percent value
var(ShieldGenerator) float Health;

var(ShieldGeneratorMessage) string enableMessage;
var(ShieldGeneratorMessage) string disableMessage;

var MaterialInterface shieldMaterial;
var StaticMeshComponent baseComponent;
var StaticMeshComponent sphereComponent;

var float LastAttackMessageTime;
var bool genEnable;

replication
{
        if (bNetDirty && Role == ROLE_Authority)
                Health;
}

simulated function PostBeginPlay()
{
   super.PostBeginPlay();

        bCanBeDamaged = true;
        bProjTarget = true;
        SetCollision(true,bBlockActors);

    baseComponent.SetMaterial(0, shieldMaterial);
    sphereComponent.SetMaterial(0, shieldMaterial);
}

simulated event SetInitialState()
{
  GotoState('SG_Active');
}


simulated event bool IsActive()
{
        return true;
}

function bool Shootable()
{
        return true;
}

function Actor GetShootTarget()
{
        return self;
}

simulated function bool NeedsHealing()
{
    return (Health < class'UR_ShieldGenerator'.default.Health);
}

state SG_Active
{
 event TakeDamage(int Damage, Controller InstigatedBy, vector HitLocation, vector Momentum, class<DamageType> DamageType, optional TraceHitInfo HitInfo, optional Actor  DamageCauser)
 {
    if (Damage <= 0 || WorldInfo.Game.bGameEnded || UTGame(WorldInfo.Game).ResetCountdown > 0)
                return;

        Health -= Damage;

        bForceNetUpdate = true;

        if((health*100/class'UR_ShieldGenerator'.default.Health) < disableThreshold)
    {
      GotoState('SG_Disable');
    }
 }

 function bool HealDamage(int Amount, Controller Healer, class<DamageType> DamageType)
 {
    if (Health >= class'UR_ShieldGenerator'.default.Health )
        {
                return false;
        }

        health += amount;

        bForceNetUpdate = TRUE;

    if(health > class'UR_ShieldGenerator'.default.Health)
    {
      health = class'UR_ShieldGenerator'.default.Health;
    }
        return true;
 }

 simulated function BeginState(Name PreviousStateName)
 {
 }

 simulated function endState(Name NextStateName)
 {
    genEnable = false;
    BroadcastNotification();
    notifyShieldGeneratorEvent('disable');
 }
}

state SG_Disable
{

 event TakeDamage(int Damage, Controller InstigatedBy, vector HitLocation, vector Momentum, class<DamageType> DamageType, optional TraceHitInfo HitInfo, optional Actor  DamageCauser)
 {
        if (Damage <= 0 || WorldInfo.Game.bGameEnded || UTGame(WorldInfo.Game).ResetCountdown > 0)
                return;

    if(health > 0)
       Health -= Damage;
    if(health <= 0)
       health = 1;

    bForceNetUpdate = true;
 }

 function bool HealDamage(int Amount, Controller Healer, class<DamageType> DamageType)
 {
        if (Health >= class'UR_ShieldGenerator'.default.Health )
        {
                return false;
        }

        health += amount;

        bForceNetUpdate = TRUE;

    if((health*100/class'UR_ShieldGenerator'.default.Health) > disableThreshold)
    {
      GoToState('SG_Active');
    }

        return true;
 }

 simulated function BeginState(Name PreviousStateName)
 {

 }

 simulated function endState(Name NextStateName)
 {
    genEnable = true;
    BroadcastNotification();
    notifyShieldGeneratorEvent('enable');
 }
}

function enableGenerator()
{
  Health = default.Health;
  GotoState('SG_Active');
}


function BroadcastNotification(/*Controller InstigatedBy*/)
{
        //attack notification
        if (LastAttackMessageTime + 1 < WorldInfo.TimeSeconds)
        {
                BroadcastLocalizedMessage(MessageClass, DefenderTeamIndex,,, self);
                LastAttackMessageTime = WorldInfo.TimeSeconds;
        }
}

/**
*  This method is dedicated to throw event when Generator has been enabled
*  or disabled.
*  Available Event Type are:
*  - disable
*  - enable
*/
function notifyShieldGeneratorEvent(name eventType)
{
          local int i;
          local UR_ShieldGeneratorEvent shieldGenEvent;

          for (i = 0; i < GeneratedEvents.length; i++)
          {
                 shieldGenEvent = UR_ShieldGeneratorEvent(GeneratedEvents[i]);
                 if (shieldGenEvent != None)
                 {
                        shieldGenEvent.notifyShieldGeneratorEvent(eventType, self);
                 }
          }
}

simulated event PostRenderFor(PlayerController PC, Canvas Canvas, vector CameraPosition, vector CameraDir)
{
        local float XL, HealthX;
        local vector ScreenLoc;

        ScreenLoc = Canvas.Project(Location) ;

    // make sure not clipped out
        if (screenLoc.X < 0 ||
                screenLoc.X >= Canvas.ClipX ||
                screenLoc.Y < 0 ||
                screenLoc.Y >= Canvas.ClipY)
        {
                return;
        }

       healthX = health*100/class'UR_ShieldGenerator'.default.Health;
       XL = 100 ;
           class'UTHUD'.static.DrawHealth(ScreenLoc.X-0.5*XL, ScreenLoc.Y - 50, HealthX, XL, 20, Canvas);
}

defaultproperties
{
  genEnable = true
  disableThreshold = 50
  Health = 1000
  enableMessage = "ShieldGenerator has been enabled"
  disableMessage = "ShieldGenerator has been disabled"

    Begin Object name=CollisionCylinder
                CollideActors=true
                BlockActors=true
                CollisionRadius=100.0
                CollisionHeight=150.0
        End Object

    Begin Object class=DynamicLightEnvironmentComponent Name=LightEnvironment
            bDynamic=FALSE
                bCastShadows=FALSE
        End Object
    Components.Add(LightEnvironment)

    Begin Object Class=StaticMeshComponent Name=StaticMeshComponent0
                StaticMesh=StaticMesh'UnrealRider_shieldgenerator.mesh.shield_generator_stand'
                CollideActors=true
                BlockActors=true
                CastShadow=true
                bCastDynamicShadow=false
                LightEnvironment=LightEnvironment
                scale=4.0
                Translation=(X=0.0,Y=0.0,Z=0.0)
                bUseAsOccluder=FALSE
        End Object
        baseComponent=StaticMeshComponent0
         Components.Add(StaticMeshComponent0)

    Begin Object class=StaticMeshComponent Name=StaticMeshComponent1
                StaticMesh=StaticMesh'UnrealRider_shieldgenerator.mesh.shield_sphere'
                CollideActors=true
                BlockActors=true
                CastShadow=true
                bCastDynamicShadow=false
                LightEnvironment=LightEnvironment
                scale=4.0
                Translation=(X=0.0,Y=0.0,Z=200.0)
                bUseAsOccluder=FALSE
        End Object
        sphereComponent=StaticMeshComponent1
         Components.Add(StaticMeshComponent1)

    bStatic=False
        bNoDelete=True
    bDestinationOnly=false
        bAlwaysRelevant=true
        RemoteRole=ROLE_SimulatedProxy
    NetUpdateFrequency=1
        Score=6
        bNotBased=True
        bCollideActors=True
        bCollideWorld=True
        bIgnoreEncroachers=True
        bBlockActors=True
        bProjTarget=True
        bHidden=False

        bBlocksTeleport=true
        bHasSensor=true
        bCanWalkOnToReach=true
        MessageClass=class'UR_ShieldGeneratorMessage'

        SupportedEvents.Empty
        SupportedEvents(0)=class'UTSeqEvent_ObjectiveCompleted'
        SupportedEvents(1)=class'SeqEvent_TakeDamage'
        SupportedEvents(2)=class'SeqEvent_Touch'
        SupportedEvents(3)=class'UR_ShieldGeneratorEvent'
}