diff options
-rw-r--r-- | html/tower/js/game.js | 27 | ||||
-rw-r--r-- | html/tower/js/gameState.js | 33 | ||||
-rw-r--r-- | html/tower/js/mechanics.js | 113 | ||||
-rw-r--r-- | html/tower/js/renderer.js | 6 | ||||
-rw-r--r-- | html/tower/js/uiHandlers.js | 4 |
5 files changed, 85 insertions, 98 deletions
diff --git a/html/tower/js/game.js b/html/tower/js/game.js index 7d362ae..57eaa3a 100644 --- a/html/tower/js/game.js +++ b/html/tower/js/game.js @@ -1,19 +1,6 @@ const canvas = document.getElementById('gameCanvas'); const ctx = canvas.getContext('2d'); -// Initialize game state -let gameState = { - grid: Array(20).fill().map(() => Array(20).fill('empty')), - path: [], - towers: [], - enemies: [], - playerCurrency: 100, - phase: 'placement', - isGameOver: false, - particles: [], - projectiles: [] -}; - let lastTimestamp = 0; const ENEMY_SPAWN_INTERVAL = 1000; // 1 second let lastEnemySpawn = 0; @@ -38,7 +25,7 @@ function gameLoop(timestamp) { function handleCombatPhase(timestamp, deltaTime) { spawnEnemies(timestamp); - updateEnemies(gameState.enemies, gameState.path, deltaTime); + updateEnemies(); gameState.particles = updateParticles(gameState.particles, timestamp, deltaTime); gameState.projectiles = gameState.projectiles.filter(p => timestamp - p.createdAt < p.lifetime); @@ -63,7 +50,13 @@ function handleCombatPhase(timestamp, deltaTime) { ); // Remove dead enemies and destroyed towers - gameState.enemies = gameState.enemies.filter(enemy => enemy.currentHealth > 0); + gameState.enemies = gameState.enemies.filter(enemy => { + if (enemy.currentHealth <= 0) { + gameState.awardEnemyDestroyed(); + return false; + } + return true; + }); gameState.towers = gameState.towers.filter(tower => tower.currentHealth > 0); } @@ -146,11 +139,11 @@ function initializeEventListeners() { const tower = TowerTypes[draggedTowerType]; if ( gameState.grid[hoverCell.y][hoverCell.x] === 'empty' && - gameState.playerCurrency >= tower.cost + gameState.currency >= tower.cost ) { gameState.grid[hoverCell.y][hoverCell.x] = 'tower'; gameState.towers.push(createTower(draggedTowerType, { ...hoverCell })); - gameState.playerCurrency -= tower.cost; + gameState.currency -= tower.cost; } draggedTowerType = null; diff --git a/html/tower/js/gameState.js b/html/tower/js/gameState.js index 0f379f6..63270fa 100644 --- a/html/tower/js/gameState.js +++ b/html/tower/js/gameState.js @@ -109,16 +109,25 @@ function createParticle(type, position, angle) { }; } -// Add to gameState object -gameState.particles = []; -gameState.projectiles = []; -gameState.enemiesDestroyed = 0; -gameState.enemiesEscaped = 0; -gameState.path = []; // This will be populated when the path is generated - -gameState.awardEnemyDestroyed = function() { - this.enemiesDestroyed++; - // Random reward between 5 and 10 - const reward = Math.floor(Math.random() * 6) + 5; - this.currency += reward; +// Initialize game state at the bottom of the file +const gameState = { + grid: Array(20).fill().map(() => Array(20).fill('empty')), + path: [], + towers: [], + enemies: [], + currency: 100, + phase: 'placement', + isGameOver: false, + particles: [], + projectiles: [], + enemiesDestroyed: 0, + enemiesEscaped: 0, + + // Define the function as part of the initial object + awardEnemyDestroyed() { + this.enemiesDestroyed++; + // Random reward between 1 and 3 + const reward = Math.floor(Math.random() * 3) + 1; + this.currency += reward; + } }; \ No newline at end of file diff --git a/html/tower/js/mechanics.js b/html/tower/js/mechanics.js index 5d3eacb..c9c4202 100644 --- a/html/tower/js/mechanics.js +++ b/html/tower/js/mechanics.js @@ -1,20 +1,52 @@ // Combat mechanics -function updateEnemies(enemies, path, deltaTime) { - enemies.forEach(enemy => { - if (enemy.pathIndex < path.length - 1) { - const targetPos = path[enemy.pathIndex + 1]; - const dx = targetPos.x - enemy.position.x; - const dy = targetPos.y - enemy.position.y; - const distance = Math.sqrt(dx * dx + dy * dy); - - if (distance < enemy.speed * deltaTime / 1000) { - enemy.position = { ...targetPos }; - enemy.pathIndex++; - } else { - enemy.position.x += (dx / distance) * enemy.speed * deltaTime / 1000; - enemy.position.y += (dy / distance) * enemy.speed * deltaTime / 1000; - } +function updateEnemies() { + gameState.enemies = gameState.enemies.filter(enemy => { + // Add progress property if it doesn't exist + if (typeof enemy.progress === 'undefined') { + enemy.progress = 0; + } + + // Update progress + enemy.progress += enemy.speed * 0.001; + + // Check if enemy has completed the path + if (enemy.progress >= 1) { + gameState.enemiesEscaped++; + return false; // Remove from array + } + + // Check for collisions with projectiles + const hitByProjectile = gameState.projectiles.some(projectile => { + const distance = Math.hypot( + enemy.position.x - projectile.startPos.x, + enemy.position.y - projectile.startPos.y + ); + return distance < 0.5; + }); + + if (hitByProjectile) { + gameState.awardEnemyDestroyed(); + return false; // Remove from array } + + // Update enemy position based on progress + const pathPosition = getPathPosition(enemy.progress, gameState.path); + enemy.position.x = pathPosition.x; + enemy.position.y = pathPosition.y; + + return true; + }); + + // Remove projectiles that hit enemies + gameState.projectiles = gameState.projectiles.filter(projectile => { + const hitEnemy = gameState.enemies.some(enemy => { + const distance = Math.hypot( + enemy.position.x - projectile.startPos.x, + enemy.position.y - projectile.startPos.y + ); + return distance < 0.5; + }); + return !hitEnemy; }); } @@ -140,55 +172,4 @@ function handleEnemyAttack(enemy, tower, particles, timestamp, cellSize) { // Reduce tower's damage as it takes damage tower.damage = TowerTypes[tower.type].damage * (tower.currentHealth / tower.maxHealth); -} - -function updateEnemies() { - gameState.enemies = gameState.enemies.filter(enemy => { - // Add progress property if it doesn't exist - if (typeof enemy.progress === 'undefined') { - enemy.progress = 0; - } - - // Reduce the multiplier from 0.01 to 0.001 for more reasonable speed - enemy.progress += enemy.speed * 0.001; // Smaller multiplier for slower movement - - // Check if enemy has completed the path - if (enemy.progress >= 1) { - gameState.enemiesEscaped++; - return false; // Remove from array - } - - // Check for collisions with projectiles - const hitByProjectile = gameState.projectiles.some(projectile => { - const distance = Math.hypot( - enemy.position.x - projectile.startPos.x, - enemy.position.y - projectile.startPos.y - ); - return distance < 0.5; // Adjust collision radius as needed - }); - - if (hitByProjectile) { - gameState.awardEnemyDestroyed(); - return false; // Remove from array - } - - // Update enemy position based on progress - const pathPosition = getPathPosition(enemy.progress, gameState.path); - enemy.position.x = pathPosition.x; - enemy.position.y = pathPosition.y; - - return true; - }); - - // Remove projectiles that hit enemies - gameState.projectiles = gameState.projectiles.filter(projectile => { - const hitEnemy = gameState.enemies.some(enemy => { - const distance = Math.hypot( - enemy.position.x - projectile.startPos.x, - enemy.position.y - projectile.startPos.y - ); - return distance < 0.5; - }); - return !hitEnemy; - }); } \ No newline at end of file diff --git a/html/tower/js/renderer.js b/html/tower/js/renderer.js index b0d1da0..24a18ef 100644 --- a/html/tower/js/renderer.js +++ b/html/tower/js/renderer.js @@ -100,8 +100,12 @@ function renderEnemies(ctx, enemies) { function renderUI(ctx, gameState) { ctx.fillStyle = 'black'; ctx.font = '20px Arial'; - ctx.fillText(`Currency: ${gameState.playerCurrency}`, 10, 30); + ctx.fillText(`Currency: $${gameState.currency}`, 10, 30); ctx.fillText(`Phase: ${gameState.phase}`, 10, 60); + + // Add enemy stats + ctx.fillText(`Destroyed: ${gameState.enemiesDestroyed}`, 10, 90); + ctx.fillText(`Escaped: ${gameState.enemiesEscaped}`, 10, 120); } function renderTowers(ctx, towers) { diff --git a/html/tower/js/uiHandlers.js b/html/tower/js/uiHandlers.js index 5dc048a..6e0789e 100644 --- a/html/tower/js/uiHandlers.js +++ b/html/tower/js/uiHandlers.js @@ -39,10 +39,10 @@ function placeTower(gameState, towerType, position) { const tower = TowerTypes[towerType]; if ( gameState.grid[position.y][position.x] === 'empty' && - gameState.playerCurrency >= tower.cost + gameState.currency >= tower.cost ) { gameState.grid[position.y][position.x] = 'tower'; gameState.towers.push(createTower(towerType, { ...position })); - gameState.playerCurrency -= tower.cost; + gameState.currency -= tower.cost; } } \ No newline at end of file |