- Weltgenerierung: FBM-Terrain, 3 Biome (Plains/Wald/Wüste), Höhlen, Wasser, Bäume - Blöcke: Grass, Dirt, Stein, Sand, Holz, Blätter, Wasser (+Bedrock) - Interaktion: DDA-Voxel-Raycast, Abbauen (LMB) & Setzen (RMB), Hotbar 1-6 - Spieler: First-Person, Gravitation, AABB-Kollision, Springen, Fly-Mode (F) - Engine: Chunk-Streaming (16x16x64), Face-Culling, opaker+transparenter Pass - Stack: Three.js + simplex-noise via CDN, prozedurale Texturen, kein Build-Step Co-Authored-By: Claude <noreply@anthropic.com>
48 lines
1.3 KiB
JavaScript
48 lines
1.3 KiB
JavaScript
// Input-State: Tastatur, Maus-Look (Edge-Flags fuer Klicks).
|
|
export class Input {
|
|
constructor(domElement) {
|
|
this.el = domElement;
|
|
this.keys = new Set();
|
|
this.mouseDX = 0;
|
|
this.mouseDY = 0;
|
|
this.leftClick = false; // Edge (diese Frame)
|
|
this.rightClick = false; // Edge (diese Frame)
|
|
this.locked = false;
|
|
|
|
window.addEventListener('keydown', (e) => {
|
|
this.keys.add(e.code);
|
|
if (e.code === 'Space') e.preventDefault();
|
|
});
|
|
window.addEventListener('keyup', (e) => this.keys.delete(e.code));
|
|
|
|
document.addEventListener('pointerlockchange', () => {
|
|
this.locked = document.pointerLockElement === this.el;
|
|
});
|
|
|
|
this.el.addEventListener('mousedown', (e) => {
|
|
if (!this.locked) return;
|
|
if (e.button === 0) this.leftClick = true;
|
|
if (e.button === 2) this.rightClick = true;
|
|
});
|
|
this.el.addEventListener('contextmenu', (e) => e.preventDefault());
|
|
|
|
document.addEventListener('mousemove', (e) => {
|
|
if (!this.locked) return;
|
|
this.mouseDX += e.movementX || 0;
|
|
this.mouseDY += e.movementY || 0;
|
|
});
|
|
}
|
|
|
|
requestLock() { this.el.requestPointerLock(); }
|
|
|
|
// Am Ende jedes Frames aufrufen.
|
|
endFrame() {
|
|
this.mouseDX = 0;
|
|
this.mouseDY = 0;
|
|
this.leftClick = false;
|
|
this.rightClick = false;
|
|
}
|
|
|
|
down(code) { return this.keys.has(code); }
|
|
}
|