-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircle.js
98 lines (84 loc) · 2.39 KB
/
circle.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
import dist from '../util/dist';
class Enemy {
constructor(start, playerRadius) {
this.img1 = new Image();
this.img1.src = "./assets/brown.png";
this.img2 = new Image();
this.img2.src = "./assets/black.png";
this.img3 = new Image();
this.img3.src = "./assets/yellow.png";
this.IMAGES = [
this.img1,
this.img2,
this.img3
];
this.playerRadius = playerRadius;
this.pos = this.generateRandomPosition();
this.vel = this.generateRandomVelocity();
this.radius = this.generateRandomRadius(playerRadius);
this.image = this.IMAGES[Math.floor(Math.random()*this.IMAGES.length)];
this.shouldDraw = true;
this.collidable = false;
if(!start){
this.blinking();
}
setTimeout(() => {
this.collidable = true;
}, 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);
}
generateRandomRadius() {
return Math.floor(Math.random() * ((this.playerRadius*5) - this.playerRadius/3)) + this.playerRadius/3;
}
generateRandomVelocity() {
let randX = Math.floor(Math.random() * (2.2 - 0)) + 0;
let randY = Math.floor(Math.random() * (2.2 - 0)) + 0;
return [randX, randY];
}
generateRandomPosition() {
let randX = Math.floor(Math.random() * (1400 - 0)) + 0;
let randY = Math.floor(Math.random() * (1400 - 0)) + 0;
return [randX, randY];
}
collidesWith(player) {
const centerDist = dist(this.pos, player.pos)*1.1;
return centerDist < (this.radius + player.radius);
}
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);
}
}
outOfBounds(pos){
return (pos[0] < 0 || pos[0] > 1400 ||
pos[1] < 0 || pos[1] > 700);
}
move() {
this.pos = [this.pos[0] + this.vel[0], this.pos[1] + this.vel[1]];
if(this.outOfBounds(this.pos)){
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;
}
}
}
export default Enemy;