-
Notifications
You must be signed in to change notification settings - Fork 0
/
ball.js
159 lines (118 loc) · 4.03 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
import {width as screenWidth, height as screenHeight, bounds} from './data.js';
import {aabb, partial_collision} from './collision.js';
import {Graphics} from './pixi.min.mjs';
class BallEvents {
onInit = [];
onMove = [];
onGoal = [];
onPaddleCollide = [];
onBoundsCollide = [];
call(listener, event) {
for(let func of this[listener]) {
func(event);
}
}
}
class Ball extends Graphics {
constructor(paddle) {
super();
this.beginFill(0xffffff);
this.drawRect(0, 0, 20, 20);
this.listener = new BallEvents();
this.paddle = paddle;
this.goal = bounds.goal;
this.obstacle = bounds.arena;
this.#init();
}
#init() {
let params = {};
// Selection between 30 & 150 degrees (game canvas is first quadrant flipped over the x-axis)
params.direction = (Math.random() * 4 * Math.PI + Math.PI) / 6;
params.speed = 7;
// Center ball & initialize velocity (dy, dx)
params.x = (screenWidth - this.width) / 2;
params.y = (screenHeight - this.height) / 2;
params.dx = Math.cos(params.direction) * params.speed;
params.dy = Math.sin(params.direction) * params.speed;
let event = {'event': params, 'object': this, 'canceled': false};
this.listener.call("onInit", event);
if(!event['canceled']) {
for(let field in params)
this[field] = params[field];
}
}
move() {
if(aabb(this, this.goal)['collision']) {
let event = {'object': this, 'canceled': false};
this.listener.call("onGoal", event);
if(!event['canceled'])
this.#init();
return;
}
if(aabb(this, this.paddle)['collision']) {
let params = {};
let paddle_center = this.paddle.width / 2;
// Position of ball relative to paddle center
let x_relative = (this.x + this.dx + this.width / 2) - (this.paddle.x + this.paddle.dx + paddle_center) ;
// Angle ball should bounce off paddle, defined in quadrant 2 (game canvas is first quadrant flipped over x-axis)
let quad2angle = Math.PI * 3 / 4;
let up = Math.PI / 2;
// This equation satisfies 3 main properties
// 1. if x is -paddle_center (left edge), ball bounces at quad2angle
// 2. if x is 0 (middle), ball bounces at 90 degrees
// 3. if x is +paddle_center (right edge), ball bounces at 180 degrees - quad2angle
let direction = up + (x_relative * (up - quad2angle)) / paddle_center;
// Increase speed by 0.5, reset to 7 if direction is within 4 degrees of 90
params.speed = Math.min(21, this.speed + 0.5);
if(Math.abs(up - direction) <= Math.PI / 45)
params.speed = 7;
params.dx = Math.cos(direction) * params.speed;
params.dy = Math.sin(direction) * params.speed;
params.x = this.x + params.dx;
params.y = this.y + params.dy;
let event = {'event': params, 'object': this, 'canceled': false};
this.listener.call("onPaddleCollide", event);
if(!event['canceled']) {
for(let field in params)
this[field] = params[field];
}
return;
}
// partial_collision is good for keeping an object inside of an area
let collide = partial_collision(this, this.obstacle);
if(collide['collision']) {
let params = {};
if(collide['x']) {
params.x = this.obstacle.x;
if(this.dx > 0)
params.x += this.obstacle.width - this.width;
params.dx = this.dx * -1;
}
if(collide['y']) {
params.y = this.obstacle.y;
if(this.dy > 0)
params.y += this.obstacle.height - this.height;
params.dy = this.dy * -1;
}
let event = {'event': params, 'object': this, 'canceled': false};
this.listener.call("onBoundsCollide", event);
if(!event['canceled']) {
for(let field in params)
this[field] = params[field];
}
}
let params = {};
params.x = this.x + this.dx;
params.y = this.y + this.dy;
params.dx = this.dx;
params.dy = this.dy;
let event = {'event': params, 'object': this, 'canceled': false};
this.listener.call("onMove", event);
if(!event['canceled']) {
for(let field in params) {
this[field] = params[field];
}
}
}
}
export default Ball;