소스 검색

Basketball game: clarify events

customisations
alemart 8 달 전
부모
커밋
5d69774348

+ 1
- 1
demos/basketball/src/entities/ball.js 파일 보기

438
                 this._locked = true;
438
                 this._locked = true;
439
                 break;
439
                 break;
440
 
440
 
441
-            case 'restarted':
441
+            case 'gameoverdismissed':
442
                 this._locked = false;
442
                 this._locked = false;
443
                 break;
443
                 break;
444
         }
444
         }

+ 22
- 1
demos/basketball/src/entities/game-controller.js 파일 보기

26
 
26
 
27
         this._ballsLeft = NUMBER_OF_BALLS;
27
         this._ballsLeft = NUMBER_OF_BALLS;
28
         this._score = 0;
28
         this._score = 0;
29
+        this._gameStarted = false;
29
     }
30
     }
30
 
31
 
31
     /**
32
     /**
61
                     this._broadcast(new GameEvent('newball', { ballsLeft: this._ballsLeft }));
62
                     this._broadcast(new GameEvent('newball', { ballsLeft: this._ballsLeft }));
62
                 break;
63
                 break;
63
 
64
 
65
+            case 'started':
64
             case 'restarted':
66
             case 'restarted':
65
                 this._score = 0;
67
                 this._score = 0;
66
                 this._ballsLeft = NUMBER_OF_BALLS;
68
                 this._ballsLeft = NUMBER_OF_BALLS;
68
                 this._broadcast(new GameEvent('newball', { ballsLeft: this._ballsLeft }));
70
                 this._broadcast(new GameEvent('newball', { ballsLeft: this._ballsLeft }));
69
                 break;
71
                 break;
70
 
72
 
71
-            case 'targetlost':
73
+            case 'tutorialdismissed':
74
+                this._gameStarted = true;
75
+                this._broadcast(new GameEvent('started'));
76
+                break;
77
+
78
+            case 'gameoverdismissed':
72
                 this._broadcast(new GameEvent('restarted'));
79
                 this._broadcast(new GameEvent('restarted'));
73
                 break;
80
                 break;
81
+
82
+            case 'targetfound':
83
+                if(this._gameStarted)
84
+                    this._broadcast(new GameEvent('restarted'));
85
+                else
86
+                    this._broadcast(new GameEvent('awakened'));
87
+                break;
88
+
89
+            case 'targetlost':
90
+                if(this._gameStarted)
91
+                    this._broadcast(new GameEvent('paused'));
92
+                else
93
+                    this._broadcast(new GameEvent('slept'));
94
+                break;
74
         }
95
         }
75
     }
96
     }
76
 
97
 

+ 12
- 7
demos/basketball/src/entities/gui/ball-counter.js 파일 보기

78
      */
78
      */
79
     handleEvent(event)
79
     handleEvent(event)
80
     {
80
     {
81
-        if(event.type == 'newball') {
82
-            const ballsLeft = event.detail.ballsLeft;
83
-            this.control.notRenderable = !(this._id < ballsLeft);
81
+        switch(event.type) {
82
+            case 'targetfound':
83
+                this.control.isVisible = true;
84
+                break;
85
+
86
+            case 'targetlost':
87
+                this.control.isVisible = false;
88
+                break;
89
+
90
+            case 'newball':
91
+                this.control.notRenderable = !(this._id < event.detail.ballsLeft);
92
+                break;
84
         }
93
         }
85
-        else if(event.type == 'targetfound')
86
-            this.control.isVisible = true;
87
-        else if(event.type == 'targetlost')
88
-            this.control.isVisible = false;
89
     }
94
     }
90
 }
95
 }
91
 
96
 

+ 1
- 1
demos/basketball/src/entities/gui/gameover-overlay.js 파일 보기

104
 
104
 
105
         // hide the overlay when touching the screen
105
         // hide the overlay when touching the screen
106
         container.isVisible = false;
106
         container.isVisible = false;
107
-        this._broadcast(new GameEvent('restarted'));
107
+        this._broadcast(new GameEvent('gameoverdismissed'));
108
     }
108
     }
109
 
109
 
110
     /**
110
     /**

+ 9
- 4
demos/basketball/src/entities/gui/mute-button.js 파일 보기

67
      */
67
      */
68
     handleEvent(event)
68
     handleEvent(event)
