SeveralCent/luminous-reflections
0
1// Global game state2let currentLevel = 1;3let gameState = {4 grid: [],5 lasers: [],6 targets: [],7 mirrors: [],8 walls: []9};10 11// Sample level data12const levels = {13 1: {14 gridSize: { rows: 5, cols: 5 },15 lasers: [{ row: 0, col: 0, direction: 'right' }],16 targets: [{ row: 4, col: 4 }],17 walls: [],18 maxMoves: 319 },20 2: {21 gridSize: { rows: 6, cols: 6 },22 lasers: [{ row: 0, col: 0, direction: 'right' }],23 targets: [{ row: 5, col: 5 }],24 walls: [{ row: 2, col: 2 }, { row: 3, col: 3 }],25 maxMoves: 426 },27 3: {28 gridSize: { rows: 7, cols: 7 },29 lasers: [30 { row: 0, col: 0, direction: 'right' },31 { row: 6, col: 0, direction: 'up' }32 ],33 targets: [{ row: 3, col: 6 }, { row: 0, col: 6 }],34 walls: [{ row: 1, col: 1 }, { row: 5, col: 5 }],35 maxMoves: 636 }37};38 39// Initialize game40function initializeGame(level) {41 const levelData = levels[level];42 if (!levelData) return;43 44 gameState = {45 grid: Array(levelData.gridSize.rows).fill().map(() => 46 Array(levelData.gridSize.cols).fill(null)47 ),48 lasers: levelData.lasers,49 targets: levelData.targets,50 mirrors: [],51 walls: levelData.walls52 };53 54 // Update UI55 updateGameUI();56}57 58// Update game UI59function updateGameUI() {60 // This would be implemented in the game.html page61 console.log('Updating game UI for level:', currentLevel);62}63 64// Check if level is complete65function checkLevelComplete() {66 const allTargetsHit = gameState.targets.every(target => 67 gameState.lasers.some(laser => 68 laser.path && laser.path.some(point => 69 point.row === target.row && point.col === target.col70 )71 )72 );73 74 if (allTargetsHit) {75 showLevelComplete();76 }77}78 79// Show level complete animation80function showLevelComplete() {81 const gameBoard = document.getElementById('game-board');82 if (gameBoard) {83 gameBoard.classList.add('level-complete');84 setTimeout(() => {85 gameBoard.classList.remove('level-complete');86 }, 1500);87 }88}89 90// Calculate laser path91function calculateLaserPath(laser) {92 const path = [];93 let currentRow = laser.row;94 let currentCol = laser.col;95 let direction = laser.direction;96 97 while (98 currentRow >= 0 && currentRow < gameState.grid.length &&99 currentCol >= 0 && currentCol < gameState.grid[0].length100 ) {101 path.push({ row: currentRow, col: currentCol });102 103 // Check for walls104 if (gameState.walls.some(wall => wall.row === currentRow && wall.col === currentCol)) {105 break;106 }107 108 // Check for mirrors109 const mirror = gameState.mirrors.find(m => m.row === currentRow && m.col === currentCol);110 if (mirror) {111 direction = reflectLaser(direction, mirror.orientation);112 }113 114 // Move in current direction115 switch (direction) {116 case 'up': currentRow--; break;117 case 'down': currentRow++; break;118 case 'left': currentCol--; break;119 case 'right': currentCol++; break;120 }121 122 // Prevent infinite loops123 if (path.length > 100) break;124 }125 126 return path;127}128 129// Reflect laser based on mirror orientation130function reflectLaser(direction, mirrorOrientation) {131 const reflections = {132 'up': { '/': 'right', '\\': 'left' },133 'down': { '/': 'left', '\\': 'right' },134 'left': { '/': 'down', '\\': 'up' },135 'right': { '/': 'up', '\\': 'down' }136 };137 138 return reflections[direction][mirrorOrientation];139}140 141// Event listeners142document.addEventListener('DOMContentLoaded', function() {143 // Initialize first level if on game page144 if (window.location.pathname.includes('game.html')) {145 initializeGame(currentLevel);146 }147 148 // Add smooth scrolling for anchor links149 document.querySelectorAll('a[href^="#"]').forEach(anchor => {150 anchor.addEventListener('click', function (e) {151 e.preventDefault();152 const target = document.querySelector(this.getAttribute('href'));153 if (target) {154 target.scrollIntoView({ behavior: 'smooth' });155 }156 });157 });158});