@@ -72,14 +72,16 @@ export default class Scene extends THREE.Scene {
7272 Config . bodyIDs . player2ID = this . playerTwo . getBody ( ) . id ;
7373 this . resetRacketsPosition ( ) ;
7474
75- // Player stuff -------- CHECK THIS
75+ // Match status
76+ this . pausedGame = true ;
7677 this . lastPlayerCollided = this . lastHalfOfCourtCollided = Config . playerOne . playerOneLabel ;
7778 this . deuce = false ;
7879 this . served = false ;
7980 this . lastPlayerCollided === Config . playerOne . playerOneLabel ?
8081 this . playerOne . serving = true : this . playerTwo . serving = true ;
8182
82- this . setsForMatch = 1 ;
83+ this . gamesPerSet = 1 ;
84+ this . setsPerMatch = 1 ;
8385
8486 // Collisions work correctly without contact materials, but there aren't any bounces.
8587 // Contact material between the ball and the ground
@@ -148,7 +150,6 @@ export default class Scene extends THREE.Scene {
148150 self . lastHalfOfCourtCollided = Config . playerOne . playerOneLabel ;
149151 this . numBounces ++ ;
150152 self . checkGameState ( ) ;
151- self . checkSetState ( ) ;
152153 break ;
153154 case Config . bodyIDs . netID :
154155 break ;
@@ -198,23 +199,53 @@ export default class Scene extends THREE.Scene {
198199 }
199200
200201 /**
201- * It checks the score from 0 to 6+ games in a single set
202+ * It checks the score from 0 to maximum of games in a single set
202203 */
203204 checkSetState ( ) {
204-
205205 // Check whether player one or two has scored the current set
206- if ( this . playerOne . currentGames >= 6 && ( this . playerOne . currentGames - this . playerTwo . currentGames ) >= 2 ) {
206+ if ( this . playerOne . currentGames >= this . gamesPerSet && ( this . playerOne . currentGames - this . playerTwo . currentGames ) >= 2 ) {
207207 this . playerOne . incrementSets ( ) ;
208208 this . playerTwo . resetCurrentGames ( ) ;
209209 this . playerTwo . resetCurrentPoints ( ) ;
210210 }
211-
212- if ( this . playerTwo . currentGames >= 6 && ( this . playerTwo . currentGames - this . playerOne . currentGames ) >= 2 ) {
211+ if ( this . playerTwo . currentGames >= this . gamesPerSet && ( this . playerTwo . currentGames - this . playerOne . currentGames ) >= 2 ) {
213212 this . playerTwo . incrementSets ( ) ;
214213 this . playerOne . resetCurrentGames ( ) ;
215214 this . playerOne . resetCurrentPoints ( ) ;
216215 }
216+ this . checkMatchState ( ) ;
217+ }
218+
219+ /**
220+ * It checks the score from 0 to maximum of sets in a single match
221+ */
222+ checkMatchState ( ) {
223+ if ( this . playerOne . currentSets >= this . setsPerMatch )
224+ this . endedMatch ( this . playerOne ) ;
225+ else if ( this . playerTwo . currentSets >= this . setsPerMatch )
226+ this . endedMatch ( this . playerTwo ) ;
227+ }
217228
229+ /**
230+ * Triggered when a player wins the game. It allows to play again
231+ */
232+ endedMatch ( player ) {
233+ var playerNumber ;
234+ if ( player === this . playerOne )
235+ playerNumber = '1' ;
236+ else if ( player === this . playerTwo )
237+ playerNumber = '2' ;
238+ this . pausedGame = true ;
239+
240+ var endDiv = document . getElementById ( 'endOfTheGameDiv' ) ;
241+ endDiv . style . display = 'initial' ;
242+ endDiv . innerText = 'The winner is the player ' + playerNumber + '!' ;
243+ var reloadButton = document . createElement ( 'button' ) ;
244+ reloadButton . innerText = 'Play again' ;
245+ reloadButton . onclick = function ( ) { window . location . reload ( ) ; } ;
246+ endDiv . appendChild ( document . createElement ( 'br' ) ) ;
247+ endDiv . appendChild ( document . createElement ( 'br' ) ) ;
248+ endDiv . appendChild ( reloadButton ) ;
218249 }
219250
220251 /**
@@ -265,6 +296,7 @@ export default class Scene extends THREE.Scene {
265296 this . playerOne . resetCurrentPoints ( ) ;
266297 }
267298 }
299+ this . checkSetState ( ) ;
268300
269301 // Update the score on GUI
270302 this . updateScore ( ) ;
@@ -288,11 +320,11 @@ export default class Scene extends THREE.Scene {
288320 var textoP1 , textoP2 ;
289321 textoP1 = ( this . playerOne . currentPoints === 40 && this . playerOne . advantage > this . playerTwo . advantage ) ? 'A' : this . playerOne . currentPoints ;
290322 textoP2 = ( this . playerTwo . currentPoints === 40 && this . playerTwo . advantage > this . playerOne . advantage ) ? 'A' : this . playerTwo . currentPoints ;
291- document . getElementById ( 'scoreboard' ) . innerText = ( textoP1 + ' - ' + textoP2 ) ;
292-
323+
293324 // Text for each player's global Sets-Games status
294- document . getElementById ( 'scoreboardPlayer1' ) . innerText = ( this . playerOne . currentSets + ' - ' + this . playerOne . currentGames ) ;
295- document . getElementById ( 'scoreboardPlayer2' ) . innerText = ( this . playerTwo . currentSets + ' - ' + this . playerTwo . currentGames ) ;
325+ document . getElementById ( 'pointsText' ) . innerText = ( textoP1 + ' - ' + textoP2 ) ;
326+ document . getElementById ( 'gamesText' ) . innerText = 'Games: ' + this . playerOne . currentGames + ' - ' + this . playerTwo . currentGames ;
327+ document . getElementById ( 'setsText' ) . innerText = 'Sets: ' + this . playerOne . currentSets + ' - ' + this . playerTwo . currentSets ;
296328
297329 this . updateServingPlayer ( ) ;
298330 }
0 commit comments