Added the ability to sprint

When you double-tap the W key, the character starts moving faster
This commit is contained in:
Sergey Karmanov 2021-01-11 00:17:31 +03:00
parent 9d305890d1
commit a67d3ba1bb
2 changed files with 354 additions and 320 deletions

View File

@ -65,6 +65,11 @@ public class LayerTestGUI extends GUILayer {
() -> String.format("Flying: %5s (Space bar x2)", TestPlayerControls.getInstance().isFlying())
));
panel.addChild(new Label(
"IsSprintDisplay", font,
() -> String.format("Sprint: %5s (W x2)", TestPlayerControls.getInstance().isSprint())
));
panel.addChild(new Label(
"IsMouseCapturedDisplay", font,
() -> String.format("Mouse captured: %5s (esc)", TestPlayerControls.getInstance().isMouseCaptured())

View File

@ -1,11 +1,10 @@
package ru.windcorp.progressia.test;
import org.lwjgl.glfw.GLFW;
import glm.Glm;
import glm.mat._3.Mat3;
import glm.vec._2.Vec2;
import glm.vec._3.Vec3;
import org.lwjgl.glfw.GLFW;
import ru.windcorp.progressia.client.ClientState;
import ru.windcorp.progressia.client.graphics.backend.GraphicsBackend;
import ru.windcorp.progressia.client.graphics.backend.GraphicsInterface;
@ -31,9 +30,11 @@ public class TestPlayerControls {
return INSTANCE;
}
private TestPlayerControls() {}
private TestPlayerControls() {
}
private static final double MODE_SWITCH_MAX_DELAY = 300 * Units.MILLISECONDS;
private static final double MODE_SPRINT_SWITCH_MAX_DELAY = 100 * Units.MILLISECONDS;
private static final double MIN_JUMP_DELAY = 300 * Units.MILLISECONDS;
// Horizontal and vertical max control speed when flying
@ -45,6 +46,9 @@ public class TestPlayerControls {
// Horizontal and vertical max control speed when walking
private static final float WALKING_SPEED = 4.0f * Units.METERS_PER_SECOND;
// Horizontal and vertical max control speed when run
private static final float RUN_SPEED = 6.0f * Units.METERS_PER_SECOND;
// (0; 1], 1 is instant change, 0 is no control authority
private static final float WALKING_CONTROL_AUTHORITY = Units.get("15 1/s");
@ -52,12 +56,14 @@ public class TestPlayerControls {
private static final float JUMP_VELOCITY = 5f * Units.METERS_PER_SECOND;
private boolean isFlying = true;
private boolean isSprint = false;
private int movementForward = 0;
private int movementRight = 0;
private int movementUp = 0;
private double lastSpacePress = Double.NEGATIVE_INFINITY;
private double lastSprintPress = Double.NEGATIVE_INFINITY;
private boolean captureMouse = true;
private boolean useMinecraftGravity = false;
@ -80,6 +86,9 @@ public class TestPlayerControls {
if (isFlying) {
speed = FLYING_SPEED;
authority = FLYING_CONTROL_AUTHORITY;
} else if (isSprint) {
speed = RUN_SPEED;
authority = WALKING_CONTROL_AUTHORITY;
} else {
speed = WALKING_SPEED;
authority = WALKING_CONTROL_AUTHORITY;
@ -132,12 +141,11 @@ public class TestPlayerControls {
private boolean onKeyEvent(KeyEvent event) {
if (event.isRepeat()) return false;
int multiplier = event.isPress() ? 1 : -1;
switch (event.getKey()) {
case GLFW.GLFW_KEY_W:
movementForward += +1 * multiplier;
handleSprint(event);
break;
case GLFW.GLFW_KEY_S:
movementForward += -1 * multiplier;
@ -154,7 +162,6 @@ public class TestPlayerControls {
case GLFW.GLFW_KEY_LEFT_SHIFT:
handleShift(multiplier);
break;
case GLFW.GLFW_KEY_ESCAPE:
if (!event.isPress()) return false;
handleEscape();
@ -188,6 +195,7 @@ public class TestPlayerControls {
double timeSinceLastSpacePress = GraphicsInterface.getTime() - lastSpacePress;
if (isPressed && timeSinceLastSpacePress < MODE_SWITCH_MAX_DELAY) {
isSprint = false;
isFlying = !isFlying;
updateGUI();
movementUp = +1;
@ -204,6 +212,23 @@ public class TestPlayerControls {
lastSpacePress = GraphicsInterface.getTime();
}
private void handleSprint(KeyEvent event) {
double timeSinceLastSpacePress = GraphicsInterface.getTime() - lastSprintPress;
if (event.isPress() && timeSinceLastSpacePress < MODE_SPRINT_SWITCH_MAX_DELAY && !isFlying) {
isSprint = !isSprint;
updateGUI();
}
lastSprintPress = GraphicsInterface.getTime();
if (isSprint && event.isRelease()) {
isSprint = false;
updateGUI();
}
}
private void jump() {
if (ClientState.getInstance() == null || !ClientState.getInstance().isReady()) {
return;
@ -269,7 +294,7 @@ public class TestPlayerControls {
// Clamp pitch
dir.y = Glm.clamp(
dir.y, -FloatMathUtils.PI_F/2, +FloatMathUtils.PI_F/2
dir.y, -FloatMathUtils.PI_F / 2, +FloatMathUtils.PI_F / 2
);
}
@ -334,6 +359,10 @@ public class TestPlayerControls {
return isFlying;
}
public boolean isSprint() {
return isSprint;
}
public boolean isMouseCaptured() {
return captureMouse;
}