-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharwing.js
197 lines (180 loc) · 7.46 KB
/
arwing.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
"use strict";
var CHARGED = 25;
class Arwing {
constructor(type, scene) {
var loader = new THREE.ObjectLoader();
var that = this;
var test = null;
this.velocity = new Velocity();
this.orientationXZ = 0;
this.orientationYZ = 0;
this.orientationZ = 0;
this.soundPlayed = false;
this.lasers = [];
this.charge = 0;
this.alive = true;
this.bullets = 0;
loader.load(type, function ( obj ) {
obj.scale.set(.03, .03, .03); // for arwing
obj.position.set(0, 0, -100);
obj.rotateOnAxis( new THREE.Vector3(0,1,0), 3.14);
that.model = obj;
that.colBox = new THREE.Box3(new THREE.Vector3(), new THREE.Vector3());
that.colBox.setFromObject(that.model);
});
}
advance() {
this.model.translateZ(this.velocity.dz);
this.model.translateX(this.velocity.dx);
this.model.translateY(this.velocity.dy);
this.colBox.setFromObject(this.model);
}
kill() {
this.velocity.dx = this.velocity.dy = this.velocity.dz = 0;
laser2.velocity.dx = laser2.velocity.dy = laser2.velocity.dz = 0;
scene.remove(this.model);
parts.push(new ExplodeAnimation(this.model.position.x, this.model.position.y, this.model.position.z, true));
audio = new Audio('asteroid_explosion.mp3');
audio.play();
audio = new Audio('fox-ahhh.mp3');
audio.play();
this.alive = false;
}
keyPress(pad) {
keyboard.update();
var delta = clock.getDelta(); // seconds.
// delta was causing jitters (probably due to floating point precision)
var moveDistance = .05// * delta; // 200 pixels per second
var rotateAngle = .031; // pi/2 radians (90 degrees) per second
var fix = moveDistance;
var dec = 30;
// move forwards/backwards and rotate left/right
if ( keyboard.pressed("O") ) {
if (this.velocity.dz > -maxShipSpeed) {
this.velocity.setDz(-moveDistance);
laser2.velocity.setDz(-moveDistance);
}
}
if ( keyboard.pressed("U") ) {
if (this.velocity.dz < maxShipSpeed) {
this.velocity.setDz(moveDistance);
laser2.velocity.setDz(moveDistance);
}
}
if ( keyboard.pressed(">") ) {
this.model.rotateOnAxis( new THREE.Vector3(0,1,0), rotateAngle);
laser2.model.rotateOnAxis( new THREE.Vector3(0,1,0), rotateAngle);
}
if ( keyboard.pressed("<") ) {
this.model.rotateOnAxis( new THREE.Vector3(0,1,0), -rotateAngle);
laser2.model.rotateOnAxis( new THREE.Vector3(0,1,0), -rotateAngle);
}
// rotate left/right/up/down
var rotation_matrix = new THREE.Matrix4().identity();
if ( keyboard.pressed("I") ) {
this.model.rotateOnAxis( new THREE.Vector3(1,0,0), rotateAngle);
laser2.model.rotateOnAxis( new THREE.Vector3(1,0,0), rotateAngle);
this.orientationYZ += rotateAngle;
}
if ( keyboard.pressed("K") ) {
this.model.rotateOnAxis( new THREE.Vector3(1,0,0), -rotateAngle);
laser2.model.rotateOnAxis( new THREE.Vector3(1,0,0), -rotateAngle);
this.orientationYZ -= rotateAngle;
}
if ( keyboard.pressed("J") ) {
this.model.rotateOnAxis( new THREE.Vector3(0,0,1), rotateAngle * 2);
laser2.model.rotateOnAxis( new THREE.Vector3(0,0,1), rotateAngle * 2);
}
if ( keyboard.pressed("L") ) {
this.model.rotateOnAxis( new THREE.Vector3(0,0,1), -rotateAngle * 2);
laser2.model.rotateOnAxis( new THREE.Vector3(0,0,1), -rotateAngle * 2);
}
// Controller Support
if (pad != undefined) {
if (pad.buttons[5] != undefined && pad.buttons[5].pressed == true ) {
if (this.velocity.dz > -maxShipSpeed) {
this.velocity.setDz(-moveDistance);
laser2.velocity.setDz(-moveDistance);
}
}
if (pad.buttons[4] != undefined && pad.buttons[4].pressed == true ) {
if (this.velocity.dz < maxShipSpeed) {
this.velocity.setDz(moveDistance);
laser2.velocity.setDz(moveDistance);
}
}
if ( pad.axes[2] < -0.5 ) {
this.model.rotateOnAxis( new THREE.Vector3(0,1,0), -pad.axes[2] / dec );
laser2.model.rotateOnAxis( new THREE.Vector3(0,1,0), -pad.axes[2] / dec );
}
if ( pad.axes[2] > 0.5 ) {
this.model.rotateOnAxis( new THREE.Vector3(0,1,0), -pad.axes[2] / dec );
laser2.model.rotateOnAxis( new THREE.Vector3(0,1,0), -pad.axes[2] / dec );
}
if ( pad.axes[3] > 0.2 ) {
this.model.rotateOnAxis( new THREE.Vector3(1,0,0), pad.axes[3] / dec );
laser2.model.rotateOnAxis( new THREE.Vector3(1,0,0), pad.axes[3] / dec );
this.orientationYZ += rotateAngle;
}
if ( pad.axes[3] < -0.2 ) {
this.model.rotateOnAxis( new THREE.Vector3(1,0,0), pad.axes[3] / dec );
laser2.model.rotateOnAxis( new THREE.Vector3(1,0,0), pad.axes[3] / dec );
this.orientationYZ -= rotateAngle;
}
if ( pad.axes[0] < -0.2 ) {
this.model.rotateOnAxis( new THREE.Vector3(0,0,1), -pad.axes[0] / (dec/2) );
laser2.model.rotateOnAxis( new THREE.Vector3(0,0,1), -pad.axes[0] / (dec/2) );
}
if ( pad.axes[0] > 0.2 ) {
this.model.rotateOnAxis( new THREE.Vector3(0,0,1), -pad.axes[0] / (dec/2) );
laser2.model.rotateOnAxis( new THREE.Vector3(0,0,1), -pad.axes[0] / (dec/2) );
}
}
if ( keyboard.pressed("N") ) {
this.charge++;
if (this.charge >= CHARGED && !this.soundPlayed) {
audio = new Audio('charge_laser.mp3');
audio.play();
this.soundPlayed = true;
}
if (this.charge > CHARGED + 100) {
temp.material.visible = true;
}
}
// fire on key up so we can do charging bullets
if ( keyboard.pressed("N") || (pad != undefined && pad.buttons[7] != undefined && (pad.buttons[7].pressed == true))) { //TODO: Should be keyboard.up. Fix this.
if (this.charge > CHARGED + 100) {
audio = new Audio('explosion.mp3');
audio.play();
var chargedBolt = new Bolt(bullet, scene, 2); // TODO: Fix arwing laser light
chargedBolt.model.position.set(laser2.model.position.x, laser2.model.position.y, laser2.model.position.z);
chargedBolt.model.rotation.set(laser2.model.rotation.x, laser2.model.rotation.y, laser2.model.rotation.z);
chargedBolt.model.updateMatrix();
this.lasers.push(chargedBolt);
scene.add(chargedBolt.model);
this.charge = 0;
this.soundPlayed = false;
temp.material.visible = false;
}
else {
this.bullets++;
audio = new Audio('arwingOneShot.mp3');
audio.play();
var bolt = new Bolt(laser2.model, scene, 2);
bolt.model.position.set(laser2.model.position.x, laser2.model.position.y, laser2.model.position.z);
bolt.model.rotation.set(laser2.model.rotation.x, laser2.model.rotation.y, laser2.model.rotation.z);
this.lasers.push(bolt);
scene.add(bolt.model);
this.charge = 0;
this.soundPlayed = false;
// }
}
}
var relativeCameraOffset2 = new THREE.Vector3(0,40,150);
var cameraOffset2 = relativeCameraOffset2.applyMatrix4( this.model.matrixWorld );
camera2.position.x = cameraOffset2.x;
camera2.position.y = cameraOffset2.y;
camera2.position.z = cameraOffset2.z;
camera2.lookAt( this.model.position );
}
}