-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.ts
More file actions
70 lines (53 loc) · 1.89 KB
/
player.ts
File metadata and controls
70 lines (53 loc) · 1.89 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
/// <reference path="typings/index.d.ts" />
class Player extends GameObject {
private controls = new Array<string>();
private _health : Health;
private _score : Score;
public get health() : Health{
return this._health;
}
public get score() : Score{
return this._score;
}
constructor(g : Game, h : Health, s : Score, controls : Array<string>, hexColor : number = 0x00ff00){
super(hexColor, 1, 1, 1, 0, g);
this._health = h;
this._score = s;
this.controls = controls;
window.addEventListener("keyup", (e : KeyboardEvent) => this.onKeyUp(e));
window.addEventListener("keydown", (e : KeyboardEvent) => this.onkeydown(e));
}
private onKeyUp(e : KeyboardEvent) : void {
if(e.key == this.controls[0] || e.key == this.controls[1]){
this._speedY = 0;
}
if(e.key == this.controls[2] || e.key == this.controls[3]){
this._speedX = 0;
}
}
private onkeydown(e : KeyboardEvent) : void {
//["ArrowUp", "ArrowDown", "ArrayLeft", "ArrayRight"]
if(e.key == " "){
this._game.addLaser(this.object.position.x, this.object.position.y, this.object.position.z);
}
if(e.key == this.controls[0]){
this._speedY = 0.03;
}
if(e.key == this.controls[1]){
this._speedY = -0.03;
}
if(e.key == this.controls[2]){
this._speedX = -0.03;
}
if(e.key == this.controls[3]){
this._speedX = 0.03;
}
}
public remove() : void {
window.removeEventListener("keyup", (e : KeyboardEvent) => this.onKeyUp(e));
window.removeEventListener("keydown", (e : KeyboardEvent) => this.onkeydown(e));
this.game.scene.remove(this.object);
this.health.remove();
this.score.remove();
}
}