implement touch control
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<title>Minecraft</title>
|
<title>Minecraft</title>
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
|
||||||
<link rel="stylesheet" href="style.css"/>
|
<link rel="stylesheet" href="style.css"/>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ window.GameWindow = class {
|
|||||||
this.mouseLocked = false;
|
this.mouseLocked = false;
|
||||||
this.actualMouseLocked = false;
|
this.actualMouseLocked = false;
|
||||||
|
|
||||||
|
this.isMobile = this.detectTouchDevice();
|
||||||
|
|
||||||
// Get canvas wrapper
|
// Get canvas wrapper
|
||||||
this.wrapper = document.getElementById(this.canvasWrapperId);
|
this.wrapper = document.getElementById(this.canvasWrapperId);
|
||||||
|
|
||||||
@@ -63,12 +65,14 @@ window.GameWindow = class {
|
|||||||
// Mouse buttons
|
// Mouse buttons
|
||||||
document.addEventListener('click', function (event) {
|
document.addEventListener('click', function (event) {
|
||||||
// Create sound engine (It has to be created after user interaction)
|
// Create sound engine (It has to be created after user interaction)
|
||||||
if(!minecraft.soundManager.isCreated()) {
|
if (!minecraft.soundManager.isCreated()) {
|
||||||
minecraft.soundManager.create(minecraft.worldRenderer);
|
minecraft.soundManager.create(minecraft.worldRenderer);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle in-game mouse click
|
// Handle in-game mouse click
|
||||||
minecraft.onMouseClicked(event.button);
|
if (!scope.isMobile) {
|
||||||
|
minecraft.onMouseClicked(event.button);
|
||||||
|
}
|
||||||
|
|
||||||
// Handle mouse click on screen
|
// Handle mouse click on screen
|
||||||
if (!(minecraft.currentScreen === null)) {
|
if (!(minecraft.currentScreen === null)) {
|
||||||
@@ -84,6 +88,11 @@ window.GameWindow = class {
|
|||||||
|
|
||||||
// Keyboard interaction with screen
|
// Keyboard interaction with screen
|
||||||
window.addEventListener('keydown', function (event) {
|
window.addEventListener('keydown', function (event) {
|
||||||
|
if (event.code === "F11") {
|
||||||
|
return; // Toggle fullscreen
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prevent key
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
|
|
||||||
if (!(minecraft.currentScreen === null)) {
|
if (!(minecraft.currentScreen === null)) {
|
||||||
@@ -96,18 +105,139 @@ window.GameWindow = class {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Touch interaction
|
||||||
|
let touchStart;
|
||||||
|
window.addEventListener('touchstart', function (event) {
|
||||||
|
for (let i = 0; i < event.touches.length; i++) {
|
||||||
|
let touch = event.touches[i];
|
||||||
|
|
||||||
|
let x = touch.pageX;
|
||||||
|
let y = touch.pageY;
|
||||||
|
|
||||||
|
let isRightHand = x > scope.wrapper.offsetWidth / 2;
|
||||||
|
|
||||||
|
if (isRightHand) {
|
||||||
|
touchStart = Date.now();
|
||||||
|
} else {
|
||||||
|
let tileSize = scope.wrapper.offsetWidth / 8;
|
||||||
|
|
||||||
|
let tileX = 0;
|
||||||
|
let tileY = scope.wrapper.offsetHeight - tileSize * 3;
|
||||||
|
|
||||||
|
let relX = x - tileX;
|
||||||
|
let relY = y - tileY;
|
||||||
|
|
||||||
|
let tileIndex = Math.floor(relX / tileSize) + Math.floor(relY / tileSize) * 3;
|
||||||
|
|
||||||
|
// Walk buttons
|
||||||
|
switch (tileIndex) {
|
||||||
|
case 0:
|
||||||
|
case 1:
|
||||||
|
case 2:
|
||||||
|
Keyboard.setState("KeyW", true);
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
Keyboard.setState("KeyA", true);
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
Keyboard.setState("Space", true);
|
||||||
|
break;
|
||||||
|
case 5:
|
||||||
|
Keyboard.setState("KeyD", true);
|
||||||
|
break;
|
||||||
|
case 6:
|
||||||
|
case 7:
|
||||||
|
case 8:
|
||||||
|
Keyboard.setState("KeyS", true);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create sound engine (It has to be created after user interaction)
|
||||||
|
if (!minecraft.soundManager.isCreated()) {
|
||||||
|
minecraft.soundManager.create(minecraft.worldRenderer);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Touch movement
|
||||||
|
let prevTouch;
|
||||||
|
window.addEventListener('touchmove', function (event) {
|
||||||
|
for (let i = 0; i < event.touches.length; i++) {
|
||||||
|
let touch = event.touches[i];
|
||||||
|
|
||||||
|
let x = touch.pageX;
|
||||||
|
let y = touch.pageY;
|
||||||
|
|
||||||
|
// Right hand
|
||||||
|
let isRightHand = x > scope.wrapper.offsetWidth / 2;
|
||||||
|
|
||||||
|
if (isRightHand) {
|
||||||
|
// Player movement
|
||||||
|
if (prevTouch) {
|
||||||
|
scope.mouseMotionX = (x - prevTouch.pageX) * 10;
|
||||||
|
scope.mouseMotionY = -(y - prevTouch.pageY) * 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
prevTouch = touch;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
window.addEventListener('touchend', function (event) {
|
||||||
|
// Break block
|
||||||
|
if (!prevTouch && touchStart && (Date.now() - touchStart) < 1000) {
|
||||||
|
minecraft.onMouseClicked(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
prevTouch = null;
|
||||||
|
touchStart = null;
|
||||||
|
|
||||||
|
// Stop pressing keys
|
||||||
|
for (let i = 0; i < event.changedTouches.length; i++) {
|
||||||
|
let touch = event.changedTouches[i];
|
||||||
|
|
||||||
|
// Left hand
|
||||||
|
let isLeftHand = touch.pageX < scope.wrapper.offsetWidth / 2;
|
||||||
|
|
||||||
|
// Release all keys
|
||||||
|
if (isLeftHand) {
|
||||||
|
Keyboard.unPressAll();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Break block listener
|
||||||
|
if (this.isMobile) {
|
||||||
|
setInterval(() => {
|
||||||
|
if (touchStart && (Date.now() - touchStart) > 1000) {
|
||||||
|
touchStart = Date.now();
|
||||||
|
minecraft.onMouseClicked(0);
|
||||||
|
}
|
||||||
|
}, 200);
|
||||||
|
}
|
||||||
|
|
||||||
// Create keyboard
|
// Create keyboard
|
||||||
Keyboard.create();
|
Keyboard.create();
|
||||||
}
|
}
|
||||||
|
|
||||||
requestFocus() {
|
requestFocus() {
|
||||||
window.focus();
|
if (this.isMobile) {
|
||||||
this.canvas.requestPointerLock();
|
document.body.requestFullscreen();
|
||||||
document.body.style.cursor = 'none';
|
} else {
|
||||||
|
window.focus();
|
||||||
|
this.canvas.requestPointerLock();
|
||||||
|
document.body.style.cursor = 'none';
|
||||||
|
}
|
||||||
|
|
||||||
this.mouseLocked = true;
|
this.mouseLocked = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
exitFocus() {
|
exitFocus() {
|
||||||
|
if (this.isMobile) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
document.exitPointerLock();
|
document.exitPointerLock();
|
||||||
document.body.style.cursor = 'default';
|
document.body.style.cursor = 'default';
|
||||||
}
|
}
|
||||||
@@ -180,4 +310,13 @@ window.GameWindow = class {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
detectTouchDevice() {
|
||||||
|
let match = window.matchMedia || window.msMatchMedia;
|
||||||
|
if (match) {
|
||||||
|
let mq = match("(pointer:coarse)");
|
||||||
|
return mq.matches;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -4,7 +4,7 @@ window.Keyboard = class {
|
|||||||
|
|
||||||
static create() {
|
static create() {
|
||||||
window.addEventListener('keydown', function (event) {
|
window.addEventListener('keydown', function (event) {
|
||||||
event.preventDefault();
|
//event.preventDefault();
|
||||||
Keyboard.state[event.code] = true;
|
Keyboard.state[event.code] = true;
|
||||||
//console.log("Key " + event.code + " down");
|
//console.log("Key " + event.code + " down");
|
||||||
});
|
});
|
||||||
@@ -15,6 +15,14 @@ window.Keyboard = class {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static setState(key, state) {
|
||||||
|
Keyboard.state[key] = state;
|
||||||
|
}
|
||||||
|
|
||||||
|
static unPressAll() {
|
||||||
|
Keyboard.state = {};
|
||||||
|
}
|
||||||
|
|
||||||
static isKeyDown(key) {
|
static isKeyDown(key) {
|
||||||
return Keyboard.state[key];
|
return Keyboard.state[key];
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user