Skip to content

Commit 82fa432

Browse files
committed
feat: tap and jump
1 parent 23931df commit 82fa432

6 files changed

Lines changed: 27 additions & 8 deletions

File tree

model/character.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { type Dir, DOWN, LEFT, RIGHT, UP } from "../util/dir.ts"
44
import { CELL_SIZE } from "../util/constants.ts"
55
import { choice, randomInt } from "../util/random.ts"
66
import { bindToggleFullscreenOnce } from "../util/fullscreen.ts"
7+
import { inputQueue } from "../player/ui/input-queue.ts"
78
import * as signal from "../util/signal.ts"
89

910
const fallbackImagePhase0 = await fetch(
@@ -462,7 +463,15 @@ export class MainCharacter extends Character {
462463
return LEFT
463464
} else if (input.right) {
464465
return RIGHT
465-
} else if (input.space) {
466+
}
467+
468+
const queueHead = inputQueue[0]
469+
470+
if (
471+
queueHead === "space" ||
472+
queueHead === "touchendempty"
473+
) {
474+
inputQueue.shift()
466475
return "jump"
467476
}
468477
return undefined

player/ui/input-queue.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
type QueueInput = "space" | "touchendempty"
2+
export const inputQueue: QueueInput[] = []

player/ui/key-monitor.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import type { Context } from "@kt3k/cell"
22
import { Input } from "../../util/dir.ts"
3+
import { inputQueue } from "./input-queue.ts"
34

45
const KEY_UP = new Set(["ArrowUp", "w", "k"])
56
const KEY_DOWN = new Set(["ArrowDown", "s", "j"])
67
const KEY_LEFT = new Set(["ArrowLeft", "a", "h"])
78
const KEY_RIGHT = new Set(["ArrowRight", "d", "l"])
89

10+
let spaceQueued = false
11+
912
/** The component which monitors the user input.
1013
*
1114
* Mount <body> tag.
@@ -27,9 +30,11 @@ export function KeyMonitor({ on }: Context) {
2730
e.preventDefault()
2831
Input.right = true
2932
} else if (e.key === " ") {
30-
console.log("Space key pressed, marking Input.space as true")
3133
e.preventDefault()
32-
Input.space = true
34+
if (!spaceQueued) {
35+
spaceQueued = true
36+
inputQueue.push("space")
37+
}
3338
}
3439
})
3540
on("keyup", (e) => {
@@ -42,7 +47,7 @@ export function KeyMonitor({ on }: Context) {
4247
} else if (KEY_RIGHT.has(e.key)) {
4348
Input.right = false
4449
} else if (e.key === " ") {
45-
Input.space = false
50+
spaceQueued = false
4651
}
4752
})
4853
}

player/ui/swipe-handler.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Context } from "@kt3k/cell"
22

33
import { clearInput, Input } from "../../util/dir.ts"
44
import { getDir, getDistance } from "../../util/touch.ts"
5+
import { inputQueue } from "./input-queue.ts"
56

67
const TOUCH_SENSITIVITY_THRESHOLD = 25
78

@@ -26,6 +27,10 @@ export function SwipeHandler({ on }: Context) {
2627
prevTouch = touch
2728
})
2829
on("touchend", () => {
29-
clearInput()
30+
if (Input.up || Input.down || Input.left || Input.right) {
31+
clearInput()
32+
} else {
33+
inputQueue.push("touchendempty")
34+
}
3035
})
3136
}

static/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
</head>
2020
<body
2121
class="js-key-monitor js-swipe-handler"
22-
style="overscroll-behavior-y: none; overscroll-behavior-x: none; min-height: 100svh"
22+
style="overscroll-behavior-y: none; overscroll-behavior-x: none; min-height: 100svh; touch-action: manipulation"
2323
>
2424
<div
2525
class="js-game-screen js-item-get-effector relative h-[320px] w-[320px] overflow-hidden"

util/dir.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@ export const Input = {
1818
down: false,
1919
left: false,
2020
right: false,
21-
space: false,
2221
}
2322

2423
/** Clear the current direction input state */
2524
export function clearInput() {
2625
for (const dir of DIRS) {
2726
Input[dir] = false
2827
}
29-
Input.space = false
3028
}

0 commit comments

Comments
 (0)