Skip to content

Commit

Permalink
Simplify logic for hearts, makes it flexible and dynamic
Browse files Browse the repository at this point in the history
  • Loading branch information
Eray Chumak committed Jan 11, 2024
1 parent b382f51 commit 177fba8
Showing 1 changed file with 8 additions and 25 deletions.
33 changes: 8 additions & 25 deletions Sprites/Player.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default class Player {
this.projectiles = 1;
this.cooldown = 5_000;

this.difficulty = sessionStorage.getItem("difficulty");
this.difficulty = sessionStorage.getItem("gameDifficulty");

if (this.difficulty === "hard") {
this.hp = 1;
Expand Down Expand Up @@ -255,31 +255,14 @@ export default class Player {
updateHearts() {
const hearts = this.hearts.getChildren();

if (this.hp === 2) {
hearts[2].setAlpha(.1);
}

if (this.hp === 1) {
hearts[1].setAlpha(.1);
hearts[2].setAlpha(.1);
}

if (this.hp === 0) {
hearts[0].setAlpha(.1);
hearts[1].setAlpha(.1);
hearts[2].setAlpha(.1);
}
// handles hearts and half hearts
for (let x = 0; x < hearts.length; x++) {
const currentHeart = hearts[x];
const heartNum = x + 1;

if (this.hp === 2.5) {
hearts[2].setAlpha(.5);
}

if (this.hp === 1.5) {
hearts[1].setAlpha(.5);
}

if (this.hp === .5) {
hearts[0].setAlpha(.5);
if (this.hp < heartNum) {
currentHeart.setAlpha(this.hp % 1 === 0 ? 0.1 : 0.5);
}
}
}

Expand Down

0 comments on commit 177fba8

Please sign in to comment.