-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.ts
442 lines (345 loc) · 11.5 KB
/
Player.ts
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
import { Anim, AnimField, PhysField, AnimFrame } from './lib/juego/Anim.js'
import { Entity } from './lib/juego/Entity.js'
import { Contact } from './lib/juego/Contact.js'
import { Material } from './lib/juego/Material.js'
import { Shape } from './lib/juego/Shape.js'
import { Sound } from './lib/juego/Sound.js'
import { Vec2 } from './lib/juego/Vec2.js'
import { Keyboard, KeyCode } from './lib/juego/keyboard.js'
import { CenteredEntity } from './CenteredEntity.js'
import { COL } from './collisionGroup.js'
import { Coin } from './Coin.js'
import { Bullet, Gutter } from './Bullet.js'
import { Particle } from './Particle.js'
import { GravityInverter } from './entity/GravityInverter.js'
import * as Debug from './Debug.js'
import child_process from 'child_process'
export type PlayerStatus = {
startTime: number;
lives: number;
defeatedNames: Array<string>;
messages: Array<string>;
}
let RETAIN_COLLIDE_LR_WINDOW_FRAMES = 5;
let JUMP_INTENT_WINDOW_FRAMES = 5;
let slideMaterial = new Material( 60, 1.0, 0.6 );
let walkMaterial = new Material( 180, 1.0, 0.6 );
export class Player extends Entity {
/*
Pressing the jump button signals "jump intent"
--> Save the jump press for a number of frames after the button press
[Z]: press jump
(<-): release left or right
intended order: (<-)[Z] (same frame)
allowed order: [Z],,,,(<-) (jump intent window)
not allowed: [Z],,,,,(<-) (gap too long)
allowed order: (<-),,,,[Z] (retain collide LR window)
not allowed: (<-),,,,,[Z] (gap too long)
*/
jumpIntentFrames: number = 0;
jumps: number = 0;
jumping: boolean = false;
maxJumpFrames: number = 20;
jumpFrames: number = 0;
blockedDirs: Array<Vec2> = [];
collideDown: boolean = false;
collideRight: boolean = false;
collideLeft: boolean = false;
velIntent: Vec2 = new Vec2(); // used for moving platforms
originOffset: Vec2 = new Vec2( 0, 0 );
lastFireTime: number = 0;
fireInterval: number = 100;
transponderCharge: number = 1.0;
health = 10;
wince: number = 0;
causeOfDeath: string = '';
messages: Array<string> = [];
slideSoundProc: any = null;
slideSoundObj: Sound = null;
gravSign: number = 1.0;
jumpSign: number = 1.0; // tracks gravSign, but not updated until the jump starts
/* property overrides */
anim = new Anim( {
'wince': new AnimField( this, 'wince', 0.02 ),
'transponderCharge': new AnimField( this, 'transponderCharge', 0.01 ),
'originOffset': new AnimField( this, 'originOffset', 0 ),
}, new AnimFrame( {
'wince': { value: 0.0 },
'transponderCharge': { value: 1.0 }, // in case player anim gets cleared for some reason
} ) );
constructor( pos: Vec2 ) {
super( pos, 16, 16 );
this.originOffset.setValues( 0, -this.width / 4 );
if ( typeof document !== 'undefined' ) {
this.slideSoundObj = new Sound( './sfx/slide.mp3', new Vec2( 0, 0 ), { loop: true } );
}
}
hitWith( otherEntity: Entity, contact: Contact ): void {
let damage = 0;
if ( otherEntity instanceof Bullet ) {
damage = 1;
otherEntity.removeThis = true;
this.causeOfDeath = 'You have died';
} else if ( otherEntity instanceof Gutter ) {
damage = 1;
this.causeOfDeath = 'You have been incinerated';
} else if ( otherEntity.collisionGroup == COL.ENEMY_BULLET ) {
damage = 1;
this.causeOfDeath = 'You have died';
} else if ( otherEntity instanceof Coin ) {
otherEntity.removeThis = true;
}
if ( damage > 0 && this.wince == 0.0 ) {
this.health -= damage;
this.wince += damage * 0.3;
this.messages.push( 'Remaining health: ' + this.health );
}
}
endSlideSound() {
if ( this.slideSoundProc ) {
this.slideSoundProc.kill( 'SIGINT' );
this.slideSoundProc = null;
}
if ( this.slideSoundObj ) {
this.slideSoundObj.audio.pause();
}
}
updateGrav( grav: Vec2 ) {
this.vel.x = this.velIntent.x;
if ( this.collideDown ) {
this.vel.y = 0;
}
// left/right
if ( Keyboard.keyHeld( KeyCode.LEFT ) ) {
this.vel.x += -5;
}
if ( Keyboard.keyHeld( KeyCode.RIGHT ) ) {
this.vel.x += 5;
}
// jump
if ( this.jumpIntentFrames > 0 ) {
this.jumpIntentFrames -= 1;
}
if ( Keyboard.keyHit( KeyCode.Z ) && this.jumps > 0 ) {
this.jumpIntentFrames = JUMP_INTENT_WINDOW_FRAMES;
}
if ( this.jumpIntentFrames > 0 ) {
let jumpOk = false;
if ( this.collideDown ) jumpOk = true;
if ( this.collideLeft &&
!Keyboard.keyHeld( KeyCode.LEFT ) ) jumpOk = true;
if ( this.collideRight &&
!Keyboard.keyHeld( KeyCode.RIGHT ) ) jumpOk = true;
if ( jumpOk ) {
this.jumpIntentFrames = 0;
this.jumping = true;
this.jumpFrames = this.maxJumpFrames;
this.jumps = 0;//-= 1;
this.jumpSign = this.gravSign;
this.endSlideSound();
if ( typeof document === 'undefined' ) {
child_process.exec( 'aplay ./sfx/jump.wav' );
} else {
let s = new Sound( './sfx/jump.wav' );
s.play();
}
}
}
if ( Keyboard.keyHeld( KeyCode.Z ) && this.jumping && this.jumpFrames > 0 ) {
this.vel.y = -7 * this.jumpSign;
this.jumpFrames -= 1;
} else {
this.jumping = false;
}
// apply gravity
this.vel.y += grav.y * this.gravSign;
}
flipGravSign() {
this.gravSign *= -1;
// this happens at rate 0 (instantaneous), leaving it as an animated value for posterity
if ( this.gravSign > 0 ) {
this.anim.pushFrame( new AnimFrame( {
'originOffset': { value: new Vec2( 0, -this.width / 4 ) }
} ), {
threadIndex: 1
} );
} else if ( this.gravSign < 0 ) {
this.anim.pushFrame( new AnimFrame( {
'originOffset': { value: new Vec2( 0, this.width / 4 ) }
} ), {
threadIndex: 1
} );
}
}
updateCollisionFlags( blockedContacts: Array<Contact>, grav: Vec2 ) {
let prevCollideDown = this.collideDown;
this.collideDown = false;
if ( this.vel.y * this.gravSign > grav.y * RETAIN_COLLIDE_LR_WINDOW_FRAMES ) { // save collideLeft/Right for 10 frames after key is released
this.collideRight = false;
this.collideLeft = false;
}
if ( this.vel.x > 0.0001 ) {
this.collideRight = false;
}
if ( this.vel.x < -0.0001 ) {
this.collideLeft = false;
}
let playLandSound = false;
if ( typeof document === 'undefined' ) {
if ( this.slideSoundProc && this.slideSoundProc.exitCode !== null ) {
this.slideSoundProc = null;
}
}
this.velIntent.x = 0;
// NOTE: if colliding down, this.vel.y is already cleared by this point (by collision solver)
let prevVelY = this.vel.y;
let rightParticle: number = null;
let leftParticle: number = null;
for ( let contact of blockedContacts ) {
let dir = contact.normal;
if ( dir.dot( grav.times( this.gravSign ).unit() ) < -0.5 ) { // down
if ( !prevCollideDown ) {
playLandSound = true;
}
this.collideDown = true;
this.jumps = 1;
let p = contact.otherSub.unapplyTransform( contact.point.copy(), 0.0 );
let p2 = contact.otherSub.applyTransform( p.copy(), 1.0 );
let v = p2.minus( contact.point );
if ( Math.abs( v.x ) < 0.0001 ) v.x = 0; // VEL_EPSILON
if ( Math.abs( v.y ) < 0.0001 ) v.y = 0;
this.velIntent.x += v.x;
this.endSlideSound();
}
// these sometimes trigger when hitting the ground due to a diagonal normal
// this is ok since collideLeft/Right don't inhibit horizontal movement
// NOTE: vel.y doesn't stay at 0, gravity is added before advance() is called
if ( dir.dot( new Vec2( -1, 0 ) ) > 0.1 && contact.otherSub.isSkiddable ) { // right
this.collideRight = true;
this.jumps = 1;
this.vel.y = 0;
if ( prevVelY * this.gravSign > 0.0001 && Keyboard.keyHeld( KeyCode.RIGHT ) ) {
let y = Math.max( this.pos.y, contact.otherShape.minmax[0].y );
y = Math.min( y, contact.otherShape.minmax[1].y );
if ( rightParticle === null || Math.abs( y - this.pos.y ) < Math.abs( rightParticle - this.pos.y ) ) {
rightParticle = y;
}
} else {
let x = 0;
}
} else {
let x = 0;
}
if ( dir.dot( new Vec2( 1, 0 ) ) > 0.1 && contact.otherSub.isSkiddable ) { // left
this.collideLeft = true;
this.jumps = 1;
this.vel.y = 0;
if ( prevVelY * this.gravSign > 0.0001 && Keyboard.keyHeld( KeyCode.LEFT ) ) {
let y = Math.max( this.pos.y, contact.otherShape.minmax[0].y );
y = Math.min( y, contact.otherShape.minmax[1].y );
if ( leftParticle === null || Math.abs( y - this.pos.y ) < Math.abs( leftParticle - this.pos.y ) ) {
leftParticle = y;
}
}
}
if ( this.collideLeft || this.collideRight ) {
if ( !this.collideDown ) {
if ( !this.slideSoundProc ) {
if ( typeof document === 'undefined' ) {
this.slideSoundProc = child_process.spawn( 'aplay', ['./sfx/slide.wav'] );
} else {
this.slideSoundObj.play();
}
}
}
}
}
if ( playLandSound ) {
if ( typeof document === 'undefined' ) {
child_process.exec( 'aplay ./sfx/land.wav' );
} else {
let s = new Sound( './sfx/land.wav' );
s.play();
}
}
if ( !Keyboard.keyHeld( KeyCode.LEFT ) &&
!Keyboard.keyHeld( KeyCode.RIGHT ) ) {
this.endSlideSound();
}
if ( !this.collideLeft &&
!this.collideRight ) {
this.endSlideSound();
}
if ( rightParticle !== null ) {
let alpha = 1.0;
let rate = 0.17;
let width = 2.5; // slightly longer than max fall speed
let part = new Particle( new Vec2( this.pos.x + this.width / 2, rightParticle ), 1, width, alpha, rate );
part.material = slideMaterial;
this.spawned.push( part );
}
if ( leftParticle !== null ) {
let alpha = 1.0;
let rate = 0.17;
let width = 2.5; // slightly longer than max fall speed
let part = new Particle( new Vec2( this.pos.x - this.width / 2, leftParticle ), 1, width, alpha, rate );
part.material = slideMaterial;
this.spawned.push( part );
}
}
getOrigin(): Vec2 {
return this.pos.plus( this.originOffset );
}
getOwnShapes(): Array<Shape> {
/*
Chamfer corners in jump direction (away from gravity) make player
slide outward if jumping close to the corner of the underside of
a platform (most of player body is not under platform, player
intent is likely to jump above platform)
*/
let topWidth = this.gravSign > 0 ? this.width / 2 : this.width;
let bottomWidth = this.gravSign < 0 ? this.width / 2 : this.width;
// square with two corners blunted
let shape = Shape.fromPoints( [
new Vec2( this.width / 2, -this.height / 4 ),
new Vec2( this.width / 2, this.height / 4 ),
new Vec2( bottomWidth / 2, this.height / 2 ), // bottom right
new Vec2( -bottomWidth / 2, this.height / 2 ), // bottom left
new Vec2( -this.width / 2, this.height / 4 ),
new Vec2( -this.width / 2, -this.height / 4 ),
new Vec2( -topWidth / 2, -this.height / 2 ), // top left
new Vec2( topWidth / 2, -this.height / 2 ), // top right
] );
shape.material = this.material;
shape.parent = this;
return [shape];
}
draw( context: CanvasRenderingContext2D ) {
super.draw( context );
context.strokeStyle = 'red';
context.lineWidth = 2;
for ( let blockedDir of this.blockedDirs ) {
let a = this.pos.plus( blockedDir.times( 10 ) );
context.beginPath();
context.moveTo( this.pos.x, this.pos.y );
context.lineTo( a.x, a.y );
context.stroke();
}
context.fillStyle = 'red';
context.save();
context.translate( this.pos.x, this.pos.y );
if ( this.collideDown ) {
context.fillRect( -this.width / 4, this.height / 4,
this.width / 2, this.height / 4);
}
if ( this.collideLeft ) {
context.fillRect(-this.width / 2, -this.height / 4,
this.width / 4, this.height / 2);
}
if ( this.collideRight ) {
context.fillRect(this.width / 4, -this.height / 4,
this.width / 4, this.height / 2);
}
context.restore();
}
}