22
33goog . provide ( 'app.Game' ) ;
44
5- goog . require ( 'app.Constants' ) ;
65goog . require ( 'app.Scene' ) ;
76goog . require ( 'app.Present' ) ;
7+ goog . require ( 'app.Constants' ) ;
8+ goog . require ( 'app.Level' ) ;
89goog . require ( 'app.shared.Scoreboard' ) ;
9- goog . require ( 'app.systems.CameraSystem' ) ;
10- goog . require ( 'app.systems.ElvesSystem' ) ;
11- goog . require ( 'app.systems.RaycasterSystem' ) ;
12- goog . require ( 'app.systems.PresentSystem' ) ;
10+ goog . require ( 'app.shared.Gameover' ) ;
11+
12+ /**
13+ * Maximum time allowed between updates. If the player's computer was locked or
14+ * the window was inactive, we could potentially have a very large delta between
15+ * frames. This keeps the game somewhat paused when the user is inactive.
16+ */
17+ const maxTimeStep = 0.5 ;
1318
1419class Game {
1520
1621 constructor ( ) {
1722 this . paused = false ;
23+
24+ this . scoreboard = new app . shared . Scoreboard ( this , null , app . Constants . LEVEL_COUNT ) ;
25+ this . gameoverView = new app . shared . Gameover ( this ) ;
26+
27+ this . renderer = new THREE . WebGLRenderer ( ) ;
28+ this . renderer . setSize ( window . innerWidth , window . innerHeight ) ;
29+ this . renderer . shadowMap . enabled = true ;
30+
31+ this . level = undefined ;
32+
1833 this . previousSeconds = Date . now ( ) / 1000 ;
1934 }
2035
@@ -30,37 +45,28 @@ class Game {
3045 * game.
3146 */
3247 async start ( container ) {
33- this . renderer = new THREE . WebGLRenderer ( ) ;
34- this . renderer . setSize ( window . innerWidth , window . innerHeight ) ;
35- this . renderer . shadowMap . enabled = true ;
3648 container . appendChild ( this . renderer . domElement ) ;
3749
3850 await Promise . all ( [
3951 app . Scene . preload ( ) ,
4052 app . Present . preload ( ) ,
4153 ] ) ;
4254
43- this . placeholderScene = new app . Scene ( ) ;
44- this . camera = this . placeholderScene . getCamera ( ) ;
45- this . scoreboard = new app . shared . Scoreboard ( this , undefined , app . Constants . NUM_LEVELS ) ;
46- this . elvesSystem = new app . ElvesSystem ( this . camera , this . placeholderScene ) ;
47- this . presentSystem = new app . PresentSystem ( this . placeholderScene ) ;
48- this . raycasterSystem = new app . RaycasterSystem ( this . renderer , this . camera , this . placeholderScene , this . scoreboard ) ;
49-
5055 this . setUpListeners ( ) ;
5156 this . mainLoop ( ) ;
5257
53- window . addEventListener ( 'resize' , ( ) => {
54- this . camera . aspect = window . innerWidth / window . innerHeight ;
55- this . camera . updateProjectionMatrix ( ) ;
56- this . renderer . setSize ( window . innerWidth , window . innerHeight ) ;
57- console . log ( this . camera ) ;
58- } ) ;
58+ this . startLevel ( ) ;
59+ }
60+
61+ startLevel ( ) {
62+ if ( this . level ) {
63+ this . level . cleanUp ( ) ;
64+ }
65+ this . level = new Level ( this . renderer , ( score ) => this . scoreboard . addScore ( score ) ) ;
5966 }
6067
6168 pause ( ) {
6269 this . paused = true ;
63- this . previousSeconds = null ;
6470 }
6571
6672 resume ( ) {
@@ -70,65 +76,67 @@ class Game {
7076 }
7177 this . paused = false ;
7278 this . previousSeconds = Date . now ( ) / 1000 ;
73- this . mainLoop ( ) ;
7479 }
7580
7681 restart ( ) {
77- if ( ! this . paused ) {
78- console . warn ( 'Game must be paused before it can be restarted' ) ;
79- return ;
80- }
81- console . log ( 'TODO' ) ;
82+ this . startLevel ( ) ;
8283 this . paused = false ;
83- this . mainLoop ( ) ;
84+ this . scoreboard . restart ( )
8485 }
8586
8687 gameover ( ) {
87- console . error ( 'TODO' ) ;
88+ this . paused = true ;
89+ this . gameoverView . show ( ) ;
8890 }
8991
9092 mainLoop ( ) {
91- if ( this . paused ) {
92- return ;
93- }
9493 this . update ( ) ;
9594 this . render ( ) ;
9695
9796 requestAnimationFrame ( ( ) => this . mainLoop ( ) ) ;
9897 }
9998
10099 render ( ) {
101- this . renderer . render ( this . placeholderScene . scene , this . camera ) ;
100+ if ( this . paused ) {
101+ return ;
102+ }
103+
104+ if ( this . level ) {
105+ this . level . render ( this . renderer ) ;
106+ }
102107 }
103108
104109 /**
105110 * Handles the main logic for the game by making each system update.
106111 */
107112 update ( ) {
108113 const nowSeconds = Date . now ( ) / 1000 ;
109- const deltaSeconds = nowSeconds - this . previousSeconds ;
110- this . placeholderScene . update ( deltaSeconds ) ;
111- this . elvesSystem . update ( deltaSeconds ) ;
112- this . presentSystem . update ( deltaSeconds ) ;
113- this . scoreboard . onFrame ( deltaSeconds ) ;
114+ const deltaSeconds = Math . min ( nowSeconds - this . previousSeconds , maxTimeStep ) ;
115+
116+ if ( ! this . paused && this . level ) {
117+ this . level . update ( deltaSeconds ) ;
118+ this . scoreboard . onFrame ( deltaSeconds ) ;
119+ }
120+
114121 this . previousSeconds = nowSeconds ;
115-
116122 }
117123
118124 setUpListeners ( ) {
119- this . clickListener = this . renderer . domElement . addEventListener ( 'click' , ( click ) => {
125+ this . renderer . domElement . addEventListener ( 'click' , ( click ) => {
120126 this . handleClick ( click ) ;
121127 } ) ;
128+
129+ window . addEventListener ( 'resize' , ( ) => {
130+ this . renderer . setSize ( window . innerWidth , window . innerHeight ) ;
131+ if ( this . level ) {
132+ this . level . scene . updateCameraSize ( ) ;
133+ }
134+ } ) ;
122135 }
123136
124137 handleClick ( clickEvent ) {
125- const nowSeconds = Date . now ( ) / 1000 ;
126- const intersections = this . raycasterSystem . cast ( clickEvent ) ;
127-
128- // Test present shot
129- // const aim = this.placeholderScene.getCameraPosition(nowSeconds + 30);
130- if ( intersections . length > 0 ) {
131- this . presentSystem . shoot ( intersections [ 0 ] . point ) ;
138+ if ( ! this . paused && this . level ) {
139+ this . level . handleClick ( clickEvent ) ;
132140 }
133141 }
134142}
0 commit comments