Guest User

PotatoShotgun.lua

a guest
Jun 28th, 2020
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 21.46 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. PotatoShotgun = 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_frier/char_spudgun_barrel_frier.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 PotatoShotgun.client_onCreate( self )
  25.     self.shootEffect = sm.effect.createEffect( "SpudgunFrier - FrierMuzzel" )
  26.     self.shootEffectFP = sm.effect.createEffect( "SpudgunFrier - FPFrierMuzzel" )
  27. end
  28.  
  29.  
  30.  
  31. function PotatoShotgun.client_onRefresh( self )
  32.     self:loadAnimations()
  33. end
  34.  
  35. function PotatoShotgun.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 PotatoShotgun.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.     local dir = sm.localPlayer.getDirection()
  204.  
  205.     effectPos = pos + dir * 0.2
  206.  
  207.     rot = sm.vec3.getRotation( sm.vec3.new( 0, 0, 1 ), dir )
  208.  
  209.  
  210.     self.shootEffect:setPosition( effectPos )
  211.     self.shootEffect:setVelocity( self.tool:getMovementVelocity() )
  212.     self.shootEffect:setRotation( rot )
  213.  
  214.     -- Timers
  215.     self.fireCooldownTimer = math.max( self.fireCooldownTimer - dt, 0.0 )
  216.     self.spreadCooldownTimer = math.max( self.spreadCooldownTimer - dt, 0.0 )
  217.     self.sprintCooldownTimer = math.max( self.sprintCooldownTimer - dt, 0.0 )
  218.  
  219.  
  220.     if self.tool:isLocal() then
  221.         local dispersion = 0.0
  222.         local fireMode = self.aiming and self.aimFireMode or self.normalFireMode
  223.         local recoilDispersion = 1.0 - ( math.max( fireMode.minDispersionCrouching, fireMode.minDispersionStanding ) + fireMode.maxMovementDispersion )
  224.  
  225.         if isCrouching then
  226.             dispersion = fireMode.minDispersionCrouching
  227.         else
  228.             dispersion = fireMode.minDispersionStanding
  229.         end
  230.  
  231.         if self.tool:getRelativeMoveDirection():length() > 0 then
  232.             dispersion = dispersion + fireMode.maxMovementDispersion * self.tool:getMovementSpeedFraction()
  233.         end
  234.  
  235.         if not self.tool:isOnGround() then
  236.             dispersion = dispersion * fireMode.jumpDispersionMultiplier
  237.         end
  238.  
  239.         self.movementDispersion = dispersion
  240.  
  241.         self.spreadCooldownTimer = clamp( self.spreadCooldownTimer, 0.0, fireMode.spreadCooldown )
  242.         local spreadFactor = fireMode.spreadCooldown > 0.0 and clamp( self.spreadCooldownTimer / fireMode.spreadCooldown, 0.0, 1.0 ) or 0.0
  243.  
  244.         self.tool:setDispersionFraction( clamp( self.movementDispersion + spreadFactor * recoilDispersion, 0.0, 1.0 ) )
  245.  
  246.         if self.aiming then
  247.             if self.tool:isInFirstPersonView() then
  248.                 self.tool:setCrossHairAlpha( 0.0 )
  249.             else
  250.                 self.tool:setCrossHairAlpha( 1.0 )
  251.             end
  252.             self.tool:setInteractionTextSuppressed( true )
  253.         else
  254.             self.tool:setCrossHairAlpha( 1.0 )
  255.             self.tool:setInteractionTextSuppressed( false )
  256.         end
  257.     end
  258.  
  259.     -- Sprint block
  260.     local blockSprint = self.aiming or self.sprintCooldownTimer > 0.0
  261.     self.tool:setBlockSprint( blockSprint )
  262.  
  263.     local playerDir = self.tool:getDirection()
  264.     local angle = math.asin( playerDir:dot( sm.vec3.new( 0, 0, 1 ) ) ) / ( math.pi / 2 )
  265.     local linareAngle = playerDir:dot( sm.vec3.new( 0, 0, 1 ) )
  266.  
  267.     local linareAngleDown = clamp( -linareAngle, 0.0, 1.0 )
  268.  
  269.     down = clamp( -angle, 0.0, 1.0 )
  270.     fwd = ( 1.0 - math.abs( angle ) )
  271.     up = clamp( angle, 0.0, 1.0 )
  272.  
  273.     local crouchWeight = self.tool:isCrouching() and 1.0 or 0.0
  274.     local normalWeight = 1.0 - crouchWeight
  275.  
  276.     local totalWeight = 0.0
  277.     for name, animation in pairs( self.tpAnimations.animations ) do
  278.         animation.time = animation.time + dt
  279.  
  280.         if name == self.tpAnimations.currentAnimation then
  281.             animation.weight = math.min( animation.weight + ( self.tpAnimations.blendSpeed * dt ), 1.0 )
  282.  
  283.             if animation.time >= animation.info.duration - self.blendTime then
  284.                 if ( name == "shoot" or name == "aimShoot" ) then
  285.                     setTpAnimation( self.tpAnimations, self.aiming and "aim" or "idle", 10.0 )
  286.                 elseif name == "pickup" then
  287.                     setTpAnimation( self.tpAnimations, self.aiming and "aim" or "idle", 0.001 )
  288.                 elseif animation.nextAnimation ~= "" then
  289.                     setTpAnimation( self.tpAnimations, animation.nextAnimation, 0.001 )
  290.                 end
  291.             end
  292.         else
  293.             animation.weight = math.max( animation.weight - ( self.tpAnimations.blendSpeed * dt ), 0.0 )
  294.         end
  295.  
  296.         totalWeight = totalWeight + animation.weight
  297.     end
  298.  
  299.     totalWeight = totalWeight == 0 and 1.0 or totalWeight
  300.     for name, animation in pairs( self.tpAnimations.animations ) do
  301.         local weight = animation.weight / totalWeight
  302.         if name == "idle" then
  303.             self.tool:updateMovementAnimation( animation.time, weight )
  304.         elseif animation.crouch then
  305.             self.tool:updateAnimation( animation.info.name, animation.time, weight * normalWeight )
  306.             self.tool:updateAnimation( animation.crouch.name, animation.time, weight * crouchWeight )
  307.         else
  308.             self.tool:updateAnimation( animation.info.name, animation.time, weight )
  309.         end
  310.     end
  311.  
  312.     -- Third Person joint lock
  313.     local relativeMoveDirection = self.tool:getRelativeMoveDirection()
  314.     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
  315.         self.jointWeight = math.min( self.jointWeight + ( 10.0 * dt ), 1.0 )
  316.     else
  317.         self.jointWeight = math.max( self.jointWeight - ( 6.0 * dt ), 0.0 )
  318.     end
  319.  
  320.     if ( not isSprinting ) then
  321.         self.spineWeight = math.min( self.spineWeight + ( 10.0 * dt ), 1.0 )
  322.     else
  323.         self.spineWeight = math.max( self.spineWeight - ( 10.0 * dt ), 0.0 )
  324.     end
  325.  
  326.     local finalAngle = ( 0.5 + angle * 0.5 )
  327.     self.tool:updateAnimation( "spudgun_spine_bend", finalAngle, self.spineWeight )
  328.  
  329.     local totalOffsetZ = lerp( -22.0, -26.0, crouchWeight )
  330.     local totalOffsetY = lerp( 6.0, 12.0, crouchWeight )
  331.     local crouchTotalOffsetX = clamp( ( angle * 60.0 ) -15.0, -60.0, 40.0 )
  332.     local normalTotalOffsetX = clamp( ( angle * 50.0 ), -45.0, 50.0 )
  333.     local totalOffsetX = lerp( normalTotalOffsetX, crouchTotalOffsetX , crouchWeight )
  334.  
  335.     local finalJointWeight = ( self.jointWeight )
  336.  
  337.  
  338.     self.tool:updateJoint( "jnt_hips", sm.vec3.new( totalOffsetX, totalOffsetY, totalOffsetZ ), 0.35 * finalJointWeight * ( normalWeight ) )
  339.  
  340.     local crouchSpineWeight = ( 0.35 / 3 ) * crouchWeight
  341.  
  342.     self.tool:updateJoint( "jnt_spine1", sm.vec3.new( totalOffsetX, totalOffsetY, totalOffsetZ ), ( 0.10 + crouchSpineWeight )  * finalJointWeight )
  343.     self.tool:updateJoint( "jnt_spine2", sm.vec3.new( totalOffsetX, totalOffsetY, totalOffsetZ ), ( 0.10 + crouchSpineWeight ) * finalJointWeight )
  344.     self.tool:updateJoint( "jnt_spine3", sm.vec3.new( totalOffsetX, totalOffsetY, totalOffsetZ ), ( 0.45 + crouchSpineWeight ) * finalJointWeight )
  345.     self.tool:updateJoint( "jnt_head", sm.vec3.new( totalOffsetX, totalOffsetY, totalOffsetZ ), 0.3 * finalJointWeight )
  346.  
  347.  
  348.     -- Camera update
  349.     local bobbing = 1
  350.     if self.aiming then
  351.         local blend = 1 - math.pow( 1 - 1 / self.aimBlendSpeed, dt * 60 )
  352.         self.aimWeight = sm.util.lerp( self.aimWeight, 1.0, blend )
  353.         bobbing = 0.12
  354.     else
  355.         local blend = 1 - math.pow( 1 - 1 / self.aimBlendSpeed, dt * 60 )
  356.         self.aimWeight = sm.util.lerp( self.aimWeight, 0.0, blend )
  357.         bobbing = 1
  358.     end
  359.  
  360.     self.tool:updateCamera( 2.8, 30.0, sm.vec3.new( 0.65, 0.0, 0.05 ), self.aimWeight )
  361.     self.tool:updateFpCamera( 30.0, sm.vec3.new( 0.0, 0.0, 0.0 ), self.aimWeight, bobbing )
  362. end
  363.  
  364. function PotatoShotgun.client_onEquip( self )
  365.     sm.audio.play( "PotatoRifle - Equip" )
  366.     self.wantEquipped = true
  367.     self.aiming = false
  368.     self.aimWeight = 0.0
  369.     self.jointWeight = 0.0
  370.  
  371.     currentRenderablesTp = {}
  372.     currentRenderablesFp = {}
  373.  
  374.     for k,v in pairs( renderablesTp ) do currentRenderablesTp[#currentRenderablesTp+1] = v end
  375.     for k,v in pairs( renderablesFp ) do currentRenderablesFp[#currentRenderablesFp+1] = v end
  376.     for k,v in pairs( renderables ) do currentRenderablesTp[#currentRenderablesTp+1] = v end
  377.     for k,v in pairs( renderables ) do currentRenderablesFp[#currentRenderablesFp+1] = v end
  378.  
  379.     self.tool:setTpRenderables( currentRenderablesTp )
  380.  
  381.     self:loadAnimations()
  382.  
  383.     setTpAnimation( self.tpAnimations, "pickup", 0.0001 )
  384.  
  385.     if self.tool:isLocal() then
  386.         -- Sets PotatoShotgun renderable, change this to change the mesh
  387.         self.tool:setFpRenderables( currentRenderablesFp )
  388.         swapFpAnimation( self.fpAnimations, "unequip", "equip", 0.2 )
  389.     end
  390. end
  391.  
  392. function PotatoShotgun.client_onUnequip( self )
  393.     sm.audio.play( "PotatoRifle - Unequip" )
  394.     self.wantEquipped = false
  395.     self.equipped = false
  396.     setTpAnimation( self.tpAnimations, "putdown" )
  397.     if self.tool:isLocal() and self.fpAnimations.currentAnimation ~= "unequip" then
  398.         swapFpAnimation( self.fpAnimations, "equip", "unequip", 0.2 )
  399.     end
  400. end
  401.  
  402. function PotatoShotgun.sv_n_onAim( self, aiming )
  403.     self.network:sendToClients( "cl_n_onAim", aiming )
  404. end
  405.  
  406. function PotatoShotgun.cl_n_onAim( self, aiming )
  407.     if not self.tool:isLocal() then
  408.         self:onAim( aiming )
  409.     end
  410. end
  411.  
  412. function PotatoShotgun.onAim( self, aiming )
  413.     self.aiming = aiming
  414.     if self.tpAnimations.currentAnimation == "idle" or self.tpAnimations.currentAnimation == "aim" or self.tpAnimations.currentAnimation == "relax" and self.aiming then
  415.         setTpAnimation( self.tpAnimations, self.aiming and "aim" or "idle", 5.0 )
  416.     end
  417. end
  418.  
  419. function PotatoShotgun.sv_n_onShoot( self, dir )
  420.     self.network:sendToClients( "cl_n_onShoot", dir )
  421. end
  422.  
  423. function PotatoShotgun.cl_n_onShoot( self, dir )
  424.     if not self.tool:isLocal() then
  425.         self:onShoot( dir )
  426.     end
  427. end
  428.  
  429. function PotatoShotgun.onShoot( self, dir )
  430.     self.tpAnimations.animations.idle.time = 0
  431.     self.tpAnimations.animations.shoot.time = 0
  432.     self.tpAnimations.animations.aimShoot.time = 0
  433.  
  434.     setTpAnimation( self.tpAnimations, self.aiming and "aimShoot" or "shoot", 10.0 )
  435.  
  436.     if self.tool:isInFirstPersonView() then
  437.         self.shootEffectFP:start()
  438.     else
  439.         self.shootEffect:start()
  440.     end
  441.  
  442. end
  443.  
  444. function PotatoShotgun.calculateFirePosition( self )
  445.     local crouching = self.tool:isCrouching()
  446.     local firstPerson = self.tool:isInFirstPersonView()
  447.     local dir = sm.localPlayer.getDirection()
  448.     local pitch = math.asin( dir.z )
  449.     local right = sm.localPlayer.getRight()
  450.  
  451.     local fireOffset = sm.vec3.new( 0.0, 0.0, 0.0 )
  452.  
  453.     if crouching then
  454.         fireOffset.z = 0.15
  455.     else
  456.         fireOffset.z = 0.45
  457.     end
  458.  
  459.     if firstPerson then
  460.         if not self.aiming then
  461.             fireOffset = fireOffset + right * 0.05
  462.         end
  463.     else
  464.         fireOffset = fireOffset + right * 0.25
  465.         fireOffset = fireOffset:rotate( math.rad( pitch ), right )
  466.     end
  467.     local firePosition = sm.localPlayer.getPosition() + fireOffset
  468.     return firePosition
  469. end
  470.  
  471. function PotatoShotgun.calculateTpMuzzlePos( self )
  472.     local crouching = self.tool:isCrouching()
  473.     local dir = sm.localPlayer.getDirection()
  474.     local pitch = math.asin( dir.z )
  475.     local right = sm.localPlayer.getRight()
  476.     local up = right:cross(dir)
  477.  
  478.     local fakeOffset = sm.vec3.new( 0.0, 0.0, 0.0 )
  479.  
  480.     --General offset
  481.     fakeOffset = fakeOffset + right * 0.25
  482.     fakeOffset = fakeOffset + dir * 0.5
  483.     fakeOffset = fakeOffset + up * 0.25
  484.  
  485.     --Action offset
  486.     local pitchFraction = pitch / ( math.pi * 0.5 )
  487.     if crouching then
  488.         fakeOffset = fakeOffset + dir * 0.2
  489.         fakeOffset = fakeOffset + up * 0.1
  490.         fakeOffset = fakeOffset - right * 0.05
  491.  
  492.         if pitchFraction > 0.0 then
  493.             fakeOffset = fakeOffset - up * 0.2 * pitchFraction
  494.         else
  495.             fakeOffset = fakeOffset + up * 0.1 * math.abs( pitchFraction )
  496.         end
  497.     else
  498.         fakeOffset = fakeOffset + up * 0.1 *  math.abs( pitchFraction )
  499.     end
  500.  
  501.     local fakePosition = fakeOffset + sm.localPlayer.getPosition()
  502.     return fakePosition
  503. end
  504.  
  505. function PotatoShotgun.calculateFpMuzzlePos( self )
  506.     local fovScale = ( sm.camera.getFov() - 45 ) / 45
  507.  
  508.     local up = sm.localPlayer.getUp()
  509.     local dir = sm.localPlayer.getDirection()
  510.     local right = sm.localPlayer.getRight()
  511.  
  512.     local muzzlePos45 = sm.vec3.new( 0.0, 0.0, 0.0 )
  513.     local muzzlePos90 = sm.vec3.new( 0.0, 0.0, 0.0 )
  514.  
  515.     if self.aiming then
  516.         muzzlePos45 = muzzlePos45 - up * 0.2
  517.         muzzlePos45 = muzzlePos45 + dir * 0.5
  518.  
  519.         muzzlePos90 = muzzlePos90 - up * 0.5
  520.         muzzlePos90 = muzzlePos90 - dir * 0.6
  521.     else
  522.         muzzlePos45 = muzzlePos45 - up * 0.15
  523.         muzzlePos45 = muzzlePos45 + right * 0.2
  524.         muzzlePos45 = muzzlePos45 + dir * 1.25
  525.  
  526.         muzzlePos90 = muzzlePos90 - up * 0.15
  527.         muzzlePos90 = muzzlePos90 + right * 0.2
  528.         muzzlePos90 = muzzlePos90 + dir * 0.25
  529.     end
  530.  
  531.     return self.tool:getFpBonePos( "pejnt_barrel" ) + sm.vec3.lerp( muzzlePos45, muzzlePos90, fovScale )
  532. end
  533.  
  534. function PotatoShotgun.cl_onPrimaryUse( self, state )
  535.  
  536.     if self.fireCooldownTimer <= 0.0 and state == sm.tool.interactState.start then
  537.  
  538.         if sm.container.canSpend( sm.localPlayer.getInventory(), obj_plantables_potato, 2 ) then
  539.  
  540.             local firstPerson = self.tool:isInFirstPersonView()
  541.  
  542.             local dir = sm.localPlayer.getDirection()
  543.  
  544.             local firePos = self:calculateFirePosition()
  545.             local fakePosition = self:calculateTpMuzzlePos()
  546.             local fakePositionSelf = fakePosition
  547.             if firstPerson then
  548.                 fakePositionSelf = self:calculateFpMuzzlePos()
  549.             end
  550.  
  551.             -- Aim assist
  552.             if not firstPerson then
  553.                 local raycastPos = sm.camera.getPosition() + sm.camera.getDirection() * sm.camera.getDirection():dot(sm.localPlayer.getPosition() - sm.camera.getPosition())
  554.                 local hit, result = sm.localPlayer.getRaycast( 250, raycastPos, sm.camera.getDirection() )
  555.                 if hit then
  556.                     local norDir = sm.vec3.normalize( result.pointWorld - firePos )
  557.                     local dirDot = norDir:dot( dir )
  558.  
  559.                     if dirDot > 0.96592583 then -- max 15 degrees off
  560.                         dir = norDir
  561.                     else
  562.                         local radsOff = math.asin( dirDot )
  563.                         dir = sm.vec3.lerp( dir, norDir, math.tan( radsOff ) / 3.7320508 ) -- if more than 15, make it 15
  564.                     end
  565.                 end
  566.             end
  567.  
  568.             dir = dir:rotate( math.rad( 0.955 ), sm.camera.getRight() ) -- 50 m sight calibration
  569.  
  570.             -- Spread
  571.             local fireMode = self.aiming and self.aimFireMode or self.normalFireMode
  572.             local recoilDispersion = 1.0 - ( math.max(fireMode.minDispersionCrouching, fireMode.minDispersionStanding ) + fireMode.maxMovementDispersion )
  573.  
  574.             local spreadFactor = fireMode.spreadCooldown > 0.0 and clamp( self.spreadCooldownTimer / fireMode.spreadCooldown, 0.0, 1.0 ) or 0.0
  575.             spreadFactor = clamp( self.movementDispersion + spreadFactor * recoilDispersion, 0.0, 1.0 )
  576.             local spreadDeg =  fireMode.spreadMinAngle + ( fireMode.spreadMaxAngle - fireMode.spreadMinAngle ) * spreadFactor
  577.  
  578.             dir = sm.noise.gunSpread( dir, spreadDeg )
  579.  
  580.             local owner = self.tool:getOwner()
  581.             if owner then
  582.                 sm.projectile.projectileAttack( "fries", Damage, firePos, dir * fireMode.fireVelocity, owner, fakePosition, fakePositionSelf )
  583.             end
  584.  
  585.             -- Timers
  586.             self.fireCooldownTimer = fireMode.fireCooldown
  587.             self.spreadCooldownTimer = math.min( self.spreadCooldownTimer + fireMode.spreadIncrement, fireMode.spreadCooldown )
  588.             self.sprintCooldownTimer = self.sprintCooldown
  589.  
  590.             -- Send TP shoot over network and dircly to self
  591.             self:onShoot( dir )
  592.             self.network:sendToServer( "sv_n_onShoot", dir )
  593.  
  594.             -- Play FP shoot animation
  595.             setFpAnimation( self.fpAnimations, self.aiming and "aimShoot" or "shoot", 0.05 )
  596.         else
  597.             local fireMode = self.aiming and self.aimFireMode or self.normalFireMode
  598.             self.fireCooldownTimer = fireMode.fireCooldown
  599.             sm.audio.play( "PotatoRifle - NoAmmo" )
  600.         end
  601.     end
  602. end
  603.  
  604. function PotatoShotgun.cl_onSecondaryUse( self, state )
  605.     if state == sm.tool.interactState.start and not self.aiming then
  606.         self.aiming = true
  607.         self.tpAnimations.animations.idle.time = 0
  608.  
  609.         self:onAim( self.aiming )
  610.         self.tool:setMovementSlowDown( self.aiming )
  611.         self.network:sendToServer( "sv_n_onAim", self.aiming )
  612.     end
  613.  
  614.     if self.aiming and (state == sm.tool.interactState.stop or state == sm.tool.interactState.null) then
  615.         self.aiming = false
  616.         self.tpAnimations.animations.idle.time = 0
  617.  
  618.         self:onAim( self.aiming )
  619.         self.tool:setMovementSlowDown( self.aiming )
  620.         self.network:sendToServer( "sv_n_onAim", self.aiming )
  621.     end
  622. end
  623.  
  624. function PotatoShotgun.client_onEquippedUpdate( self, primaryState, secondaryState )
  625.     if primaryState ~= self.prevPrimaryState then
  626.         self:cl_onPrimaryUse( primaryState )
  627.         self.prevPrimaryState = primaryState
  628.     end
  629.  
  630.     if secondaryState ~= self.prevSecondaryState then
  631.         self:cl_onSecondaryUse( secondaryState )
  632.         self.prevSecondaryState = secondaryState
  633.     end
  634.  
  635.     return true, true
  636. end
Add Comment
Please, Sign In to add comment