Update scoreboard

This commit is contained in:
Ronan 2023-01-06 14:56:16 +01:00
parent 89c3d822c6
commit b4c19ed6e0
4 changed files with 49 additions and 5 deletions

View file

@ -49,7 +49,12 @@ export class LevelManager {
// 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) {
let score = gameState.timer.getTime();
gameState.scoreboard.updateScoreCurrentGamer(score);
self.Completed[self.CurrentLevelId] = true;
let allLevelsFinished = self.Completed.reduce((a, b) => {
return a && b;

View file

@ -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 {
/**
* @constructor
* @param {Gamer} currentGamer - The current gamer.
* @param {Gamer[]} gamers - The list of gamers.
*/
constructor() {
this.currentGamer = new Gamer("user", 0);
this.gamers = [];
this.gamers.push(this.currentGamer);
}
/**
* @description
* @param {String} name - The name of the gamer.
*
*/
updatedName(name) {
let previousGamer = this.currentGamer;
if(name != this.currentGamer.name) {
console.log("updated");
this.currentGamer = new Gamer(name, 0);
this.addGamer(this.currentGamer);
}
if (previousGamer != this.currentGamer && previousGamer.score == 0) {
this.removeGamer(previousGamer);
console.log("removed");
console.log(this.gamers);
}
@ -53,10 +72,26 @@ export class Scoreboard {
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 {
constructor(name, score) {
this.name = name;

View file

@ -60,6 +60,10 @@ export class Timer {
self.time = time;
}
getTime() {
return self.time;
}
start() {
self.timeRunning = true;
}

View file

@ -31,6 +31,7 @@ let gameState = {
gameState.timer.stop();
} ),
levelId: 0,
scoreboard: new Scoreboard(),
tutorial: new TutorialControler(),
};
@ -82,7 +83,6 @@ window.addEventListener("keydown", (event) => {
// let joueur = new Gamer("user", 0);
// joueur.Scoreboard();
let scoreboard = new Scoreboard();
let inputName = document.getElementById("name");
inputName.addEventListener("input", (event) => {
console.log(event.target.value);
@ -92,7 +92,7 @@ inputName.addEventListener("input", (event) => {
let submitButton = document.getElementById("submit-name");
submitButton.addEventListener("click", () => {
let value = inputName.value;
scoreboard.updatedName(value);
gamestate.scoreboard.updatedName(value);
});
window.gamestate = gameState;