class HockeyManager { constructor() { this.gameEngine = null; this.canvas = null; this.initialized = false; } async init() { if (this.initialized) return; try { this.canvas = document.getElementById('game-canvas'); if (!this.canvas) { throw new Error('Canvas element not found'); } this.setupCanvas(); this.gameEngine = new GameEngine(this.canvas); this.setupGlobalEvents(); console.log('Hockey Manager initialized successfully'); this.initialized = true; } catch (error) { console.error('Failed to initialize Hockey Manager:', error); this.showError('Failed to initialize game: ' + error.message); } } setupCanvas() { const container = document.getElementById('game-container'); const containerRect = container.getBoundingClientRect(); this.canvas.width = Math.min(1200, containerRect.width - 40); this.canvas.height = Math.min(800, containerRect.height - 200); this.canvas.style.width = this.canvas.width + 'px'; this.canvas.style.height = this.canvas.height + 'px'; } setupGlobalEvents() { window.addEventListener('resize', () => { this.handleResize(); }); window.addEventListener('beforeunload', () => { this.cleanup(); }); document.addEventListener('visibilitychange', () => { if (document.hidden) { this.gameEngine.gameState.isPaused = true; } }); window.addEventListener('keydown', (e) => { if (e.key === 'F11') { e.preventDefault(); this.toggleFullscreen(); } }); } handleResize() { if (!this.initialized) return; clearTimeout(this.resizeTimeout); this.resizeTimeout = setTimeout(() => { this.setupCanvas(); }, 250); } toggleFullscreen() { if (!document.fullscreenElement) { document.documentElement.requestFullscreen().catch(err => { console.warn('Could not enable fullscreen:', err); }); } else { document.exitFullscreen(); } } start() { if (!this.initialized) { console.error('Game not initialized. Call init() first.'); return; } this.gameEngine.start(); console.log('Hockey Manager started'); } stop() { if (this.gameEngine) { this.gameEngine.stop(); console.log('Hockey Manager stopped'); } } cleanup() { this.stop(); // Clean up any resources, event listeners, etc. } showError(message) { const errorDiv = document.createElement('div'); errorDiv.style.cssText = ` position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); background: #ff4444; color: white; padding: 20px; border-radius: 10px; z-index: 1000; font-family: Arial, sans-serif; `; errorDiv.textContent = message; document.body.appendChild(errorDiv); setTimeout(() => { if (errorDiv.parentNode) { errorDiv.parentNode.removeChild(errorDiv); } }, 5000); } getGameEngine() { return this.gameEngine; } } let hockeyManager; let players; document.addEventListener('DOMContentLoaded', async () => { try { hockeyManager = new HockeyManager(); await hockeyManager.init(); hockeyManager.start(); players = hockeyManager.gameEngine.players; // Initial faceoff to start the game setTimeout(() => { hockeyManager.gameEngine.startFaceoff(); }, 2000); console.log('Hockey Manager 2D Match Engine loaded successfully!'); console.log('Controls:'); console.log('- SPACE: Pause/Resume'); console.log('- D: Toggle debug mode'); console.log('- R: Reset game'); console.log('- Mouse wheel: Zoom in/out'); console.log('- F11: Toggle fullscreen'); } catch (error) { console.error('Failed to start Hockey Manager:', error); } }); window.hockeyManager = hockeyManager;