-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpaceShip.js
165 lines (153 loc) · 4.87 KB
/
SpaceShip.js
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import * as THREE from './sources/three/build/three.module.js';
import {Player} from "./Player.js";
export class SpaceShip extends Player {
constructor(model) {
// model.rotation.set(Math.PI*0.1, 0, Math.PI*0.1)
// model.getObjectByName('shipCam').name = 'PlayerCam'
super(model)
model.getObjectByName('Arrows').visible = this.active;
this.engine = false
this.land = true;
this.inAtmosphere = true;
this.model.getObjectByName('SpotLight').position.set(0,0.5,0)
this.model.getObjectByName('lightTarget').position.set(0,-1,0)
model.getObjectByName("SpotLight").target = model.getObjectByName('lightTarget');
this.root.add(this.model.getObjectByName('shipLight'))
this.model.getObjectByName('shipLight').position.set(0,2,0)
// this.alignToZenith()
this.animations.Boarding.onComplete = function() {
model.getObjectByName('Legs').visible = !model.getObjectByName('Legs').visible
}
this.engine = false;
this.gear = 4,
this.speed = [-0.2, -0.1, -0.05, -0.01, 0, 0.01, 0.05, 0.1, 0.2]
this.yaw = 0;
this.pitch = 0;
// Input Configuration:
window.addEventListener('keydown', (e) => {
if (this.active) {
switch(e.code) {
case "ShiftRight": this.ssEngine(); // Shift
break;
case "ArrowUp": this.shiftUp(); // Up Arrow
break;
case "ArrowDown": this.shiftDown(); // Down
break;
case "KeyW":
if (this.engine && this.pitch < 0.025) this.pitch = -0.025;
break;
case "KeyS":
if (this.engine && this.pitch > -0.025) this.pitch = 0.025;
break;
case "KeyA":
if (this.engine && this.yaw < 0.025) this.yaw = 0.025;
break;
case "KeyD": // D
if (this.engine && this.yaw > -0.025) this.yaw = -0.025;
break;
case "ArrowLeft":
this.dodge(-90);
break;
// case "ArrowRight": this.dodge(90);
// break;
// case "KeyQ": this.land();
}
}
});
window.addEventListener('keyup', (e) => {
switch(e.code){
case "KeyW": this.pitch = 0;
break;
case "KeyS": this.pitch = 0;
break;
case "KeyA":
if(this.yaw > 0){
//this.cam.rotateZ(0.2);
this.yaw = 0;
}
break;
case "KeyD":
if(this.yaw < 0){
//this.cam.rotateZ(-0.2);
this.yaw = 0;
}
break;
}
});
window.addEventListener('wheel', (e) => {
if (e.deltaY < 0) this.shiftUp()
else this.shiftDown()
})
}
update() {
if (this.engine) {
if(this.engine) this.root.translateZ(this.speed[this.gear])
// this.root.translateOnAxis(this.fw, this.speed[this.gear])
// this.root.translateOnAxis(this.root.worldToLocal(this.fw), this.speed[this.gear])
// if(this.yaw != 0) this.root.rotateOnAxis(new THREE.Vector3(0,1,0), this.yaw)
// if(this.yaw != 0) this.root.rotateOnWorldAxis(this.up, this.yaw)
if(this.yaw != 0) this.root.rotateY(this.yaw)
// if(this.pitch != 0) this.root.rotateOnAxis(new THREE.Vector3(0,0,1), this.pitch)
// if(this.pitch != 0) this.root.rotateOnWorldAxis(this.w, this.pitch)
if(this.pitch != 0) this.root.rotateX(this.pitch)
}
super.update();
}
updateAxis() {
const root = this.model.getObjectByName('root')
root.getWorldDirection( this.fw ) // Returns a vector representing the direction of object's positive z-axis in world space.
root.getWorldPosition(this.up)
let x = new THREE.Vector3()
root.getObjectByName('shipLight').getWorldPosition(x)
x.add(this.up.negate())
this.up = x
this.up.normalize()
this.w.crossVectors(this.fw, this.up)
const arrows = root.getObjectByName("Arrows").children
const fw_1 = this.fw.clone()
fw_1.z *= -1
fw_1.x *= -1
arrows[1].setDirection(this.fw)
arrows[1].setDirection(fw_1)
}
inAtmosphere(position) {
const origin = new THREE.Vector3(0,0,0)
if(position.distanceTo(origin) <=13) {
return true
}
else return false
}
ssEngine() {
this.engine = !this.engine;
console.log("Engine: ",this.engine);
this.gear = 4;
// this.lights.children[1].intensity = this.engine;
}
shiftUp(){
if (this.engine && this.gear < 8 && !this.landed) {
this.gear +=1;
// this.lights.children[1].intensity += (2)*Math.sign(this.speed[this.gear]);
}
}
shiftDown(){
if (this.engine && this.gear > 0 && !this.landed) {
this.gear -=1;
// this.lights.children[1].intensity -= 2*Math.sign(this.speed[this.gear]);
}
}
moveTo(rotationFrame) {
this.animations.MoveTo.frames[0] = [rotationFrame]
this.animations.MoveTo.periods[0] = 1000
this.animations.MoveTo.delay = false
this.animations.MoveTo.concat = [this.animations.Doors]
if (this.land) {
this.animations.Boarding.concat = [this.animations.MoveTo]
this.animations.Boarding.start()
this.land = false
}
else {
this.animations.MoveTo.start()
}
this.active = true
}
}