forked from Snapchat/Spectacles-Sample
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLensInitializer.ts
More file actions
115 lines (94 loc) · 3.46 KB
/
Copy pathLensInitializer.ts
File metadata and controls
115 lines (94 loc) · 3.46 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import { PathMaker } from './PathMaker';
import { PathWalker } from './PathWalker';
import { UI } from './UI';
import { SprintPathData } from "./BuiltPathData";
import { TutorialController } from "./TutorialController";
@component
export class LensInitializer extends BaseScriptComponent {
@input
ui: UI
@input
tutorialController: TutorialController;
@input
pathMaker: PathMaker
@input
pathWalker: PathWalker
@input
camSo: SceneObject
private camTr: Transform
private floorOffsetFromCamera = -100;
private static instance: LensInitializer;
private floorIsSet: boolean = false;
private vec3up: vec3 = vec3.up();
private constructor() {
super();
}
public static getInstance(): LensInitializer {
if (!LensInitializer.instance) {
throw new Error("Trying to get LensInitializer instance, but it hasn't been set. You need to call it later.");
}
return LensInitializer.instance;
}
onAwake() {
if (!LensInitializer.instance) {
LensInitializer.instance = this;
} else {
throw new Error("LensInitializer already has an instance but another one is initializing. Aborting.")
}
this.camTr = this.camSo.getTransform();
this.pathMaker.init();
this.pathWalker.init();
this.ui.getSceneObject().enabled = true;
this.tutorialController.startTutorial(() => {
this.startHomeState();
})
}
setFloorOffsetFromCamera(floorPos: vec3) {
// Get the difference between current cam height and this Y value
// Meaning, we take the camera's height at floor set to be the player's "height" for this path
let camPos = this.camTr.getWorldPosition();
let offset = floorPos.sub(camPos);
// Because player is looking down when height is taken,
// offset is closer than it will be (when player is looking out)
this.floorOffsetFromCamera = offset.y - 10;
this.floorIsSet = true;
}
getPlayerGroundPos() {
if (!this.floorIsSet) {
throw Error("Floor not set. You need to call it later.");
}
return this.camTr.getWorldPosition().add(this.vec3up.uniformScale(this.floorOffsetFromCamera));
}
private startHomeState() {
this.ui.showHomeUi();
const pathClickedRemover = this.ui.createPathClicked.add(() => {
pathClickedRemover();
this.pathMaker.start();
const remover = this.pathMaker.pathMade.add((data) => {
remover();
if (!data.isLoop) {
const dataSprint = data as SprintPathData;
this.pathWalker.start(
dataSprint.splinePoints,
dataSprint.isLoop,
dataSprint.startObject.getTransform(),
dataSprint.finishObject.getTransform(),
() => {
this.startHomeState();
}
);
} else {
this.pathWalker.start(
data.splinePoints,
data.isLoop,
data.startObject.getTransform(),
undefined,
() => {
this.startHomeState();
}
);
}
});
})
}
}