-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.js
89 lines (80 loc) · 2.15 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
class Player {
constructor() {
this.pos = [250, 250];
this.color = '#000000';
this.radius = 18.0;
this.collidable = false;
this.dead = false;
this.image = new Image();
this.image.src = "./assets/green.png";
this.shouldDraw = true;
this.blinking();
setTimeout(() => {
this.collidable = true;
this.color = '#000000';
}, 2000);
}
blinking() {
let startTime = new Date().getTime();
let interval = setInterval(() => {
if(new Date().getTime() - startTime > 2000){
this.shouldDraw = true;
clearInterval(interval);
return;
}
this.shouldDraw = !this.shouldDraw;
}, 250);
}
handleInput() {
if(window.input.isDown('DOWN') && window.input.isDown('RIGHT')){
this.pos[0] += 1.7;
this.pos[1] += 1.7;
} else if(window.input.isDown('DOWN') && window.input.isDown('LEFT')){
this.pos[1] += 1.7;
this.pos[0] -= 1.7;
} else if(window.input.isDown('UP') && window.input.isDown('LEFT')){
this.pos[1] -= 1.7;
this.pos[0] -= 1.7;
} else if(window.input.isDown('UP') && window.input.isDown('RIGHT')){
this.pos[1] -= 1.7;
this.pos[0] += 1.7;
} else if(window.input.isDown('DOWN')){
this.pos[1] += 1.7;
} else if(window.input.isDown('UP')){
this.pos[1] -= 1.7;
} else if(window.input.isDown('LEFT')){
this.pos[0] -= 1.7;
} else if(window.input.isDown('RIGHT')){
this.pos[0] += 1.7;
}
this.wrap();
}
wrap(){
if(this.pos[0] < -30){
this.pos[0] = 1430;
} else if(this.pos[0] > 1430) {
this.pos[0] = -30;
} else if(this.pos[1] < -30){
this.pos[1] = 730;
} else if(this.pos[1] > 730){
this.pos[1] = -30;
}
}
handleCollision(circle){
if(this.collidable){
if(this.radius >= circle.radius){
this.radius += (circle.radius * .20);
circle.radius = 0;
} else {
this.radius = 0;
this.dead = true;
}
}
}
draw(ctx) {
if(this.shouldDraw){
ctx.drawImage(this.image, this.pos[0]-this.radius, this.pos[1]-this.radius, this.radius*2, this.radius*2);
}
}
}
export default Player;