fix crouching collision

This commit is contained in:
LabyStudio
2022-01-31 13:54:13 +01:00
parent 7f4d7f88bf
commit 9ede174bf0
5 changed files with 30 additions and 12 deletions
+4 -4
View File
@@ -13,11 +13,11 @@ window.GameWindow = class {
document.getElementById(this.canvasWrapperId).appendChild(canvas);
// Init
this.initialize();
this.onResize();
// On resize
let scope = this;
window.addEventListener('resize', _ => scope.initialize(), false);
window.addEventListener('resize', _ => scope.onResize(), false);
// Request focus
canvas.onclick = function () {
@@ -29,12 +29,12 @@ window.GameWindow = class {
// Mouse motion
document.addEventListener('mousemove', event => this.onMouseMove(event), false);
}
initialize() {
// Create keyboard
Keyboard.create();
}
onResize() {
// Get canvas size
let canvasElement = document.getElementById(this.canvasWrapperId);
this.canvasWidth = canvasElement.offsetWidth;
+6 -6
View File
@@ -23,12 +23,12 @@ window.World = class {
getCollisionBoxes(aabb) {
let boundingBoxList = [];
let minX = Math.floor(aabb.minX) - 1;
let maxX = Math.ceil(aabb.maxX) + 1;
let minY = Math.floor(aabb.minY) - 1;
let maxY = Math.ceil(aabb.maxY) + 1;
let minZ = Math.floor(aabb.minZ) - 1;
let maxZ = Math.ceil(aabb.maxZ) + 1;
let minX = MathHelper.floor_double(aabb.minX);
let maxX = MathHelper.floor_double(aabb.maxX + 1.0);
let minY = MathHelper.floor_double(aabb.minY);
let maxY = MathHelper.floor_double(aabb.maxY + 1.0);
let minZ = MathHelper.floor_double(aabb.minZ);
let maxZ = MathHelper.floor_double(aabb.maxZ + 1.0);
for (let x = minX; x < maxX; x++) {
for (let y = minY; y < maxY; y++) {
+8 -2
View File
@@ -3,8 +3,14 @@ window.Keyboard = class {
static state = {};
static create() {
window.addEventListener('keyup', (e) => Keyboard.state[e.code] = false);
window.addEventListener('keydown', (e) => Keyboard.state[e.code] = true);
window.addEventListener('keydown', function (event) {
Keyboard.state[event.code] = true;
//console.log("Key " + event.code + " down");
});
window.addEventListener('keyup', function (event) {
delete Keyboard.state[event.code];
//console.log("Key " + event.code + " up");
});
};
static isKeyDown(key) {
+11
View File
@@ -0,0 +1,11 @@
window.MathHelper = class {
/**
* Returns the greatest integer less than or equal to the double argument
*/
static floor_double(value) {
let i = parseInt(value);
return value < i ? i - 1 : i;
}
}
+1
View File
@@ -48,6 +48,7 @@ loadScripts([
// Minecraft Source
"src/net/minecraft/util/Timer.js",
"src/net/minecraft/util/MathHelper.js",
"src/net/minecraft/util/BoundingBox.js",
"src/net/minecraft/util/Keyboard.js",
"src/net/minecraft/client/GameWindow.js",