Skip to content

Commit

Permalink
#46 improve esc camera jump in chromium
Browse files Browse the repository at this point in the history
  • Loading branch information
L3P3 committed Jul 24, 2024
1 parent 8de08e4 commit 2552c8a
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 39 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "minicraft",
"version": "0.10.5",
"version": "0.10.6",
"description": "voxel-based 3d game, written in javascript",
"homepage": "https://l3p3.de/minicraft",
"repository": {
Expand Down
6 changes: 3 additions & 3 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
addEventListener_,
BroadcastChannel_,
clearTimeout_,
Date_,
Date_now,
handler_noop,
localStorage_,
localStorage_getItem,
Expand Down Expand Up @@ -75,7 +75,7 @@ function Root() {
};
setInterval(() => (
actions.config_save(),
!BroadcastChannel_ && localStorage_setItem('minicraft.lock', Date_.now())
!BroadcastChannel_ && localStorage_setItem('minicraft.lock', Date_now())
), 500);

addEventListener_('touchend', event => {
Expand Down Expand Up @@ -170,7 +170,7 @@ else if (BroadcastChannel_) {
else {
// first make sure lock has expired
const lock_found = Number_(localStorage_getItem('minicraft.lock'));
const lock_limit = Date_.now() - 1000;
const lock_limit = Date_now() - 1000;
// if already expired
if (lock_found < lock_limit) init(Root);
// if not, wait and check again
Expand Down
5 changes: 4 additions & 1 deletion src/etc/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {

export const window_ = window;
export const document_ = document;
export const navigator_ = navigator;
const Math_ = Math;
export const Math_PI = Math_.PI;
export const Math_PI_h = Math_PI * .5;
Expand All @@ -21,7 +22,8 @@ export const Math_log2 = Math_.log2;
export const String_fromCharCode = String.fromCharCode;

export const BroadcastChannel_ = BroadcastChannel;
export const Date_ = Date;
const Date_ = Date;
export const Date_now = () => Date_.now();
const JSON_ = JSON;
export const Promise_ = Promise;
export const JSON_stringify = JSON_.stringify;
Expand All @@ -44,6 +46,7 @@ export const clearTimeout_ = clearTimeout;
export const clearInterval_ = clearInterval;
export const addEventListener_ = addEventListener;
export const removeEventListener_ = removeEventListener;
export const flag_chromium = navigator_.userAgent.includes('WebKit');

/**
@param {number} num
Expand Down
3 changes: 2 additions & 1 deletion src/etc/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
VERSION,
} from './env.js';
import {
Date_now,
JSON_parse,
JSON_stringify,
Object_keys,
Expand Down Expand Up @@ -88,7 +89,7 @@ export const reducers = {
label: (
prompt(locale_unknown_world_found, '') || locale_unknown_world
).substring(0, 16),
mod_l: Date.now(),
mod_l: Date_now(),
mod_r: 0,
};
needs_save = true;
Expand Down
3 changes: 2 additions & 1 deletion src/etc/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import {
localStorage_getItem,
localStorage_removeItem,
localStorage_setItem,
navigator_,
} from './helpers.js';

// to assert chunk deletion fix is done before this
import './state.js';

let chunks_db = null;
if (indexedDB_) {
navigator.storage?.persist?.();
navigator_.storage?.persist?.();
const request = indexedDB_.open('minicraft', 1);
request.onupgradeneeded = event => {
// migrate from localStorage to indexedDB
Expand Down
48 changes: 34 additions & 14 deletions src/game/c_game.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,27 @@ import {
} from '../etc/constants.js';
import {
document_,
flag_chromium,
Math_max,
window_,
} from '../etc/helpers.js';

import {
game_create,
game_destroy,
game_key,
game_message_print,
game_mouse_catch,
game_mouse_move,
game_mouse_move_menu,
game_mouse_move_player,
game_render,
game_renderer_init,
game_resolution_update,
game_view_distance_update,
game_destroy,
} from './m_game.js';
import {
player_rotate,
} from './m_player.js';

import Bar from './c_bar.js';
import Inventory from './c_inventory.js';
Expand Down Expand Up @@ -74,14 +80,11 @@ export default function Game({

return false;
};
const handler_mousemove = event => {
if (
model.menu !== MENU_NONE ||
document_.pointerLockElement === frame
) {
game_mouse_move(model, event);
}
};
const handler_mousemove = event => (
document_.pointerLockElement === frame
? game_mouse_move_player(model, event)
: game_mouse_move_menu(model, event)
);
const handler_mousewheel = event => {
actions.config_touch_set(false);

Expand All @@ -100,16 +103,18 @@ export default function Game({
}
};

const passive_true = {passive: true};

frame.addEventListener('mousedown', handler_mousebutton);
frame.addEventListener('mouseup', handler_mousebutton);
frame.addEventListener('mousemove', handler_mousemove);
frame.addEventListener('wheel', handler_mousewheel);
frame.addEventListener('mousemove', handler_mousemove, passive_true);
frame.addEventListener('wheel', handler_mousewheel, passive_true);

return () => {
frame.removeEventListener('mousedown', handler_mousebutton);
frame.removeEventListener('mouseup', handler_mousebutton);
frame.removeEventListener('mousemove', handler_mousemove);
frame.removeEventListener('wheel', handler_mousewheel);
frame.removeEventListener('mousemove', handler_mousemove, passive_true);
frame.removeEventListener('wheel', handler_mousewheel, passive_true);
game_destroy(model);
ref.game = null;
};
Expand Down Expand Up @@ -144,6 +149,21 @@ export default function Game({
model.menu === MENU_NONE
) {
model.menu = MENU_SETTINGS;
// #46 move camera back after accidental jump
if (
flag_chromium &&
(model.rotate_last_h | model.rotate_last_v) &&
model.rotate_last_time > time_now - 100
) {
game_message_print(model, `move back ${Math.round(model.rotate_last_h*100)}% ${Math.round(model.rotate_last_v*100)}%`);
player_rotate(
model.player,
-model.rotate_last_h,
-model.rotate_last_v
);
model.rotate_last_h = model.rotate_last_v = 0;
model.renderer.flag_dirty = true;
}
}

model.world.flag_paused = !ingame;
Expand Down
3 changes: 2 additions & 1 deletion src/game/c_menu_start.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
VERSION,
} from '../etc/env.js';
import {
Date_now,
JSON_stringify,
Math_max,
Math_min,
Expand Down Expand Up @@ -513,7 +514,7 @@ export default function MenuStart({
actions.world_add({
id: Math_min(0, ...config.worlds.map(world => world.id)) - 1,
label: name,
mod_l: Date.now(),
mod_l: Date_now(),
mod_r: 0,
});
}
Expand Down
41 changes: 24 additions & 17 deletions src/game/m_game.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ import {
import {
clearInterval_,
clearTimeout_,
Date_now,
flag_chromium,
JSON_stringify,
Math_ceil,
Math_floor,
Expand Down Expand Up @@ -157,6 +159,9 @@ export const game_create = (actions, frame_element, config, account) => {
resolution_raw_y: 1,
resolution_x: 0,
resolution_y: 0,
rotate_last_h: 0,
rotate_last_time: 0,
rotate_last_v: 0,
tick_interval: setInterval_(() => (
world_tick(world, player)
), 50),
Expand Down Expand Up @@ -185,7 +190,7 @@ export const game_renderer_init = (model, canvas_element) => {
export const game_save = model => {
world_save(model.world, model.player);
model.actions.world_prop(model.world.id, {
mod_l: Date.now(),
mod_l: Date_now(),
});
}

Expand Down Expand Up @@ -243,22 +248,24 @@ export const game_mouse_catch = async model => {
catch (error) {}
}

export const game_mouse_move = (model, event) => {
if (!model.world.flag_paused) {
const factor = model.config.mouse_sensitivity * Math_PI / (Math_max(
model.resolution_raw_x,
model.resolution_raw_y
) * model.resolution_css_ratio);
player_rotate(
model.player,
event.movementX * factor,
-event.movementY * factor
);
}
else {
model.cursor_x = event.clientX;
model.cursor_y = event.clientY;
export const game_mouse_move_player = (model, event) => {
const factor = model.config.mouse_sensitivity * Math_PI / (Math_max(
model.resolution_raw_x,
model.resolution_raw_y
) * model.resolution_css_ratio);
if (flag_chromium) {
model.rotate_last_time = event.timeStamp;
}
player_rotate(
model.player,
model.rotate_last_h = event.movementX * factor,
model.rotate_last_v = -event.movementY * factor
);
}

export const game_mouse_move_menu = (model, event) => {
model.cursor_x = event.clientX;
model.cursor_y = event.clientY;
}

/**
Expand Down Expand Up @@ -825,7 +832,7 @@ export const game_message_send = (model, value) => {
}
}

const game_message_print = (model, value, minor = false) => {
export const game_message_print = (model, value, minor = false) => {
const id = ++message_id_counter;
(
model.messages = model.messages.slice(-49)
Expand Down

0 comments on commit 2552c8a

Please sign in to comment.