implement settings and controls screen

This commit is contained in:
LabyStudio
2022-02-05 16:58:30 +01:00
parent 52427e610d
commit e1c5ccfa89
10 changed files with 108 additions and 14 deletions
@@ -0,0 +1,8 @@
window.GameSettings = class {
constructor() {
this.crouching = 'ShiftLeft';
this.sprinting = 'ControlLeft';
}
}
+3 -7
View File
@@ -79,16 +79,12 @@ window.GameWindow = class {
// Keyboard interaction with screen
window.addEventListener('keydown', function (event) {
event.preventDefault();
if (!(minecraft.currentScreen === null)) {
// Handle key type on screen
let consumed = minecraft.currentScreen.keyTyped(event.code);
if (consumed) {
// Cancel browser interaction
event.preventDefault();
}
minecraft.currentScreen.keyTyped(event.code);
} else if (event.code === 'Escape') {
event.preventDefault();
minecraft.displayScreen(new GuiIngameMenu());
} else {
minecraft.onKeyPressed(event.code);
+7
View File
@@ -10,6 +10,8 @@ window.Minecraft = class {
// Tick timer
this.timer = new Timer(20);
this.settings = new GameSettings();
// Create window and world renderer
this.window = new GameWindow(this, canvasWrapperId);
@@ -125,6 +127,11 @@ window.Minecraft = class {
}
displayScreen(screen) {
if (typeof screen === "undefined") {
console.log("Tried to display an undefined screen");
return;
}
// Switch screen
this.currentScreen = screen;
+4 -4
View File
@@ -311,9 +311,9 @@ window.Player = class {
let sneaking = false;
if (this.minecraft.hasInGameFocus()) {
if (Keyboard.isKeyDown("KeyR")) { // R
/*if (Keyboard.isKeyDown("KeyR")) { // R
this.respawn();
}
}*/
if (Keyboard.isKeyDown("KeyW")) { // W
moveForward++;
}
@@ -329,14 +329,14 @@ window.Player = class {
if (Keyboard.isKeyDown("Space")) { // Space
jumping = true;
}
if (Keyboard.isKeyDown("ShiftLeft")) { // Shift
if (Keyboard.isKeyDown(this.minecraft.settings.sprinting)) {
if (this.moveForward > 0 && !this.sneaking && !this.sprinting && this.motionX !== 0 && this.motionZ !== 0) {
this.sprinting = true;
this.updateFOVModifier();
}
}
if (Keyboard.isKeyDown("KeyQ")) { // Q
if (Keyboard.isKeyDown(this.minecraft.settings.crouching)) { // Q
sneaking = true;
}
+9 -2
View File
@@ -25,11 +25,18 @@ window.GuiScreen = class extends Gui {
}
}
keyTyped(code) {
if (code === "Escape") {
keyTyped(key) {
if (key === "Escape") {
this.minecraft.displayScreen(null);
return true;
}
for (let i in this.buttonList) {
let button = this.buttonList[i];
button.keyTyped(key);
}
return false;
}
@@ -0,0 +1,40 @@
window.GuiControls = class extends GuiScreen {
constructor(previousScreen) {
super();
this.previousScreen = previousScreen;
}
init() {
super.init();
let settings = this.minecraft.settings;
let scope = this;
this.buttonList.push(new GuiKeyButton("Crouch", settings.crouching, this.width / 2 - 100, this.height / 2 - 20, 200, 20, function (key) {
settings.crouching = key;
scope.init();
}));
this.buttonList.push(new GuiKeyButton("Sprint", settings.sprinting, this.width / 2 - 100, this.height / 2 + 5, 200, 20, function (key) {
settings.sprinting = key;
scope.init();
}));
this.buttonList.push(new GuiButton("Done", this.width / 2 - 100, this.height / 2 + 70, 200, 20, function () {
scope.minecraft.displayScreen(scope.previousScreen);
}));
}
drawScreen(stack, mouseX, mouseY, partialTicks) {
// Background
this.drawRect(stack, 0, 0, this.width, this.height, 'black', 0.6);
// Title
this.drawCenteredString(stack, "Controls", this.width / 2, 50);
super.drawScreen(stack, mouseX, mouseY, partialTicks);
}
}
@@ -11,6 +11,10 @@ window.GuiIngameMenu = class extends GuiScreen {
this.buttonList.push(new GuiButton("Back to game", this.width / 2 - 100, this.height / 2 - 20, 200, 20, function () {
scope.minecraft.displayScreen(null);
}));
this.buttonList.push(new GuiButton("Controls...", this.width / 2 - 100, this.height / 2 + 20, 200, 20, function () {
scope.minecraft.displayScreen(new GuiControls(scope));
}));
}
drawScreen(stack, mouseX, mouseY, partialTicks) {
@@ -19,10 +19,18 @@ window.GuiButton = class extends Gui {
this.drawCenteredString(stack, this.string, this.x + this.width / 2, this.y + this.height / 2 - 5);
}
mouseClicked(mouseX, mouseY, mouseButton) {
onPress() {
this.callback();
}
mouseClicked(mouseX, mouseY, mouseButton) {
this.onPress();
}
keyTyped(key) {
}
isMouseOver(mouseX, mouseY) {
return mouseX > this.x && mouseX < this.x + this.width && mouseY > this.y && mouseY < this.y + this.height;
}
@@ -0,0 +1,21 @@
window.GuiKeyButton = class extends GuiButton {
constructor(name, key, x, y, width, height, callback) {
super(name + ": " + key, x, y, width, height, _ => callback(this.key));
this.listening = false;
}
onPress() {
this.listening = true;
this.string = "...";
}
keyTyped(key) {
if (this.listening) {
this.string = name + ": " + key;
this.listening = false;
this.key = key;
this.callback();
}
}
}
+3
View File
@@ -62,8 +62,10 @@ loadScripts([
"src/js/net/minecraft/client/gui/Gui.js",
"src/js/net/minecraft/client/gui/GuiScreen.js",
"src/js/net/minecraft/client/gui/widgets/GuiButton.js",
"src/js/net/minecraft/client/gui/widgets/GuiKeyButton.js",
"src/js/net/minecraft/client/gui/IngameOverlay.js",
"src/js/net/minecraft/client/gui/screens/GuiLoadingScreen.js",
"src/js/net/minecraft/client/gui/screens/GuiControls.js",
"src/js/net/minecraft/client/gui/screens/GuiIngameMenu.js",
"src/js/net/minecraft/client/GameWindow.js",
"src/js/net/minecraft/client/world/block/Block.js",
@@ -84,6 +86,7 @@ loadScripts([
"src/js/net/minecraft/client/world/generator/WorldGenerator.js",
"src/js/net/minecraft/client/entity/Player.js",
"src/js/net/minecraft/client/inventory/Inventory.js",
"src/js/net/minecraft/client/GameSettings.js",
"src/js/net/minecraft/client/Minecraft.js",
"src/js/net/minecraft/client/render/isometric/IsometricRenderer.js",
"src/js/net/minecraft/client/render/isometric/Point.js",