Guest User

PotatoRifle.lua

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