Skip to content

Commit

Permalink
removed count variables and spliced to remove
Browse files Browse the repository at this point in the history
  • Loading branch information
GreeningSiren committed Nov 10, 2024
1 parent 79c1663 commit f8c00b8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 38 deletions.
32 changes: 13 additions & 19 deletions towerdefence/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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({
Expand All @@ -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({
Expand All @@ -89,7 +84,6 @@ function spawnBullet(x, y, dX, dY) {
dX: dX,
dY: dY
})
bulletC++;
}
function spawnWave1(numEnemies, x, enemiesSize, enemiesHealth, enemySpeed) {
// Task idea
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -223,15 +217,15 @@ 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;
}
}

// 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++;
}
Expand All @@ -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
Expand All @@ -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);
Expand All @@ -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);

Expand All @@ -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)) {
Expand Down
25 changes: 6 additions & 19 deletions towerdefence/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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) {

Expand Down

0 comments on commit f8c00b8

Please sign in to comment.