-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathball.js
196 lines (180 loc) · 6.35 KB
/
ball.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
/*
Ball class
*/
import { walls } from './wall.js';
import { fps_ratio, canvas, ctx, timer } from './game.js';
import { grid_size, gridsafe } from './grid.js';
export const balls = [];
export class Ball {
static speed = 4;
static radius = 12;
static sq_radius = Math.pow(12, 2);
static colours = [
"purple",
"blue",
"green",
"#f4a32e",
"#ff4c00",
"#9a0200",
"#da482e",
"#0a827c",
"#043836",
"black",
];
constructor(i, x, y) {
this.i = i; // index in ball array
this.x = x;
this.y = y;
this.dx = Ball.speed;
this.dy = -Ball.speed;
this.colour = Ball.colours[i];
// left is 0, right is 1
}
collideWithBall(x, y) {
return Ball.sq_radius > Math.pow(this.x - x, 2) + Math.pow(this.y - y, 2);
}
draw(x, y) {
ctx.beginPath();
ctx.arc(this.x, this.y, Ball.radius, 0, Math.PI * 2);
ctx.fillStyle = this.colour;
ctx.fill();
ctx.closePath();
}
move(delta) {
if (timer.ballPause > 0) {
timer.ballPause--;
return;
}
const calcPotentialX = () => this.x + (this.dx * fps_ratio(delta))
const calcPotentialY = () => this.y + (this.dy * fps_ratio(delta))
let potentialX = calcPotentialX();
let potentialY = calcPotentialY();
/* Bounce off canvas edges */
let hit = false;
if (
potentialX > canvas.width - Ball.radius ||
potentialX < Ball.radius
) {
hit = true;
this.dx = Ball.bounce(this.dx);
potentialX = calcPotentialX();
}
if (
potentialY < Ball.radius ||
potentialY > canvas.height - Ball.radius
) {
hit = true;
this.dy = Ball.bounce(this.dy);
potentialY = calcPotentialY();
}
this.x = potentialX;
this.y = potentialY;
if (!hit) {
for (let ball of balls) {
if (ball.i == this.i) { continue }
if (this.collideWithBall(ball.x, ball.y)) {
hit = true;
if (Math.random() > 0.5) {
this.dx = Ball.bounce(this.dx);
} else {
this.dy = Ball.bounce(this.dy);
}
}
}
}
if (!hit) {
hit = this.collideWithWalls(delta)
} else {
hit = -1;
}
return hit;
}
static bounce(speed) {
/* Reverse ball direction and add some random angle */
speed = -speed;
if ((Ball.speed - (Math.abs(speed))) > 0.7) {
if (speed < 0) {
speed = -Ball.speed;
} else {
speed = Ball.speed;
}
}
if (speed < 0) {
speed += Math.random() / 4;
} else {
speed -= Math.random() / 4;
}
return speed;
}
/** Returns true if the hypothesized x-coord would intersect the given wall */
intersectsX(x, wall) {
let y = gridsafe(this.y - Ball.radius);
return (
x + Ball.radius > wall.x && x - Ball.radius < wall.x + wall.width &&
wall.y + wall.height > y - Ball.radius && wall.y < y + Ball.radius &&
x < canvas.width - Ball.radius && x > Ball.radius
)
}
/** Returns true if the hypothesized y-coord would intersect the given wall */
intersectsY(y, wall) {
let x = gridsafe(this.x - Ball.radius);
return (
y + Ball.radius > wall.y && y - Ball.radius < wall.y + wall.height &&
wall.x + wall.width > x - Ball.radius && wall.x < x + Ball.radius &&
y > Ball.radius && y < canvas.height - Ball.radius
)
}
collideWithWalls(delta) {
let new_hit = false;
let grid_aligned_boundary_coord;
// Iterate over each wall
// 0 is vertical, 1 is horizontal
for (let i = 0; i < walls.length; i++) {
if (walls[i].dir == 0) {
// if wall is vertical
if (this.dx < 0) {
// if ball is travelling left
grid_aligned_boundary_coord = gridsafe(this.x)
} else {
// if ball is travelling right
grid_aligned_boundary_coord = gridsafe(this.x + Ball.radius)
}
// Ensure new X position does not continue to intersect the wall
if (this.intersectsX(grid_aligned_boundary_coord, walls[i])) {
new_hit = true;
if (this.intersectsX(grid_aligned_boundary_coord + Ball.bounce(this.dx) * fps_ratio(delta), walls[i])) {
this.x = this.dx > 0 ? grid_aligned_boundary_coord - grid_size : grid_aligned_boundary_coord + grid_size
}
this.dx = Ball.bounce(this.dx);
}
} else {
// if wall is horizontal
if (this.dy < 0) {
// if ball is travelling up
grid_aligned_boundary_coord = gridsafe(this.y)
} else {
// if ball is travelling down
grid_aligned_boundary_coord = gridsafe(this.y + Ball.radius)
}
// Ensure new Y position does not continue to intersect the wall
if (this.intersectsY(grid_aligned_boundary_coord, walls[i])) {
new_hit = true;
if (this.intersectsY(grid_aligned_boundary_coord + Ball.bounce(this.dy) * fps_ratio(delta), walls[i])) {
this.y = this.dy > 0 ? grid_aligned_boundary_coord - grid_size : grid_aligned_boundary_coord + grid_size
}
this.dy = Ball.bounce(this.dy);
}
}
// If a hit/collision occurred, return the index of the wall this ball collided with
if (new_hit) {
if (walls[i].building === null) {
// If this wall isn't a building yet, return its index to destroy it
return i;
} else {
// If no hit/collision occurred, return -1
return -1;
}
}
}
}
}