This commit is contained in:
Emilia 2024-05-09 21:12:45 -07:00 committed by GitHub
commit 4f43392395
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -888,8 +888,17 @@ class Player extends PrimaryView {
});
// Similarly, grab touch events and translate them to directions
this.current_touches = {}; // ident => action
this.touch_history = {} // ident => {x, y} representing start of touch
this.touch_restart_delay = new util.DelayTimer;
let touch_target = this.root.querySelector('#player-game-area .level');
let start_touches = ev => {
ev.stopPropagation();
ev.preventDefault();
this.using_touch = true;
for (let touch of ev.changedTouches) {
this.touch_history[touch.identifier] = {x: touch.clientX, y: touch.clientY};
}
}
let collect_touches = ev => {
ev.stopPropagation();
ev.preventDefault();
@ -899,11 +908,10 @@ class Player extends PrimaryView {
// TODO allow starting a level without moving?
// TODO if you don't move the touch, the player can pass it and will keep going in that
// direction?
let [px, py] = this.level.player.visual_position();
px += 0.5;
py += 0.5;
for (let touch of ev.changedTouches) {
let start = this.touch_history[touch.identifier]
let [x, y] = this.renderer.point_to_real_cell_coords(touch.clientX, touch.clientY);
let [px, py] = this.renderer.point_to_real_cell_coords(start.x, start.y);
let dx = x - px;
let dy = y - py;
// Divine a direction from the results
@ -932,11 +940,12 @@ class Player extends PrimaryView {
this.set_state('playing');
}
};
touch_target.addEventListener('touchstart', collect_touches);
touch_target.addEventListener('touchstart', start_touches);
touch_target.addEventListener('touchmove', collect_touches);
let dismiss_touches = ev => {
for (let touch of ev.changedTouches) {
delete this.current_touches[touch.identifier];
delete this.touch_history[touch.identifier];
}
};
touch_target.addEventListener('touchend', dismiss_touches);