@@ -13,6 +13,9 @@ var scene, renderer, debugRenderer;
1313var playerOneCamera , playerOneControls ;
1414var playerTwoCamera , playerTwoControls ;
1515
16+ // It helps with the pause/end of the game
17+ var pausedGame = false ;
18+
1619//// Methods to run on load
1720// Init world, players, parameters and all that stuff
1821init ( ) ;
@@ -51,12 +54,8 @@ function init(){
5154 // The scene with the court, players & stuff
5255 scene = new Scene ( ) ;
5356
54- // If necessary, we can preview the CANNON meshes
55- //debugRenderer = new THREE.CannonDebugRenderer(scene, scene.world);
56-
5757 // Dispose scoreboard and keys helpers
58- createUIElements ( ) ;
59- scene . updateServingPlayer ( ) ;
58+ initializeGameParameters ( ) ;
6059
6160 // Views
6261 createCameras ( ) ;
@@ -72,11 +71,13 @@ function animate(){
7271 // Load this method in every frame
7372 requestAnimationFrame ( animate ) ;
7473
75- // Update physics world, stepping CANNON world and waiting for collisions
76- scene . updateMeshPosition ( ) ;
74+ if ( ! pausedGame ) {
75+ // Update physics world, stepping CANNON world and waiting for collisions
76+ scene . updateMeshPosition ( ) ;
7777
78- // Reload cameras and points of view
79- updateCameras ( ) ;
78+ // Reload cameras and points of view
79+ updateCameras ( ) ;
80+ }
8081}
8182
8283/**
@@ -108,14 +109,18 @@ function updateCameras(){
108109 * Event triggered when a key starts being pressed
109110 */
110111function computeKeyDown ( event ) {
111- scene . computeKeyDown ( event ) ;
112+ if ( event . code === "Space" )
113+ pausedGame = ! pausedGame ;
114+ else if ( ! pausedGame )
115+ scene . computeKeyDown ( event ) ;
112116}
113117
114118/**
115119 * Event triggered when a key stops being pressed
116120 */
117121function computeKeyUp ( event ) {
118- scene . computeKeyUp ( event ) ;
122+ if ( ! pausedGame )
123+ scene . computeKeyUp ( event ) ;
119124}
120125
121126/**
@@ -139,12 +144,98 @@ function linkHTMLDependencies() {
139144}
140145
141146/**
142- * It draws HTML elements on the screen to inform about the score and the controls for each player
147+ * It displays a container used to define the parameters of the game (such as games, sets, etc)
143148 */
144- function createUIElements ( ) {
149+ function initializeGameParameters ( ) {
145150 // Link needed external dependencies (fonts, etc.)
146151 linkHTMLDependencies ( ) ;
147152
153+ var matchOptions = document . createElement ( 'div' ) ;
154+ matchOptions . setAttribute ( 'id' , 'matchOptions' ) ;
155+ matchOptions . style . width = '30vw' ;
156+ matchOptions . style . height = '30vh' ;
157+ matchOptions . style . marginLeft = '30vw' ;
158+ matchOptions . style . marginTop = '25vh' ;
159+ matchOptions . style . backgroundColor = '#404040' ;
160+ matchOptions . style . boxShadow = '0px 0px 30px 5px rgba(48,48,48,1)' ;
161+ matchOptions . style . fontFamily = 'sans-serif' ;
162+ matchOptions . style . fontSize = '110%' ;
163+ matchOptions . style . color = 'white' ;
164+ matchOptions . style . position = 'absolute' ;
165+ matchOptions . style . textAlign = 'center' ;
166+ matchOptions . style . paddingTop = matchOptions . style . paddingBottom = '5vh' ;
167+ matchOptions . style . paddingLeft = matchOptions . style . paddingRight = '5vw' ;
168+
169+ var gamesAndSetsForm = document . createElement ( 'div' ) ;
170+ gamesAndSetsForm . appendChild ( document . createTextNode ( 'Choose the number of games per set:' ) ) ;
171+ gamesAndSetsForm . appendChild ( document . createElement ( 'br' ) ) ;
172+ gamesAndSetsForm . appendChild ( document . createElement ( 'br' ) ) ;
173+ var radioGames1 = document . createElement ( 'input' ) ;
174+ radioGames1 . setAttribute ( 'type' , 'radio' ) ;
175+ radioGames1 . setAttribute ( 'name' , 'games' ) ;
176+ radioGames1 . setAttribute ( 'value' , 'one' ) ;
177+ radioGames1 . checked = true ;
178+ gamesAndSetsForm . appendChild ( document . createTextNode ( '1 ' ) ) ;
179+ gamesAndSetsForm . appendChild ( radioGames1 ) ;
180+ gamesAndSetsForm . appendChild ( document . createElement ( 'br' ) ) ;
181+ var radioGames3 = document . createElement ( 'input' ) ;
182+ radioGames3 . setAttribute ( 'type' , 'radio' ) ;
183+ radioGames3 . setAttribute ( 'name' , 'games' ) ;
184+ radioGames3 . setAttribute ( 'value' , 'three' ) ;
185+ gamesAndSetsForm . appendChild ( document . createTextNode ( '3 ' ) ) ;
186+ gamesAndSetsForm . appendChild ( radioGames3 ) ;
187+ gamesAndSetsForm . appendChild ( document . createElement ( 'br' ) ) ;
188+ var radioGames5 = document . createElement ( 'input' ) ;
189+ radioGames5 . setAttribute ( 'type' , 'radio' ) ;
190+ radioGames5 . setAttribute ( 'name' , 'games' ) ;
191+ radioGames5 . setAttribute ( 'value' , 'five' ) ;
192+ gamesAndSetsForm . appendChild ( document . createTextNode ( '5 ' ) ) ;
193+ gamesAndSetsForm . appendChild ( radioGames5 ) ;
194+ gamesAndSetsForm . appendChild ( document . createElement ( 'br' ) ) ;
195+
196+ gamesAndSetsForm . appendChild ( document . createElement ( 'br' ) ) ;
197+ gamesAndSetsForm . appendChild ( document . createElement ( 'br' ) ) ;
198+ gamesAndSetsForm . appendChild ( document . createTextNode ( 'Choose the number of sets per match:' ) ) ;
199+ gamesAndSetsForm . appendChild ( document . createElement ( 'br' ) ) ;
200+ gamesAndSetsForm . appendChild ( document . createElement ( 'br' ) ) ;
201+ var radioSets1 = document . createElement ( 'input' ) ;
202+ radioSets1 . setAttribute ( 'type' , 'radio' ) ;
203+ radioSets1 . setAttribute ( 'name' , 'sets' ) ;
204+ radioSets1 . setAttribute ( 'value' , 'one' ) ;
205+ radioSets1 . checked = true ;
206+ gamesAndSetsForm . appendChild ( document . createTextNode ( '1 ' ) ) ;
207+ gamesAndSetsForm . appendChild ( radioSets1 ) ;
208+ gamesAndSetsForm . appendChild ( document . createElement ( 'br' ) ) ;
209+ var radioSets3 = document . createElement ( 'input' ) ;
210+ radioSets3 . setAttribute ( 'type' , 'radio' ) ;
211+ radioSets3 . setAttribute ( 'name' , 'sets' ) ;
212+ radioSets3 . setAttribute ( 'value' , 'three' ) ;
213+ gamesAndSetsForm . appendChild ( document . createTextNode ( '3 ' ) ) ;
214+ gamesAndSetsForm . appendChild ( radioSets3 ) ;
215+ gamesAndSetsForm . appendChild ( document . createElement ( 'br' ) ) ;
216+ var radioSets5 = document . createElement ( 'input' ) ;
217+ radioSets5 . setAttribute ( 'type' , 'radio' ) ;
218+ radioSets5 . setAttribute ( 'name' , 'sets' ) ;
219+ radioSets5 . setAttribute ( 'value' , 'five' ) ;
220+ gamesAndSetsForm . appendChild ( document . createTextNode ( '5 ' ) ) ;
221+ gamesAndSetsForm . appendChild ( radioSets5 ) ;
222+ gamesAndSetsForm . appendChild ( document . createElement ( 'br' ) ) ;
223+ gamesAndSetsForm . appendChild ( document . createElement ( 'br' ) ) ;
224+ var submitButton = document . createElement ( 'button' ) ;
225+ submitButton . onclick = function ( ) { createUIElements ( ) ; } ;
226+ gamesAndSetsForm . appendChild ( submitButton ) ;
227+
228+ matchOptions . appendChild ( gamesAndSetsForm ) ;
229+ document . body . appendChild ( matchOptions ) ;
230+ }
231+
232+ /**
233+ * It draws HTML elements on the screen to inform about the score and the controls for each player
234+ */
235+ function createUIElements ( ) {
236+ // Hide parameters chooser dialog
237+ document . getElementById ( 'matchOptions' ) . style . display = 'none' ;
238+
148239 // Create scoreboard
149240 var scoreboard = document . createElement ( 'div' ) ;
150241 scoreboard . setAttribute ( 'id' , 'scoreboard' ) ;
@@ -183,7 +274,6 @@ function createUIElements() {
183274 player1helper . appendChild ( player1helperText ) ;
184275 document . body . appendChild ( player1helper ) ;
185276
186-
187277 // Player 2
188278 var player2helper = document . createElement ( 'div' ) ;
189279 player2helper . setAttribute ( 'id' , 'player2helper' ) ;
@@ -238,7 +328,6 @@ function createUIElements() {
238328 scoreboardPlayer2 . appendChild ( textPlayer2 ) ;
239329 document . body . appendChild ( scoreboardPlayer2 ) ;
240330
241-
242331 // Create serve indicators for both players
243332 var player1indicator = document . createElement ( 'div' ) ;
244333 player1indicator . setAttribute ( 'id' , 'player1indicator' ) ;
@@ -264,7 +353,7 @@ function createUIElements() {
264353 player2indicator . style . backgroundColor = 'rgba(20, 20, 20, 0.5)' ;
265354 document . body . appendChild ( player2indicator ) ;
266355
267-
356+ scene . updateServingPlayer ( ) ;
268357}
269358
270359// When ready, load these things
0 commit comments