class TRAP_SHIELD_PICKUP extends Actor;

var enum M_Shield_Pickup_Mode {
                               P_Shield,
                               P_Charging,
                               P_Push
                              } TSP_Mode;
var Pawn Master;
var float Energy, Stored_Energy;
var SHIELD Shield_Actor;
var array<Projectile> Projectiles;
var array<float> Velocities;
var Emitter Top_Balst, Bottom_Balst, Left_Balst, Right_Balst;
var float Next_Blast_Time;

simulated function Spawn_Blasts()
{
   Top_Balst.Emitters[0].SpawnParticle(1);
   Bottom_Balst.Emitters[0].SpawnParticle(1);
   Left_Balst.Emitters[0].SpawnParticle(1);
   Right_Balst.Emitters[0].SpawnParticle(1);

   Next_Blast_Time = Level.TimeSeconds + 0.01;
}

simulated event Touch(Actor Other)
{
 local Pickup SG;

 if (Other == Master)
  {
    SG = Other.Spawn(Class'SHIELD_2016.SHIELD_PICKUP',Other);
    if (SG != none)
     {
       SG.Touch(Other);
       SG.Destroy();
       Shield_Actor.Destroy();
       Destroy();
     }
  }
}

function TakeDamage(int Damage, Pawn instigatedBy, Vector Hitlocation, Vector Momentum, class<DamageType> DamageType)
{
 Energy = Energy - Damage/6;
}

