Browse Source

Basketball game: clarify events

customisations
alemart 8 months ago
parent
commit
5d69774348

+ 1
- 1
demos/basketball/src/entities/ball.js View File

@@ -438,7 +438,7 @@ export class Ball extends Entity
438 438
                 this._locked = true;
439 439
                 break;
440 440
 
441
-            case 'restarted':
441
+            case 'gameoverdismissed':
442 442
                 this._locked = false;
443 443
                 break;
444 444
         }

+ 22
- 1
demos/basketball/src/entities/game-controller.js View File

@@ -26,6 +26,7 @@ export class GameController extends Entity
26 26
 
27 27
         this._ballsLeft = NUMBER_OF_BALLS;
28 28
         this._score = 0;
29
+        this._gameStarted = false;
29 30
     }
30 31
 
31 32
     /**
@@ -61,6 +62,7 @@ export class GameController extends Entity
61 62
                     this._broadcast(new GameEvent('newball', { ballsLeft: this._ballsLeft }));
62 63
                 break;
63 64
 
65
+            case 'started':
64 66
             case 'restarted':
65 67
                 this._score = 0;
66 68
                 this._ballsLeft = NUMBER_OF_BALLS;
@@ -68,9 +70,28 @@ export class GameController extends Entity
68 70
                 this._broadcast(new GameEvent('newball', { ballsLeft: this._ballsLeft }));
69 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 79
                 this._broadcast(new GameEvent('restarted'));
73 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 View File

@@ -78,14 +78,19 @@ class BallIcon extends GUIControl
78 78
      */
79 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 View File

@@ -104,7 +104,7 @@ export class GameOverOverlay extends GUIControl
104 104
 
105 105
         // hide the overlay when touching the screen
106 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 View File

@@ -67,9 +67,14 @@ export class MuteButton extends GUIControl
67 67
      */
68 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 View File

@@ -26,7 +26,6 @@ export class TutorialOverlay extends GUIControl
26 26
     {
27 27
         super(game);
28 28
         this._timer = 0;
29
-        this._enabled = true;
30 29
     }
31 30
 
32 31
     /**
@@ -76,20 +75,18 @@ export class TutorialOverlay extends GUIControl
76 75
      */
77 76
     update()
78 77
     {
78
+        const ar = this.ar;
79 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 83
             return;
84 84
 
85 85
         // hide the overlay when touching the screen
86
-        const ar = this.ar;
87 86
         if(ar.pointers.length > 0) {
88 87
             const pointer = ar.pointers[0];
89 88
             if(pointer.phase == 'began') {
90
-                container.isVisible = false;
91
-                this._enabled = false;
92
-                this._broadcast(new GameEvent('started'));
89
+                this._dismiss();
93 90
                 return;
94 91
             }
95 92
         }
@@ -118,15 +115,35 @@ export class TutorialOverlay extends GUIControl
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 128
      * Handle an event
122 129
      * @param {GameEvent} event
123 130
      * @returns {void}
124 131
      */
125 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 View File

@@ -25,7 +25,6 @@ export class Jukebox extends Entity
25 25
         super(game);
26 26
         this._sound = new Map();
27 27
         this._music = null;
28
-        this._gameStarted = false;
29 28
     }
30 29
 
31 30
     /**
@@ -70,22 +69,14 @@ export class Jukebox extends Entity
70 69
                     this._play('lose');
71 70
                 break;
72 71
 
73
-            case 'restarted':
74
-                this._music.setVolume(0.5);
75
-                break;
76
-
77 72
             case 'started':
78
-                this._gameStarted = true;
73
+            case 'restarted':
79 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 76
                     this._music.play();
86 77
                 break;
87 78
 
88
-            case 'targetlost':
79
+            case 'paused':
89 80
                 this._music.stop();
90 81
                 break;
91 82
 

Loading…
Cancel
Save