ソースを参照

Basketball game: introduce event "hoopready"

customisations
alemart 8ヶ月前
コミット
ad1217ec1e

+ 2
- 2
demos/basketball/src/entities/ball.js ファイルの表示

@@ -257,8 +257,8 @@ export class Ball extends Entity
257 257
         else if(trigger.name == 'Trigger_B') {
258 258
             if(this._lastTrigger == 'A')
259 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 263
         else if(trigger.name == 'Trigger_C') {
264 264
             if(this._lastTrigger == 'B') {

+ 26
- 0
demos/basketball/src/entities/goal.js ファイルの表示

@@ -102,5 +102,31 @@ export class Goal extends PhysicsEntity
102 102
             const net = event.detail.entity;
103 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 ファイルの表示

@@ -194,7 +194,7 @@ export class BasketballNet extends PhysicsEntity
194 194
         this._indices.length = 0;
195 195
 
196 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 198
         this._physicsRoot.position.copyFrom(center);
199 199
 
200 200
         // setup the top ring
@@ -434,7 +434,7 @@ export class BasketballNet extends PhysicsEntity
434 434
      * @param {BABYLON.Mesh[]} hooks non-empty array
435 435
      * @returns {BABYLON.Vector3}
436 436
      */
437
-    _findCenter(hooks)
437
+    _findCenterOfHooks(hooks)
438 438
     {
439 439
         const center = new BABYLON.Vector3(0, 0, 0);
440 440
 

+ 8
- 24
demos/basketball/src/entities/score-text.js ファイルの表示

@@ -19,6 +19,9 @@ const ANIMATION_DURATION = 3.0;
19 19
 /** Duration of the fade-out effect, in seconds */
20 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 26
  * Score Text
24 27
  */
@@ -129,37 +132,18 @@ export class ScoreText extends Entity
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 135
      * Handle an event
151 136
      * @param {GameEvent} event
152 137
      * @returns {void}
153 138
      */
154 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 147
             this._mesh.position.copyFrom(this._initialPosition);
164 148
         }
165 149
         else if(event.type == 'scored') {

読み込み中…
キャンセル
保存