//-----------------------------------------------------------
//
//  - UnrealRider -  UR_ShieldVolume
//  Written/tested by Erwan Allain (zall1@hotmail.com)
//
// The ShieldVolume prevents the player to reach an objective (capture the flag)
// ShieldGenerators linked to the ShieldVolume have to be disabled in order to
// disable the ShieldVolume
//
//-----------------------------------------------------------
class UR_ShieldVolume extends PhysicsVolume
  placeable;


enum ShieldState
{
 SHIELD_ENABLE, SHIELD_DISABLE
};

var(ShieldVolume) string shieldTagName;
var(ShieldVolume) float killRadiusWhenEnabled;

var array<UR_ShieldGenerator> generators;
var ShieldState shState;

var StaticMeshComponent baseComponent;


simulated function PostBeginPlay()
{
  local UR_ShieldGenerator UR_S;
  foreach WorldInfo.AllActors(class'UR_ShieldGenerator', UR_S)
  {
     if(UR_S.shieldTagName == shieldTagName)
     {
       generators.addItem(UR_S);
     }
  }
    enableShield();
}

//Method call from SeqAct ===============================
function generatorChangeState(UR_ShieldGenerator generator)
{
  checkGenerators(generator);
}
//End Method call from SeqAct ===========================

function checkGenerators(UR_ShieldGenerator generator)
{
  local int i;
  local ShieldState newState;

  if(generators.find(generator) > 1)
  {
    generators[generators.find(generator)] = generator;
  }

  newState = SHIELD_DISABLE;
  for(i=0;i<generators.length;i++)
  {
    if(generators[i].genEnable)
    {
       newState = SHIELD_ENABLE;
    }
  }

  if(newState != shState)
  {
    StateChange(newState);
  }
}

function StateChange(ShieldState newState)
{
  shState = newState;

  switch(newState)
  {
     case ShieldState.SHIELD_ENABLE:
             enableShield();
             break;

     case ShieldState.SHIELD_DISABLE:
             disableShield();
             break;
  }
}



event Bump( Actor Other, PrimitiveComponent OtherComp, Vector HitNormal )
{
  local UTCTFFlag flag;
  local int teamIndex;
  local Controller C;

  if(generators.length == 0)
    return;

  if(Pawn(Other) !=None && Pawn(Other).Controller != None)
    C = Pawn(Other).Controller;

  if(C==None)
   return;
  //assume the ShieldVolume TeamIndex by taking the first ShiedGenerator TeamIndex
  //linked to the ShieldVolume
  teamIndex = generators[0].DefenderTeamIndex;

  if (C.PlayerReplicationInfo.Team.TeamIndex == teamIndex && C.PlayerReplicationInfo.bHasFlag)
  {
        // Score!
        flag = UTCTFFlag(UTPlayerReplicationInfo(C.PlayerReplicationInfo).GetFlag());
        UTCTFGame(WorldInfo.Game).ScoreFlag(C, flag);
        flag.SuccessfulCaptureSystem.SetActive(true);
        flag.Score();
    notifyScoreEvent('score');
        if (C.Pawn != None && UTBot(C) != None && UTBot(C).Squad.GetOrders() == 'Attack')
        {
                UTBot(C).Pawn.SetAnchor(flag.HomeBase);
                UTBot(C).Squad.SetAlternatePathTo(UTCTFSquadAI(UTBot(C).Squad).EnemyFlag.HomeBase, UTBot(C));
        }
  }
}

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

          for (i = 0; i < GeneratedEvents.length; i++)
          {
                 scoreEvent = UR_ScoreFlagEvent(GeneratedEvents[i]);
                 if (scoreEvent != None)
                 {
                        scoreEvent.notifyScoreEvent(eventType, self);
                 }
          }
}

function enableShield()
{
    Local Pawn P;
    baseComponent.SetHidden(false);
    SetCollision(true, true);

    foreach CollidingActors ( class'Pawn', P, killRadiusWhenEnabled )
    {
      P.Suicide();
    }
}

function disableShield()
{
    baseComponent.SetHidden(true);
    SetCollision(false, false);
}

defaultproperties
{
    killRadiusWhenEnabled=300
    SupportedEvents.Empty
        SupportedEvents(0)=class'UR_ScoreFlagEvent'

    RemoteRole=ROLE_SimulatedProxy
        bStatic=false
        bNoDelete=false
        bHidden=false
        bProjTarget=true

    Begin Object name=BrushComponent0
                BlockActors=false
        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'Pickups.Deployables.mesh.S_Deployables_SlowVolume_Collision'
                CollideActors=true
                BlockActors=true
                HiddenGame=false
                CastShadow=true
                bCastDynamicShadow=false
                LightEnvironment=LightEnvironment
            Materials(0) = MaterialInterface'LP_UR_Assets.Materials.Shield_Alt_Demo'
                scale=0.5
                Translation=(X=0.0,Y=0.0,Z=0.0)
        End Object
        baseComponent=StaticMeshComponent0
         Components.Add(StaticMeshComponent0)
}