69
     {
69
     {
70
-        if(event.type == 'targetfound')
71
-            this.control.isVisible = true;
72
-        else if(event.type == 'targetlost')
73
-            this.control.isVisible = false;
70
+        switch(event.type) {
71
+            case 'targetfound':
72
+                this.control.isVisible = true;
73
+                break;
74
+
75
+            case 'targetlost':
76
+                this.control.isVisible = false;
77
+                break;
78
+        }
74
     }
79
     }
75
 }
80
 }

+ 28
- 11
demos/basketball/src/entities/gui/tutorial-overlay.js 파일 보기

26
     {
26
     {
27
         super(game);
27
         super(game);
28
         this._timer = 0;
28
         this._timer = 0;
29
-        this._enabled = true;
30
     }
29
     }
31
 
30
 
32
     /**
31
     /**
76
      */
75
      */
77
     update()
76
     update()
78
     {
77
     {
78
+        const ar = this.ar;
79
         const container = this.control;
79
         const container = this.control;
80
 
80
 
81
-        // check if the tutorial is enabled / being displayed
82
-        if(!this._enabled || !container.isVisible)
81
+        // check if the tutorial is being displayed
82
+        if(!container.isVisible)
83
             return;
83
             return;
84
 
84
 
85
         // hide the overlay when touching the screen
85
         // hide the overlay when touching the screen
86
-        const ar = this.ar;
87
         if(ar.pointers.length > 0) {
86
         if(ar.pointers.length > 0) {
88
             const pointer = ar.pointers[0];
87
             const pointer = ar.pointers[0];
89
             if(pointer.phase == 'began') {
88
             if(pointer.phase == 'began') {
90
-                container.isVisible = false;
91
-                this._enabled = false;
92
-                this._broadcast(new GameEvent('started'));
89
+                this._dismiss();
93
                 return;
90
                 return;
94
             }
91
             }
95
         }
92
         }
118
     }
115
     }
119
 
116
 
120
     /**
117
     /**
118
+     * Dismiss the tutorial
119
+     * @returns {void}
120
+     */
121
+    _dismiss()
122
+    {
123
+        this.control.isVisible = false;
124
+        this._broadcast(new GameEvent('tutorialdismissed'));
125
+    }
126
+
127
+    /**
121
      * Handle an event
128
      * Handle an event
122
      * @param {GameEvent} event
129
      * @param {GameEvent} event
123
      * @returns {void}
130
      * @returns {void}
124
      */
131
      */
125
     handleEvent(event)
132
     handleEvent(event)
126
     {
133
     {
127
-        if(event.type == 'targetfound')
128
-            this.control.isVisible = this._enabled;
129
-        else if(event.type == 'targetlost' || event.type == 'guiresized')
130
-            this.control.isVisible = false;
134
+        switch(event.type) {
135
+            case 'awakened':
136
+                this.control.isVisible = true;
137
+                break;
138
+
139
+            case 'targetlost':
140
+                this.control.isVisible = false;
141
+                break;
142
+
143
+            case 'guiresized':
144
+                if(this.control.isVisible)
145
+                    this._dismiss();
146
+                break;
147
+        }
131
     }
148
     }
132
 }
149
 }

+ 3
- 12
demos/basketball/src/entities/jukebox.js 파일 보기

25
         super(game);
25
         super(game);
26
         this._sound = new Map();
26
         this._sound = new Map();
27
         this._music = null;
27
         this._music = null;
28
-        this._gameStarted = false;
29
     }
28
     }
30
 
29
 
31
     /**
30
     /**
70
                     this._play('lose');
69
                     this._play('lose');
71
                 break;
70
                 break;
72
 
71
 
73
-            case 'restarted':
74
-                this._music.setVolume(0.5);
75
-                break;
76
-
77
             case 'started':
72
             case 'started':
78
-                this._gameStarted = true;
73
+            case 'restarted':
79
                 this._music.setVolume(0.5);
74
                 this._music.setVolume(0.5);
80
-                this._music.play();
81
-                break;
82
-
83
-            case 'targetfound':
84
-                if(this._gameStarted)
75
+                if(!this._music.isPlaying)
85
                     this._music.play();
76
                     this._music.play();
86
                 break;
77
                 break;
87
 
78
 
88
-            case 'targetlost':
79
+            case 'paused':
89
                 this._music.stop();
80
                 this._music.stop();
90
                 break;
81
                 break;
91
 
82
 

Loading…
취소
저장