-
Notifications
You must be signed in to change notification settings - Fork 1
/
player.js
324 lines (268 loc) · 12.1 KB
/
player.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
function Player() {
this.health = 100;
updateMapColor(this.health);
this.playerheight = 1.75;
this.playerSpeed = 5;
this.palyerMass = 5.0;
this.jumpSpeed = 5;
this.bbSizeX = 1;
this.bbSizeZ = 2;
this.moveForward = false;
this.moveBackward = false;
this.moveLeft = false;
this.moveRight = false;
this.jumpPress = false;
this.mousePress = false;
this.isDead = false;
this.tookDamage = false;
this.timeGlitchDamage= 0.5;
this.timeGlitchDead = 3;
this.timeCurrentGlitch = this.timeGlitch;
this.weaponAtPreviousUpdate = -1;
this.velocityVertical = 0;
this.raycaster = new THREE.Raycaster( new THREE.Vector3(), new THREE.Vector3( 0, - 1, 0 ), 0, this.playerheight + 1);
this.wallDistance = 0.8;
this.raycasterWalls = new THREE.Raycaster(new THREE.Vector3(),new THREE.Vector3(),0,this.wallDistance+0.1);
//RAYCAST DEBUG
//this.rayWallDebug = new THREE.Mesh(new THREE.BoxGeometry(0.005,0.005,0.005), new THREE.MeshBasicMaterial({color:0xFF0000}) );
//game.scene.add(this.rayWallDebug);
this.directitionRay = new THREE.Vector3(0,0,0);
this.boosts = []; //lista de boost que se aplicam ao player
//BOOSTS
var audio = new Audio('audio/boost.wav');
var playerBoost = function() {
game.player.velocityVertical = 16;
game.player.isJumping = true;
audio.play();
};
var boostPos = 17;
var posBoostCenter = new THREE.Vector3(0,0,0);
this.boosts.push(new Boost(posBoostCenter,playerBoost));
posBoostCenter = new THREE.Vector3(boostPos,0,boostPos);
this.boosts.push(new Boost(posBoostCenter,playerBoost));
posBoostCenter = new THREE.Vector3(-boostPos,0,boostPos);
this.boosts.push(new Boost(posBoostCenter,playerBoost));
posBoostCenter = new THREE.Vector3(boostPos,0,-boostPos);
this.boosts.push(new Boost(posBoostCenter,playerBoost));
posBoostCenter = new THREE.Vector3(-boostPos,0,-boostPos);
this.boosts.push(new Boost(posBoostCenter,playerBoost));
var takeShotAudio = new Audio('audio/Homer_DOH.mp3');
var gameoverAudio = new Audio('audio/gameover.wav');
var self = this; // utilizar a referencia self para funcinar em multplas callbacks (problema dos eventos)
//BoundingBox
this.playerBB = new THREE.Box3(new THREE.Vector3(game.controls.getObject().position.x-this.bbSizeX, game.controls.getObject().position.y-this.playerheight ,game.controls.getObject().position.z-this.bbSizeZ),
new THREE.Vector3(game.controls.getObject().position.x+this.bbSizeX, game.controls.getObject().position.y+1 ,game.controls.getObject().position.z+this.bbSizeZ));
// ARMAS
this.weapons = [];
this.currentWeapon = 0;
this.addWeapon = function(weapon) {
this.weapons.push(weapon);
game.controls.getObject().children[0].add(weapon.mesh);
};
this.updateWeaponGUI = function() {
switch (this.currentWeapon){
case 0:
document.getElementById("weaponicon").src = "icons/pistol.png";
break;
case 1:
document.getElementById("weaponicon").src = "icons/auto.png";
break;
}
};
// Muda a arma para a que esta no index passado
// é necessário parar as outras armas, se estavam a recarregar e não acabaram depois tem que se recommeçar
this.switchWeapon = function(weaponId) {
if (!game.controlsEnabled || this.currentWeapon == weaponId)
return;
for (i=0; i<this.weapons.length; i++){
var w = this.weapons[i];
w.stopShooting();
w.stopReloading();
w.mesh.visible = false;
}
this.currentWeapon = weaponId;
var cw = this.weapons[weaponId];
cw.mesh.visible = true;
cw.changeState(cw.previousState);
};
this.mousedown = function () {
self.weapons[self.currentWeapon].startShooting();
};
this.mouseup = function () {
self.weapons[self.currentWeapon].stopShooting();
};
this.onKeyDown = function (event) {
switch (event.keyCode) {
case 49: // 1 weapon
self.switchWeapon(0);
break;
case 50: // 2 weapon
self.switchWeapon(1);
break;
case 82: // r reload
self.weapons[self.currentWeapon].reload();
break;
case 38: // up
case 87: // w
self.moveForward = true;
self.directitionRay.z = -self.wallDistance;
break;
case 37: // left
case 65: // a
self.moveLeft = true;
self.directitionRay.x = -self.wallDistance;
break;
case 40: // down
case 83: // s
self.moveBackward = true;
self.directitionRay.z = self.wallDistance;
break;
case 39: // right
case 68: // d
self.moveRight = true;
self.directitionRay.x = self.wallDistance;
break;
case 32: // space
self.jumpPress = true;
break;
}
};
this.onKeyUp = function (event) {
switch (event.keyCode) {
case 38: // up
case 87: // w
self.moveForward = false;
self.directitionRay.z = 0;
break;
case 37: // left
case 65: // a
self.moveLeft = false;
self.directitionRay.x = 0;
break;
case 40: // down
case 83: // s
self.directitionRay.z = 0;
self.moveBackward = false;
break;
case 39: // right
case 68: // d
self.moveRight = false;
self.directitionRay.x = 0;
break;
case 32: // space
self.jumpPress = false;
break;
}
};
//binding de eventos
document.addEventListener('keydown', this.onKeyDown , false);
document.addEventListener('keyup', this.onKeyUp , false);
document.addEventListener('mousedown', this.mousedown , false);
document.addEventListener('mouseup', this.mouseup , false);
this.updateGUI = function(cw) {
// time speed update
var timeBar = document.getElementById("timespeeddiv");
var timeHeight = 100 - game.currentTimeSpeed / game.maxTimeSpeed * 100;
timeBar.style.height = timeHeight + '%';
};
this.updateBB = function () {
this.playerBB.min.set(game.controls.getObject().position.x-this.bbSizeX,game.controls.getObject().position.y-this.playerheight,game.controls.getObject().position.z-1);
this.playerBB.max.set(game.controls.getObject().position.x+this.bbSizeX,game.controls.getObject().position.y+1,game.controls.getObject().position.z+1);
};
//FUNCAO CHAMADA EM TODOS OS FRAMES
this.update = function (delta,objectIndex) {
if (this.isDead){
if (this.timeCurrentGlitch<0){
this.timeCurrentGlitch = this.timeGlitchDead;
game.stopGlitch();
game.endGame();
}
this.timeCurrentGlitch -= delta;
return;
}
if (this.tookDamage){
if (this.timeCurrentGlitch<0){
this.timeCurrentGlitch = this.timeGlitchDamage;
game.stopGlitch();
this.tookDamage=false;
}
this.timeCurrentGlitch -= delta;
}
var cw = this.weapons[this.currentWeapon];
cw.update(delta,0);
// a arma foi trocada
cw.mesh.visible = true;
this.updateWeaponGUI();
this.updateGUI(cw);
this.velocityVertical -= game.gravity * this.palyerMass * delta; // * game.currentTimeSpeed; TODO para experimentar sem camera lenta
this.updateBB();
if (!this.isJumping && this.jumpPress){
this.velocityVertical += this.jumpSpeed ; // TODO rever isto pq quando tempo para salta mais alto
this.isJumping = true;
}
//TODO otimizar as variaveis e tirar o bloco de debug
var dirCopy = new THREE.Vector3().copy(this.directitionRay);
var vectorDir = dirCopy.applyMatrix4(new THREE.Matrix4().extractRotation(game.controls.getObject().matrix)).normalize();
//this.rayWallDebug.position.copy(new THREE.Vector3().addVectors(game.controls.getObject().position,vectorDir.multiplyScalar(this.wallDistance)));
this.raycasterWalls.ray.origin.copy(game.controls.getObject().position);
this.raycasterWalls.ray.direction.copy(vectorDir);
var intersectionWalls = this.raycasterWalls.intersectObjects(game.walls.children, true);
if ( this.moveForward && !(dirCopy.z!==0 && intersectionWalls.length>=1)) game.controls.getObject().translateZ(-this.playerSpeed * delta * game.currentTimeSpeed);
if ( this.moveBackward && !(dirCopy.z!==0 && intersectionWalls.length>=1)) game.controls.getObject().translateZ(this.playerSpeed * delta * game.currentTimeSpeed);
if ( this.moveLeft && !(dirCopy.x!==0 && intersectionWalls.length>=1)) game.controls.getObject().translateX(-this.playerSpeed * delta * game.currentTimeSpeed);
if ( this.moveRight && !(dirCopy.x!==0 && intersectionWalls.length>=1)) game.controls.getObject().translateX(this.playerSpeed * delta * game.currentTimeSpeed);
//aplicar lógica dos boosts
for (var i = 0;i<this.boosts.length; i++) {
this.boosts[i].update(new THREE.Vector3().subVectors(game.controls.getObject().position,new THREE.Vector3(0,this.playerheight,0)))//ver se é preciso passar o delta
}
this.raycaster.ray.origin.copy(game.controls.getObject().position);
//raycaster.ray.origin.y -= 10;
var intersection = this.raycaster.intersectObjects(game.floors.children, true);
//game.rayInter.visible = false; //OBJETO DO CENARIO DEBUG
if (intersection.length>=1){
//game.rayInter.visible = true;
//game.rayInter.position.copy(intersection[0].point);
if (game.controls.getObject().position.y-this.playerheight <= intersection[0].point.y) { //colisao com o chao
if (this.velocityVertical<0) {
this.velocityVertical = 0;
game.controls.getObject().position.y = intersection[0].point.y+this.playerheight;
this.isJumping=false; //deteta qd n esta a salar
}
}
}
game.controls.getObject().translateY(this.velocityVertical * delta);
// update da velocidade do tempo
var acceleratingTimeStep = 5;
var stoppingTimeStep = 2;
if ( cw.isShooting || cw.isReloading) {
/* TODO refazer isto, o tempo so devia andar se a arma disparar
Esta logica provavelmente deve passar para a arma (o oponent usa a mesma arma por isso nao passei agora)
se a arma nao tiver balas tambem nao devia parar
*/
game.currentTimeSpeed = game.maxTimeSpeed;
}
else if (this.moveForward || this.moveBackward || this.moveLeft || this.moveRight || this.isJumping ) {
var tempTimeSpeed = game.currentTimeSpeed + acceleratingTimeStep * delta ;
game.currentTimeSpeed = Math.min(tempTimeSpeed, game.maxTimeSpeed);
}
else {
var tempTimeSpeed = game.currentTimeSpeed - stoppingTimeStep * delta ;
game.currentTimeSpeed = Math.max(tempTimeSpeed, game.minTimeSpeed);
}
};
this.takeDamage = function(damage) {
this.health -= damage;
game.startGlitch();
if (this.health <= 0) {
game.currentTimeSpeed = game.minTimeSpeed;
this.isDead = true;
gameoverAudio.play();
this.timeCurrentGlitch = this.timeGlitchDead;
return ;
}
takeShotAudio.play();
this.tookDamage = true;
this.timeCurrentGlitch = this.timeGlitchDamage;
updateMapColor(this.health);
};
}