-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from JediWattson/controller
controller and keyboard are both in their own respective files with the camera as a separate module as well
- Loading branch information
Showing
10 changed files
with
5,694 additions
and
455 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { WebGPUFun } from "../types"; | ||
|
||
const equ = (x: number) => (Math.abs(x) / (Math.abs(x) + 1)) * 222 | ||
|
||
const makeGamepadHandler = (camera: WebGPUFun.CameraType) => { | ||
let lastTime = Date.now(); | ||
function pollGamepad() { | ||
if (navigator.getGamepads().length === 0) return; | ||
const gamepad = navigator.getGamepads()[0]; | ||
|
||
const dt = (Date.now() - lastTime) / 100; | ||
gamepad?.axes.forEach((a, i) => { | ||
if (Math.abs(a) < 0.1) return; | ||
const diff = (a > 0 ? 1 : -1) * dt; | ||
const dynRot = equ(a) | ||
if (i === 0) | ||
camera.move(diff, true); | ||
if (i === 1) | ||
camera.move(-diff); | ||
if (i === 2) | ||
camera.rotate({ y: diff * dynRot }); | ||
if (i === 3) | ||
camera.rotate({ z: diff * dynRot }); | ||
}) | ||
|
||
camera.setMovement() | ||
lastTime = Date.now(); | ||
requestAnimationFrame(() => pollGamepad()); | ||
} | ||
|
||
window.addEventListener("gamepadconnected", pollGamepad); | ||
|
||
} | ||
|
||
export default makeGamepadHandler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import type { WebGPUFun } from "../types"; | ||
|
||
const MOVEMENT_DIFF = 0.01; | ||
|
||
const handleKeys = (camera: WebGPUFun.CameraType) => { | ||
camera.setMovement() | ||
let frameId: number | null = null; | ||
const strife: string[] = [] | ||
const forward: string[] = [] | ||
return (key: string, isUpPress?: boolean) => { | ||
if (!['w', 'a', 's', 'd'].includes(key)) return | ||
const isStrife = ['a', 'd'].includes(key) | ||
|
||
if (isUpPress) { | ||
if (isStrife && strife.includes(key)) | ||
strife.splice(strife.indexOf(key), 1) | ||
else if(!isStrife && forward.includes(key)) | ||
forward.splice(forward.indexOf(key), 1) | ||
|
||
if (forward.length === 0 && strife.length === 0 && frameId !== null) { | ||
cancelAnimationFrame(frameId) | ||
frameId = null; | ||
} | ||
return; | ||
} | ||
|
||
if (isStrife && !strife.includes(key)) strife.unshift(key) | ||
else if(!isStrife && !forward.includes(key)) forward.unshift(key) | ||
|
||
let lastTime = Date.now() | ||
const onKey = () => { | ||
const dx = Date.now() - lastTime | ||
if (strife.length > 0) { | ||
const diff = strife[0] === 'd' ? MOVEMENT_DIFF : -MOVEMENT_DIFF | ||
camera.move(diff*dx, true) | ||
} | ||
|
||
if (forward.length > 0) { | ||
const diff = forward[0] === 'w' ? MOVEMENT_DIFF : -MOVEMENT_DIFF | ||
camera.move(diff*dx) | ||
} | ||
|
||
camera.setMovement() | ||
lastTime = Date.now() | ||
frameId = requestAnimationFrame(onKey) | ||
} | ||
|
||
if (frameId === null) | ||
frameId = requestAnimationFrame(onKey) | ||
} | ||
} | ||
|
||
const makeGamePadeEvents = (init: () => void, cleanup: () => void) => { | ||
const events = [ | ||
{ event: 'gamepadconnected', cb: cleanup }, | ||
{ event: 'gamepaddisconnected', cb: init } | ||
] as WebGPUFun.MakeEventsType | ||
|
||
init() | ||
events.forEach( | ||
({ event: ev, cb }) => window.addEventListener(ev, cb) | ||
) | ||
} | ||
|
||
const makeKeyboardEvents = (canvas: HTMLCanvasElement, camera: WebGPUFun.CameraType) => { | ||
const handleKey = handleKeys(camera) | ||
const events = [ | ||
{ event: 'keydown', cb: e => handleKey((e as KeyboardEvent).key) }, | ||
{ event: 'keyup', cb: e => handleKey((e as KeyboardEvent).key, true) }, | ||
{ event: 'click', cb: () => canvas.requestPointerLock() }, | ||
{ event: 'mouseout', cb: () => camera.reset() }, | ||
{ event: 'mousemove', cb: e => { | ||
if(!document.pointerLockElement) return; | ||
const { movementX: y, movementY: z } = e as MouseEvent; | ||
camera.rotate({ y, z }); | ||
camera.setMovement() | ||
}} | ||
] as WebGPUFun.MakeEventsType | ||
|
||
|
||
const init = () => events.forEach( | ||
({ event: ev, cb }) => canvas.addEventListener(ev, cb) | ||
) | ||
|
||
const cleanup = () => { | ||
events.forEach( | ||
({ event: ev, cb }) => canvas.removeEventListener(ev, cb) | ||
) | ||
} | ||
|
||
makeGamePadeEvents(init, cleanup) | ||
|
||
return cleanup | ||
} | ||
|
||
export default makeKeyboardEvents; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters