generated from CFC-Servers/cfc_repo_template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Apply the chute forces in a predicted hook #3
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
local cvFallZVel | ||
local cvFallLerp | ||
local cvHorizontalSpeed | ||
local cvHorizontalSpeedLimit | ||
local cvSprintBoost | ||
local cvHandling | ||
|
||
local function setupConVars() | ||
local FALL_SPEED = GetConVar( "cfc_parachute_fall_speed" ) | ||
local FALL_LERP = GetConVar( "cfc_parachute_fall_lerp" ) | ||
local HORIZONTAL_SPEED = GetConVar( "cfc_parachute_horizontal_speed" ) | ||
local HORIZONTAL_SPEED_LIMIT = GetConVar( "cfc_parachute_horizontal_speed_limit" ) | ||
local SPRINT_BOOST = GetConVar( "cfc_parachute_sprint_boost" ) | ||
local HANDLING = GetConVar( "cfc_parachute_handling" ) | ||
|
||
cvFallZVel = -FALL_SPEED:GetFloat() | ||
cvars.AddChangeCallback( "cfc_parachute_fall_speed", function( _, _, new ) | ||
cvFallZVel = -assert( tonumber( new ) ) | ||
end ) | ||
|
||
cvFallLerp = FALL_LERP:GetFloat() | ||
cvars.AddChangeCallback( "cfc_parachute_fall_lerp", function( _, _, new ) | ||
cvFallLerp = assert( tonumber( new ) ) | ||
end ) | ||
|
||
cvHorizontalSpeed = HORIZONTAL_SPEED:GetFloat() | ||
cvars.AddChangeCallback( "cfc_parachute_horizontal_speed", function( _, _, new ) | ||
cvHorizontalSpeed = assert( tonumber( new ) ) | ||
end ) | ||
|
||
cvHorizontalSpeedLimit = HORIZONTAL_SPEED_LIMIT:GetFloat() | ||
cvars.AddChangeCallback( "cfc_parachute_horizontal_speed_limit", function( _, _, new ) | ||
cvHorizontalSpeedLimit = assert( tonumber( new ) ) | ||
end ) | ||
|
||
cvSprintBoost = SPRINT_BOOST:GetFloat() | ||
cvars.AddChangeCallback( "cfc_parachute_sprint_boost", function( _, _, new ) | ||
cvSprintBoost = assert( tonumber( new ) ) | ||
end ) | ||
|
||
cvHandling = HANDLING:GetFloat() | ||
cvars.AddChangeCallback( "cfc_parachute_handling", function( _, _, new ) | ||
cvHandling = assert( tonumber( new ) ) | ||
end ) | ||
end | ||
|
||
hook.Add( "InitPostEntity", "CFC_Shared_Parachute_GetConvars", setupConVars ) | ||
if Entity( 0 ) != NULL then | ||
setupConVars() -- AutoRefresh :D | ||
end | ||
|
||
--[[ | ||
- Returns moveDir, increasing its magnitude if it opposes vel. | ||
- Ultimately makes it faster to brake and change directions. | ||
- moveDir should be given as a unit vector. | ||
--]] | ||
local function improveHandling( vel, moveDir ) | ||
local velLength = vel:Length() | ||
if velLength == 0 then return moveDir end | ||
|
||
local dot = vel:Dot( moveDir ) | ||
dot = dot / velLength -- Get dot product on 0-1 scale | ||
if dot >= 0 then return moveDir end -- moveDir doesn't oppose vel. | ||
|
||
local mult = math.max( -dot * cvHandling, 1 ) | ||
|
||
return moveDir * mult | ||
end | ||
|
||
local function getHorizontalMoveSpeed( ply ) | ||
local hSpeed = cvHorizontalSpeed | ||
|
||
if ply:KeyDown( IN_SPEED ) then | ||
return hSpeed * cvSprintBoost | ||
end | ||
|
||
return hSpeed | ||
end | ||
|
||
-- Acquire direction based on chuteDirRel applied to the player's eye angles. | ||
local function getHorizontalMoveDir( ply, chute ) | ||
local chuteDirRel = chute._chuteDirRel | ||
if chuteDirRel == VEC_ZERO then return chuteDirRel, false end | ||
|
||
local eyeAngles = ply:EyeAngles() | ||
local eyeForward = eyeAngles:Forward() | ||
local eyeRight = eyeAngles:Right() | ||
|
||
local moveDir = ( eyeForward * chuteDirRel.x + eyeRight * chuteDirRel.y ) * Vector( 1, 1, 0 ) | ||
moveDir:Normalize() | ||
|
||
return moveDir, true | ||
end | ||
|
||
local function addHorizontalVel( ply, chute, vel, timeMult ) | ||
-- Acquire player's desired movement direction | ||
local hDir, hDirIsNonZero = getHorizontalMoveDir( ply, chute ) | ||
|
||
-- Add movement velocity (WASD control) | ||
if hDirIsNonZero then | ||
hDir = improveHandling( vel, hDir ) | ||
vel = vel + hDir * timeMult * getHorizontalMoveSpeed( ply ) | ||
end | ||
|
||
-- Limit the horizontal speed | ||
local hSpeedCur = vel:Length2D() | ||
local hSpeedLimit = cvHorizontalSpeedLimit | ||
|
||
if hSpeedCur > hSpeedLimit then | ||
local mult = hSpeedLimit / hSpeedCur | ||
|
||
vel[1] = vel[1] * mult | ||
vel[2] = vel[2] * mult | ||
end | ||
|
||
return vel | ||
end | ||
|
||
-- Not meant to be called manually. | ||
function CFC_Parachute._ApplyChuteForces( ply, chute, mv ) | ||
local vel = mv and mv:GetVelocity() or ply:GetVelocity() | ||
local velZ = vel[3] | ||
|
||
if velZ > cvFallZVel then return end | ||
|
||
local timeMult = FrameTime() | ||
|
||
-- Modify velocity. | ||
vel = addHorizontalVel( ply, chute, vel, timeMult ) | ||
velZ = velZ + ( cvFallZVel - velZ ) * cvFallLerp * timeMult | ||
|
||
vel[3] = velZ | ||
if mv then | ||
vel:Mul( 2 ) | ||
end | ||
|
||
-- Counteract gravity. | ||
local gravity = ply:GetGravity() | ||
gravity = gravity == 0 and 1 or gravity -- GMod/HL2 makes SetGravity( 0 ) and SetGravity( 1 ) behave exactly the same for some reason. | ||
gravity = physenv.GetGravity() * gravity | ||
|
||
-- Have to counteract gravity twice over to actually cancel it out. Source spaghetti or natural consequence? Unsure. | ||
-- Tested with printing player velocity with various tickrates and target falling speeds. | ||
vel = vel - gravity * timeMult * 2 | ||
|
||
if mv then | ||
mv:SetVelocity( vel - mv:GetVelocity() ) | ||
else | ||
ply:SetVelocity( vel - ply:GetVelocity() ) -- SetVelocity() on Players actually adds. | ||
end | ||
end | ||
|
||
if SERVER then | ||
hook.Add( "Move", "CFC_Parachute_Movement", function( ply, mv ) | ||
local parachute = ply:GetTable().cfcParachuteChute | ||
if parachute and parachute != NULL and parachute._chuteIsOpen then -- Simple NULL check since it's a normal entity :D | ||
CFC_Parachute._ApplyChuteForces( ply, parachute, mv ) | ||
end | ||
end ) | ||
else | ||
hook.Add( "Move", "CFC_Parachute_Movement", function( ply, mv ) -- Only called for the local player | ||
local parachute = ply:GetNW2Entity( "CFC_Parachute" ) | ||
if parachute and parachute != NULL then -- Simple NULL check since it's a normal entity :D | ||
CFC_Parachute._ApplyChuteForces( ply, parachute, mv ) | ||
end | ||
end ) | ||
end |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking over it again, I noticed that I fked up here xd