-
-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathControllerPlayerMovement.java
More file actions
139 lines (116 loc) · 5.62 KB
/
ControllerPlayerMovement.java
File metadata and controls
139 lines (116 loc) · 5.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package dev.isxander.controlify.ingame;
import dev.isxander.controlify.Controlify;
import dev.isxander.controlify.bindings.ControlifyBindings;
import dev.isxander.controlify.api.bind.InputBinding;
import dev.isxander.controlify.controller.ControllerEntity;
import net.minecraft.client.Minecraft;
import net.minecraft.client.player.KeyboardInput;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.world.phys.Vec2;
import org.jetbrains.annotations.Nullable;
import net.minecraft.client.player.ClientInput;
import net.minecraft.world.entity.player.Input;
public class ControllerPlayerMovement extends ClientInput {
private final ControllerEntity controller;
private final LocalPlayer player;
private boolean wasFlying, wasPassenger;
public ControllerPlayerMovement(ControllerEntity controller, LocalPlayer player) {
this.controller = controller;
this.player = player;
}
@Override
public void tick() {
if (Minecraft.getInstance().screen != null || player == null) {
this.setMoveVec(0, 0);
this.keyPresses = Input.EMPTY;
return;
}
float forwardImpulse = ControlifyBindings.WALK_FORWARD.on(controller).analogueNow()
- ControlifyBindings.WALK_BACKWARD.on(controller).analogueNow();
float leftImpulse = ControlifyBindings.WALK_LEFT.on(controller).analogueNow()
- ControlifyBindings.WALK_RIGHT.on(controller).analogueNow();
if (Controlify.instance().config().getSettings().globalSettings().shouldUseKeyboardMovement()) {
float threshold = controller.input().orElseThrow().settings().buttonActivationThreshold;
forwardImpulse = Math.abs(forwardImpulse) >= threshold ? Math.copySign(1, forwardImpulse) : 0;
leftImpulse = Math.abs(leftImpulse) >= threshold ? Math.copySign(1, leftImpulse) : 0;
}
boolean shiftKeyDown = keyPresses.shift();
boolean jumping = keyPresses.jump();
boolean up = forwardImpulse > 0;
boolean down = forwardImpulse < 0;
boolean left = leftImpulse > 0;
boolean right = leftImpulse < 0;
this.setMoveVec(forwardImpulse, leftImpulse);
// this over-complication is so exiting a GUI with the button still held doesn't trigger a jump.
InputBinding jump = ControlifyBindings.JUMP.on(controller);
if (jump.justPressed())
jumping = true;
if (!jump.digitalNow())
jumping = false;
InputBinding sneak = ControlifyBindings.SNEAK.on(controller);
if (player.getAbilities().flying || (player.isInWater() && !player.onGround()) || player.getVehicle() != null || !controller.settings().generic.toggleSneak) {
if (sneak.justPressed())
shiftKeyDown = true;
if (!sneak.digitalNow())
shiftKeyDown = false;
} else {
if (sneak.justPressed()) {
shiftKeyDown = !shiftKeyDown;
}
}
if ((!player.getAbilities().flying && wasFlying && player.onGround()) || (!player.isPassenger() && wasPassenger)) {
shiftKeyDown = false;
}
boolean sprinting = ControlifyBindings.SPRINT.on(controller).digitalNow();
this.keyPresses = new Input(up, down, left, right, jumping, shiftKeyDown, sprinting);
this.wasFlying = player.getAbilities().flying;
this.wasPassenger = player.isPassenger();
}
private void setMoveVec(float forward, float left) {
/*
Starting 25w02a, movement vector is normalised (length set to 1). This won't work in analogue input, as
it would mean you wouldn't be able to move any slower than full speed. So instead, Controlify *limits* the
vector length to 1, but doesn't normalise it.
With regular thumb-sticks, circularity is already a thing,
so this won't actually make any difference for most people.
But custom joystick configurations may produce irregular results, hence this is necessary.
*/
//? if >=1.21.5 {
/*
// Starting snapshot 25w02a, move vector is now normalised.
// This slows down the player when moving diagonally, this will be reflected
// by Controlify. This means that 25w02a and later targets will have differing movement mechanics
// in Controlify.
this.moveVector = new Vec2(left, forward).normalized();
*///?} else {
this.moveVector = new Vec2(left, forward);
float length = this.moveVector.length();
if (length > 1) {
this.moveVector = this.moveVector.scale(1f / length);
}
//?}
}
public static void updatePlayerInput(@Nullable LocalPlayer player) {
if (player == null)
return;
if (shouldBeControllerInput()) {
player.input = new DualInput(
new KeyboardInput(Minecraft.getInstance().options),
new ControllerPlayerMovement(Controlify.instance().getCurrentController().get(), player)
);
} else if (!(player.input instanceof KeyboardInput)) {
player.input = new KeyboardInput(Minecraft.getInstance().options);
}
}
public static void ensureCorrectInput(@Nullable LocalPlayer player) {
if (player == null)
return;
if (shouldBeControllerInput() && player.input.getClass() == KeyboardInput.class) {
updatePlayerInput(player);
}
}
public static boolean shouldBeControllerInput() {
return Controlify.instance().getCurrentController().isPresent()
&& Controlify.instance().currentInputMode().isController();
}
}