Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.49 KB | None | 0 0
  1. local weaponClass = {}
  2. weaponClass.__index = weaponClass
  3.  
  4. -- Setup
  5. local import = require(game:GetService("ReplicatedStorage"):WaitForChild("Import"))
  6. local network = import("Shared/Network")
  7.  
  8. -- Constants
  9. local players = game:GetService("Players")
  10. local runService = game:GetService("RunService")
  11. local userInputService = game:GetService("UserInputService")
  12. local tweenService = game:GetService("TweenService")
  13.  
  14. local player = players.LocalPlayer
  15. local camera = game.Workspace.CurrentCamera
  16.  
  17. local weaponFolder = import("Client/Weapons")
  18. local springClass = import("Shared/Spring")
  19. local maidClass = import("Shared/Maid")
  20.  
  21. -- Module
  22. function weaponClass.new(weaponName)
  23.     local self = setmetatable({}, weaponClass)
  24.     self.maid = maidClass.new()
  25.     self.state = "idle"
  26.     self.equipped = false
  27.    
  28.     self.inspecting = false
  29.    
  30.     self.ammo = 30
  31.     self.maxAmmo = 90
  32.     self.reloading = false
  33.    
  34.     self.folder = weaponFolder:FindFirstChild(weaponName)
  35.     self.settings = require(self.folder.settings)
  36.     self.maid.model = self.folder.weapon:Clone()
  37.     self.maid.model.Parent = camera
  38.    
  39.     self.springs = {
  40.         sway = springClass.new(Vector3.new(), Vector3.new(), Vector3.new(), 25, 1),
  41.         tilt = springClass.new(0, 0, 0, 10, 1),
  42.         recoil = springClass.new(Vector3.new(), Vector3.new(), Vector3.new(), 20, 1),
  43.     }
  44.    
  45.     -- updating userInputService
  46.     userInputService.MouseIconEnabled = false
  47.    
  48.     -- maid tasks
  49.     self.maid:GiveTask(userInputService.InputChanged:Connect(function(input)
  50.         if input.UserInputType == Enum.UserInputType.MouseMovement then
  51.             local delta = userInputService:GetMouseDelta()
  52.             self.springs.sway.target = Vector3.new(math.rad(delta.X * 0.05), math.rad(delta.Y * 0.05), 0)
  53.         end
  54.     end))
  55.    
  56.     self.maid:GiveTask(runService.RenderStepped:Connect(function(dt)
  57.         self.springs["sway"]:update(dt)
  58.         self.springs["tilt"]:update(dt)
  59.         self.springs["recoil"]:update(dt)
  60.        
  61.         -- update tilt
  62.         local moveDir = player.Character.Humanoid.MoveDirection
  63.         local strafe = -player.Character.HumanoidRootPart.CFrame.rightVector:Dot(moveDir)
  64.         self.springs.tilt.target = math.rad(5 * strafe)
  65.        
  66.         if self.state == "walk" then
  67.             self.maid.model:SetPrimaryPartCFrame(camera.CFrame * CFrame.new( (math.sin(tick() * 9) * 0.03) , (math.sin(tick() * 16) * 0.02) , 0))
  68.         elseif self.state == "run" then
  69.             self.maid.model:SetPrimaryPartCFrame(camera.CFrame * CFrame.new( (math.sin(tick() * 12) * 0.03) , (math.sin(tick() * 16) * 0.03) , 0))
  70.         elseif self.state == "idle" then
  71.             self.maid.model:SetPrimaryPartCFrame(camera.CFrame * CFrame.new( (math.sin(tick() * 1) * 0.03) , (math.sin(tick() * 2) * 0.02) , 0))
  72.         end
  73.        
  74.         self.maid.model:SetPrimaryPartCFrame(self.maid.model.PrimaryPart.CFrame
  75.              * CFrame.Angles((self.springs["sway"].p.x * -1), (self.springs["sway"].p.x * -1), 0)
  76.              * CFrame.new(0, 0, self.springs["recoil"].p.x)
  77.              * CFrame.fromEulerAnglesYXZ(0, 0, self.springs["tilt"].p / 4))
  78.            
  79.         camera.CFrame = camera.CFrame * CFrame.fromEulerAnglesYXZ(self.springs["recoil"].p.y, self.springs["recoil"].p.z, 0)
  80.     end))
  81.        
  82.     self.maid:GiveTask(player.Character.Humanoid.Running:Connect(function(speed)
  83.         if speed > 0 and speed < 20 then
  84.             if self.state ~= "walk" then
  85.                 self.state = "walk"
  86.             end
  87.         elseif speed == 0 then
  88.             if self.state ~= "idle" then
  89.                 self.state = "idle"
  90.             end
  91.         else
  92.             if self.state ~= "run" then
  93.                 self.state = "run"
  94.             end
  95.         end
  96.     end))
  97.        
  98.     self.maid:GiveTask(userInputService.InputBegan:Connect(function(input, processed)
  99.         if input.UserInputType == Enum.UserInputType.Keyboard then
  100.             if input.KeyCode == Enum.KeyCode.LeftShift then
  101.                 local updateWalkspeed = network:GetEvent("UpdateWalkspeed")
  102.                 updateWalkspeed:Fire(22)
  103.                 self:AnimateKeyframe(self.settings.sprintPlacement)
  104.             end
  105.         end
  106.     end))
  107.        
  108.     self.maid:GiveTask(userInputService.InputEnded:Connect(function(input, processed)
  109.         if input.UserInputType == Enum.UserInputType.Keyboard then
  110.             if input.KeyCode == Enum.KeyCode.LeftShift then
  111.                 local updateWalkspeed = network:GetEvent("UpdateWalkspeed")
  112.                 updateWalkspeed:Fire(16)
  113.                 self:AnimateKeyframe(self.settings.defaultPlacement)
  114.             end
  115.         end
  116.     end))
  117.    
  118.     return self
  119. end
  120.  
  121. function weaponClass:AnimateKeyframe(keyframeData, tweenTime)
  122.     for i,v in pairs(self.maid.model:GetDescendants()) do
  123.         if keyframeData[v.Name] and v:IsA("Motor6D") then
  124.             tweenService:Create(v, TweenInfo.new(tweenTime or keyframeData.Time), {C1 = keyframeData[v.Name]}):Play()
  125.         end
  126.     end
  127. end
  128.  
  129. function weaponClass:Destroy()
  130.     self.maid:Destroy()
  131. end
  132.  
  133.  
  134. return weaponClass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement