-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
205 lines (181 loc) · 4.72 KB
/
main.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
(function(){
self.Board = function(width, height) {
this.width = width;
this.height = height;
this.playing = false;
this.game_over = false;
this.bars = [];
this.ball = null;
}
self.Board.prototype = {
get elements() {
var elements = this.bars.map(function(bar){ return bar });
elements.push(this.ball);
return elements;
}
}
})();
(function(){
self.Ball = function(x,y,radius,board){
this.x = x;
this.y = y;
this.radius = radius;
this.speed_y = 0;
this.speed_x = 3;
this.board = board;
this.direction = 1;
this.bounce_angle = 0;
this.max_bounce_angle = Math.PI / 12;
this.speed = 3;
board.ball = this;
this.kind = 'circle';
}
self.Ball.prototype = {
move: function(){
this.x += (this.speed_x * this.direction);
this.y += (this.speed_y);
},
get width(){
return this.radius * 2;
},
get height(){
return this.radius * 2;
},
collision: function(bar){
//Reacciona a la colision con una barra que recibe como parametro
var relative_intersect_y = ( bar.y + (bar.height / 2) ) - this.y;
var normalized_intersect_y = relative_intersect_y / (bar.height / 2);
this.bounce_angle = normalized_intersect_y * this.max_bounce_angle;
this.speed_y = this.speed * -Math.sin(this.bounce_angle);
this.speed_x = this.speed * Math.cos(this.bounce_angle);
if(this.x > (this.board.width / 2)) this.direction = -1;
else this.direction = 1;
}
}
})();
(function(){
self.Bar = function(x, y, width, height, board) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.board = board;
this.board.bars.push(this);
this.kind = 'rectangle';
this.speed = 20;
}
self.Bar.prototype = {
down: function(){
this.y += this.speed;
},
up: function(){
this.y -= this.speed;
},
toString: function() {
return "x: " + this.x + " y: " + this.y;
}
}
})();
(function(){
self.BoardView = function(canvas, board) {
this.canvas = canvas;
this.canvas.width = board.width;
this.canvas.height = board.height;
this.board = board;
this.ctx = canvas.getContext("2d");
}
self.BoardView.prototype = {
clean: function() {
this.ctx.clearRect(0,0,this.board.width,this.board.height);
},
draw: function(){
for(var i = this.board.elements.length -1;i >= 0;i--){
var el = this.board.elements[i];
draw(this.ctx,el)
}
},
check_collisions: function(){
for (var i = this.board.bars.length - 1; i >= 0; i--) {
var bar = this.board.bars[i]
if(hit(bar, this.board.ball)){
this.board.ball.collision(bar);
}
};
},
play: function(){
if (this.board.playing){
this.clean();
this.draw();
this.check_collisions();
this.board.ball.move();
}
}
}
function hit(a,b){
//Check if a collisions with b
var hit = false;
// Colisiones horizontales
if(b.x + b.width >= a.x && b.x < a.x + a.width){
// Colisiones verticales
if (b.y + b.height >= a.y && b.y < a.y + a.height){
hit = true
}
}
// Colisiones de a con b
if(b.x <= a.x && b.x + b.width >= a.x + a.width){
if(b.y <= a.y && b.y + b.height >= a.y + a.height){
hit = true;
}
}
// Colisiones de b con a
if(a.x <= b.x && a.x + a.width >= b.x + b.width){
if(a.y <= b.y && a.y + a.height >= b.y + b.height){
hit = true;
}
}
return hit;
}
function draw(ctx, element){
switch(element.kind){
case 'rectangle' :
ctx.fillRect(element.x, element.y, element.width, element.height)
break;
case 'circle' :
ctx.beginPath();
ctx.arc(element.x, element.y, element.radius,0,7);
ctx.fill();
ctx.closePath();
break;
}
}
})();
var board = new Board(800,400);
var bar = new Bar(20,100,40,100,board);
var bar_2 = new Bar(735,100,40,100,board);
var canvas = document.getElementById('canvas');
var board_view = new BoardView(canvas, board);
var ball = new Ball(350,100,10,board);
document.addEventListener("keydown", function(ev){
if(ev.keyCode == 38){
ev.preventDefault();
bar_2.up();
} else if(ev.keyCode == 40){
ev.preventDefault();
bar_2.down();
} else if(ev.keyCode == 87){
ev.preventDefault();
bar.up();
} else if(ev.keyCode == 83){
ev.preventDefault();
bar.down();
} else if (ev.keyCode == 32) {
ev.preventDefault();
board.playing = !board.playing
}
});
board_view.draw();
window.requestAnimationFrame(controller);
function controller() {
board_view.play();
window.requestAnimationFrame(controller);
}