|
| 1 | +import { CONFIG } from '../core/config.js'; |
| 2 | + |
| 3 | +// Input Manager - Handles keyboard and mouse input |
| 4 | +export class InputManager { |
| 5 | + constructor(gameClient) { |
| 6 | + this.game = gameClient; |
| 7 | + this.keys = {}; |
| 8 | + this.mouseX = 0; |
| 9 | + this.mouseY = 0; |
| 10 | + } |
| 11 | + |
| 12 | + setupEventListeners(canvas) { |
| 13 | + this.setupKeyboardEvents(); |
| 14 | + this.setupMouseEvents(canvas); |
| 15 | + this.setupUIButtonEvents(); |
| 16 | + } |
| 17 | + |
| 18 | + setupKeyboardEvents() { |
| 19 | + document.addEventListener('keydown', (e) => { |
| 20 | + this.keys[e.code] = true; |
| 21 | + |
| 22 | + if (e.code === 'Enter') { |
| 23 | + this.handleEnterKey(e); |
| 24 | + } |
| 25 | + |
| 26 | + if (e.code === 'Escape') { |
| 27 | + this.handleEscapeKey(); |
| 28 | + } |
| 29 | + }); |
| 30 | + |
| 31 | + document.addEventListener('keyup', (e) => { |
| 32 | + this.keys[e.code] = false; |
| 33 | + }); |
| 34 | + } |
| 35 | + |
| 36 | + handleEnterKey(e) { |
| 37 | + const joinScreen = document.getElementById('join-screen'); |
| 38 | + const playerNameInput = document.getElementById('player-name'); |
| 39 | + const serverAddressInput = document.getElementById('server-address'); |
| 40 | + |
| 41 | + if (joinScreen && joinScreen.style.display !== 'none') { |
| 42 | + if (document.activeElement === playerNameInput || |
| 43 | + document.activeElement === serverAddressInput) { |
| 44 | + return; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + const chatInput = document.getElementById('chat-input'); |
| 49 | + if (document.activeElement === chatInput) { |
| 50 | + this.game.sendChat(); |
| 51 | + chatInput.blur(); |
| 52 | + } else if (chatInput) { |
| 53 | + chatInput.focus(); |
| 54 | + } |
| 55 | + |
| 56 | + e.preventDefault(); |
| 57 | + } |
| 58 | + |
| 59 | + handleEscapeKey() { |
| 60 | + const chatInput = document.getElementById('chat-input'); |
| 61 | + if (chatInput) { |
| 62 | + chatInput.blur(); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + setupMouseEvents(canvas) { |
| 67 | + canvas.addEventListener('mousemove', (e) => { |
| 68 | + const rect = canvas.getBoundingClientRect(); |
| 69 | + const scaleX = canvas.width / rect.width; |
| 70 | + const scaleY = canvas.height / rect.height; |
| 71 | + |
| 72 | + this.mouseX = (e.clientX - rect.left) * scaleX; |
| 73 | + this.mouseY = (e.clientY - rect.top) * scaleY; |
| 74 | + }); |
| 75 | + |
| 76 | + canvas.addEventListener('click', () => { |
| 77 | + this.game.tryFire(); |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + setupUIButtonEvents() { |
| 82 | + const voiceBtn = document.getElementById('voice-toggle'); |
| 83 | + if (voiceBtn) { |
| 84 | + voiceBtn.addEventListener('click', () => { |
| 85 | + this.game.voiceChatManager.toggle((msg, type) => { |
| 86 | + this.game.uiManager.showNotification(msg, type); |
| 87 | + }); |
| 88 | + }); |
| 89 | + } |
| 90 | + |
| 91 | + const sendBtn = document.getElementById('send-btn'); |
| 92 | + if (sendBtn) { |
| 93 | + sendBtn.addEventListener('click', () => { |
| 94 | + this.game.sendChat(); |
| 95 | + }); |
| 96 | + } |
| 97 | + |
| 98 | + const chatToggleBtn = document.getElementById('chat-toggle'); |
| 99 | + if (chatToggleBtn) { |
| 100 | + chatToggleBtn.addEventListener('click', () => { |
| 101 | + this.game.uiManager.chatOpen = !this.game.uiManager.chatOpen; |
| 102 | + const chatPanel = document.getElementById('chat-panel'); |
| 103 | + chatPanel.style.height = this.game.uiManager.chatOpen ? 'auto' : '45px'; |
| 104 | + document.getElementById('chat-messages').style.display = this.game.uiManager.chatOpen ? 'block' : 'none'; |
| 105 | + document.querySelector('.chat-input-wrapper').style.display = this.game.uiManager.chatOpen ? 'flex' : 'none'; |
| 106 | + }); |
| 107 | + } |
| 108 | + |
| 109 | + const settingsBtn = document.getElementById('settings-btn'); |
| 110 | + if (settingsBtn) { |
| 111 | + settingsBtn.addEventListener('click', () => { |
| 112 | + this.game.uiManager.toggleSettings( |
| 113 | + this.game.aimLineEnabled, |
| 114 | + (enabled) => { this.game.aimLineEnabled = enabled; } |
| 115 | + ); |
| 116 | + }); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + getAngle() { |
| 121 | + if (this.game.myPlayer) { |
| 122 | + return Math.atan2( |
| 123 | + this.mouseY - this.game.myPlayer.y, |
| 124 | + this.mouseX - this.game.myPlayer.x |
| 125 | + ) * 180 / Math.PI; |
| 126 | + } |
| 127 | + return 0; |
| 128 | + } |
| 129 | + |
| 130 | + getMovement() { |
| 131 | + if (!this.game.myPlayer || !this.game.isAlive) return null; |
| 132 | + |
| 133 | + let x = this.game.myPlayer.x; |
| 134 | + let y = this.game.myPlayer.y; |
| 135 | + const speed = this.game.myPlayer.speedBoost ? CONFIG.PLAYER.SPEED_BOOSTED : CONFIG.PLAYER.SPEED_NORMAL; |
| 136 | + |
| 137 | + if (this.keys['KeyW'] || this.keys['ArrowUp']) y -= speed; |
| 138 | + if (this.keys['KeyS'] || this.keys['ArrowDown']) y += speed; |
| 139 | + if (this.keys['KeyA'] || this.keys['ArrowLeft']) x -= speed; |
| 140 | + if (this.keys['KeyD'] || this.keys['ArrowRight']) x += speed; |
| 141 | + |
| 142 | + x = Math.max(CONFIG.PLAYER.MIN_X, Math.min(CONFIG.PLAYER.MAX_X, x)); |
| 143 | + y = Math.max(CONFIG.PLAYER.MIN_Y, Math.min(CONFIG.PLAYER.MAX_Y, y)); |
| 144 | + |
| 145 | + return { x, y }; |
| 146 | + } |
| 147 | + |
| 148 | + isSpacePressed() { |
| 149 | + return this.keys['Space']; |
| 150 | + } |
| 151 | +} |
0 commit comments