Update scoreboard
This commit is contained in:
parent
89c3d822c6
commit
b4c19ed6e0
4 changed files with 49 additions and 5 deletions
|
@ -49,7 +49,12 @@ export class LevelManager {
|
||||||
// self.getFirstUncompleted();
|
// self.getFirstUncompleted();
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
// This function is called when the player completes a level
|
||||||
|
// It checks if all levels are completed and calls the winFunction
|
||||||
|
// If not, it selects the next level
|
||||||
next(ctx, gameState) {
|
next(ctx, gameState) {
|
||||||
|
let score = gameState.timer.getTime();
|
||||||
|
gameState.scoreboard.updateScoreCurrentGamer(score);
|
||||||
self.Completed[self.CurrentLevelId] = true;
|
self.Completed[self.CurrentLevelId] = true;
|
||||||
let allLevelsFinished = self.Completed.reduce((a, b) => {
|
let allLevelsFinished = self.Completed.reduce((a, b) => {
|
||||||
return a && b;
|
return a && b;
|
||||||
|
|
|
@ -1,23 +1,42 @@
|
||||||
|
/**
|
||||||
|
* @fileoverview Scoreboard module.
|
||||||
|
* This module is responsible for the scoreboard.
|
||||||
|
* @class Scoreboard
|
||||||
|
* @class Gamer
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @class Scoreboard
|
||||||
|
* @classdesc This class is responsible for the scoreboard.
|
||||||
|
*/
|
||||||
export class Scoreboard {
|
export class Scoreboard {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @constructor
|
||||||
|
* @param {Gamer} currentGamer - The current gamer.
|
||||||
|
* @param {Gamer[]} gamers - The list of gamers.
|
||||||
|
*/
|
||||||
constructor() {
|
constructor() {
|
||||||
this.currentGamer = new Gamer("user", 0);
|
this.currentGamer = new Gamer("user", 0);
|
||||||
this.gamers = [];
|
this.gamers = [];
|
||||||
this.gamers.push(this.currentGamer);
|
this.gamers.push(this.currentGamer);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description
|
||||||
|
* @param {String} name - The name of the gamer.
|
||||||
|
*
|
||||||
|
*/
|
||||||
updatedName(name) {
|
updatedName(name) {
|
||||||
let previousGamer = this.currentGamer;
|
let previousGamer = this.currentGamer;
|
||||||
if(name != this.currentGamer.name) {
|
if(name != this.currentGamer.name) {
|
||||||
console.log("updated");
|
|
||||||
this.currentGamer = new Gamer(name, 0);
|
this.currentGamer = new Gamer(name, 0);
|
||||||
this.addGamer(this.currentGamer);
|
this.addGamer(this.currentGamer);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (previousGamer != this.currentGamer && previousGamer.score == 0) {
|
if (previousGamer != this.currentGamer && previousGamer.score == 0) {
|
||||||
this.removeGamer(previousGamer);
|
this.removeGamer(previousGamer);
|
||||||
console.log("removed");
|
|
||||||
console.log(this.gamers);
|
console.log(this.gamers);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,10 +72,26 @@ export class Scoreboard {
|
||||||
return this.currentGamer;
|
return this.currentGamer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
updateScoreCurrentGamer(score) {
|
||||||
|
let newScore = this.currentGamer.score + score;
|
||||||
|
this.currentGamer.updateScore(newScore);
|
||||||
|
// Update score of the gamer in the table gamers
|
||||||
|
this.gamers.forEach((gamer, index) => {
|
||||||
|
if (gamer.name == this.currentGamer.name) {
|
||||||
|
gamer.updateScore(newScore);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
this.renderArray();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @class Gamer
|
||||||
|
* @classdesc This class is responsible for the gamer.
|
||||||
|
*/
|
||||||
export class Gamer {
|
export class Gamer {
|
||||||
constructor(name, score) {
|
constructor(name, score) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
|
|
|
@ -60,6 +60,10 @@ export class Timer {
|
||||||
self.time = time;
|
self.time = time;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getTime() {
|
||||||
|
return self.time;
|
||||||
|
}
|
||||||
|
|
||||||
start() {
|
start() {
|
||||||
self.timeRunning = true;
|
self.timeRunning = true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,6 +31,7 @@ let gameState = {
|
||||||
gameState.timer.stop();
|
gameState.timer.stop();
|
||||||
} ),
|
} ),
|
||||||
levelId: 0,
|
levelId: 0,
|
||||||
|
scoreboard: new Scoreboard(),
|
||||||
tutorial: new TutorialControler(),
|
tutorial: new TutorialControler(),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -82,7 +83,6 @@ window.addEventListener("keydown", (event) => {
|
||||||
// let joueur = new Gamer("user", 0);
|
// let joueur = new Gamer("user", 0);
|
||||||
// joueur.Scoreboard();
|
// joueur.Scoreboard();
|
||||||
|
|
||||||
let scoreboard = new Scoreboard();
|
|
||||||
let inputName = document.getElementById("name");
|
let inputName = document.getElementById("name");
|
||||||
inputName.addEventListener("input", (event) => {
|
inputName.addEventListener("input", (event) => {
|
||||||
console.log(event.target.value);
|
console.log(event.target.value);
|
||||||
|
@ -92,7 +92,7 @@ inputName.addEventListener("input", (event) => {
|
||||||
let submitButton = document.getElementById("submit-name");
|
let submitButton = document.getElementById("submit-name");
|
||||||
submitButton.addEventListener("click", () => {
|
submitButton.addEventListener("click", () => {
|
||||||
let value = inputName.value;
|
let value = inputName.value;
|
||||||
scoreboard.updatedName(value);
|
gamestate.scoreboard.updatedName(value);
|
||||||
});
|
});
|
||||||
|
|
||||||
window.gamestate = gameState;
|
window.gamestate = gameState;
|
||||||
|
|
Loading…
Reference in a new issue