Bladeren bron

Basketball game: introduce event "hoopready"

customisations
alemart 8 maanden geleden
bovenliggende
commit
ad1217ec1e

+ 2
- 2
demos/basketball/src/entities/ball.js Bestand weergeven

257
         else if(trigger.name == 'Trigger_B') {
257
         else if(trigger.name == 'Trigger_B') {
258
             if(this._lastTrigger == 'A')
258
             if(this._lastTrigger == 'A')
259
                 this._lastTrigger = 'B';
259
                 this._lastTrigger = 'B';
260
-            else if(this._mesh.physicsImpostor.getLinearVelocity().y > 0)
261
-                this._lastTrigger = 'X';
260
+            /*else if(this._mesh.physicsImpostor.getLinearVelocity().y > 0)
261
+                this._lastTrigger = 'X';*/
262
         }
262
         }
263
         else if(trigger.name == 'Trigger_C') {
263
         else if(trigger.name == 'Trigger_C') {
264
             if(this._lastTrigger == 'B') {
264
             if(this._lastTrigger == 'B') {

+ 26
- 0
demos/basketball/src/entities/goal.js Bestand weergeven

102
             const net = event.detail.entity;
102
             const net = event.detail.entity;
103
             net.moveBy(this._physicsRoot.position);
103
             net.moveBy(this._physicsRoot.position);
104
         }
104
         }
105
+        else if(event.type == 'hooksready') {
106
+            const hooks = event.detail.hooks;
107
+            const centerOfHooks = this._findCenterOfHooks(hooks);
108
+            const radius = BABYLON.Vector3.Distance(centerOfHooks, hooks[0].position);
109
+            const position = centerOfHooks.add(this._physicsRoot.position); // position relative to ar.root
110
+
111
+            this._broadcast(new GameEvent('hoopready', { position, radius }));
112
+        }
113
+    }
114
+
115
+    /**
116
+     * Find the geometrical center of a set of hooks
117
+     * @param {BABYLON.Mesh[]} hooks non-empty array
118
+     * @returns {BABYLON.Vector3}
119
+     */
120
+    _findCenterOfHooks(hooks)
121
+    {
122
+        const center = new BABYLON.Vector3(0, 0, 0);
123
+
124
+        for(const hook of hooks) {
125
+            center.x += hook.position.x;
126
+            center.y += hook.position.y;
127
+            center.z += hook.position.z;
128
+        }
129
+
130
+        return center.scaleInPlace(1 / hooks.length);
105
     }
131
     }
106
 }
132
 }

+ 2
- 2
demos/basketball/src/entities/net.js Bestand weergeven

194
         this._indices.length = 0;
194
         this._indices.length = 0;
195
 
195
 
196
         // the origin of the mesh will be positioned at the geometrical center of the hooks
196
         // the origin of the mesh will be positioned at the geometrical center of the hooks
197
-        const center = this._findCenter(hooks);
197
+        const center = this._findCenterOfHooks(hooks);
198
         this._physicsRoot.position.copyFrom(center);
198
         this._physicsRoot.position.copyFrom(center);
199
 
199
 
200
         // setup the top ring
200
         // setup the top ring
434
      * @param {BABYLON.Mesh[]} hooks non-empty array
434
      * @param {BABYLON.Mesh[]} hooks non-empty array
435
      * @returns {BABYLON.Vector3}
435
      * @returns {BABYLON.Vector3}
436
      */
436
      */
437
-    _findCenter(hooks)
437
+    _findCenterOfHooks(hooks)
438
     {
438
     {
439
         const center = new BABYLON.Vector3(0, 0, 0);
439
         const center = new BABYLON.Vector3(0, 0, 0);
440
 
440
 

+ 8
- 24
demos/basketball/src/entities/score-text.js Bestand weergeven

19
 /** Duration of the fade-out effect, in seconds */
19
 /** Duration of the fade-out effect, in seconds */
20
 const FADE_OUT_DURATION = 1.0;
20
 const FADE_OUT_DURATION = 1.0;
21
 
21
 
22
+/** Y offset from the hoop, in world units */
23
+const Y_OFFSET = 0.25;
24
+
22
 /**
25
 /**
23
  * Score Text
26
  * Score Text
24
  */
27
  */
129
     }
132
     }
130
 
133
 
131
     /**
134
     /**
132
-     * Find the geometrical center, in world space, of a set of hooks
133
-     * @param {BABYLON.Mesh[]} hooks non-empty array
134
-     * @returns {BABYLON.Vector3}
135
-     */
136
-    _findCenter(hooks)
137
-    {
138
-        const center = new BABYLON.Vector3(0, 0, 0);
139
-
140
-        for(const hook of hooks) {
141
-            center.x += hook.absolutePosition.x;
142
-            center.y += hook.absolutePosition.y;
143
-            center.z += hook.absolutePosition.z;
144
-        }
145
-
146
-        return center.scaleInPlace(1 / hooks.length);
147
-    }
148
-
149
-    /**
150
      * Handle an event
135
      * Handle an event
151
      * @param {GameEvent} event
136
      * @param {GameEvent} event
152
      * @returns {void}
137
      * @returns {void}
153
      */
138
      */
154
     handleEvent(event)
139
     handleEvent(event)
155
     {
140
     {
156
-        if(event.type == 'hooksready') {
157
-            const hooks = event.detail.hooks;
158
-            const center = this._findCenter(hooks);
159
-            const parent = this._mesh.parent;
160
-            const offset = center.subtract(parent.absolutePosition);
141
+        if(event.type == 'hoopready') {
142
+            const hoopPosition = event.detail.position;
143
+            const offset = new BABYLON.Vector3(0, Y_OFFSET, 0);
144
+            const position = hoopPosition.add(offset);
161
 
145
 
162
-            this._initialPosition.copyFrom(offset);
146
+            this._initialPosition.copyFrom(position);
163
             this._mesh.position.copyFrom(this._initialPosition);
147
             this._mesh.position.copyFrom(this._initialPosition);
164
         }
148
         }
165
         else if(event.type == 'scored') {
149
         else if(event.type == 'scored') {

Laden…
Annuleren
Opslaan