fix: fix sprinting

This commit is contained in:
daoge_cmd
2026-03-02 01:21:56 +08:00
parent b5111232aa
commit 6cba5705dd
3 changed files with 33 additions and 12 deletions

View File

@@ -12,9 +12,11 @@ Input::Input()
{
xa = 0;
ya = 0;
sprintForward = 0;
wasJumping = false;
jumping = false;
sneaking = false;
usingKeyboardMovement = false;
lReset = false;
rReset = false;
@@ -40,16 +42,18 @@ void Input::tick(LocalPlayer *player)
ya = InputManager.GetJoypadStick_LY(iPad);
else
ya = 0.0f;
sprintForward = ya;
usingKeyboardMovement = false;
#ifdef _WINDOWS64
// WASD movement (combine with gamepad)
if (iPad == 0)
{
float kbX = 0.0f, kbY = 0.0f;
if (KMInput.IsKeyDown('W')) kbY += 1.0f;
if (KMInput.IsKeyDown('S')) kbY -= 1.0f;
if (KMInput.IsKeyDown('A')) kbX += 1.0f; // inverted like gamepad
if (KMInput.IsKeyDown('D')) kbX -= 1.0f;
if (KMInput.IsKeyDown('W')) { kbY += 1.0f; sprintForward += 1.0f; usingKeyboardMovement = true; }
if (KMInput.IsKeyDown('S')) { kbY -= 1.0f; sprintForward -= 1.0f; usingKeyboardMovement = true; }
if (KMInput.IsKeyDown('A')) { kbX += 1.0f; usingKeyboardMovement = true; } // inverted like gamepad
if (KMInput.IsKeyDown('D')) { kbX -= 1.0f; usingKeyboardMovement = true; }
// Normalize diagonal
if (kbX != 0.0f && kbY != 0.0f) { kbX *= 0.707f; kbY *= 0.707f; }
if (pMinecraft->localgameModes[iPad]->isInputAllowed(MINECRAFT_ACTION_LEFT) || pMinecraft->localgameModes[iPad]->isInputAllowed(MINECRAFT_ACTION_RIGHT))
@@ -58,11 +62,13 @@ void Input::tick(LocalPlayer *player)
ya = max(min(ya + kbY, 1.0f), -1.0f);
}
#endif
sprintForward = max(min(sprintForward, 1.0f), -1.0f);
#ifndef _CONTENT_PACKAGE
if (app.GetFreezePlayers())
{
xa = ya = 0.0f;
sprintForward = 0.0f;
player->abilities.flying = true;
}
#endif
@@ -74,6 +80,7 @@ void Input::tick(LocalPlayer *player)
lReset = true;
}
xa = ya = 0.0f;
sprintForward = 0.0f;
}
// 4J - in flying mode, don't actually toggle sneaking
@@ -168,4 +175,4 @@ void Input::tick(LocalPlayer *player)
#endif
//OutputDebugString("INPUT: End input tick\n");
}
}

View File

@@ -6,10 +6,12 @@ class Input
public:
float xa;
float ya;
float sprintForward;
bool wasJumping;
bool jumping;
bool sneaking;
bool usingKeyboardMovement;
Input(); // 4J - added
@@ -20,4 +22,4 @@ private:
bool lReset;
bool rReset;
bool m_gamepadSneaking;
};
};

View File

@@ -274,11 +274,13 @@ void LocalPlayer::aiStep()
if (changingDimensionDelay > 0) changingDimensionDelay--;
bool wasJumping = input->jumping;
float runTreshold = 0.8f;
float sprintForward = input->sprintForward;
bool wasRunning = input->ya >= runTreshold;
bool wasRunning = sprintForward >= runTreshold;
//input->tick( dynamic_pointer_cast<Player>( shared_from_this() ) );
// 4J-PB - make it a localplayer
input->tick( this );
sprintForward = input->sprintForward;
if (isUsingItem())
{
input->xa *= 0.2f;
@@ -302,9 +304,20 @@ void LocalPlayer::aiStep()
// world with low food, then reload it in creative.
if(abilities.mayfly || isAllowedToFly() ) enoughFoodToSprint = true;
bool forwardEnoughToTriggerSprint = sprintForward >= runTreshold;
bool forwardReturnedToDeadzone = sprintForward == 0.0f;
bool forwardEnoughToContinueSprint = sprintForward >= runTreshold;
#ifdef _WINDOWS64
if (GetXboxPad() == 0 && input->usingKeyboardMovement)
{
forwardEnoughToContinueSprint = sprintForward > 0.0f;
}
#endif
#ifdef _WINDOWS64
// Keyboard sprint: Ctrl held while moving forward
if (GetXboxPad() == 0 && KMInput.IsKeyDown(VK_CONTROL) && input->ya > 0.0f &&
if (GetXboxPad() == 0 && input->usingKeyboardMovement && KMInput.IsKeyDown(VK_CONTROL) && sprintForward > 0.0f &&
enoughFoodToSprint && !isUsingItem() && !hasEffect(MobEffect::blindness) && onGround)
{
if (!isSprinting()) setSprinting(true);
@@ -314,7 +327,7 @@ void LocalPlayer::aiStep()
// 4J - altered this slightly to make sure that the joypad returns to below returnTreshold in between registering two movements up to runThreshold
if (onGround && !isSprinting() && enoughFoodToSprint && !isUsingItem() && !hasEffect(MobEffect::blindness))
{
if( !wasRunning && input->ya >= runTreshold )
if( !wasRunning && forwardEnoughToTriggerSprint )
{
if (sprintTriggerTime == 0)
{
@@ -331,7 +344,7 @@ void LocalPlayer::aiStep()
}
}
}
else if( ( sprintTriggerTime > 0 ) && ( input->ya == 0.0f ) ) // ya of 0.0f here signifies that we have returned to the deadzone
else if( ( sprintTriggerTime > 0 ) && forwardReturnedToDeadzone ) // zero sprintForward here signifies that we have returned to the deadzone
{
sprintTriggerRegisteredReturn = true;
}
@@ -339,7 +352,7 @@ void LocalPlayer::aiStep()
if (isSneaking()) sprintTriggerTime = 0;
// 4J-PB - try not stopping sprint on collision
//if (isSprinting() && (input->ya < runTreshold || horizontalCollision || !enoughFoodToSprint))
if (isSprinting() && (input->ya < runTreshold || !enoughFoodToSprint))
if (isSprinting() && (!forwardEnoughToContinueSprint || !enoughFoodToSprint || isSneaking() || isUsingItem()))
{
setSprinting(false);
}
@@ -1622,4 +1635,3 @@ void LocalPlayer::SetPlayerAdditionalModelParts(vector<ModelPart *>pAdditionalMo
{
m_pAdditionalModelParts=pAdditionalModelParts;
}