Skip to content

Commit 279311d

Browse files
author
Stanley Seow
committed
Merge branch 'gh-pages' of https://github.com/stanleyseow/js into gh-pages
2 parents aa56d54 + a416f67 commit 279311d

1 file changed

Lines changed: 90 additions & 29 deletions

File tree

phaser/animation-weapon-slash/animationScene.js

Lines changed: 90 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,22 @@ class animationScene extends Phaser.Scene {
4545

4646
// Player setup
4747
this.player = this.physics.add.sprite(100, 250, "gen128");
48+
this.player.hp = 100; // Starting health
4849

4950
// Initialize state variables
5051
this.player.facing = "down";
5152
this.isAttacking = false;
5253
this.lastFired = 0;
5354

55+
// Add Health Text
56+
this.hpText = this.add.text(16, 16, "HP: 100", {
57+
fontSize: "24px",
58+
fill: "#fff",
59+
stroke: "#000",
60+
strokeThickness: 4,
61+
});
62+
this.hpText.setScrollFactor(0); // Keeps text fixed on screen if camera moves
63+
5464
// Set initial walking hitbox
5565
this.resetPlayerBody();
5666

@@ -89,6 +99,14 @@ class animationScene extends Phaser.Scene {
8999
null,
90100
this,
91101
);
102+
103+
this.physics.add.overlap(
104+
this.player,
105+
[this.npc1, this.npc2],
106+
this.hitNPC,
107+
null,
108+
this,
109+
);
92110
}
93111

94112
// Helper to keep body size consistent for walking
@@ -106,15 +124,19 @@ class animationScene extends Phaser.Scene {
106124
return;
107125
}
108126
// Only move knife1 if it hasn't hit the player
109-
if (this.knife1.isActive) {
127+
// Inside update(time)
128+
if (this.knife1.isActive && this.npc1.active) {
110129
this.angle1 = Phaser.Math.Angle.BetweenPoints(this.npc1, this.player);
111130
this.physics.velocityFromRotation(this.angle1, 300, this.knife1.body.velocity);
131+
} else if (!this.npc1.active) {
132+
this.knife1.isActive = false; // Stop knife logic if NPC is dead
112133
}
113134

114-
// Only move knife2 if it hasn't hit the player
115-
if (this.knife2.isActive) {
135+
if (this.knife2.isActive && this.npc2.active) {
116136
this.angle2 = Phaser.Math.Angle.BetweenPoints(this.npc2, this.player);
117137
this.physics.velocityFromRotation(this.angle2, 300, this.knife2.body.velocity);
138+
} else if (!this.npc2.active) {
139+
this.knife2.isActive = false;
118140
}
119141

120142
// Movement Logic
@@ -145,26 +167,51 @@ class animationScene extends Phaser.Scene {
145167
}
146168
}
147169

170+
reduceHP(amount) {
171+
this.player.hp -= amount;
172+
173+
// Prevent HP from going below 0
174+
if (this.player.hp < 0) this.player.hp = 0;
175+
176+
// Update Display
177+
this.hpText.setText(`HP: ${this.player.hp}`);
178+
179+
// Game Over Check
180+
if (this.player.hp <= 0) {
181+
this.physics.pause(); // Stop all movement
182+
this.player.setTint(0xff0000);
183+
this.add
184+
.text(400, 300, "GAME OVER", { fontSize: "64px", fill: "#ff0000" })
185+
.setOrigin(0.5);
186+
187+
// Restart after 2 seconds
188+
this.time.delayedCall(5000, () => {
189+
this.scene.restart();
190+
});
191+
}
192+
}
193+
148194
shootKnife() {
149-
// 1. CRITICAL: Cancel any pending reset timers so they don't
150-
// hide the knife we are about to shoot.
151195
if (this.resetTimer) {
152196
this.resetTimer.remove();
153197
}
154198

155-
// 2. Prepare Knife 1
156-
this.knife1.enableBody(true, this.npc1.x, this.npc1.y, true, true);
157-
this.knife1.isActive = true;
158-
this.knife1.setVisible(true).setAlpha(1).clearTint();
159-
this.knife1.play("knifeAnim", true);
199+
// Only shoot Knife 1 if NPC1 is still alive
200+
if (this.npc1.active) {
201+
this.knife1.enableBody(true, this.npc1.x, this.npc1.y, true, true);
202+
this.knife1.isActive = true;
203+
this.knife1.setVisible(true).setAlpha(1).clearTint();
204+
this.knife1.play("knifeAnim", true);
205+
}
160206

161-
// 3. Prepare Knife 2
162-
this.knife2.enableBody(true, this.npc2.x, this.npc2.y, true, true);
163-
this.knife2.isActive = true;
164-
this.knife2.setVisible(true).setAlpha(1).clearTint();
165-
this.knife2.play("knifeAnim", true);
207+
// Only shoot Knife 2 if NPC2 is still alive
208+
if (this.npc2.active) {
209+
this.knife2.enableBody(true, this.npc2.x, this.npc2.y, true, true);
210+
this.knife2.isActive = true;
211+
this.knife2.setVisible(true).setAlpha(1).clearTint();
212+
this.knife2.play("knifeAnim", true);
213+
}
166214

167-
// 4. Store the timer in a variable so we can cancel it next time
168215
this.resetTimer = this.time.delayedCall(2500, this.resetKnife, [], this);
169216
}
170217

@@ -181,27 +228,41 @@ class animationScene extends Phaser.Scene {
181228
}
182229

183230
hitPlayer(player, knife) {
184-
// If the player is currently in the middle of a slash animation
185231
if (this.isAttacking) {
186-
// OPTIONAL: Play a "clink" sound or show a spark here
187-
188-
// Deflect the knife:
189-
// We disable it immediately so it doesn't trigger again
190232
knife.isActive = false;
191-
knife.disableBody(true, true); // Hide it immediately
192-
233+
knife.disableBody(true, true);
193234
console.log("Attack parried!");
194-
return; // Exit the function so the player doesn't get hurt
235+
return;
195236
}
196237

197-
// --- Normal Hit Logic (when not attacking) ---
238+
// --- Player takes damage ---
239+
this.reduceHP(10); // Take 10 damage
240+
this.cameras.main.shake(200, 0.02);
241+
198242
knife.isActive = false;
199243
knife.setVelocity(0);
200-
knife.disableBody(true, true); // Keep it visible but stuck
201-
knife.setTint(0xff0000);
244+
knife.disableBody(true, true);
245+
console.log("Player hit! HP remaining: " + this.player.hp);
246+
}
247+
248+
hitNPC(player, npc) {
249+
if (this.isAttacking) {
250+
npc.disableBody(true, true);
251+
console.log("Player Attack NPC!!!");
252+
253+
if (!this.npc1.active && !this.npc2.active) {
254+
this.timer1.remove();
255+
}
256+
return;
257+
}
258+
259+
// --- Player touches NPC without attacking ---
260+
this.reduceHP(5); // Touching an enemy hurts!
261+
this.cameras.main.shake(200, 0.01);
202262

203-
// Optional: Add player knockback or health reduction here
204-
console.log("Player hit!");
263+
// Optional: Add a brief invincibility tint so HP doesn't drain instantly
264+
this.player.setTint(0xff0000);
265+
this.time.delayedCall(200, () => this.player.clearTint());
205266
}
206267

207268
attackAction(time) {

0 commit comments

Comments
 (0)