Double jump!

This commit is contained in:
Paul Mathieu 2021-12-22 02:31:28 -08:00
parent 8939a90307
commit eec60fcc91
2 changed files with 28 additions and 16 deletions

42
game.js
View File

@ -70,6 +70,7 @@ function handleKeys(params) {
const dir = [right, 0, -forward, 1.0];
const ori = se3.roty(params.camera.orientation[1]);
const tf = se3.apply(ori, dir);
const maxSpeed = 0.1;
if (params.flying) {
params.camera.position[0] += tf[0];
@ -82,11 +83,11 @@ function handleKeys(params) {
params.camera.velocity[0] += tf[0] / 60;
params.camera.velocity[2] += tf[2] / 60;
if (Math.abs(params.camera.velocity[0]) > Math.abs(tf[0])) {
params.camera.velocity[0] = tf[0];
if (Math.abs(params.camera.velocity[0]) > maxSpeed) {
params.camera.velocity[0] = Math.sign(params.camera.velocity[0]) * maxSpeed;
}
if (Math.abs(params.camera.velocity[2]) > Math.abs(tf[2])) {
params.camera.velocity[2] = tf[2];
if (Math.abs(params.camera.velocity[2]) > maxSpeed) {
params.camera.velocity[2] = Math.sign(params.camera.velocity[2]) * maxSpeed;
}
}
};
@ -107,17 +108,12 @@ function handleKeys(params) {
return;
case 'Space':
if(params.flying) {
if (params.flying) {
params.camera.position[1] += 0.1;
} else {
if (params.jumpAmount > 0) {
const amount = 0.4 * params.jumpAmount;
params.camera.velocity[1] += amount / 60;
params.jumpAmount -= amount;
}
}
return;
case 'ControlLeft':
case 'ShiftLeft':
params.camera.position[1] -= 0.1;
return;
}
@ -158,7 +154,7 @@ function updatePhysics(params) {
params.camera.position = newPos;
params.camera.velocity = newPos.map((v, i) => v - oldPos[i]);
if (isOnGround) {
params.jumpAmount = 6;
params.jumpAmount = 2;
params.camera.velocity = params.camera.velocity.map(v => v * 0.7);
}
params.isOnGround = isOnGround;
@ -202,15 +198,21 @@ function tagABlock(gl, params, objects) {
// [x] generating & loading of more chunks
// [x] distance fog
// [x] better controls
// [x] non-flowy water
// [x] slightly cooler terrain
// [ ] fullscreen
// [ ] a soundrack
// [ ] trees and stuff
// [ ] different biomes (with different noise stats)
// [ ] non-flowy water
// [ ] flowy water
// [ ] save the world (yay) to local storage (bah)
// [ ] mines
// [ ] caves
// [ ] crafting
// [ ] fix bugs
// [ ] better light
// [ ] inventory
// [ ] monsters
// [ ] multi player
export function setupParamPanel(params) {
document.querySelector('#lightx').oninput = e => {
@ -287,6 +289,16 @@ export function initUiListeners(params, canvas) {
switch (e.code) {
case 'KeyF':
params.flying = !params.flying;
break;
case 'Space':
if (!params.flying) {
if (params.jumpAmount > 0) {
const amount = 0.08;
params.camera.velocity[1] = amount;
params.jumpAmount -= 1;
}
}
break;
}
} else {
params.keys.delete(e.code);

View File

@ -334,7 +334,7 @@ export function updateWorldGeometry(gl, world, z, x, timeLimit = 10000) {
}
export function checkCollision(curPos, newPos, world) {
// I guess Steve is about 1.7 m tall?
// I guess Gontrand is about 1.7 m tall?
// he also has a 60x60 cm axis-aligned square section '^_^
// box is centered around the camera
const steveBB = [