-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTarget.js
More file actions
100 lines (84 loc) · 2.63 KB
/
Target.js
File metadata and controls
100 lines (84 loc) · 2.63 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
import { GLTFLoader } from 'https://cdn.jsdelivr.net/npm/three@0.127/examples/jsm/loaders/GLTFLoader.js';
import * as THREE from 'https://cdn.jsdelivr.net/npm/three@0.127/build/three.module.js';
import TWEEN from 'https://cdn.jsdelivr.net/npm/@tweenjs/tween.js@18.5.0/dist/tween.esm.js';
const DOWN_ANGLE = -Math.PI / 2;
const UP_ANGLE = 0;
const FLIP_DURATION_MS = 350;
const HOLD_DURATION_MS = 2000;
export default class Target
{
constructor(x, y, scene)
{
this.x = x;
this.y = y;
this.pivot = null;
this.model = null;
this.ready = this.loadModel('mesh/enemy1.gltf', x, y, scene);
}
loadModel(path, x, y, scene)
{
const loader = new GLTFLoader();
return new Promise((resolve, reject) => {
loader.load(path, (gltf) =>
{
const model = gltf.scene;
model.rotation.y = -Math.PI / 2;
const box = new THREE.Box3().setFromObject(model);
const center = box.getCenter(new THREE.Vector3());
const min = box.min.clone();
model.position.x -= center.x;
model.position.y -= min.y;
model.position.z -= center.z;
const pivot = new THREE.Group();
pivot.position.set(x, y, 0);
pivot.rotation.x = DOWN_ANGLE;
pivot.add(model);
scene.add(pivot);
this.pivot = pivot;
this.model = model;
resolve(this);
}, undefined, (error) =>
{
console.error(error);
reject(error);
});
});
}
raise()
{
return this.animateTo(UP_ANGLE, TWEEN.Easing.Back.Out);
}
lower()
{
return this.animateTo(DOWN_ANGLE, TWEEN.Easing.Quadratic.InOut);
}
animateTo(rotationX, easing = TWEEN.Easing.Quadratic.InOut)
{
return new Promise((resolve) => {
new TWEEN.Tween(this.pivot.rotation)
.to({ x: rotationX }, FLIP_DURATION_MS)
.easing(easing)
.onComplete(resolve)
.start();
});
}
static wait(duration)
{
return new Promise((resolve) => {
window.setTimeout(resolve, duration);
});
}
static async runCycle(targets)
{
while (true)
{
for (const target of targets)
{
await target.raise();
await Target.wait(HOLD_DURATION_MS);
await target.lower();
await Target.wait(250);
}
}
}
}