|
1 | | -import {Component, Property} from "@wonderlandengine/api"; |
2 | | -import { SwitchScene } from './switch'; |
3 | | -import { vec3 } from 'gl-matrix'; |
| 1 | +import {Component, Property} from '@wonderlandengine/api'; |
| 2 | +import {SwitchScene} from './switch'; |
| 3 | +import {vec3} from 'gl-matrix'; |
4 | 4 |
|
5 | 5 | /** |
6 | 6 | * switch-scene |
7 | 7 | */ |
8 | 8 | export class Portal extends Component { |
9 | | - static TypeName = "portal"; |
10 | | - /* Properties that are configurable in the editor */ |
11 | | - static Properties = { |
12 | | - target: Property.object(), |
13 | | - translationDuration: Property.float(0.5), |
14 | | - initialTranslate: Property.float(15), |
15 | | - portalRotateSpeed: Property.float(1.0), |
16 | | - }; |
| 9 | + static TypeName = 'portal'; |
| 10 | + /* Properties that are configurable in the editor */ |
| 11 | + static Properties = { |
| 12 | + target: Property.object(), |
| 13 | + translationDuration: Property.float(0.5), |
| 14 | + initialTranslate: Property.float(15), |
| 15 | + portalRotateSpeed: Property.float(1.0), |
| 16 | + }; |
17 | 17 |
|
18 | | - onActivate() { |
19 | | - this.target.translateObject([0, 0, this.initialTranslate]); |
20 | | - } |
| 18 | + onActivate() { |
| 19 | + this.target.translateObject([0, 0, this.initialTranslate]); |
| 20 | + } |
21 | 21 |
|
22 | | - update(dt) { |
23 | | - this.object.rotateAxisAngleDegObject( |
24 | | - [0, 0, 1], |
25 | | - dt * 10000 * this.portalRotateSpeed, |
26 | | - ); |
27 | | - const playerPosition = this.target.getPositionWorld([]); |
28 | | - const portalPosition = this.object.getPositionWorld([]); |
29 | | - const sqDist= vec3.squaredDistance(playerPosition, portalPosition) |
30 | | - if (sqDist > 0.1) { |
31 | | - const distanceRemaining = Math.abs( |
32 | | - portalPosition[0] - playerPosition[0], |
33 | | - ); |
34 | | - const alpha = |
35 | | - 1 - |
36 | | - Math.exp(-dt / (this.translationDuration * distanceRemaining)); |
37 | | - const newPosition= new Float32Array(3) |
38 | | - vec3.lerp(newPosition,playerPosition,portalPosition,alpha,) |
39 | | - this.target.setPositionWorld(newPosition); |
40 | | - } |
41 | | - else{ |
42 | | - this.object.getComponent(SwitchScene).switch(); |
43 | | - } |
44 | | - } |
| 22 | + update(dt) { |
| 23 | + this.object.rotateAxisAngleDegObject( |
| 24 | + [0, 0, 1], |
| 25 | + dt * 10000 * this.portalRotateSpeed |
| 26 | + ); |
| 27 | + const playerPosition = this.target.getPositionWorld([]); |
| 28 | + const portalPosition = this.object.getPositionWorld([]); |
| 29 | + const sqDist = vec3.squaredDistance(playerPosition, portalPosition); |
| 30 | + if (sqDist > 0.1) { |
| 31 | + const distanceRemaining = Math.abs(portalPosition[0] - playerPosition[0]); |
| 32 | + const alpha = |
| 33 | + 1 - Math.exp(-dt / (this.translationDuration * distanceRemaining)); |
| 34 | + const newPosition = new Float32Array(3); |
| 35 | + vec3.lerp(newPosition, playerPosition, portalPosition, alpha); |
| 36 | + this.target.setPositionWorld(newPosition); |
| 37 | + } else { |
| 38 | + this.object.getComponent(SwitchScene).switch(); |
| 39 | + } |
| 40 | + } |
45 | 41 | } |
0 commit comments