simulated function Tick(float dt)
{
 local vector HitLocation,HitNormal, TraceStart, TraceEnd;
 local rotator TraceDirection;
 local Actor Victim;
 local float VDist, VDistK;
 local float Momentum;
 local int pj;
 local bool pjinlist;
 local SHIELD_PICKUP Pickup;

 if (Next_Blast_Time < Level.TimeSeconds)
   Spawn_Blasts();

 if (TSP_Mode == P_Shield)
 {
  if (Energy > 0)
  {
   if (Shield_Actor == none)
    {
     Shield_Actor = Spawn(class'SHIELD',Master);
     Shield_Actor.SG = self;
    }
   else
    {
     Shield_Actor.SetLocation(Location + Vector(Rotation)*16);
     Shield_Actor.SetRotation(Rotation);
    }
  }
  else
  {
   Spawn(class'NewExplosionA');
   Shield_Actor.Destroy();
   Destroy();
  }
 }
 else if (TSP_Mode == P_Push)
  {
   TraceStart = Location;
   TraceDirection = Rotation;
   TraceEnd = TraceStart + vector(TraceDirection)*256;

   if (Energy > 0)
   {
     if (Top_Balst == none)
     {
      Top_Balst = Spawn(class'Blast_TOP',self);
      Top_Balst.bOnlyOwnerSee=False;
      AttachToBone(Top_Balst, 'top_emitter');
     }
     if (Bottom_Balst == none)
     {
      Bottom_Balst = Spawn(class'Blast_BOTTOM',self);
      Bottom_Balst.bOnlyOwnerSee=False;
      AttachToBone(Bottom_Balst, 'bottom_emitter');
     }
     if (Left_Balst == none)
     {
      Left_Balst = Spawn(class'Blast_LEFT',self);
      Left_Balst.bOnlyOwnerSee=False;
      AttachToBone(Left_Balst, 'left_emitter');
     }
     if (Right_Balst == none)
     {
      Right_Balst = Spawn(class'Blast_RIGHT',self);
      Right_Balst .bOnlyOwnerSee=False;
      AttachToBone(Right_Balst, 'right_emitter');
     }

    ForEach RadiusActors(class'Actor', Victim, 256, TraceEnd )
    {
     VDist = Max(0,(VSize(Victim.Location - TraceStart) - Victim.CollisionRadius));
     VDistK = 1 - VDist/128;

     if ((VDistK > 0) && (VSize(Victim.Location - TraceEnd) < (256-VSize(Victim.Location - TraceStart)*0.75)))
     {
      Momentum = 100000*VDistK;

      if (Victim.IsA('Projectile') == true)
       {
       pjinlist = false;
        for (pj=0; pj<Projectiles.Length; pj++)
         {
           if (Projectiles[pj] == Victim)
             pjinlist = true;
         }
        if (pjinlist == false)
         {
          Projectiles[Projectiles.Length] = Projectile(Victim);
          Velocities[Projectiles.Length] = VSize(Victim.Velocity);
          PlaySound(Sound'ONSBPSounds.ShockTank.ShieldOff',,255,,256);
         }
       }
      else if (Victim != self)
       {
        Victim.TakeDamage(0, Pawn(Owner), Victim.Location, Momentum*vector(TraceDirection), Class'XWeapons.DamTypeShieldImpact');
        Velocity = Victim.Velocity/10;
       }
     }
    }
   for (pj=0; pj<Projectiles.Length; pj++)
    {
     Projectiles[pj].Velocity = Projectiles[pj].Velocity + 1000*Vector(TraceDirection);
     Projectiles[pj].SetRotation(Rotator(Projectiles[pj].Velocity));
     Energy = Energy - Projectiles[pj].Damage/10;
     if ((VSize(Projectiles[pj].Velocity) >= Velocities[pj]) || (VSize(Projectiles[pj].Location - TraceStart) > 128))
      {
       Projectiles.Remove(pj, 1);
       Velocities.Remove(pj, 1);
       pj = pj - 1;
      }
    }
   }
  else
  {
   Spawn(class'NewExplosionA');
   if (Top_Balst != None)
     Top_Balst.Destroy();
   if (Bottom_Balst != None)
     Bottom_Balst.Destroy();
   if (Left_Balst != None)
     Left_Balst.Destroy();
   if (Right_Balst != None)
     Right_Balst.Destroy();
   if (Shield_Actor != None)
     Shield_Actor.Destroy();
   Destroy();
  }

  }
 else if (TSP_Mode == P_Charging)
  {
   if (Stored_Energy > 0)
   {
     if (VSize(Velocity) > 1)
     SetRotation(Rotator(Velocity));
     TraceStart = Location;
     TraceDirection = Rotation;
     TraceEnd = TraceStart + vector(TraceDirection)*96;
     Victim = Trace(HitLocation,HitNormal,TraceEnd,TraceStart);
     if ((Victim != none) && (Victim.IsA('Pawn'))) // && (Victim != Master)
      {
       Victim.TakeDamage(2*(20+Stored_Energy), Master, Victim.Location, 2000*Stored_Energy*vector(Rotation), Class'XWeapons.DamTypeShieldImpact');
       PlaySound(ProceduralSound'WeaponSounds.PShieldGunFire.P1ShieldGunFire',,(50+Stored_Energy*2)/255,,256);
	   Pickup = Spawn(Class'SHIELD_2016.SHIELD_PICKUP',,, Location,Rotator(-Vector(Rotation)));
   	   if ( Pickup != None )
   	   {
	    Pickup.InitDroppedPickupFor(none);
        Pickup.Velocity = -10*vector(Rotation)*Stored_Energy;
       }
       Destroy();
      }
   }
  }
}

DefaultProperties
{
  DrawType=DT_Mesh
  StaticMesh=StaticMesh'SHIELD_2016_SM.PICKUP_MESH'
  Mesh=SkeletalMesh'SHIELD_2016_ANI.SHIELD_GUN'
  DrawScale=0.400000
  AmbientGlow=128
  CollisionRadius=32.000000
  CollisionHeight=48.000000
  Physics = Phys_Falling
  bOrientOnSlope=True
  bShouldBaseAtStartup=True
  bCollideActors=True
  bCollideWorld=True
  bUseCylinderCollision=True
  SoundVolume = 0
}
