Guest User

PotatoGatling.lua

a guest
Jun 28th, 2020
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 22.90 KB | None | 0 0
  1. dofile "$GAME_DATA/Scripts/game/AnimationUtil.lua"
  2. dofile "$SURVIVAL_DATA/Scripts/util.lua"
  3. dofile "$SURVIVAL_DATA/Scripts/game/survival_shapes.lua"
  4.  
  5. local Damage = 4
  6.  
  7. PotatoGatling = class()
  8.  
  9. local renderables = {
  10.     "$GAME_DATA/Character/Char_Tools/Char_spudgun/Base/char_spudgun_base_basic.rend",
  11.     "$GAME_DATA/Character/Char_Tools/Char_spudgun/Barrel/Barrel_spinner/char_spudgun_barrel_spinner.rend",
  12.     "$GAME_DATA/Character/Char_Tools/Char_spudgun/Sight/Sight_spinner/char_spudgun_sight_spinner.rend",
  13.     "$GAME_DATA/Character/Char_Tools/Char_spudgun/Stock/Stock_broom/char_spudgun_stock_broom.rend",
  14.     "$GAME_DATA/Character/Char_Tools/Char_spudgun/Tank/Tank_basic/char_spudgun_tank_basic.rend"
  15. }
  16.  
  17. local renderablesTp = {"$GAME_DATA/Character/Char_Male/Animations/char_male_tp_spudgun.rend", "$GAME_DATA/Character/Char_Tools/Char_spudgun/char_spudgun_tp_animlist.rend"}
  18. local renderablesFp = {"$GAME_DATA/Character/Char_Tools/Char_spudgun/char_spudgun_fp_animlist.rend"}
  19.  
  20. sm.tool.preloadRenderables( renderables )
  21. sm.tool.preloadRenderables( renderablesTp )
  22. sm.tool.preloadRenderables( renderablesFp )
  23.  
  24. function PotatoGatling.client_onCreate( self )
  25.     self.shootEffect = sm.effect.createEffect( "SpudgunSpinner - SpinnerMuzzel" )
  26.     self.shootEffectFP = sm.effect.createEffect( "SpudgunSpinner - FPSpinnerMuzzel" )
  27.     self.windupEffect = sm.effect.createEffect( "SpudgunSpinner - Windup" )
  28. end
  29.  
  30.  
  31.  
  32. function PotatoGatling.client_onRefresh( self )
  33.     self:loadAnimations()
  34. end
  35.  
  36. function PotatoGatling.loadAnimations( self )
  37.  
  38.     self.tpAnimations = createTpAnimations(
  39.         self.tool,
  40.         {
  41.             shoot = { "spudgun_shoot", { crouch = "spudgun_crouch_shoot" } },
  42.             aim = { "spudgun_aim", { crouch = "spudgun_crouch_aim" } },
  43.             aimShoot = { "spudgun_aim_shoot", { crouch = "spudgun_crouch_aim_shoot" } },
  44.             idle = { "spudgun_idle" },
  45.             pickup = { "spudgun_pickup", { nextAnimation = "idle" } },
  46.             putdown = { "spudgun_putdown" }
  47.         }
  48.     )
  49.     local movementAnimations = {
  50.         idle = "spudgun_idle",
  51.         idleRelaxed = "spudgun_relax",
  52.  
  53.         sprint = "spudgun_sprint",
  54.         runFwd = "spudgun_run_fwd",
  55.         runBwd = "spudgun_run_bwd",
  56.  
  57.         jump = "spudgun_jump",
  58.         jumpUp = "spudgun_jump_up",
  59.         jumpDown = "spudgun_jump_down",
  60.  
  61.         land = "spudgun_jump_land",
  62.         landFwd = "spudgun_jump_land_fwd",
  63.         landBwd = "spudgun_jump_land_bwd",
  64.  
  65.         crouchIdle = "spudgun_crouch_idle",
  66.         crouchFwd = "spudgun_crouch_fwd",
  67.         crouchBwd = "spudgun_crouch_bwd"
  68.     }
  69.  
  70.     for name, animation in pairs( movementAnimations ) do
  71.         self.tool:setMovementAnimation( name, animation )
  72.     end
  73.  
  74.     setTpAnimation( self.tpAnimations, "idle", 5.0 )
  75.  
  76.     if self.tool:isLocal() then
  77.         self.fpAnimations = createFpAnimations(
  78.             self.tool,
  79.             {
  80.                 equip = { "spudgun_pickup", { nextAnimation = "idle" } },
  81.                 unequip = { "spudgun_putdown" },
  82.  
  83.                 idle = { "spudgun_idle", { looping = true } },
  84.                 shoot = { "spudgun_shoot", { nextAnimation = "idle" } },
  85.                
  86.                 aimInto = { "spudgun_aim_into", { nextAnimation = "aimIdle" } },
  87.                 aimExit = { "spudgun_aim_exit", { nextAnimation = "idle", blendNext = 0 } },
  88.                 aimIdle = { "spudgun_aim_idle", { looping = true} },
  89.                 aimShoot = { "spudgun_aim_shoot", { nextAnimation = "aimIdle"} },
  90.  
  91.                 sprintInto = { "spudgun_sprint_into", { nextAnimation = "sprintIdle",  blendNext = 0.2 } },
  92.                 sprintExit = { "spudgun_sprint_exit", { nextAnimation = "idle",  blendNext = 0 } },
  93.                 sprintIdle = { "spudgun_sprint_idle", { looping = true } },
  94.             }
  95.         )
  96.     end
  97.  
  98.     self.normalFireMode = {
  99.         fireCooldown = 0.1,
  100.         spreadCooldown = 0.18,
  101.         spreadIncrement = 3.9,
  102.         spreadMinAngle = 0.25,
  103.         spreadMaxAngle = 32,
  104.         fireVelocity = 130.0,
  105.        
  106.         minDispersionStanding = 0.1,
  107.         minDispersionCrouching = 0.04,
  108.        
  109.         maxMovementDispersion = 0.4,
  110.         jumpDispersionMultiplier = 2
  111.     }
  112.  
  113.     self.aimFireMode = {
  114.         fireCooldown = 0.1,
  115.         spreadCooldown = 0.18,
  116.         spreadIncrement = 1.95,
  117.         spreadMinAngle = 0.25,
  118.         spreadMaxAngle = 24,
  119.         fireVelocity =  130.0,
  120.        
  121.         minDispersionStanding = 0.01,
  122.         minDispersionCrouching = 0.01,
  123.        
  124.         maxMovementDispersion = 0.4,
  125.         jumpDispersionMultiplier = 2
  126.     }
  127.  
  128.     self.fireCooldownTimer = 0.0
  129.     self.spreadCooldownTimer = 0.0
  130.    
  131.     self.movementDispersion = 0.0
  132.  
  133.     self.sprintCooldownTimer = 0.0 
  134.     self.sprintCooldown = 0.3
  135.  
  136.     self.aimBlendSpeed = 3.0
  137.     self.blendTime = 0.2
  138.  
  139.     self.jointWeight = 0.0
  140.     self.spineWeight = 0.0
  141.     self.aimWeight = 0.0
  142.    
  143.     self.gatlingActive = false
  144.     self.gatlingBlendSpeedIn = 1.5
  145.     self.gatlingBlendSpeedOut = 0.375
  146.     self.gatlingWeight = 0.0
  147.     self.gatlingTurnSpeed = ( 1 / self.normalFireMode.fireCooldown ) / 3
  148.     self.gatlingTurnFraction = 0.0
  149. end
  150.  
  151. function PotatoGatling.client_onUpdate( self, dt )
  152.  
  153.     -- First person animation  
  154.     local isSprinting =  self.tool:isSprinting()
  155.     local isCrouching =  self.tool:isCrouching()
  156.  
  157.     if self.tool:isLocal() then
  158.         if self.equipped then
  159.             if isSprinting and self.fpAnimations.currentAnimation ~= "sprintInto" and self.fpAnimations.currentAnimation ~= "sprintIdle" then
  160.                 swapFpAnimation( self.fpAnimations, "sprintExit", "sprintInto", 0.0 )
  161.             elseif not self.tool:isSprinting() and ( self.fpAnimations.currentAnimation == "sprintIdle" or self.fpAnimations.currentAnimation == "sprintInto" ) then
  162.                 swapFpAnimation( self.fpAnimations, "sprintInto", "sprintExit", 0.0 )
  163.             end
  164.  
  165.             if self.aiming and not isAnyOf( self.fpAnimations.currentAnimation, { "aimInto", "aimIdle", "aimShoot" } ) then
  166.                 swapFpAnimation( self.fpAnimations, "aimExit", "aimInto", 0.0 )
  167.             end
  168.             if not self.aiming and isAnyOf( self.fpAnimations.currentAnimation, { "aimInto", "aimIdle", "aimShoot" } ) then
  169.                 swapFpAnimation( self.fpAnimations, "aimInto", "aimExit", 0.0 )
  170.             end
  171.         end
  172.         updateFpAnimations( self.fpAnimations, self.equipped, dt )
  173.     end
  174.  
  175.     if not self.equipped then
  176.         if self.wantEquipped then
  177.             self.wantEquipped = false
  178.             self.equipped = true
  179.         end
  180.         return
  181.     end
  182.    
  183.     local effectPos, rot
  184.    
  185.     if self.tool:isLocal() then
  186.    
  187.         local zOffset = 0.6
  188.         if self.tool:isCrouching() then
  189.             zOffset = 0.29
  190.         end
  191.  
  192.         local dir = sm.localPlayer.getDirection()
  193.         local firePos = self.tool:getFpBonePos( "pejnt_barrel" )
  194.        
  195.         if not self.aiming then
  196.             effectPos = firePos + dir * 0.2
  197.         else
  198.             effectPos = firePos + dir * 0.45
  199.         end
  200.  
  201.         rot = sm.vec3.getRotation( sm.vec3.new( 0, 0, 1 ), dir )
  202.        
  203.        
  204.         self.shootEffectFP:setPosition( effectPos )
  205.         self.shootEffectFP:setVelocity( self.tool:getMovementVelocity() )
  206.         self.shootEffectFP:setRotation( rot )
  207.     end
  208.     local pos = self.tool:getTpBonePos( "pejnt_barrel" )
  209.     local dir = self.tool:getTpBoneDir( "pejnt_barrel" )
  210.    
  211.     effectPos = pos + dir * 0.2
  212.  
  213.     rot = sm.vec3.getRotation( sm.vec3.new( 0, 0, 1 ), dir )
  214.    
  215.    
  216.     self.shootEffect:setPosition( effectPos )
  217.     self.shootEffect:setVelocity( self.tool:getMovementVelocity() )
  218.     self.shootEffect:setRotation( rot )
  219.     self.windupEffect:setPosition( effectPos )
  220.  
  221.     -- Timers
  222.     self.fireCooldownTimer = math.max( self.fireCooldownTimer - dt, 0.0 )
  223.     self.spreadCooldownTimer = math.max( self.spreadCooldownTimer - dt, 0.0 )
  224.     self.sprintCooldownTimer = math.max( self.sprintCooldownTimer - dt, 0.0 )
  225.    
  226.  
  227.     if self.tool:isLocal() then
  228.         local dispersion = 0.0
  229.         local fireMode = self.aiming and self.aimFireMode or self.normalFireMode
  230.         local recoilDispersion = 1.0 - ( math.max( fireMode.minDispersionCrouching, fireMode.minDispersionStanding ) + fireMode.maxMovementDispersion )
  231.        
  232.         if isCrouching then
  233.             dispersion = fireMode.minDispersionCrouching
  234.         else
  235.             dispersion = fireMode.minDispersionStanding
  236.         end
  237.        
  238.         if self.tool:getRelativeMoveDirection():length() > 0 then
  239.             dispersion = dispersion + fireMode.maxMovementDispersion * self.tool:getMovementSpeedFraction()
  240.         end
  241.        
  242.         if not self.tool:isOnGround() then
  243.             dispersion = dispersion * fireMode.jumpDispersionMultiplier
  244.         end
  245.        
  246.         self.movementDispersion = dispersion
  247.        
  248.         self.spreadCooldownTimer = clamp( self.spreadCooldownTimer, 0.0, fireMode.spreadCooldown )
  249.         local spreadFactor = fireMode.spreadCooldown > 0.0 and clamp( self.spreadCooldownTimer / fireMode.spreadCooldown, 0.0, 1.0 ) or 0.0
  250.        
  251.         self.tool:setDispersionFraction( clamp( self.movementDispersion + spreadFactor * recoilDispersion, 0.0, 1.0 ) )
  252.        
  253.         if self.aiming then
  254.             if self.tool:isInFirstPersonView() then
  255.                 self.tool:setCrossHairAlpha( 0.0 )
  256.             else
  257.                 self.tool:setCrossHairAlpha( 1.0 )
  258.             end
  259.             self.tool:setInteractionTextSuppressed( true )
  260.         else
  261.             self.tool:setCrossHairAlpha( 1.0 )
  262.             self.tool:setInteractionTextSuppressed( false )
  263.         end
  264.     end
  265.  
  266.     -- Sprint block
  267.     local blockSprint = self.aiming or self.sprintCooldownTimer > 0.0
  268.     self.tool:setBlockSprint( blockSprint )
  269.  
  270.     local playerDir = self.tool:getDirection()
  271.     local angle = math.asin( playerDir:dot( sm.vec3.new( 0, 0, 1 ) ) ) / ( math.pi / 2 )
  272.     local linareAngle = playerDir:dot( sm.vec3.new( 0, 0, 1 ) )
  273.  
  274.     local linareAngleDown = clamp( -linareAngle, 0.0, 1.0 )
  275.  
  276.     down = clamp( -angle, 0.0, 1.0 )
  277.     fwd = ( 1.0 - math.abs( angle ) )
  278.     up = clamp( angle, 0.0, 1.0 )
  279.  
  280.     local crouchWeight = self.tool:isCrouching() and 1.0 or 0.0
  281.     local normalWeight = 1.0 - crouchWeight
  282.  
  283.     local totalWeight = 0.0
  284.     for name, animation in pairs( self.tpAnimations.animations ) do
  285.         animation.time = animation.time + dt
  286.  
  287.         if name == self.tpAnimations.currentAnimation then
  288.             animation.weight = math.min( animation.weight + ( self.tpAnimations.blendSpeed * dt ), 1.0 )
  289.            
  290.             if animation.time >= animation.info.duration - self.blendTime then
  291.                 if ( name == "shoot" or name == "aimShoot" ) then
  292.                     setTpAnimation( self.tpAnimations, self.aiming and "aim" or "idle", 10.0 )
  293.                 elseif name == "pickup" then
  294.                     setTpAnimation( self.tpAnimations, self.aiming and "aim" or "idle", 0.001 )
  295.                 elseif animation.nextAnimation ~= "" then
  296.                     setTpAnimation( self.tpAnimations, animation.nextAnimation, 0.001 )
  297.                 end
  298.             end
  299.         else
  300.             animation.weight = math.max( animation.weight - ( self.tpAnimations.blendSpeed * dt ), 0.0 )
  301.         end
  302.  
  303.         totalWeight = totalWeight + animation.weight
  304.     end
  305.  
  306.     totalWeight = totalWeight == 0 and 1.0 or totalWeight
  307.     for name, animation in pairs( self.tpAnimations.animations ) do
  308.         local weight = animation.weight / totalWeight
  309.         if name == "idle" then
  310.             self.tool:updateMovementAnimation( animation.time, weight )
  311.         elseif animation.crouch then
  312.             self.tool:updateAnimation( animation.info.name, animation.time, weight * normalWeight )
  313.             self.tool:updateAnimation( animation.crouch.name, animation.time, weight * crouchWeight )
  314.         else
  315.             self.tool:updateAnimation( animation.info.name, animation.time, weight )
  316.         end
  317.     end
  318.  
  319.     -- Third Person joint lock
  320.     local relativeMoveDirection = self.tool:getRelativeMoveDirection()
  321.     if ( ( ( isAnyOf( self.tpAnimations.currentAnimation, { "aimInto", "aim", "shoot" } ) and ( relativeMoveDirection:length() > 0 or isCrouching) ) or ( self.aiming and ( relativeMoveDirection:length() > 0 or isCrouching) ) ) and not isSprinting ) then
  322.         self.jointWeight = math.min( self.jointWeight + ( 10.0 * dt ), 1.0 )
  323.     else
  324.         self.jointWeight = math.max( self.jointWeight - ( 6.0 * dt ), 0.0 )
  325.     end
  326.  
  327.     if ( not isSprinting ) then
  328.         self.spineWeight = math.min( self.spineWeight + ( 10.0 * dt ), 1.0 )
  329.     else
  330.         self.spineWeight = math.max( self.spineWeight - ( 10.0 * dt ), 0.0 )
  331.     end
  332.  
  333.     local finalAngle = ( 0.5 + angle * 0.5 )
  334.     self.tool:updateAnimation( "spudgun_spine_bend", finalAngle, self.spineWeight )
  335.  
  336.     local totalOffsetZ = lerp( -22.0, -26.0, crouchWeight )
  337.     local totalOffsetY = lerp( 6.0, 12.0, crouchWeight )
  338.     local crouchTotalOffsetX = clamp( ( angle * 60.0 ) -15.0, -60.0, 40.0 )
  339.     local normalTotalOffsetX = clamp( ( angle * 50.0 ), -45.0, 50.0 )
  340.     local totalOffsetX = lerp( normalTotalOffsetX, crouchTotalOffsetX , crouchWeight )
  341.  
  342.     local finalJointWeight = ( self.jointWeight )
  343.      
  344.  
  345.     self.tool:updateJoint( "jnt_hips", sm.vec3.new( totalOffsetX, totalOffsetY, totalOffsetZ ), 0.35 * finalJointWeight * ( normalWeight ) )
  346.  
  347.     local crouchSpineWeight = ( 0.35 / 3 ) * crouchWeight
  348.  
  349.     self.tool:updateJoint( "jnt_spine1", sm.vec3.new( totalOffsetX, totalOffsetY, totalOffsetZ ), ( 0.10 + crouchSpineWeight )  * finalJointWeight )
  350.     self.tool:updateJoint( "jnt_spine2", sm.vec3.new( totalOffsetX, totalOffsetY, totalOffsetZ ), ( 0.10 + crouchSpineWeight ) * finalJointWeight )
  351.     self.tool:updateJoint( "jnt_spine3", sm.vec3.new( totalOffsetX, totalOffsetY, totalOffsetZ ), ( 0.45 + crouchSpineWeight ) * finalJointWeight )
  352.     self.tool:updateJoint( "jnt_head", sm.vec3.new( totalOffsetX, totalOffsetY, totalOffsetZ ), 0.3 * finalJointWeight )
  353.  
  354.     -- Camera update
  355.     local bobbing = 1
  356.     if self.aiming then
  357.         local blend = 1 - math.pow( 1 - 1 / self.aimBlendSpeed, dt * 60 )
  358.         self.aimWeight = sm.util.lerp( self.aimWeight, 1.0, blend )
  359.         bobbing = 0.12
  360.     else
  361.         local blend = 1 - math.pow( 1 - 1 / self.aimBlendSpeed, dt * 60 )
  362.         self.aimWeight = sm.util.lerp( self.aimWeight, 0.0, blend )
  363.         bobbing = 1
  364.     end
  365.  
  366.     self.tool:updateCamera( 2.8, 30.0, sm.vec3.new( 0.65, 0.0, 0.05 ), self.aimWeight )
  367.     self.tool:updateFpCamera( 30.0, sm.vec3.new( 0.0, 0.0, 0.0 ), self.aimWeight, bobbing )
  368.    
  369.     self:cl_updateGatling( dt )
  370. end
  371.  
  372. function PotatoGatling.client_onEquip( self )
  373.     sm.audio.play( "PotatoRifle - Equip" )
  374.     self.windupEffect:start()
  375.     self.wantEquipped = true
  376.     self.aiming = false
  377.     self.aimWeight = 0.0
  378.     self.jointWeight = 0.0
  379.    
  380.     currentRenderablesTp = {}
  381.     currentRenderablesFp = {}
  382.    
  383.     for k,v in pairs( renderablesTp ) do currentRenderablesTp[#currentRenderablesTp+1] = v end
  384.     for k,v in pairs( renderablesFp ) do currentRenderablesFp[#currentRenderablesFp+1] = v end
  385.     for k,v in pairs( renderables ) do currentRenderablesTp[#currentRenderablesTp+1] = v end
  386.     for k,v in pairs( renderables ) do currentRenderablesFp[#currentRenderablesFp+1] = v end
  387.  
  388.     self.tool:setTpRenderables( currentRenderablesTp )
  389.  
  390.     self:loadAnimations()
  391.  
  392.     setTpAnimation( self.tpAnimations, "pickup", 0.0001 )
  393.  
  394.     if self.tool:isLocal() then
  395.         -- Sets PotatoGatling renderable, change this to change the mesh
  396.         self.tool:setFpRenderables( currentRenderablesFp )
  397.         swapFpAnimation( self.fpAnimations, "unequip", "equip", 0.2 )
  398.     end
  399. end
  400.  
  401. function PotatoGatling.client_onUnequip( self )
  402.     sm.audio.play( "PotatoRifle - Unequip" )
  403.     self.windupEffect:stop()
  404.     self.wantEquipped = false
  405.     self.equipped = false
  406.     setTpAnimation( self.tpAnimations, "putdown" )
  407.     if self.tool:isLocal() and self.fpAnimations.currentAnimation ~= "unequip" then
  408.         swapFpAnimation( self.fpAnimations, "equip", "unequip", 0.2 )
  409.     end
  410. end
  411.  
  412. function PotatoGatling.sv_n_onAim( self, aiming )
  413.     self.network:sendToClients( "cl_n_onAim", aiming )
  414. end
  415.  
  416. function PotatoGatling.cl_n_onAim( self, aiming )
  417.     if not self.tool:isLocal() then
  418.         self:onAim( aiming )
  419.     end
  420. end
  421.  
  422. function PotatoGatling.onAim( self, aiming )
  423.     self.aiming = aiming
  424.     if self.tpAnimations.currentAnimation == "idle" or self.tpAnimations.currentAnimation == "aim" or self.tpAnimations.currentAnimation == "relax" and self.aiming then
  425.         setTpAnimation( self.tpAnimations, self.aiming and "aim" or "idle", 5.0 )
  426.     end
  427. end
  428.  
  429. function PotatoGatling.sv_n_onShoot( self, dir )
  430.     self.network:sendToClients( "cl_n_onShoot", dir )
  431. end
  432.  
  433. function PotatoGatling.cl_n_onShoot( self, dir )
  434.     if not self.tool:isLocal() then
  435.         self:onShoot( dir )
  436.     end
  437. end
  438.  
  439. function PotatoGatling.onShoot( self, dir )
  440.     self.tpAnimations.animations.idle.time = 0
  441.     self.tpAnimations.animations.shoot.time = 0
  442.     self.tpAnimations.animations.aimShoot.time = 0
  443.  
  444.     setTpAnimation( self.tpAnimations, self.aiming and "aimShoot" or "shoot", 10.0 )
  445.    
  446.     if self.tool:isInFirstPersonView() then
  447.         self.shootEffectFP:start()
  448.     else
  449.         self.shootEffect:start()
  450.     end
  451.  
  452. end
  453.  
  454. function PotatoGatling.calculateFirePosition( self )
  455.     local crouching = self.tool:isCrouching()
  456.     local firstPerson = self.tool:isInFirstPersonView()
  457.     local dir = sm.localPlayer.getDirection()
  458.     local pitch = math.asin( dir.z )       
  459.     local right = sm.localPlayer.getRight()
  460.    
  461.     local fireOffset = sm.vec3.new( 0.0, 0.0, 0.0 )
  462.  
  463.     if crouching then
  464.         fireOffset.z = 0.15
  465.     else
  466.         fireOffset.z = 0.45
  467.     end
  468.  
  469.     if firstPerson then
  470.         if not self.aiming then
  471.             fireOffset = fireOffset + right * 0.05
  472.         end
  473.     else
  474.         fireOffset = fireOffset + right * 0.25     
  475.         fireOffset = fireOffset:rotate( math.rad( pitch ), right )
  476.     end
  477.     local firePosition = sm.localPlayer.getPosition() + fireOffset
  478.     return firePosition
  479. end
  480.  
  481. function PotatoGatling.calculateTpMuzzlePos( self )
  482.     local crouching = self.tool:isCrouching()
  483.     local dir = sm.localPlayer.getDirection()
  484.     local pitch = math.asin( dir.z )       
  485.     local right = sm.localPlayer.getRight()
  486.     local up = right:cross(dir)
  487.    
  488.     local fakeOffset = sm.vec3.new( 0.0, 0.0, 0.0 )
  489.    
  490.     --General offset
  491.     fakeOffset = fakeOffset + right * 0.25
  492.     fakeOffset = fakeOffset + dir * 0.5
  493.     fakeOffset = fakeOffset + up * 0.25
  494.    
  495.     --Action offset
  496.     local pitchFraction = pitch / ( math.pi * 0.5 )
  497.     if crouching then
  498.         fakeOffset = fakeOffset + dir * 0.2
  499.         fakeOffset = fakeOffset + up * 0.1
  500.         fakeOffset = fakeOffset - right * 0.05
  501.        
  502.         if pitchFraction > 0.0 then
  503.             fakeOffset = fakeOffset - up * 0.2 * pitchFraction
  504.         else
  505.             fakeOffset = fakeOffset + up * 0.1 * math.abs( pitchFraction )
  506.         end    
  507.     else
  508.         fakeOffset = fakeOffset + up * 0.1 *  math.abs( pitchFraction )    
  509.     end
  510.    
  511.     local fakePosition = fakeOffset + sm.localPlayer.getPosition()
  512.     return fakePosition
  513. end
  514.  
  515. function PotatoGatling.calculateFpMuzzlePos( self )
  516.     local fovScale = ( sm.camera.getFov() - 45 ) / 45
  517.    
  518.     local up = sm.localPlayer.getUp()
  519.     local dir = sm.localPlayer.getDirection()
  520.     local right = sm.localPlayer.getRight()
  521.    
  522.     local muzzlePos45 = sm.vec3.new( 0.0, 0.0, 0.0 )
  523.     local muzzlePos90 = sm.vec3.new( 0.0, 0.0, 0.0 )
  524.        
  525.     if self.aiming then
  526.         muzzlePos45 = muzzlePos45 - up * 0.2
  527.         muzzlePos45 = muzzlePos45 + dir * 0.5
  528.        
  529.         muzzlePos90 = muzzlePos90 - up * 0.5
  530.         muzzlePos90 = muzzlePos90 - dir * 0.6
  531.     else
  532.         muzzlePos45 = muzzlePos45 - up * 0.15
  533.         muzzlePos45 = muzzlePos45 + right * 0.2
  534.         muzzlePos45 = muzzlePos45 + dir * 1.25
  535.        
  536.         muzzlePos90 = muzzlePos90 - up * 0.15
  537.         muzzlePos90 = muzzlePos90 + right * 0.2
  538.         muzzlePos90 = muzzlePos90 + dir * 0.25
  539.     end
  540.  
  541.     return self.tool:getFpBonePos( "pejnt_barrel" ) + sm.vec3.lerp( muzzlePos45, muzzlePos90, fovScale )
  542. end
  543.  
  544.  
  545. function PotatoGatling.cl_updateGatling( self, dt )
  546.    
  547.     self.gatlingWeight = self.gatlingActive and ( self.gatlingWeight + self.gatlingBlendSpeedIn * dt ) or ( self.gatlingWeight - self.gatlingBlendSpeedOut * dt )
  548.     self.gatlingWeight = math.min( math.max( self.gatlingWeight, 0.0 ), 1.0 )
  549.     local frac
  550.     frac, self.gatlingTurnFraction = math.modf( self.gatlingTurnFraction + self.gatlingTurnSpeed * self.gatlingWeight * dt )
  551.    
  552.     self.windupEffect:setParameter( "velocity", self.gatlingWeight )
  553.     if self.equipped and not self.windupEffect:isPlaying() then
  554.         self.windupEffect:start()
  555.     elseif not self.equipped and self.windupEffect:isPlaying() then
  556.         self.windupEffect:stop()
  557.     end
  558.  
  559.     -- Update gatling animation
  560.     if self.tool:isLocal() then
  561.         self.tool:updateFpAnimation( "spudgun_spinner_shoot_fp", self.gatlingTurnFraction, 1.0, true )
  562.     end
  563.     self.tool:updateAnimation( "spudgun_spinner_shoot_tp", self.gatlingTurnFraction, 1.0 )
  564.    
  565.     if self.fireCooldownTimer <= 0.0 and self.gatlingWeight >= 1.0 and self.gatlingActive then
  566.        
  567.         if sm.container.canSpend( sm.localPlayer.getInventory(), obj_plantables_potato, 1 ) then
  568.        
  569.             local firstPerson = self.tool:isInFirstPersonView()
  570.            
  571.             local dir = sm.localPlayer.getDirection()
  572.            
  573.             local firePos = self:calculateFirePosition()
  574.             local fakePosition = self:calculateTpMuzzlePos()
  575.             local fakePositionSelf = fakePosition
  576.             if firstPerson then
  577.                 fakePositionSelf = self:calculateFpMuzzlePos()
  578.             end
  579.  
  580.             -- Aim assist
  581.             if not firstPerson then
  582.                 local raycastPos = sm.camera.getPosition() + sm.camera.getDirection() * sm.camera.getDirection():dot(sm.localPlayer.getPosition() - sm.camera.getPosition())
  583.                 local hit, result = sm.localPlayer.getRaycast( 250, raycastPos, sm.camera.getDirection() )
  584.                 if hit then
  585.                     local norDir = sm.vec3.normalize( result.pointWorld - firePos )
  586.                     local dirDot = norDir:dot( dir )
  587.                    
  588.                     if dirDot > 0.96592583 then -- max 15 degrees off
  589.                         dir = norDir
  590.                     else
  591.                         local radsOff = math.asin( dirDot )
  592.                         dir = sm.vec3.lerp( dir, norDir, math.tan( radsOff ) / 3.7320508 ) -- if more than 15, make it 15
  593.                     end
  594.                 end
  595.             end
  596.  
  597.             dir = dir:rotate( math.rad( 0.955 ), sm.camera.getRight() ) -- 50 m sight calibration
  598.            
  599.             -- Spread
  600.             local fireMode = self.aiming and self.aimFireMode or self.normalFireMode
  601.             local recoilDispersion = 1.0 - ( math.max(fireMode.minDispersionCrouching, fireMode.minDispersionStanding ) + fireMode.maxMovementDispersion )
  602.            
  603.             local spreadFactor = fireMode.spreadCooldown > 0.0 and clamp( self.spreadCooldownTimer / fireMode.spreadCooldown, 0.0, 1.0 ) or 0.0
  604.             spreadFactor = clamp( self.movementDispersion + spreadFactor * recoilDispersion, 0.0, 1.0 )
  605.             local spreadDeg =  fireMode.spreadMinAngle + ( fireMode.spreadMaxAngle - fireMode.spreadMinAngle ) * spreadFactor
  606.            
  607.             dir = sm.noise.gunSpread( dir, spreadDeg )
  608.  
  609.             local owner = self.tool:getOwner()
  610.             if owner then
  611.                 sm.projectile.projectileAttack( "smallpotato", Damage, firePos, dir * fireMode.fireVelocity, owner, fakePosition, fakePositionSelf )
  612.             end
  613.            
  614.             -- Timers
  615.             self.fireCooldownTimer = fireMode.fireCooldown
  616.             self.spreadCooldownTimer = math.min( self.spreadCooldownTimer + fireMode.spreadIncrement, fireMode.spreadCooldown )
  617.             self.sprintCooldownTimer = self.sprintCooldown
  618.  
  619.             -- Send TP shoot over network and dircly to self
  620.             self:onShoot( dir )
  621.             self.network:sendToServer( "sv_n_onShoot", dir )
  622.  
  623.             -- Play FP shoot animation
  624.             setFpAnimation( self.fpAnimations, self.aiming and "aimShoot" or "shoot", 0.05 )
  625.         else
  626.             local fireMode = self.aiming and self.aimFireMode or self.normalFireMode
  627.             self.fireCooldownTimer = fireMode.fireCooldown
  628.             sm.audio.play( "PotatoRifle - NoAmmo" )
  629.         end
  630.     end
  631. end
  632.  
  633. function PotatoGatling.cl_onSecondaryUse( self, state )
  634.     if state == sm.tool.interactState.start and not self.aiming then
  635.         self.aiming = true
  636.         self.tpAnimations.animations.idle.time = 0
  637.        
  638.         self:onAim( self.aiming )
  639.         self.tool:setMovementSlowDown( self.aiming )
  640.         self.network:sendToServer( "sv_n_onAim", self.aiming )
  641.     end
  642.  
  643.     if self.aiming and (state == sm.tool.interactState.stop or state == sm.tool.interactState.null) then
  644.         self.aiming = false
  645.         self.tpAnimations.animations.idle.time = 0
  646.        
  647.         self:onAim( self.aiming )
  648.         self.tool:setMovementSlowDown( self.aiming )
  649.         self.network:sendToServer( "sv_n_onAim", self.aiming )
  650.     end
  651. end
  652.  
  653. function PotatoGatling.client_onEquippedUpdate( self, primaryState, secondaryState )
  654.     if primaryState == sm.tool.interactState.start or primaryState == sm.tool.interactState.hold then
  655.         self.gatlingActive = true
  656.     else
  657.         self.gatlingActive = false
  658.     end
  659.    
  660.     if secondaryState ~= self.prevSecondaryState then
  661.         self:cl_onSecondaryUse( secondaryState )
  662.         self.prevSecondaryState = secondaryState
  663.     end
  664.  
  665.     return true, true
  666. end
Add Comment
Please, Sign In to add comment