From f8c00b8546dcd8c7001e6d01a91b8d7a24699dad Mon Sep 17 00:00:00 2001 From: GreeningSiren Date: Sun, 10 Nov 2024 18:55:20 +0200 Subject: [PATCH] removed count variables and spliced to remove --- towerdefence/engine.js | 32 +++++++++++++------------------- towerdefence/game.js | 25 ++++++------------------- 2 files changed, 19 insertions(+), 38 deletions(-) diff --git a/towerdefence/engine.js b/towerdefence/engine.js index 81be684..3ac88ea 100644 --- a/towerdefence/engine.js +++ b/towerdefence/engine.js @@ -4,15 +4,12 @@ let particleSystem = {}; let isGameStarted = false; // Towers -let towerC = 0; let towers = []; // Enemies -let enemyC = 0; let enemies = []; // Bullets -let bulletC = 0; let bullets = []; // Images @@ -67,7 +64,6 @@ function spawnEnemy(x, y, size, health, speed) { health: health, speed: speed }) - enemyC++; } function spawnTower(x, y, cooldown, health, shootingMode, range) { towers.push({ @@ -80,7 +76,6 @@ function spawnTower(x, y, cooldown, health, shootingMode, range) { timer: 0, lastShot: -1 }) - towerC++; } function spawnBullet(x, y, dX, dY) { bullets.push({ @@ -89,7 +84,6 @@ function spawnBullet(x, y, dX, dY) { dX: dX, dY: dY }) - bulletC++; } function spawnWave1(numEnemies, x, enemiesSize, enemiesHealth, enemySpeed) { // Task idea @@ -120,7 +114,7 @@ function spawnBossWave(x) { function controlTower(key) { let towerIndex = -1; - for (let i = 0; i < towerC; i++) { + for (let i = 0; i < towers.length; i++) { if (areColliding(towers[i].x, towers[i].y, towerSize, towerSize, mouseX, mouseY, 1, 1)) { towerIndex = i; } @@ -166,7 +160,7 @@ function update() { return; } // Move bullets - for (let i = 0; i < bulletC; i++) { + for (let i = 0; i < bullets.length; i++) { bullets[i].x += bullets[i].dX; bullets[i].y += bullets[i].dY; // Remove bullets outside of screen @@ -177,12 +171,12 @@ function update() { } // Move enemies - for (let i = 0; i < enemyC; i++) { + for (let i = 0; i < enemies.length; i++) { enemies[i].x += enemies[i].speed; } // Towers cooldown and shooting - for (let i = 0; i < towerC; i++) { + for (let i = 0; i < towers.length; i++) { towers[i].timer++; if (towers[i].cooldown <= towers[i].timer) { towers[i].timer = 0; @@ -196,7 +190,7 @@ function update() { } // Collision between towers and enemies - for (let j = 0; j < enemyC; j++) { + for (let j = 0; j < enemies.length; j++) { if (areColliding(towers[i].x, towers[i].y, towerSize, towerSize, enemies[j].x, enemies[j].y, enemies[j].size, enemies[j].size)) { lives -= 1; @@ -211,8 +205,8 @@ function update() { } // Collision between enemy and bullet - for (let i = 0; i < enemyC; i++) { - for (let j = 0; j < bulletC; j++) { + for (let i = 0; i < enemies.length; i++) { + for (let j = 0; j < bullets.length; j++) { if (areColliding(enemies[i].x, enemies[i].y, enemies[i].size, enemies[i].size, bullets[j].x, bullets[j].y, bulletSize, bulletSize)) { enemies[i].health -= 1; removeBullet(j); @@ -223,7 +217,7 @@ function update() { } } // Delete bullets that are out of bounds - for (let i = 0; i < bulletC; i++) { + for (let i = 0; i < bullets.length; i++) { if (!areColliding(bullets[i].x, bullets[i].y, bulletSize, bulletSize, 0, 0, 800, 600)) { removeBullet(i); break; @@ -231,7 +225,7 @@ function update() { } // Delete enemies with no health or out of bounds - for (let i = 0; i < enemyC; i++) { + for (let i = 0; i < enemies.length; i++) { if (enemies[i].health <= 0) { money++; } @@ -255,7 +249,7 @@ function draw() { context.fillText("Lives: " + lives, 0, 50); // Draw enemies - for (let i = 0; i < enemyC; i++) { + for (let i = 0; i < enemies.length; i++) { drawImage(enemyImage, enemies[i].x, enemies[i].y+15, enemies[i].size, enemies[i].size); // Enemy health bar @@ -267,7 +261,7 @@ function draw() { context.fillRect(enemies[i].x - w / 2 + enemies[i].size / 2, enemies[i].y - h / 6, w, h); } // Draw bullets - for (let i = 0; i < bulletC; i++) { + for (let i = 0; i < bullets.length; i++) { context.save(); context.translate(bullets[i].x, bullets[i].y); context.rotate(Math.atan2(bullets[i].dY, bullets[i].dX) + Math.PI); @@ -277,7 +271,7 @@ function draw() { // Draw congratulations at end of level - if (enemyC == 0) { + if (enemies.length == 0) { context.font = "30px Courier New"; context.fillText("Congratulations!", 0, 100); @@ -297,7 +291,7 @@ function draw() { context.fillText("Click to play", 200, 300); } // Draw towers - for (let i = 0; i < towerC; i++) { + for (let i = 0; i < towers.length; i++) { drawImage(towerImage, towers[i].x, towers[i].y, towerSize, towerSize); // Selected tower highlight if (areColliding(towers[i].x, towers[i].y, towerSize, towerSize, mouseX, mouseY, 1, 1)) { diff --git a/towerdefence/game.js b/towerdefence/game.js index 268a791..f3f96cd 100644 --- a/towerdefence/game.js +++ b/towerdefence/game.js @@ -13,7 +13,7 @@ function findTarget(towerIndex) { case "mostHealth": { let maxHealth = 0; let maxHealthIndex = -1; - for (let i = 0; i < enemyC; i++) { + for (let i = 0; i < enemies.length; i++) { if (enemies[i].health > maxHealth && areCirclesColliding(enemies[i].x, enemies[i].y, enemies[i].size, towers[towerIndex].x, towers[towerIndex].y, towers[towerIndex].range) && towers[towerIndex].lastShot != i) { maxHealth = enemies[i].health; maxHealthIndex = i; @@ -25,7 +25,7 @@ function findTarget(towerIndex) { case "leastHealth": { let minHealth = Infinity; let minHealthIndex = -1; - for (let i = 0; i < enemyC; i++) { + for (let i = 0; i < enemies.length; i++) { if (enemies[i].health < minHealth && areCirclesColliding(enemies[i].x, enemies[i].y, enemies[i].size, towers[towerIndex].x, towers[towerIndex].y, towers[towerIndex].range) && towers[towerIndex].lastShot != i) { minHealth = enemies[i].health; minHealthIndex = i; @@ -35,7 +35,7 @@ function findTarget(towerIndex) { return minHealthIndex; } case "firstEnemy": { - for (let i = 0; i < enemyC; i++) { + for (let i = 0; i < enemies.length; i++) { if (areCirclesColliding(enemies[i].x, enemies[i].y, enemies[i].size, towers[towerIndex].x, towers[towerIndex].y, towers[towerIndex].range) && towers[towerIndex].lastShot != i) { towers[towerIndex].lastShot = i; return i; @@ -45,7 +45,7 @@ function findTarget(towerIndex) { case "closestEnemy": { let minDistance = Infinity; let minDistanceIndex = -1; - for (let i = 0; i < enemyC; i++) { + for (let i = 0; i < enemies.length; i++) { let distance = getDistance(towers[towerIndex].x, towers[towerIndex].y, enemies[i].x, enemies[i].y); if (distance < minDistance && areCirclesColliding(enemies[i].x, enemies[i].y, enemies[i].size, towers[towerIndex].x, towers[towerIndex].y, towers[towerIndex].range) && towers[towerIndex].lastShot != i) { minDistance = distance; @@ -63,24 +63,11 @@ function shootAtEnemy(towerIndex, enemyIndex) { spawnBullet(towers[towerIndex].x, towers[towerIndex].y, posokaX, posokaY); } function removeBullet(bulletIndex) { - let lastBulletI = bulletC - 1; - bullets[bulletIndex].x = bullets[lastBulletI].x; - bullets[bulletIndex].y = bullets[lastBulletI].y; - bullets[bulletIndex].dX = bullets[lastBulletI].dX; - bullets[bulletIndex].dY = bullets[lastBulletI].dY; - bulletC--; - bullets.pop(); + bullets.splice(bulletIndex,1) } function removeEnemy(enemyIndex) { - let lastEnemyI = enemyC - 1; - enemies[enemyIndex].x = enemies[lastEnemyI].x; - enemies[enemyIndex].y = enemies[lastEnemyI].y; - enemies[enemyIndex].size = enemies[lastEnemyI].size; - enemies[enemyIndex].health = enemies[lastEnemyI].health; - enemies[enemyIndex].speed = enemies[lastEnemyI].speed; - enemyC--; - enemies.pop(); + enemies.splice(enemyIndex,1) } function onCollideEnemyWithBullet(enemyIndex, bulletIndex) {