| 1 | package net.minecraft.client.tutorial;
|
| 2 |
|
| 3 | import net.fabricmc.api.EnvType;
|
| 4 | import net.fabricmc.api.Environment;
|
| 5 | import net.minecraft.client.Minecraft;
|
| 6 | import net.minecraft.client.gui.components.toasts.TutorialToast;
|
| 7 | import net.minecraft.client.player.ClientInput;
|
| 8 | import net.minecraft.network.chat.Component;
|
| 9 | import org.jspecify.annotations.Nullable;
|
| 10 |
|
| 11 | @Environment(EnvType.CLIENT)
|
| 12 | public class MovementTutorialStepInstance implements TutorialStepInstance {
|
| 13 | private static final int MINIMUM_TIME_MOVED = 40;
|
| 14 | private static final int MINIMUM_TIME_LOOKED = 40;
|
| 15 | private static final int MOVE_HINT_DELAY = 100;
|
| 16 | private static final int LOOK_HINT_DELAY = 20;
|
| 17 | private static final int INCOMPLETE = -1;
|
| 18 | private static final Component MOVE_TITLE = Component.translatable(
|
| 19 | "tutorial.move.title", Tutorial.key("forward"), Tutorial.key("left"), Tutorial.key("back"), Tutorial.key("right")
|
| 20 | );
|
| 21 | private static final Component MOVE_DESCRIPTION = Component.translatable("tutorial.move.description", Tutorial.key("jump"));
|
| 22 | private static final Component LOOK_TITLE = Component.translatable("tutorial.look.title");
|
| 23 | private static final Component LOOK_DESCRIPTION = Component.translatable("tutorial.look.description");
|
| 24 | private final Tutorial tutorial;
|
| 25 | private @Nullable TutorialToast moveToast;
|
| 26 | private @Nullable TutorialToast lookToast;
|
| 27 | private int timeWaiting;
|
| 28 | private int timeMoved;
|
| 29 | private int timeLooked;
|
| 30 | private boolean moved;
|
| 31 | private boolean turned;
|
| 32 | private int moveCompleted = -1;
|
| 33 | private int lookCompleted = -1;
|
| 34 |
|
| 35 | public MovementTutorialStepInstance(final Tutorial tutorial) {
|
| 36 | this.tutorial = tutorial;
|
| 37 | }
|
| 38 |
|
| 39 | @Override
|
| 40 | public void tick() {
|
| 41 | this.timeWaiting++;
|
| 42 | if (this.moved) {
|
| 43 | this.timeMoved++;
|
| 44 | this.moved = false;
|
| 45 | }
|
| 46 |
|
| 47 | if (this.turned) {
|
| 48 | this.timeLooked++;
|
| 49 | this.turned = false;
|
| 50 | }
|
| 51 |
|
| 52 | if (this.moveCompleted == -1 && this.timeMoved > 40) {
|
| 53 | if (this.moveToast != null) {
|
| 54 | this.moveToast.hide();
|
| 55 | this.moveToast = null;
|
| 56 | }
|
| 57 |
|
| 58 | this.moveCompleted = this.timeWaiting;
|
| 59 | }
|
| 60 |
|
| 61 | if (this.lookCompleted == -1 && this.timeLooked > 40) {
|
| 62 | if (this.lookToast != null) {
|
| 63 | this.lookToast.hide();
|
| 64 | this.lookToast = null;
|
| 65 | }
|
| 66 |
|
| 67 | this.lookCompleted = this.timeWaiting;
|
| 68 | }
|
| 69 |
|
| 70 | if (this.moveCompleted != -1 && this.lookCompleted != -1) {
|
| 71 | if (this.tutorial.isSurvival()) {
|
| 72 | this.tutorial.setStep(TutorialSteps.FIND_TREE);
|
| 73 | } else {
|
| 74 | this.tutorial.setStep(TutorialSteps.NONE);
|
| 75 | }
|
| 76 | }
|
| 77 |
|
| 78 | if (this.moveToast != null) {
|
| 79 | this.moveToast.updateProgress(this.timeMoved / 40.0F);
|
| 80 | }
|
| 81 |
|
| 82 | if (this.lookToast != null) {
|
| 83 | this.lookToast.updateProgress(this.timeLooked / 40.0F);
|
| 84 | }
|
| 85 |
|
| 86 | if (this.timeWaiting >= 100) {
|
| 87 | Minecraft minecraft = this.tutorial.getMinecraft();
|
| 88 | if (this.moveCompleted == -1 && this.moveToast == null) {
|
| 89 | this.moveToast = new TutorialToast(minecraft.font, TutorialToast.Icons.MOVEMENT_KEYS, MOVE_TITLE, MOVE_DESCRIPTION, true);
|
| 90 | minecraft.gui.toastManager().addToast(this.moveToast);
|
| 91 | } else if (this.moveCompleted != -1 && this.timeWaiting - this.moveCompleted >= 20 && this.lookCompleted == -1 && this.lookToast == null) {
|
| 92 | this.lookToast = new TutorialToast(minecraft.font, TutorialToast.Icons.MOUSE, LOOK_TITLE, LOOK_DESCRIPTION, true);
|
| 93 | minecraft.gui.toastManager().addToast(this.lookToast);
|
| 94 | }
|
| 95 | }
|
| 96 | }
|
| 97 |
|
| 98 | @Override
|
| 99 | public void clear() {
|
| 100 | if (this.moveToast != null) {
|
| 101 | this.moveToast.hide();
|
| 102 | this.moveToast = null;
|
| 103 | }
|
| 104 |
|
| 105 | if (this.lookToast != null) {
|
| 106 | this.lookToast.hide();
|
| 107 | this.lookToast = null;
|
| 108 | }
|
| 109 | }
|
| 110 |
|
| 111 | @Override
|
| 112 | public void onInput(final ClientInput input) {
|
| 113 | if (input.keyPresses.forward() || input.keyPresses.backward() || input.keyPresses.left() || input.keyPresses.right() || input.keyPresses.jump()) {
|
| 114 | this.moved = true;
|
| 115 | }
|
| 116 | }
|
| 117 |
|
| 118 | @Override
|
| 119 | public void onMouse(final double xd, final double yd) {
|
| 120 | if (Math.abs(xd) > 0.01 || Math.abs(yd) > 0.01) {
|
| 121 | this.turned = true;
|
| 122 | }
|
| 123 | }
|
| 124 | }
|
| 125 |
|