diff options
Diffstat (limited to 'html/space/input.js')
-rw-r--r-- | html/space/input.js | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/html/space/input.js b/html/space/input.js new file mode 100644 index 0000000..19ea56c --- /dev/null +++ b/html/space/input.js @@ -0,0 +1,83 @@ +// Input handling module +import { updatePlayerControls } from './physics.js'; + +let keys = {}; +let mouseX = 0; +let mouseY = 0; + +// Input state that other modules can read +export const inputState = { + thrust: 0, // forward/backward thrust (W/S) + strafe: 0, // left/right strafe (A/D) + yaw: 0, // left/right rotation (arrow keys) + pitch: 0, // up/down rotation (arrow keys) + firePrimary: false, + fireSecondary: false +}; + +// Initialize input handlers +export function initInput() { + // Keyboard event listeners + document.addEventListener('keydown', (e) => { + keys[e.key.toLowerCase()] = true; + }); + + document.addEventListener('keyup', (e) => { + keys[e.key.toLowerCase()] = false; + }); + + // Mouse movement for heading + document.addEventListener('mousemove', (e) => { + // Calculate mouse position relative to center of canvas + const canvas = document.querySelector('canvas'); + const rect = canvas.getBoundingClientRect(); + const centerX = rect.left + rect.width / 2; + const centerY = rect.top + rect.height / 2; + + mouseX = (e.clientX - centerX) / (rect.width / 2); + mouseY = (e.clientY - centerY) / (rect.height / 2); + }); + + // Mouse click for primary weapon + document.addEventListener('mousedown', (e) => { + if (e.button === 0) { // Left click + keys['fire'] = true; + } + }); + + document.addEventListener('mouseup', (e) => { + if (e.button === 0) { // Left click + keys['fire'] = false; + } + }); + + // E key for secondary weapon + document.addEventListener('keydown', (e) => { + if (e.key.toLowerCase() === 'e') { + keys['secondary'] = true; + } + }); + + document.addEventListener('keyup', (e) => { + if (e.key.toLowerCase() === 'e') { + keys['secondary'] = false; + } + }); +} + +// Update controls based on current input state +export function updateInput() { + const controls = { + thrust: keys[' '] || false, // Space bar for thrust + up: keys['w'] || false, // W for upward strafe + down: keys['s'] || false, // S for downward strafe + left: keys['a'] || false, // A for left strafe + right: keys['d'] || false, // D for right strafe + fire: keys['fire'] || false, + secondary: keys['secondary'] || false, + mouseX, + mouseY + }; + + updatePlayerControls(controls); +} \ No newline at end of file |