diff --git a/src/js/net/minecraft/client/GameSettings.js b/src/js/net/minecraft/client/GameSettings.js new file mode 100644 index 0000000..9177078 --- /dev/null +++ b/src/js/net/minecraft/client/GameSettings.js @@ -0,0 +1,8 @@ +window.GameSettings = class { + + constructor() { + this.crouching = 'ShiftLeft'; + this.sprinting = 'ControlLeft'; + } + +} \ No newline at end of file diff --git a/src/js/net/minecraft/client/GameWindow.js b/src/js/net/minecraft/client/GameWindow.js index ae6d35c..635ddf7 100644 --- a/src/js/net/minecraft/client/GameWindow.js +++ b/src/js/net/minecraft/client/GameWindow.js @@ -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); diff --git a/src/js/net/minecraft/client/Minecraft.js b/src/js/net/minecraft/client/Minecraft.js index afdffa3..56bce8e 100644 --- a/src/js/net/minecraft/client/Minecraft.js +++ b/src/js/net/minecraft/client/Minecraft.js @@ -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; diff --git a/src/js/net/minecraft/client/entity/Player.js b/src/js/net/minecraft/client/entity/Player.js index 21c7b3d..fb24a15 100644 --- a/src/js/net/minecraft/client/entity/Player.js +++ b/src/js/net/minecraft/client/entity/Player.js @@ -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; } diff --git a/src/js/net/minecraft/client/gui/GuiScreen.js b/src/js/net/minecraft/client/gui/GuiScreen.js index 84e62a7..1aaaf4c 100644 --- a/src/js/net/minecraft/client/gui/GuiScreen.js +++ b/src/js/net/minecraft/client/gui/GuiScreen.js @@ -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; } diff --git a/src/js/net/minecraft/client/gui/screens/GuiControls.js b/src/js/net/minecraft/client/gui/screens/GuiControls.js new file mode 100644 index 0000000..4ae8b8b --- /dev/null +++ b/src/js/net/minecraft/client/gui/screens/GuiControls.js @@ -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); + } + +} \ No newline at end of file diff --git a/src/js/net/minecraft/client/gui/screens/GuiIngameMenu.js b/src/js/net/minecraft/client/gui/screens/GuiIngameMenu.js index e34f646..61cee71 100644 --- a/src/js/net/minecraft/client/gui/screens/GuiIngameMenu.js +++ b/src/js/net/minecraft/client/gui/screens/GuiIngameMenu.js @@ -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) { diff --git a/src/js/net/minecraft/client/gui/widgets/GuiButton.js b/src/js/net/minecraft/client/gui/widgets/GuiButton.js index 6f89c12..237f7d6 100644 --- a/src/js/net/minecraft/client/gui/widgets/GuiButton.js +++ b/src/js/net/minecraft/client/gui/widgets/GuiButton.js @@ -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; } diff --git a/src/js/net/minecraft/client/gui/widgets/GuiKeyButton.js b/src/js/net/minecraft/client/gui/widgets/GuiKeyButton.js new file mode 100644 index 0000000..d425927 --- /dev/null +++ b/src/js/net/minecraft/client/gui/widgets/GuiKeyButton.js @@ -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(); + } + } +} \ No newline at end of file diff --git a/src/start.js b/src/start.js index e140cac..c744ea3 100644 --- a/src/start.js +++ b/src/start.js @@ -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",