File size: 3,420 Bytes
1390db3
 
 
 
874a51f
1390db3
 
 
 
 
 
 
 
 
 
 
 
d6dfcf1
 
 
1390db3
 
 
 
 
 
 
 
 
d6dfcf1
 
 
1390db3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf6239d
874a51f
 
 
 
 
 
1390db3
d6dfcf1
 
 
1390db3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf6239d
874a51f
 
 
 
 
 
d6dfcf1
 
 
1390db3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { G } from './globals.js';
import { CFG } from './config.js';
import { showOverlay } from './hud.js';
import { initAudio, resumeAudio } from './audio.js';
import { primeGrenade, releaseGrenade } from './grenades.js';

export function setupEvents({ startGame, restartGame, beginReload, updateWeaponAnchor }) {
  const overlay = document.getElementById('overlay');
  if (!overlay) return;

  overlay.addEventListener('click', () => {
    // Initialize audio on user gesture
    initAudio();
    if (G.state === 'menu') {
      G.controls.lock();
    } else if (G.state === 'paused') {
      G.controls.lock();
    } else if (G.state === 'gameover') {
      // Restart on click after game over
      restartGame();
    }
  });

  G.controls.addEventListener('lock', () => {
    if (G.state === 'menu') {
      startGame();
    } else if (G.state === 'paused') {
      G.state = 'playing';
      overlay.classList.add('hidden');
    } else if (G.state === 'gameover') {
      // Treat lock like a fresh start after game over
      startGame();
    }
  });

  G.controls.addEventListener('unlock', () => {
    if (G.state === 'playing') {
      G.state = 'paused';
      showOverlay('paused');
    }
  });

  document.addEventListener('keydown', (e) => {
    switch (e.code) {
      case 'KeyW': G.input.w = true; break;
      case 'KeyA': G.input.a = true; break;
      case 'KeyS': G.input.s = true; break;
      case 'KeyD': G.input.d = true; break;
      case 'ShiftLeft': G.input.sprint = true; break;
      case 'KeyC': G.input.crouch = true; break;
      case 'KeyG':
        if (G.state === 'playing') {
          G.input.grenade = true;
          primeGrenade();
        }
        break;
      case 'Space':
        if (G.state === 'playing') {
          // Use buffered jump handled in player physics
          G.input.jump = true;
        }
        break;
      case 'KeyF':
        if (G.state === 'playing' && G.flashlight) {
          G.flashlight.visible = !G.flashlight.visible;
        }
        break;
      case 'KeyP':
      case 'Escape':
        if (G.state === 'playing') {
          G.controls.unlock();
        }
        break;
      case 'KeyR':
        if (G.state === 'playing') {
          beginReload();
        }
        break;
    }
  });

  document.addEventListener('keyup', (e) => {
    switch (e.code) {
      case 'KeyW': G.input.w = false; break;
      case 'KeyA': G.input.a = false; break;
      case 'KeyS': G.input.s = false; break;
      case 'KeyD': G.input.d = false; break;
      case 'ShiftLeft': G.input.sprint = false; break;
      case 'KeyC': G.input.crouch = false; break;
      case 'KeyG':
        if (G.input.grenade) {
          G.input.grenade = false;
          releaseGrenade();
        }
        break;
      case 'Space':
        G.input.jump = false;
        break;
    }
  });

  document.addEventListener('mousedown', (e) => {
    if (e.button === 0 && G.state === 'playing') {
      // Ensure audio context is running on interaction
      resumeAudio();
      G.input.shoot = true;
    }
  });

  document.addEventListener('mouseup', (e) => {
    if (e.button === 0) {
      G.input.shoot = false;
    }
  });

  window.addEventListener('resize', () => {
    G.camera.aspect = window.innerWidth / window.innerHeight;
    G.camera.updateProjectionMatrix();
    G.renderer.setSize(window.innerWidth, window.innerHeight);
    updateWeaponAnchor();
  });
}