-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.js
135 lines (115 loc) · 3.92 KB
/
models.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
'use strict';
class Shape {
constructor(positionX, positionY, colorString) {
this.positionX = positionX;
this.positionY = positionY;
this.colorString = colorString;
}
}
class Rectangle extends Shape {
constructor(positionX, positionY, colorString, width, height) {
super(positionX, positionY, colorString);
this.width = width;
this.height = height;
}
draw() {
canvasContext.fillStyle = this.colorString;
canvasContext.fillRect(this.positionX, this.positionY, this.width, this.height);
canvasContext.closePath();
}
}
class PlayerPad extends Rectangle {
constructor(positionX, positionY, colorString, width, height) {
super(positionX, positionY, colorString, width, height);
}
move(distance, rightLimit, isKeyEvent = false) {
const padOrigen = isKeyEvent ? (this.width / 2) : 0;
const newPosition = (this.positionX + distance) - padOrigen;
if (newPosition <= rightLimit && newPosition >= 0)
this.positionX = newPosition;
}
}
class Circle extends Shape {
constructor(positionX, positionY, colorString, radius) {
super(positionX, positionY, colorString);
this.radius = radius;
}
draw() {
canvasContext.beginPath();
canvasContext.fillStyle = this.colorString;
canvasContext.arc(this.positionX, this.positionY, this.radius, 0, 180);
canvasContext.fill();
canvasContext.closePath();
}
}
class Brick extends Rectangle {
constructor(positionX, positionY, colorString, width, height) {
super(positionX, positionY, colorString, width, height);
}
hitted() {
this.colorString = `hsl(0, 0%, 30%)`;
const index = state.bricks.indexOf(this);
state.bricks.splice(index, 1);
}
}
class bouncingBall extends Circle {
constructor(positionX, positionY, colorString, radius, velocityX, velocityY) {
super(positionX, positionY, colorString, radius);
this.velocityX = velocityX;
this.velocityY = velocityY;
}
revertVelocity(axis) {
playAudio(boomAudio);
this[`velocity${axis}`] *= -1;
}
updatePosition(axis, axisLimit) {
const newPosition = this[`position${axis}`] + this[`velocity${axis}`];
if (newPosition + this.radius >= axisLimit || newPosition <= this.radius)
this.revertVelocity(axis);
this[`position${axis}`] = newPosition;
}
restartPosition() {
const safeArea = this.radius * 2;
const randomPositionX = Math.random() * (canvas.width - safeArea);
this.positionX = safeArea + randomPositionX;
this.positionY = safeArea;
if (Math.random() > 0.5) this.revertVelocity("X");
}
checkBoundariesCollision(rightLimit, bottomLimit) {
if (this.positionY + this.radius + this.velocityY >= bottomLimit) {
state.lives--;
playAudio(liveLostAudio);
this.restartPosition();
} else {
this.updatePosition("X", rightLimit);
this.updatePosition("Y", bottomLimit);
}
}
didCollideWith(otherShape) {
const circunference = this.radius - Math.PI / 2;
if (
this.positionX - circunference <
otherShape.positionX + otherShape.width &&
this.positionX + circunference > otherShape.positionX &&
this.positionY - circunference <
otherShape.positionY + otherShape.height &&
this.positionY + circunference > otherShape.positionY
)
this.changeDirection(otherShape, circunference);
}
changeDirection(otherShape, circunference) {
const touchedUpOrDowm =
this.positionX - circunference >= otherShape.positionX &&
this.positionX - circunference <= otherShape.positionX + otherShape.width;
const touchedSides =
this.positionY + circunference >= otherShape.positionY &&
this.positionY + circunference <= otherShape.positionY + otherShape.height;
if (touchedUpOrDowm) this.revertVelocity("Y");
else if (touchedSides) this.revertVelocity("X");
else {
this.revertVelocity("Y");
this.revertVelocity("X");
}
if (otherShape instanceof Brick) otherShape.hitted();
}
}