-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathComponent.js
117 lines (99 loc) · 2.79 KB
/
Component.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
// General class into Canvas
class Component {
constructor(width, height, color, x, y) {
this.width = width;
this.height = height;
this.x = x;
this.y = y;
this.color = color;
this.speedX = 0;
this.speedY = 0;
};
update() {
const ctx = gameArea.context;
ctx.lineWidth = 20;
ctx.fillStyle = this.color;
ctx.fillRect(this.x, this.y, this.width, this.height);
};
newPosition() {
this.x += this.speedX;
this.y += this.speedY;
};
stopMoving() {
this.speedX = 0;
this.speedY = 0;
};
crashWith(obstacle) {
// Bird possition
let birdLeft = this.x;
let birdRight = this.x + this.width;
let birdTop = this.y;
let birdBottom = this.y + this.height;
// Obstacle possition
let obstacleLeft = obstacle.x;
let obstacleRight = obstacle.x + obstacle.width;
let obstacleTop = obstacle.y;
let obstacleBottom = obstacle.y + obstacle.height;
if ((birdBottom > obstacleTop) && (birdTop < obstacleBottom) && (birdRight > obstacleLeft) && (birdLeft < obstacleRight)) {
// CRASH!
return true;
}
// MISS!
return false;
}
}
// Class for Text into Canvas
class ComponentText extends Component {
constructor(width, height, color, x, y, text, fontWeight, fontStyle) {
super(width, height, color, x, y);
this.text = text;
this.fontWeight = fontWeight;
this.fontStyle = fontStyle;
}
update(scoreText) {
const ctx = gameArea.context;
ctx.lineWidth = 1.5;
ctx.font = this.fontWeight + " " + this.fontStyle;
ctx.fillStyle = this.color;
ctx.fillText(scoreText, this.x, this.y);
}
}
// Class for Image into Canvas
class ComponentImage extends Component {
constructor(width, height, color, x, y) {
super(width, height, color, x, y);
this.image = new Image();
this.image.src = color;
this.gravity = 0.2;
this.gravitySpeed = 0;
}
update() {
const ctx = gameArea.context;
ctx.lineWidth = 20;
ctx.drawImage(this.image, this.x, this.y, this.width, this.height);
}
newPosition() {
this.gravitySpeed += this.gravity;
this.x += this.speedX;
this.y += this.speedY + this.gravitySpeed;
this.gravity = 0.2;
}
// Control the bird by space bar
move(x) {
this.gravity = x;
this.gravitySpeed = this.gravity;
};
/* Control the bird by arrows
moveUp() {
this.speedY -= 1.5;
};
moveDown() {
this.speedY += 1.5;
};
moveLeft() {
this.speedX -= 1.5;
};
moveRight() {
this.speedX += 1.5;
};*/
}