Advertisement
KingPhantom

Framework Issue

Nov 12th, 2018
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.02 KB | None | 0 0
  1. --// Objects
  2. local player = game.Players.LocalPlayer
  3. local char = player.Character or player.CharacterAdded:wait()
  4. local mouse = player:GetMouse()
  5. local cam = workspace.CurrentCamera
  6.  
  7. local wfc = game.WaitForChild
  8. local ffc = game.FindFirstChild
  9. local ffcoc = game.FindFirstChildOfClass
  10. local getch = game.GetChildren
  11.  
  12. --// Services
  13. local UserInput = game:GetService("UserInputService")
  14. local RunService = game:GetService("RunService")
  15. local TextService = game:GetService("TextService")
  16. local TweenService = game:GetService("TweenService")
  17. local RepStore = game:GetService("ReplicatedStorage")
  18.  
  19. local events = wfc(RepStore,"Events")
  20. local models = wfc(RepStore,"Models")
  21. local modules = wfc(RepStore,"Modules")
  22.  
  23. --// Variables
  24. local cf = CFrame.new
  25. local ang = CFrame.Angles
  26. local v3 = Vector3.new
  27. local lerpv3 = v3().lerp
  28. local lerpcf = cf().lerp
  29. local v2 = Vector2.new
  30. local c3 = Color3.new
  31. local rgb = Color3.fromRGB
  32. local brc = BrickColor.new
  33. local ud2 = UDim2.new
  34. local sin,cos,tan = math.sin,math.cos,math.sin
  35. local asin,acos,atan = math.asin,math.acos,math.atan
  36. local pi,tpi,hpi = math.pi,math.pi*2,math.pi/2
  37. local atan2 = math.atan2
  38. local sqrt = math.sqrt
  39. local abs = math.abs
  40. local random = math.random
  41. local floor,ceil,clamp = math.floor,math.ceil,math.clamp
  42. local rad,deg = math.rad,math.deg
  43. local rdm = math.random
  44.  
  45. local ncf = cf()
  46. local nv3 = v3()
  47.  
  48. local lerp = ncf.lerp
  49.  
  50. --// Modules
  51. local spring = {}
  52. local camera = {}
  53. local character = {}
  54. local util = {}
  55. local ik = {}
  56. local loadout = {}
  57. local gun = {}
  58. local animate = {}
  59. local menu = {}
  60. local hud = {}
  61.  
  62.  
  63. do --// Spring
  64.      local tick = tick
  65.     local cos = math.cos
  66.     local sin = math.sin
  67.     local e = 2.718281828459045
  68.     local function getposvel(s, d, p0, c1, c2, t0)
  69.         local t = tick() - t0
  70.         if d >= 1 then
  71.             return (c1 + c2 * s * t) / e ^ (s * t) + p0, (c2 * s * (1 - t) - c1) / e ^ (s * t)
  72.         else
  73.             local h = (1 - d * d) ^ 0.5
  74.             return (c1 * cos(s * h * t) + c2 * sin(s * h * t)) / e ^ (s * d * t) + p0, s * ((h * c2 - d * c1) * cos(s * h * t) - (h * c1 + d * c1) * sin(s * h * t)) / e ^ (s * d * t)
  75.         end
  76.     end
  77.     function spring.new(initial, s, d)
  78.         local self = {}
  79.         local s = s or 15
  80.         local d = d or 0.5
  81.         local p0 = initial or 0
  82.         local c1 = 0 * p0
  83.         local c2 = 0 * p0
  84.         local t0 = tick()
  85.         function self:impulse(a)
  86.             local p, v = getposvel(s, d, p0, c1, c2, t0)
  87.             t0 = tick()
  88.             c1 = p
  89.             if d >= 1 then
  90.                 c2 = c1 + (v + a) / s
  91.             else
  92.                 local h = (1 - d * d) ^ 0.5
  93.                 c2 = d / h * c1 + (v + a) / (s * h)
  94.             end
  95.         end
  96.         local meta = {}
  97.         function meta.__index(table, index)
  98.             if index == "position" or index == "p" then
  99.                 local p, v = getposvel(s, d, p0, c1, c2, t0)
  100.                 return p
  101.             elseif index == "velocity" or index == "v" then
  102.                 local p, v = getposvel(s, d, p0, c1, c2, t0)
  103.                 return v
  104.             elseif index == "dampen" or index == "d" then
  105.                 return d
  106.             elseif index == "speed" or index == "s" then
  107.                 return s
  108.             end
  109.         end
  110.         function meta.__newindex(table, index, value)
  111.             if index == "dampen" or index == "d" then
  112.                 d = value
  113.             elseif index == "speed" or index == "s" then
  114.                 s = value
  115.             elseif index == "target" or index == "t" then
  116.                 local p, v = getposvel(s, d, p0, c1, c2, t0)
  117.                 t0 = tick()
  118.                 p0 = value
  119.                 c1 = p - p0
  120.                 if d >= 1 then
  121.                     c2 = c1 + v / s
  122.                 else
  123.                     local h = (1 - d * d) ^ 0.5
  124.                     c2 = d / h * c1 + v / (s * h)
  125.                 end
  126.             end
  127.         end
  128.         return setmetatable(self, meta)
  129.     end
  130. end
  131.  
  132. do --// Camera
  133.     local self = camera
  134.     self.recoil = spring.new(nv3)
  135.     self.recoil.d = 0.8
  136.     self.recoil.s = 20
  137.    
  138.     self.fov = spring.new(80)
  139.     self.fov.d = 0.8
  140.     self.fov.s = 20
  141.    
  142.     self.impulserecoil = function(self,vel)
  143.         self.recoil:impulse(vel)
  144.     end
  145.    
  146.     self.setfov = function(self,fov)
  147.         self.fov.t = fov
  148.     end
  149. end
  150.  
  151. do --// Character
  152.     local self = character
  153.     self.running = false
  154.    
  155.     self.setsprint = function(self,state)
  156.         self.running = not gun.aiming and state or false
  157.         char.Humanoid.WalkSpeed = self.running and 26 or 16
  158.     end
  159.    
  160.     self.grab = v3()
  161. end
  162.  
  163. do --// Utility
  164.     local self = util
  165.    
  166.     self.weldmodel = function(self,model)
  167.         assert(model.PrimaryPart,"Model must have a PrimaryPart set in order to weld it")
  168.        
  169.         for i,v in pairs(model:GetDescendants()) do
  170.             if v:IsA("BasePart") then
  171.                 v.CanCollide = false
  172.                 v.Anchored = false
  173.                
  174.                 local weld = Instance.new("Weld")
  175.                 weld.Part0 = model.PrimaryPart
  176.                 weld.Part1 = v
  177.                 weld.C0 = model.PrimaryPart.CFrame:Inverse()
  178.                 weld.C1 = v.CFrame:Inverse()
  179.                 weld.Name = v.Name
  180.                 weld.Parent = model.PrimaryPart
  181.             end
  182.         end
  183.     end
  184.    
  185.     self.randomvector = function(self,min,max)
  186.         min = min or v3()
  187.         max = max or v3()
  188.        
  189.         local minX,minY,minZ = min.X,min.Y,min.Z
  190.         local maxX,maxY,maxZ = max.X,max.Y,max.Z
  191.        
  192.         return v3(rdm(minX,maxX),rdm(minY,maxY),rdm(minZ,maxZ))
  193.     end
  194. end
  195.  
  196. do --// Inverse Kinematics
  197.     local self = ik
  198.     local ptos = ncf.PointToObjectSpace
  199.    
  200.     self.new = function(self)
  201.         local ret = {}
  202.         function ret:solve(r0, r1, c, p)
  203.             local t = ptos(c, p)
  204.             local tx, ty, tz = t.x, t.y, t.z
  205.            
  206.             local d = (tx^2 + ty^2 + tz^2)^.5
  207.             local nx, ny, nz = tx/d, ty/d, tz/d
  208.            
  209.             d = r0 + r1 < d and r0 + r1 or d
  210.            
  211.             local l = (r1^2 - r0^2 - d^2)/(2 * r0 * d)
  212.             local h = (1 - l^2)^.5
  213.             local a = atan2(-h, -l)
  214.            
  215.             local j0 = c * cf(nv3, t) * ang(a, 0, 0)
  216.             return j0, j0 * cf(0, 0, -r0) * ang(-2 * a, 0, 0)
  217.         end
  218.        
  219.         return ret
  220.     end
  221. end
  222.  
  223. do --// Loadout
  224.    
  225. end
  226.  
  227. do --// Gun
  228.     local self = gun
  229.    
  230.     self.gunbob = function(self,s0,d0,d1,d2,d3)
  231.         local t,s = tick(),s0/2
  232.         local c = cf(not self.running and sin(t*s)/d0 or 0,cos(t*s0)/d1,self.running and sin(t*s)/d0 or 0)
  233.         local a = not self.running and ang(sin(t*s0)/d2,0,sin(t*s)/d3) or ang(sin(t*s)/d1,0,0)
  234.         return c*a
  235.     end
  236.    
  237.     self.new = function(self,name)
  238.         self.walk = cf()
  239.         self.aimpos = cf()
  240.         self.aimrot = cf()
  241.         self.rarmc0 = cf()
  242.         self.larmc0 = cf()
  243.        
  244.         local aimspring = spring.new(nv3)
  245.         aimspring.d = 0.85
  246.         aimspring.s = 20
  247.        
  248.         local aimrotspring = spring.new(nv3)
  249.         aimrotspring.d = 0.85
  250.         aimrotspring.s = 20
  251.        
  252.         local recoilspringp = spring.new(nv3)
  253.         recoilspringp.d = 0.8
  254.         recoilspringp.s = 30
  255.        
  256.         local recoilspringr = spring.new(nv3)
  257.         recoilspringr.d = 0.8
  258.         recoilspringr.s = 20
  259.        
  260.         self.lanimcf0p = spring.new(nv3)
  261.         self.lanimcf0p.d = 1
  262.         self.lanimcf0p.s = 20
  263.        
  264.         self.lanimcf0r = spring.new(nv3)
  265.         self.lanimcf0r.d = 1
  266.         self.lanimcf0r.s = 20
  267.        
  268.         self.janimcf0p = spring.new(nv3)
  269.         self.janimcf0p.d = 1
  270.         self.janimcf0p.s = 20
  271.        
  272.         self.janimcf0r = spring.new(nv3)
  273.         self.janimcf0r.d = 1
  274.         self.janimcf0r.s = 20
  275.        
  276.         recoil = {pos = recoilspringp,rot = recoilspringr,cf = cf()}
  277.        
  278.         self.model = models[name]:Clone()
  279.         util:weldmodel(self.model)
  280.         self.model.Parent = cam
  281.        
  282.         local data = require(modules[name])()
  283.        
  284.         self.getdata = function()
  285.             return data
  286.         end
  287.        
  288.         self.shooting = false
  289.         self.aiming = false
  290.        
  291.         self.animoffset = cf()
  292.        
  293.         self.rootpart = Instance.new("Part")
  294.         self.rootpart.Size = v3(1,1,1)
  295.         self.rootpart.Transparency = 1
  296.         self.rootpart.CanCollide = false
  297.         self.rootpart.Name = "root"
  298.         self.rootpart.CFrame = cam.CFrame
  299.         self.rootpart.Parent = cam
  300.        
  301.         self.rarm = Instance.new("Part")
  302.         self.rarm.Size = v3(0.5,0.5,3)
  303.         self.rarm.TopSurface = Enum.SurfaceType.SmoothNoOutlines
  304.         self.rarm.CanCollide = false
  305.         self.rarm.Parent = cam
  306.         self.rarm.Name = "rightarm"
  307.        
  308.         self.joint = Instance.new("Motor6D")
  309.         self.joint.Part0 = self.rootpart
  310.         self.joint.Part1 = self.rarm
  311.         self.joint.C0 = cf(1,-1,-0.5)
  312.         self.joint.Name = "mainweld"
  313.         self.joint.Parent = self.rarm
  314.        
  315.         self.rjoint = Instance.new("Motor6D")
  316.         self.rjoint.Part0 = self.rarm
  317.         self.rjoint.Part1 = self.model.Right
  318.         self.rjoint.C0 = cf(0,0.2,-1.5)
  319.         self.rjoint.Name = "rightarmweld"
  320.         self.rjoint.Parent = cam
  321.        
  322.         self.larm = Instance.new("Part")
  323.         self.larm.Size = v3(0.5,0.5,3)
  324.         self.larm.TopSurface = Enum.SurfaceType.SmoothNoOutlines
  325.         self.larm.BottomSurface = Enum.SurfaceType.SmoothNoOutlines
  326.         self.larm.CanCollide = false
  327.         self.larm.Parent = cam
  328.         self.larm.Name = "leftarm"
  329.        
  330.         self.lc0 = cf(0,0,-1.5)
  331.         self.ljoint = Instance.new("Motor6D")
  332.         self.ljoint.Part0 = self.larm
  333.         self.ljoint.Part1 = self.model.Left
  334.         self.ljoint.C0 = self.lc0
  335.         self.ljoint.Name = "leftarmweld"
  336.         self.ljoint.Parent = cam
  337.        
  338.         self.lanimcf0 = cf()
  339.         self.janimcf0 = cf()
  340.        
  341. --      self.upperlarm = Instance.new("Part")
  342. --      self.upperlarm.Size = v3(0.5,0.5,1.6)
  343. --      self.upperlarm.TopSurface = Enum.SurfaceType.SmoothNoOutlines
  344. --      self.upperlarm.BottomSurface = Enum.SurfaceType.SmoothNoOutlines
  345. --      self.upperlarm.CanCollide = false
  346. --      self.upperlarm.Parent = cam
  347. --      self.upperlarm.Name = "upperlarm"
  348. --     
  349. --      self.lowerlarm = Instance.new("Part")
  350. --      self.lowerlarm.Size = v3(0.5,0.5,1.6)
  351. --      self.lowerlarm.TopSurface = Enum.SurfaceType.SmoothNoOutlines
  352. --      self.lowerlarm.BottomSurface = Enum.SurfaceType.SmoothNoOutlines
  353. --      self.lowerlarm.CanCollide = false
  354. --      self.lowerlarm.Parent = cam
  355. --      self.lowerlarm.Name = "lowerlarm"
  356.        
  357.         self.lshoulder = self.rootpart.CFrame * cf(-1,-1,-0.7)
  358.         self.lhand = gun.model.Left
  359.        
  360.         character.grappart = self.lhand
  361.         character.grab = character.grappart.Position
  362.        
  363.         self.larmik = ik.new()
  364.        
  365.  
  366.        
  367.         self.animdata = {
  368.             walking = function()
  369.                 return 11,12,13,29,14
  370.             end,
  371.             running = function()
  372.                 return 18,5,8,20,10
  373.             end,
  374.             aiming = function()
  375.                 return 7,64,104,192,112
  376.             end
  377.         }
  378.        
  379.         self.aim = function(self,state)
  380.             self.aiming = state
  381.            
  382.             local offset = (self.joint.Part1.CFrame:Inverse()*self.model.Aim.CFrame).p
  383.             offset = self.joint.C0 * offset
  384.             aimspring.t = state and offset or nv3
  385.            
  386.             local aimrotx = 0
  387.             local aimroty = 1.6
  388.             local aimrotz = 8
  389.            
  390.             local aimrot = v3(0,state and -aimroty or aimroty,state and -aimrotz or aimrotz)
  391.            
  392.             aimrotspring:impulse(aimrot)
  393.            
  394.             camera:setfov(state and 80-data.zoom or 80)
  395.             character:setsprint(false)
  396.         end
  397.        
  398.         local firedebounce = false
  399.         self.fireround = function(self)
  400.             if not firedebounce then
  401.                 firedebounce = true
  402.                
  403.                 local s = gun.model.PrimaryPart.Fire:Clone()
  404.                 s.Parent = cam
  405.                 s:Play()
  406.                
  407.                 local pos = not self.aiming and data.hiprecoilpos or data.aimrecoilpos
  408.                 local rot = not self.aiming and data.aimrecoilpos or data.aimrecoilpos
  409.                 local crot = not self.aiming and v3(data.hipmincamrot.X,rdm(data.hipmincamrot.Y,data.hipmaxcamrot.Y),0) or v3(data.aimmincamrot.X,rdm(data.aimmincamrot.Y,data.aimmaxcamrot.Y),0)
  410.                
  411.                 recoil.pos:impulse(pos)
  412.                 recoil.rot:impulse(rot)
  413.                 camera.recoil:impulse(crot)
  414.                
  415.                 wait(60/800)
  416.                
  417.                 firedebounce = false
  418.             end
  419.         end
  420.        
  421.         local fakegrip = v3()
  422.        
  423.         local nah = cf()
  424.        
  425.         self.step = function(self,dt)
  426.             for i,v in pairs(getch(self.model)) do v.Velocity = v3() end
  427.             self.rarm.Velocity = v3()
  428.             self.larm.Velocity = v3()
  429.            
  430.             local vel = char.HumanoidRootPart.Velocity
  431.             local rvel = (char.HumanoidRootPart.CFrame:Inverse() - char.HumanoidRootPart.CFrame:Inverse().Position) * vel
  432.             local walking = abs(sqrt(vel.X^2+vel.Z^2)) > 1
  433.            
  434.             local aimpos = aimspring.p
  435.             local aimrot = aimrotspring.p
  436.             local recoilpos = recoil.pos.p
  437.             local recoilrot = recoil.rot.p
  438.            
  439.             local ax,ay,az = aimpos.X,aimpos.Y,aimpos.Z
  440.             local arx,ary,arz = aimrot.X,aimrot.Y,aimrot.Z
  441.             local rpx,rpy,rpz = recoilpos.X,recoilpos.Y,recoilpos.Z
  442.             local rrx,rry,rrz = recoilrot.X,recoilrot.Y,recoilrot.Z
  443.            
  444.            
  445.            
  446.             local anims = {
  447.                 walking = data.anims.walking(),
  448.                 running = data.anims.running(),
  449.                 aiming = data.anims.aiming()
  450.             }
  451.            
  452.             local anim = walking and not self.aiming and not character.running and lerp(self.walk,anims.walking(),0.2) or
  453.                          walking and not self.aiming and character.running and lerp(self.walk,anims.running(),0.2) or
  454.                          walking and self.aiming and lerp(self.walk,anims.aiming(),0.2) or lerp(self.walk,cf(),0.2)
  455.            
  456.             self.walk = anim
  457.             aimpos = cf(ax,ay,az)
  458.             aimrot = ang(arx,ary,arz)
  459.             recoil.cf = cf(rpx,rpy,rpz)*ang(rad(1)*rrx,rad(1)*rry,rad(1)*rrz)
  460.            
  461.             self.janimcf0 = cf(self.janimcf0p.p)*ang(self.janimcf0r.p.X,self.janimcf0r.p.Y,self.janimcf0r.p.Z)
  462.             self.lanimcf0 = cf(self.lanimcf0p.p)*ang(self.lanimcf0r.p.X,self.lanimcf0r.p.Y,self.lanimcf0r.p.Z)
  463.            
  464.             self.joint.C0 = cf(1,-1.2,-0.3)
  465.             * recoil.cf
  466.             * self.janimcf0
  467.            
  468.             self.joint.C1 = self.walk
  469.             * aimpos
  470.             * aimrot
  471.            
  472.             self.ljoint.C0 = self.lc0
  473.             * self.lanimcf0
  474.            
  475.            
  476.             self.larmc0 = lerp(self.larmc0,self.aiming and ang(0,rad(10),rad(-15)) or cf(),0.15)
  477.             self.ljoint.C1 = self.larmc0
  478.            
  479.             if self.shooting then
  480.                 self:fireround()
  481.             end
  482.         end
  483.     end
  484. end
  485.  
  486. do --// Animation
  487.     local self = animate
  488.    
  489.     self.inprogress = false
  490.    
  491.     self.play = function(self,animdata)
  492.         local cache = {}
  493.         self.inprogress = true
  494.        
  495.         for frame,framedata in pairs(animdata) do
  496.             if gun[framedata[1]] then
  497.                 local m = framedata[1]
  498.                 local endp = framedata[2]
  499.                 local endr = framedata[3]
  500.                 local d = framedata[4]
  501.                 local start = gun[m]
  502.                
  503.                 gun[m.."p"].t = endp
  504.                 gun[m.."r"].t = endr
  505.                
  506.                 repeat wait() until gun[m.."p"].p.Magnitude >= endp.Magnitude*0.97 and gun[m.."r"].p.Magnitude >= endr.Magnitude*0.97
  507.                
  508.                 if d then wait(d) end
  509.             elseif framedata[1] == "weldmag" then
  510.                 local mw = gun.model.PrimaryPart.Mag
  511.                
  512.                 cache.oldmagweld = mw
  513.                 mw.Parent = nil
  514.                
  515.                 mw = Instance.new("Motor6D")
  516.                 mw.Part0 = gun.larm
  517.                 mw.Part1 = gun.model.Mag
  518.                 mw.Parent = cam
  519.                 mw.C0 = cf(0,0,-1.5)
  520.                
  521.                 cache.newmagweld = mw
  522.             elseif framedata[1] == "returnmag" then
  523.                 cache.oldmagweld.Parent = gun.model.PrimaryPart
  524.                 cache.newmagweld:Destroy()
  525.             end
  526.         end
  527.            
  528.         self.inprogress = true
  529.     end
  530. end
  531.  
  532. --// Main
  533. gun:new("M4A1")
  534.  
  535. RunService.RenderStepped:Connect(function()
  536.     gun.rootpart.CFrame = cam.CFrame
  537.     local vel = char.HumanoidRootPart.Velocity
  538.     local rvel = (char.HumanoidRootPart.CFrame:Inverse() - char.HumanoidRootPart.CFrame:Inverse().Position) * vel
  539.     local walking = abs(sqrt(vel.X^2+vel.Z^2)) > 1
  540.  
  541.     local camrecoil = camera.recoil.p
  542.     local crx,cry,crz = camrecoil.X,camrecoil.Y,camrecoil.Z
  543.    
  544.     camera.cf = ang(rad(1)*crx,rad(1)*cry,rad(1)*crz)
  545.    
  546.     cam.CFrame = cam.CFrame
  547.     * camera.cf
  548.    
  549.     cam.FieldOfView = camera.fov.p
  550.    
  551.     gun:step()
  552. end)
  553.  
  554. UserInput.InputBegan:Connect(function(obj)
  555.     if obj.KeyCode == Enum.KeyCode.LeftShift then
  556.         character:setsprint(true)
  557.     elseif obj.KeyCode == Enum.KeyCode.R then
  558.         local data = gun:getdata()
  559.         animate:play(data.anims.reload())
  560.     end
  561.     if obj.UserInputType == Enum.UserInputType.MouseButton1 then
  562.         gun.shooting = true
  563.         --gun:fireround()
  564.     elseif obj.UserInputType == Enum.UserInputType.MouseButton2 then
  565.         gun:aim(true)
  566.     end
  567. end)
  568.  
  569. UserInput.InputEnded:Connect(function(obj)
  570.     if obj.KeyCode == Enum.KeyCode.LeftShift then
  571.         character:setsprint(false)
  572.     end
  573.     if obj.UserInputType == Enum.UserInputType.MouseButton1 then
  574.         gun.shooting = false
  575.     elseif obj.UserInputType == Enum.UserInputType.MouseButton2 then
  576.         gun:aim(false)
  577.     end
  578. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement