Skip to content

Commit 0ebd85f

Browse files
committed
- hp UI
1 parent 193ac19 commit 0ebd85f

File tree

12 files changed

+72
-3
lines changed

12 files changed

+72
-3
lines changed
File renamed without changes.
File renamed without changes.
1.18 KB
Loading

src/configs/assets.config.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@ export class AssetsConfig {
4444
tag: ImageTag.PROJECTILE_ARROW,
4545
url: "sprites/projectiles/arrow.webp",
4646
},
47+
{
48+
tag: ImageTag.HEART,
49+
url: "sprites/ui/heart.webp",
50+
},
51+
{
52+
tag: ImageTag.HEART_BORDER,
53+
url: "sprites/ui/heart-border.webp",
54+
},
55+
{
56+
tag: ImageTag.HEART_BACKGROUND,
57+
url: "sprites/ui/heart-background.webp",
58+
},
4759
];
4860
}
4961

@@ -122,6 +134,14 @@ export class AssetsConfig {
122134
frameHeight: 32,
123135
},
124136
},
137+
{
138+
tag: SpritesheetTag.HEART,
139+
url: "spritesheets/heart-spritesheet-36.webp",
140+
config: {
141+
frameWidth: 36,
142+
frameHeight: 36,
143+
},
144+
},
125145
];
126146
}
127147

src/game-objects/hero.game-object.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { depthsConfig } from "../configs";
33
import { HeroState } from "../helpers";
44
import { GameHelper } from "../helpers/game.helper";
55
import { ArcadeBody, ArcadeSprite } from "../phaser-aliases";
6-
import { AnimationTag, ImageTag, SfxTag, SpritesheetTag } from "../tags";
6+
import { AnimationTag, HeroEventTag, ImageTag, SfxTag, SpritesheetTag } from "../tags";
77

88
export class Hero extends ArcadeSprite {
99
public declare body: ArcadeBody;
@@ -30,6 +30,9 @@ export class Hero extends ArcadeSprite {
3030
public get hp(): number {
3131
return this._hp;
3232
}
33+
public get maximumHp(): number {
34+
return this._maximumHp;
35+
}
3336

3437
private _invincibilityWindow = 1000;
3538
private _isInvincible = false;
@@ -134,7 +137,8 @@ export class Hero extends ArcadeSprite {
134137
});
135138

136139
this._hp -= damage;
137-
console.log("aie");
140+
141+
this.scene.events.emit(HeroEventTag.HURT);
138142

139143
if (this._hp <= 0) {
140144
this.scene.sound.play(SfxTag.HERO_DIE);

src/scenes/game.scene.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Scene } from "phaser";
22
import { AssetsConfig } from "../configs";
33
import { Hero } from "../game-objects";
44
import { ForestLevel } from "../levels";
5-
import { SceneTag } from "../tags";
5+
import { AnimationTag, HeroEventTag, ImageTag, SceneTag, SpritesheetTag } from "../tags";
66

77
export class Game extends Scene {
88
private _hero: Hero | undefined;
@@ -46,7 +46,11 @@ export class Game extends Scene {
4646

4747
this._hero = new Hero(this);
4848
this._hero.on("destroy", () => this.scene.start(SceneTag.GAME_OVER));
49+
50+
this.events.on(HeroEventTag.HURT, () => {});
4951
this._currentLevel = new ForestLevel(this._hero, this);
52+
53+
this.createHeartsAnimation();
5054
}
5155

5256
/**
@@ -72,6 +76,7 @@ export class Game extends Scene {
7276

7377
this._hero?.update(time, delta);
7478
this._currentLevel?.update();
79+
this.updateHearts();
7580
}
7681

7782
private createDebug(): void {
@@ -93,4 +98,35 @@ export class Game extends Scene {
9398
this._hero.heroState.set({ action: "MOVING-RIGHT" });
9499
}
95100
}
101+
102+
private updateHearts(): void {
103+
if (!this._hero) {
104+
return;
105+
}
106+
107+
const { hp, maximumHp } = this._hero;
108+
109+
this.drawHearts(maximumHp, ImageTag.HEART_BACKGROUND);
110+
this.drawHearts(maximumHp, ImageTag.HEART_BORDER);
111+
this.drawHearts(hp, ImageTag.HEART);
112+
}
113+
114+
private drawHearts(loop: number, imageTag: ImageTag): void {
115+
for (let i = 0; i < loop; i++) {
116+
const heart = this.add.sprite(16 + 18 * i, 16, imageTag);
117+
heart.setOrigin(0, 0);
118+
heart.setScrollFactor(0);
119+
}
120+
}
121+
122+
private createHeartsAnimation(): void {
123+
this.anims.create({
124+
key: AnimationTag.HEART_CHANGE,
125+
frames: this.anims.generateFrameNumbers(SpritesheetTag.HEART, {
126+
start: 0,
127+
end: 4,
128+
}),
129+
frameRate: 8,
130+
});
131+
}
96132
}

src/tags/animation.tag.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ export enum AnimationTag {
1111
ENNEMY_DEATH = "ennemy-death_animation",
1212
ENNEMY_ATTACK = "ennemy-attack_animation",
1313
ENNEMY_ATTACK_HITBOX = "ennemy-attack-hitbox_animation",
14+
HEART_CHANGE = "heart-change_animation",
1415
}

src/tags/hero-event.tag.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export enum HeroEventTag {
2+
HURT = "hero-hurt_event_tag",
3+
}

src/tags/image.tag.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@ export enum ImageTag {
44
FOREST_BACKGROUND_DAY_3 = "forest-background-day-3_image_tag",
55
FOREST_BACKGROUND_DAY_4 = "forest-background-day-4_image_tag",
66
PROJECTILE_ARROW = "projectile-arrow_image_tag",
7+
HEART = "heart_image_tag",
8+
HEART_BORDER = "heart-border_image_tag",
9+
HEART_BACKGROUND = "heart-background_image_tag",
710
}

0 commit comments

Comments
 (0)