--[[

Useful variables to check when in "Custom Script Chunk" areas:

self.stickmode -- use this to check if the projectile has stick onto anything. 0 is nothing, 1 is an object, 2 is terrain.

self.sticktarget -- if the projectile has stuck onto an object, this pointer is of that object. Be sure to check if this is nil or not before applying it in the script.

--]]


function Create(self)

   self.mapwrapx = SceneMan.SceneWrapsX;
   self.stickmode = 0; -- 0 is not stuck, 1 is stuck on object, 2 is stuck on terrain. DON'T EDIT THIS!

--------------------------------------------------
---------- Custom Script Chunks (Create Function)



--------------------------------------------------
---------- Variables you can edit

   self.wepname = "Sticky Gun"; -- PresetName of gun.

   self.firespeed = 15; -- Speed of projectile when fired.
   self.degreespread = 30; -- How much spread the projectile has when shot.
   self.sharpdegreespread = 10; -- How much spread the projectile has when shot while Sharp Aiming.

   self.ignoreparent = true; -- if the projectile should ignore the parent when trying to stick to something. true for yes, false for no.
   self.stickchecklengthmode = 1; -- How the projectile checks for objects to stick to. 1 for constant-lengthed line check, 2 for varying-lengthed line check that's based on velocity.
   self.stickcheckrotmode = 1; -- How the projectile rotates the checking line. 1 for matching rotation with the projectile, 2 for matching rotation with the projectile's velocity.
   self.stickchecklength = 5; -- length of detection line (if using "self.stickcheckmode = 1").

   self.sticksoundname = "no sound" -- PresetName of sound FX emitter. Have as "no sound" (with  the quotation marks) for no sound to be played.

--------------------------------------------------
---------- Gun/Parent Finding

   self.curdist = 15;
   for i = 1,MovableMan:GetMOIDCount()-1 do
      self.gun = MovableMan:GetMOFromID(i);
      if self.gun.ClassName == "HDFirearm" and self.gun.PresetName == self.wepname and SceneMan:ShortestDistance(self.Pos,ToHDFirearm(self.gun).MuzzlePos,self.mapwrapx).Magnitude <= self.curdist then
         self.parentgun = ToHDFirearm(self.gun);
         self.actor = MovableMan:GetMOFromID(self.gun.RootID);
         if MovableMan:IsActor(self.actor) == true then
            self.parent = ToActor(self.actor);
         end
      end
   end

--------------------------------------------------
---------- Projectile Velocity

   if self.parentgun ~= nil then
      if math.random(1,2) == 2 then
         self.negatore = -1;
      else
         self.negatore = 1;
      end

      if self.parent ~= nil and self.parent:GetController():IsState(Controller.AIM_SHARP) then
         self.Vel = Vector(self.firespeed,0):RadRotate(self.parent:GetAimAngle(true)):DegRotate(   (   (math.random(1,100)*self.negatore)   /   100)   *   (self.sharpdegreespread/2)   );
      else
         self.Vel = Vector(self.firespeed,0):RadRotate(self.parent:GetAimAngle(true)):DegRotate(   (   (math.random(1,100)*self.negatore)   /   100)   *   (self.degreespread/2)   );
      end

      self.negatore = nil;
   end

--------------------------------------------------

end

function Update(self)

--------------------------------------------------
---------- Sticky Stuff

   if self.ignoreparent == true then
      if MovableMan:IsActor(self.parent) == true then
         self.ignoreid = self.parent.ID;
      end
   else
      self.ignoreid = 255;
   end

   if self.stickmode == 0 then

      if self.stickchecklengthmode == 1 then
         self.checklength = self.stickchecklength;
      else
         self.checklength = self.Vel.Magnitude;
      end

      if self.stickcheckrotmode == 1 then
         self.checkrot = self.RotAngle;
      else
         self.checkrot = self.Vel.AbsRadAngle;
      end

      self.stickobject = SceneMan:CastMORay(self.Pos,Vector(self.checklength,0):RadRotate(self.checkrot),self.ignoreid,0,false,0);
      self.clingpos = Vector(0,0);

      if self.stickobject ~= 255 and self.stickobject ~= self.ID then
         self.sticktarget = MovableMan:GetMOFromID(self.stickobject);
         self.stickpositionX = self.Pos.X-self.sticktarget.Pos.X;
         self.stickpositionY = self.Pos.Y-self.sticktarget.Pos.Y;
         self.stickrotation = self.sticktarget.RotAngle;
         self.stickdirection = self.RotAngle;
         self.startflipped = self.sticktarget.HFlipped;
         self.stickmode = 1;
         if self.sticksoundname ~= "no sound" then
            self.sticksound = CreateAEmitter(self.sticksoundname);
            self.sticksound.Pos = self.Pos;
            MovableMan:AddParticle(self.sticksound);
         end
      elseif SceneMan:CastStrengthRay(self.Pos,Vector(12,0):RadRotate(self.Vel.AbsRadAngle),0,self.clingpos,0,0,self.mapwrapx) == true then
         self.Vel = Vector(0,0);
         self.PinStrength = 1000;
         self.Pos = self.clingpos;
         self.stickmode = 2;
         if self.sticksoundname ~= "no sound" then
            self.sticksound = CreateAEmitter(self.sticksoundname);
            self.sticksound.Pos = self.Pos;
            MovableMan:AddParticle(self.sticksound);
         end
      end

   elseif self.stickmode == 1 then

      if self.sticktarget ~= nil and self.sticktarget.ID ~= 255 then
         self.ToDelete = false;
         self.ToSettle = false;
         self.Pos = self.sticktarget.Pos + Vector(self.stickpositionX,self.stickpositionY):RadRotate(self.sticktarget.RotAngle-self.stickrotation);
         self.RotAngle = self.stickdirection+(self.sticktarget.RotAngle-self.stickrotation);
         self.Vel = Vector(0,0);
         self.PinStrength = 1000;
      else
         self.PinStrength = 0;
         self.sticktarget = nil;
         self.stickmode = 0;
      end

   end

--------------------------------------------------
---------- Custom Script Chunks (Update Function)



--------------------------------------------------

end

function Destroy(self)

--------------------------------------------------
---------- Custom Script Chunks (Destroy Function)



--------------------------------------------------

end