forked from WonderlandEngine/components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer-height.ts
49 lines (40 loc) · 1.49 KB
/
player-height.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import {Component} from '@wonderlandengine/api';
import {property} from '@wonderlandengine/api/decorators.js';
/**
* Set player height for a Y-offset above the ground for
* 'local' and 'viewer' reference spaces.
*/
export class PlayerHeight extends Component {
static TypeName = 'player-height';
@property.float(1.75)
height: number = 1.75;
onSessionStartCallback!: () => void;
onSessionEndCallback!: () => void;
start() {
this.object.resetPositionRotation();
this.object.translateLocal([0.0, this.height, 0.0]);
this.onSessionStartCallback = this.onXRSessionStart.bind(this);
this.onSessionEndCallback = this.onXRSessionEnd.bind(this);
}
onActivate() {
this.engine.onXRSessionStart.add(this.onSessionStartCallback);
this.engine.onXRSessionEnd.add(this.onSessionEndCallback);
}
onDeactivate() {
this.engine.onXRSessionStart.remove(this.onSessionStartCallback);
this.engine.onXRSessionEnd.remove(this.onSessionEndCallback);
}
onXRSessionStart() {
const type = this.engine.xr?.currentReferenceSpaceType;
if (type !== 'local' && type !== 'viewer') {
this.object.resetPositionRotation();
}
}
onXRSessionEnd() {
const type = this.engine.xr?.currentReferenceSpaceType;
if (type !== 'local' && type !== 'viewer') {
this.object.resetPositionRotation();
this.object.translateLocal([0.0, this.height, 0.0]);
}
}
}