Advertisement
Guest User

Untitled

a guest
Dec 30th, 2016
31
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. include("sh_grapple_config.lua")
  2.  
  3. function ProjectVectorOnNormal(vec, norm)
  4.     norm:Normalize()
  5.     return vec:Dot(norm) * u
  6. end
  7.  
  8. sound.Add( {
  9.     name = "grapple_launch_loop",
  10.     channel = CHAN_AUTO,
  11.     volume = 1.0,
  12.     level = 80,
  13.     pitch = { 95, 110 },
  14.     sound = "ambient/tones/steam_loop1.wav"
  15. } )
  16.  
  17. function Grapple.CreateEntry(ply, dest)
  18.     local ret = {Player = ply, Destination = dest, Points = {}}
  19.     for i = 1, Grapple.Config.Resolution, 1 do
  20.         ret.Points[i] = Vector(0,0,0)
  21.     end
  22.     return ret
  23. end
  24.  
  25. function Grapple.MovePlayer(self)
  26.     if (!self or self == NULL) then return end
  27.     local ply = self:GetOwner()
  28.     local dest = self:GetPos()
  29.    
  30.     local nrm = (dest - ply:EyePos())
  31.     local dist = nrm:Length()
  32.    
  33.     if (dist > Grapple.Config.MaxDistance) then
  34.         local nrmLen = dest - ply:GetPos()
  35.         nrmLen:Normalize();
  36.         ply:SetPos(dest - nrmLen * Grapple.Config.MaxDistance)
  37.     end
  38.    
  39.     nrm = nrm / dist;
  40.     //ply:SetVelocity(nrm * Grapple.Config.GrapplePower * 4)
  41.    
  42.     local vel = ply:GetVelocity()
  43.     local mag = ply:GetVelocity():LengthSqr()
  44.     if (mag > Grapple.Config.MaxGrappleVelocitySqr) then
  45.         vel:Normalize()
  46.         //ply:SetAbsVelocity(vel * Grapple.Config.MaxGrappleVelocity)
  47.     end
  48.     return nrm * Grapple.Config.GrapplePower, dist
  49. end
  50.  
  51. hook.Add("PlayerSpawn", "zgrapplePlayerJump", function(ply)
  52.     ply:SetJumpPower(1200)
  53.     ply.OnWall = false
  54.     ply.WallNrm = Vector(0,0,1)
  55.     if (ply.CurrentGrapple) then
  56.         ply.CurrentGrapple:Remove()
  57.     end
  58.     print(ply:GetJumpPower())
  59. end)
  60.  
  61. hook.Add("Move", "grapplePlayerEarlyMove", function(ply, mv)
  62.     ply.OnWall = ply.OnWall or false
  63.     ply.WallNrm = ply.WallNrm or Vector(0,0,1)
  64.     if (ply:IsOnGround()) then
  65.         if (!ply.canGDoubleJump) then
  66.             ply.canGDoubleJump = true
  67.         end
  68.     elseif (ply:KeyPressed(IN_JUMP)) then
  69.         if (ply.OnWall) then
  70.             ply.OnWall = false
  71.             local vel = mv:GetVelocity()
  72.             vel.x = ply.WallNrm.x * ply:GetJumpPower()
  73.             vel.y = ply.WallNrm.y * ply:GetJumpPower()
  74.             vel.z = vel.z + ply.WallNrm.z + ply:GetJumpPower()
  75.             mv:SetVelocity(vel)
  76.         elseif (ply.canGDoubleJump) then
  77.             print("jump")
  78.             local vel = mv:GetVelocity()
  79.             vel.z = 0
  80.            
  81.             local eyeFwd = ply:GetAngles():Forward()
  82.             vel.x = eyeFwd.x
  83.             vel.y = eyeFwd.y
  84.             vel = vel * mag
  85.             vel.z = ply:GetJumpPower() * 0.75
  86.             mv:SetVelocity(vel)
  87.             ply.canGDoubleJump = false
  88.         end
  89.     else
  90.         //for i = 1, 8, 1 do
  91.         local nrmVel = mv:GetVelocity()
  92.         nrmVel:Normalize()
  93.         local tr = nil
  94.         if (ply.OnWall) then
  95.             tr = util.TraceLine({start = mv:GetOrigin(), endpos = mv:GetOrigin() + -ply.WallNrm * 100, mask = MASK_SOLID_BRUSHONLY})
  96.         else
  97.             tr = util.TraceLine({start = mv:GetOrigin(), endpos = mv:GetOrigin() + nrmVel * 100, mask = MASK_SOLID_BRUSHONLY})
  98.         end
  99.         if (tr.Hit and math.abs(tr.HitNormal.z) < 0.1) then
  100.             ply.OnWall = true
  101.             ply.WallNrm = tr.HitNormal
  102.         end
  103.         //end
  104.     end
  105. end)
  106.  
  107. hook.Add("FinishMove", "zgrapplePlayerMove", function(ply, mv)
  108.     /*if (ply:KeyDown(IN_JUMP)) then
  109.         local vel = mv:GetVelocity()
  110.         //vel.z = vel.z + 7
  111.         if (vel.z < 0) then
  112.             vel.z = vel.z / 1.1
  113.         end
  114.         //vel.z = math.max(vel.z, -0.2)
  115.         mv:SetVelocity(vel)
  116.     end*/
  117.    
  118.     if (ply.OnWall) then
  119.         print("running")
  120.         local temp = mv:GetVelocity()
  121.         temp.z = temp.z + 8
  122.         mv:SetVelocity(temp)
  123.     end
  124.  
  125.     if (ply.CurrentGrapple and IsValid(ply.CurrentGrapple) and ply.CurrentGrapple:GetState() >= 3) then
  126.         //print(Grapple.MovePlayer(ply.CurrentGrapple))
  127.         // TODO: add max length
  128.         local move, dist = Grapple.MovePlayer(ply.CurrentGrapple)
  129.         if (!move) then
  130.             ply.CurrentGrapple = nil
  131.             return
  132.         end
  133.        
  134.         local nrmmove = Vector(move.x, move.y, move.z)
  135.         nrmmove:Normalize()
  136.        
  137.         local mult = (math.Clamp(-ply:EyeAngles():Forward():Dot(nrmmove), 0, 1) + 1)
  138.         //print(mult)
  139.        
  140.         local tg = mv:GetVelocity() + move / 4 * mult
  141.         local len = tg:LengthSqr()
  142.         if (dist < 50) then
  143.             tg = tg / 1.2
  144.         else
  145.             tg = tg - Vector(0,0,1)
  146.             if (len > Grapple.Config.MaxGrappleVelocitySqr) then
  147.                 tg = tg / math.sqrt(len) * Grapple.Config.MaxGrappleVelocity
  148.             end
  149.         end
  150.         mv:SetVelocity(tg)
  151.     end
  152. end)
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement