Last active
February 15, 2018 01:13
-
-
Save meepen/bac76877c0fae52c9508cbf396ae92cf to your computer and use it in GitHub Desktop.
Predicted TTT sprinting code (double tap W)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local WAIT_TIME = 0.2 -- seconds between w presses | |
local SPEED_MULT = 2 | |
local STAMINA_REGEN_PER_SECOND = 0.1 | |
local STAMINA_LOSS_PER_SECOND = 0.2 | |
local sprinttime = function(ply, val) | |
if (val ~= nil) then | |
ply:SetNW2Float("SprintTime", val) | |
end | |
return ply:GetNW2Float("SprintTime", 0) | |
end | |
local sprintmode = function(ply, val) | |
if (val ~= nil) then | |
ply:SetNW2Bool("SprintMode", val) | |
end | |
return ply:GetNW2Bool("SprintMode", false) | |
end | |
local sprintstamina = function(ply, val) | |
if (val ~= nil) then | |
ply:SetNW2Float("SprintStaminaTime", CurTime()) | |
ply:SetNW2Float("SprintStaminaPercent", val) | |
end | |
local time = ply:GetNW2Float("SprintStaminaTime", -math.huge) | |
local percent = ply:GetNW2Float("SprintStaminaPercent", 1) | |
return math.Clamp(percent + (CurTime() - time) * (sprintmode(ply) and -STAMINA_LOSS_PER_SECOND or STAMINA_REGEN_PER_SECOND), 0, 1) | |
end | |
hook.Add("TTTPlayerSpeedModifier", "Sprinter", function(ply, slowed, mv) | |
-- no need to override | |
if (sprintstamina(ply) <= 0 or slowed or not mv:KeyDown(IN_FORWARD)) then | |
-- reset if was running | |
if (sprintmode(ply)) then | |
sprintstamina(ply, sprintstamina(ply)) -- update time | |
sprinttime(ply, 0) | |
sprintmode(ply, false) | |
end | |
return | |
end | |
if (mv:KeyPressed(IN_FORWARD)) then | |
if (sprinttime(ply) > CurTime() - WAIT_TIME) then | |
sprintstamina(ply, sprintstamina(ply)) -- update time | |
sprinttime(ply, 0) | |
sprintmode(ply, true) | |
else | |
sprinttime(ply, CurTime()) | |
end | |
end | |
if (sprintmode(ply)) then | |
return SPEED_MULT | |
end | |
end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Wow, copy minecraft mucH!? I'm telling Notch, ur gettging DMCAd