Skip to content

Commit 4fe4045

Browse files
committed
FINISHED MVP. implement timer functionality as a method in Game class and reorganized game methods
1 parent e4851d1 commit 4fe4045

File tree

1 file changed

+82
-20
lines changed

1 file changed

+82
-20
lines changed

js/Game.js

Lines changed: 82 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,72 @@ class Game {
33
this.player = playerModel;
44
this.enemy = enemyModel;
55
this.turn = "player";
6+
7+
this.turnDuration = 15;
8+
this.timeLeft = this.turnDuration;
9+
this.timerId = null;
610

711
updateNames(this.enemy, this.player);
812
updateEnemyHp(this.enemy.health, this.enemy.maxHealth);
913
updatePlayerHp(this.player.health, this.player.maxHealth);
14+
15+
// Iniciar el primer turno
16+
this.startTurnTimer();
1017
}
1118

12-
showResult() {
13-
if (this.player.health <= 0) {
14-
setTimeout(() => {
15-
showDefeat();
16-
}, 4000);
17-
} else if (this.enemy.health <= 0) {
18-
setTimeout(() => {
19-
showVictory();
20-
}, 4000);
19+
// LOGICA DEL TIMER
20+
startTurnTimer() {
21+
22+
clearInterval(this.timerId);
23+
this.timeLeft = this.turnDuration;
24+
25+
const timerBar = document.getElementById('timer-bar');
26+
const timerText = document.getElementById('timer-text');
27+
const turnText = document.getElementById('current-turn');
28+
const turnBox = document.querySelector('.turn-box');
29+
30+
if (this.turn === "player") {
31+
turnText.innerText = "PLAYER TURN";
32+
turnBox.classList.remove('enemy-turn');
33+
} else {
34+
turnText.innerText = "ENEMY TURN";
35+
turnBox.classList.add('enemy-turn');
2136
}
37+
38+
this.timerId = setInterval(() => {
39+
this.timeLeft--;
40+
41+
if (timerText) timerText.innerText = `${this.timeLeft}s`;
42+
if (timerBar) {
43+
const percentage = (this.timeLeft / this.turnDuration) * 100;
44+
timerBar.style.width = `${percentage}%`;
45+
46+
if (this.timeLeft <= 5) timerBar.classList.add('timer-warning');
47+
else timerBar.classList.remove('timer-warning');
48+
}
49+
50+
if (this.timeLeft <= 0) {
51+
this.handleTimeout();
52+
}
53+
}, 1000);
2254
}
2355

24-
gameOver() {
25-
if (this.player.isAlive() && this.enemy.isAlive()) {
26-
return false;
56+
handleTimeout() {
57+
clearInterval(this.timerId);
58+
console.log(`Time out for ${this.turn}`);
59+
60+
if (this.turn === "player") {
61+
this.turn = "enemy";
62+
setTimeout(() => this.executeEneumyAction(), 1000);
2763
} else {
28-
console.log("GAME OVER");
29-
localStorage.clear()
30-
this.showResult();
31-
return true;
64+
this.turn = "player";
65+
this.startTurnTimer();
3266
}
3367
}
3468

69+
// ACCIONES
3570
ExcutePlayerAction(e) {
36-
if (this.turn !== "player") return;
71+
if (this.turn !== "player" || this.gameOver()) return;
3772

3873
if (e.code === "KeyQ") {
3974
this.player.attack(this.enemy);
@@ -43,32 +78,59 @@ class Game {
4378
this.player.slashAttackAnim();
4479
}
4580

81+
// Detener timer actual al actuar
82+
clearInterval(this.timerId);
4683
this.turn = "enemy";
4784
updateEnemyHp(this.enemy.health, this.enemy.maxHealth);
4885

4986
if (!this.gameOver()) {
87+
this.startTurnTimer(); // Reiniciar UI para el turno enemigo
88+
const randomDelay = Math.floor(Math.random() * (14 - 3 + 1) + 3) * 1000;
5089
setTimeout(() => {
5190
this.executeEneumyAction();
52-
}, 2000);
91+
}, randomDelay);
5392
} else {
5493
this.enemy.deathkAnimation();
94+
clearInterval(this.timerId);
5595
}
5696
}
5797

5898
executeEneumyAction() {
59-
if (this.turn !== "enemy") return;
99+
if (this.turn !== "enemy" || this.gameOver()) return;
60100

61101
this.enemy.attackAnimation();
62102

63103
setTimeout(() => {
64104
this.enemy.attack(this.player);
105+
65106
if (!this.gameOver()) {
66107
this.turn = "player";
108+
this.startTurnTimer(); // Reiniciar el timer para el jugador
67109
} else {
68110
this.player.deathkAnimation();
111+
clearInterval(this.timerId);
69112
}
70113
updatePlayerHp(this.player.health, this.player.maxHealth);
71114
}, 350);
72115
}
73116

74-
}
117+
// ESTADO DEL JUEGO
118+
gameOver() {
119+
if (this.player.isAlive() && this.enemy.isAlive()) {
120+
return false;
121+
} else {
122+
clearInterval(this.timerId);
123+
localStorage.clear();
124+
this.showResult();
125+
return true;
126+
}
127+
}
128+
129+
showResult() {
130+
if (this.player.health <= 0) {
131+
setTimeout(() => { showDefeat(); }, 4000);
132+
} else if (this.enemy.health <= 0) {
133+
setTimeout(() => { showVictory(); }, 4000);
134+
}
135+
}
136+
}

0 commit comments

Comments
 (0)