-
Notifications
You must be signed in to change notification settings - Fork 0
/
breakout.js
154 lines (131 loc) · 3.54 KB
/
breakout.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
function Breakout(presenter) {
var delay = 30;
var speed = 10;
var self = this;
var gridRows;
var gridCols;
var gridWidth;
var gridHeight;
var paddleRadius;
var ballRadius;
var brickWidth;
var interval;
var v;
var w;
var x;
var y;
var paddleX;
function loop() {
var xp = x + v;
var yp = y + w;
if (xp < ballRadius || xp + ballRadius > gridWidth) {
v = -v;
} else if (yp < ballRadius) {
w = -w;
} else if (yp + ballRadius > gridHeight) {
var theta = (xp - paddleX) / paddleRadius;
if (theta > -1 && theta < 1) {
theta = theta * Math.PI * 0.4;
v = speed * Math.sin(theta);
w = -speed * Math.cos(theta);
} else {
clearInterval(interval);
presenter.reset();
interval = 0;
}
} else {
var valid = true;
var j0 = Math.floor((yp - ballRadius) / brickHeight);
var j1 = Math.floor((yp + ballRadius) / brickHeight);
if (v > 0) {
var i = Math.floor((xp + ballRadius) / brickWidth);
if (i != Math.floor((x + ballRadius) / brickWidth)) {
var valid0 = presenter.breakBrick(i, j0);
var valid1 = presenter.breakBrick(i, j1);
if (valid0 || valid1) {
v = -v;
valid = false;
}
}
} else {
var i = Math.floor((xp - ballRadius) / brickWidth);
if (i != Math.floor((x - ballRadius) / brickWidth)) {
var valid0 = presenter.breakBrick(i, j0);
var valid1 = presenter.breakBrick(i, j1);
if (valid0 || valid1) {
v = -v;
valid = false;
}
}
}
var i0 = Math.floor((xp - ballRadius) / brickWidth);
var i1 = Math.floor((xp + ballRadius) / brickWidth);
if (w > 0) {
var j = Math.floor((yp + ballRadius) / brickHeight);
if (j != Math.floor((y + ballRadius) / brickHeight)) {
var valid0 = presenter.breakBrick(i0, j);
var valid1 = presenter.breakBrick(i1, j);
if (valid0 || valid1) {
w = -w;
if (valid) {
valid = false;
} else {
v = -v;
}
}
}
} else {
var j = Math.floor((yp - ballRadius) / brickHeight);
if (j != Math.floor((y - ballRadius) / brickHeight)) {
var valid0 = presenter.breakBrick(i0, j);
var valid1 = presenter.breakBrick(i1, j);
if (valid0 || valid1) {
w = -w;
if (valid) {
valid = false;
} else {
v = -v;
}
}
}
}
if (valid) {
x = xp;
y = yp;
presenter.moveTo(x, y);
}
}
}
this.load = function(rows, cols) {
gridRows = rows;
gridCols = cols;
presenter.model = self;
presenter.createGrid(rows, cols);
presenter.reset();
interval = 0;
};
this.scroll = function(paddle) {
paddleX = paddle;
if (!interval) {
x = paddle;
presenter.moveTo(x, gridHeight - ballRadius);
}
};
this.setGrid = function(width, height, brick, paddle, ball) {
gridWidth = width;
gridHeight = height;
paddleRadius = paddle;
ballRadius = ball;
brickWidth = width / gridCols;
brickHeight = brick;
y = gridHeight - ballRadius;
};
this.start = function() {
if (!interval) {
var theta = (Math.random() - 0.5) * Math.PI * 0.8
v = speed * Math.sin(theta);
w = -speed * Math.cos(theta);
interval = setInterval(loop, delay);
}
};
}