feat: add poc
This commit is contained in:
parent
43d27b04d9
commit
4a4fa05ce4
53 changed files with 6191 additions and 0 deletions
153
src/systems/InputSystem.js
Normal file
153
src/systems/InputSystem.js
Normal file
|
|
@ -0,0 +1,153 @@
|
|||
import { System } from '../core/System.js';
|
||||
|
||||
export class InputSystem extends System {
|
||||
constructor() {
|
||||
super('InputSystem');
|
||||
this.requiredComponents = []; // No required components - handles input globally
|
||||
this.priority = 0; // Run first
|
||||
|
||||
this.keys = {};
|
||||
this.keysPrevious = {}; // Track previous frame key states
|
||||
this.mouse = {
|
||||
x: 0,
|
||||
y: 0,
|
||||
buttons: {},
|
||||
buttonsPrevious: {}
|
||||
};
|
||||
}
|
||||
|
||||
init(engine) {
|
||||
super.init(engine);
|
||||
this.setupEventListeners();
|
||||
}
|
||||
|
||||
setupEventListeners() {
|
||||
window.addEventListener('keydown', (e) => {
|
||||
const key = e.key.toLowerCase();
|
||||
const code = e.code.toLowerCase();
|
||||
|
||||
// Store by key name
|
||||
this.keys[key] = true;
|
||||
this.keys[code] = true;
|
||||
|
||||
// Handle special keys
|
||||
if (key === ' ') {
|
||||
this.keys['space'] = true;
|
||||
}
|
||||
if (code === 'space') {
|
||||
this.keys['space'] = true;
|
||||
}
|
||||
|
||||
// Arrow keys
|
||||
if (code === 'arrowup') this.keys['arrowup'] = true;
|
||||
if (code === 'arrowdown') this.keys['arrowdown'] = true;
|
||||
if (code === 'arrowleft') this.keys['arrowleft'] = true;
|
||||
if (code === 'arrowright') this.keys['arrowright'] = true;
|
||||
|
||||
// Prevent default for game keys
|
||||
if ([' ', 'w', 'a', 's', 'd', '1', '2', '3', '4', '5', '6', '7', '8', '9'].includes(key)) {
|
||||
e.preventDefault();
|
||||
}
|
||||
});
|
||||
|
||||
window.addEventListener('keyup', (e) => {
|
||||
const key = e.key.toLowerCase();
|
||||
const code = e.code.toLowerCase();
|
||||
|
||||
this.keys[key] = false;
|
||||
this.keys[code] = false;
|
||||
|
||||
// Handle special keys
|
||||
if (key === ' ') {
|
||||
this.keys['space'] = false;
|
||||
}
|
||||
if (code === 'space') {
|
||||
this.keys['space'] = false;
|
||||
}
|
||||
|
||||
// Arrow keys
|
||||
if (code === 'arrowup') this.keys['arrowup'] = false;
|
||||
if (code === 'arrowdown') this.keys['arrowdown'] = false;
|
||||
if (code === 'arrowleft') this.keys['arrowleft'] = false;
|
||||
if (code === 'arrowright') this.keys['arrowright'] = false;
|
||||
});
|
||||
|
||||
window.addEventListener('mousemove', (e) => {
|
||||
if (this.engine && this.engine.canvas) {
|
||||
const canvas = this.engine.canvas;
|
||||
const rect = canvas.getBoundingClientRect();
|
||||
this.mouse.x = e.clientX - rect.left;
|
||||
this.mouse.y = e.clientY - rect.top;
|
||||
}
|
||||
});
|
||||
|
||||
window.addEventListener('mousedown', (e) => {
|
||||
this.mouse.buttons[e.button] = true;
|
||||
});
|
||||
|
||||
window.addEventListener('mouseup', (e) => {
|
||||
this.mouse.buttons[e.button] = false;
|
||||
});
|
||||
}
|
||||
|
||||
process(_deltaTime, _entities) {
|
||||
// Don't update previous states here - that happens at end of frame
|
||||
// This allows other systems to check isKeyJustPressed during the frame
|
||||
}
|
||||
|
||||
/**
|
||||
* Update previous states - called at end of frame
|
||||
*/
|
||||
updatePreviousStates() {
|
||||
// Deep copy current states to previous for next frame
|
||||
this.keysPrevious = {};
|
||||
for (const key in this.keys) {
|
||||
this.keysPrevious[key] = this.keys[key];
|
||||
}
|
||||
this.mouse.buttonsPrevious = {};
|
||||
for (const button in this.mouse.buttons) {
|
||||
this.mouse.buttonsPrevious[button] = this.mouse.buttons[button];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a key is currently pressed
|
||||
*/
|
||||
isKeyPressed(key) {
|
||||
return this.keys[key.toLowerCase()] === true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a key was just pressed (not held from previous frame)
|
||||
*/
|
||||
isKeyJustPressed(key) {
|
||||
const keyLower = key.toLowerCase();
|
||||
const isPressed = this.keys[keyLower] === true;
|
||||
const wasPressed = this.keysPrevious[keyLower] === true;
|
||||
return isPressed && !wasPressed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get mouse position
|
||||
*/
|
||||
getMousePosition() {
|
||||
return { x: this.mouse.x, y: this.mouse.y };
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if mouse button is pressed
|
||||
*/
|
||||
isMouseButtonPressed(button = 0) {
|
||||
return this.mouse.buttons[button] === true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if mouse button was just pressed
|
||||
*/
|
||||
isMouseButtonJustPressed(button = 0) {
|
||||
const isPressed = this.mouse.buttons[button] === true;
|
||||
const wasPressed = this.mouse.buttonsPrevious[button] === true;
|
||||
return isPressed && !wasPressed;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue