Skip to content

Commit

Permalink
"multishot" alternation for shooting
Browse files Browse the repository at this point in the history
  • Loading branch information
GreeningSiren committed Oct 20, 2024
1 parent e89a2a1 commit f08603b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion towerdefence/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ let lives = 10;
var money = 0;

function init() {
towerX = []; towerY = []; towerCooldown = []; towerTimer = []; towerHealth = []; towerShootMode = []; towerRange = []; towerC = 0;
towerX = []; towerY = []; towerCooldown = []; towerTimer = []; towerHealth = []; towerShootMode = []; towerRange = []; towerC = 0, towerLastShot = [];
enemyX = []; enemyY = []; enemySize = []; enemyHealth = []; enemySpeed = []; enemyC = 0;
bulletX = []; bulletY = []; bulletDX = []; bulletDY = []; bulletC = 0;
enemyImage = tryToLoad("enemy", "red");
Expand Down
12 changes: 8 additions & 4 deletions towerdefence/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,28 @@ function findTarget(towerIndex) {
let maxHealth = 0;
let maxHealthIndex = -1;
for (let i = 0; i < enemyC; i++) {
if (enemyHealth[i] > maxHealth && areCirclesColliding(enemyX[i], enemyY[i], enemySize[i], towerX[towerIndex], towerY[towerIndex], towerRange[towerIndex])) {
if (enemyHealth[i] > maxHealth && areCirclesColliding(enemyX[i], enemyY[i], enemySize[i], towerX[towerIndex], towerY[towerIndex], towerRange[towerIndex]) && towerLastShot[towerIndex] != i) {
maxHealth = enemyHealth[i];
maxHealthIndex = i;
}
}
towerLastShot[towerIndex] = maxHealthIndex;
return maxHealthIndex;
case "leastHealth":
let minHealth = Infinity;
let minHealthIndex = -1;
for (let i = 0; i < enemyC; i++) {
if (enemyHealth[i] < minHealth && areCirclesColliding(enemyX[i], enemyY[i], enemySize[i], towerX[towerIndex], towerY[towerIndex], towerRange[towerIndex])) {
if (enemyHealth[i] < minHealth && areCirclesColliding(enemyX[i], enemyY[i], enemySize[i], towerX[towerIndex], towerY[towerIndex], towerRange[towerIndex]) && towerLastShot[towerIndex] != i) {
minHealth = enemyHealth[i];
minHealthIndex = i;
}
}
towerLastShot[towerIndex] = minHealthIndex;
return minHealthIndex;
case "firstEnemy":
for (let i = 0; i < enemyC; i++) {
if (areCirclesColliding(enemyX[i], enemyY[i], enemySize[i], towerX[towerIndex], towerY[towerIndex], towerRange[towerIndex])) {
if (areCirclesColliding(enemyX[i], enemyY[i], enemySize[i], towerX[towerIndex], towerY[towerIndex], towerRange[towerIndex]) && towerLastShot[towerIndex] != i) {
towerLastShot[towerIndex] = i;
return i;
}
}
Expand All @@ -41,11 +44,12 @@ function findTarget(towerIndex) {
let minDistanceIndex = -1;
for (let i = 0; i < enemyC; i++) {
let distance = getDistance(towerX[towerIndex], towerY[towerIndex], enemyX[i], enemyY[i]);
if (distance < minDistance && areCirclesColliding(enemyX[i], enemyY[i], enemySize[i], towerX[towerIndex], towerY[towerIndex], towerRange[towerIndex])) {
if (distance < minDistance && areCirclesColliding(enemyX[i], enemyY[i], enemySize[i], towerX[towerIndex], towerY[towerIndex], towerRange[towerIndex]) && towerLastShot[towerIndex] != i) {
minDistance = distance;
minDistanceIndex = i;
}
}
towerLastShot[towerIndex] = minDistanceIndex;
return minDistanceIndex;
}
}
Expand Down

0 comments on commit f08603b

Please sign in to comment.