ame='generator' content='cgit 1.4.1-2-gfad0'/>
about summary refs log tree commit diff stats
path: root/html/space/input.js
blob: 19ea56cecd076dc8572475c8ab57176d4fd6e31d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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);
}