-
Notifications
You must be signed in to change notification settings - Fork 0
/
pong.js
230 lines (194 loc) · 6.5 KB
/
pong.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
let anaglyph;
let fontValorax;
const BOX_WIDTH = 700;
const BOX_HEIGHT = 400;
const BOX_DEPTH = 600;
const BALL_RADIUS = 25;
const PADDLE_WIDTH = 100;
const PADDLE_HEIGHT = 70;
const PADDLE_DEPTH = 16;
let position;
let acceleration;
let gameOn = false;
let gameOver = false;
let score = 0;
function preload() {
fontValorax = loadFont('fonts/Valorax.otf');
}
function setup() {
const r = createCanvas(windowWidth, windowHeight, WEBGL);
const gl = r.canvas.getContext('webgl2');
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
position = createVector(0, 0, 0);
acceleration = createVector(5, 5, 12);
anaglyph = createAnaglyph(this);
}
function draw() {
anaglyph.draw(scene);
}
function hfov_from_vfov(vfov, aspectRatio) {
return 2 * atan(aspectRatio * tan(vfov * 0.5));
}
function scene(pg) {
pg.background(0);
// Draw the box
pg.push();
pg.stroke(255);
pg.noFill();
pg.translate(0, 0, 0);
pg.box(BOX_WIDTH, BOX_HEIGHT, BOX_DEPTH);
pg.pop();
// Draw the paddle
pg.push();
const paddleZ = BOX_DEPTH/2 + PADDLE_DEPTH/2;
let paddleX = 0;
let paddleY = 0;
// If in-game, have paddle follow the mouse
if(gameOn) {
// -- Get the mouse X and Y coords in 3D system
let paddleMouseX = mouseX - windowWidth/2;
let paddleMouseY = mouseY - windowHeight/2;
// -- Get some constants about the world and camera
const vfov = PI/3;
const hfov = hfov_from_vfov(vfov, windowWidth/windowHeight);
const eyeZ = (windowHeight/2) / (tan(PI/6));
const worldWidth = tan(hfov/2) * (9/10) * eyeZ * 2;
const worldHeight = tan(vfov/2) * (9/10) * eyeZ * 2;
// -- Normalize the mouse X and Y to the world size
paddleMouseX = map(paddleMouseX, -windowWidth/2, windowWidth/2, -worldWidth/2, worldWidth/2);
paddleMouseY = map(paddleMouseY, -windowHeight/2, windowHeight/2, -worldHeight/2, worldHeight/2);
// -- Project mouse X and Y to the paddle's Z plane to get paddle X and Y
const thetaX = atan(paddleMouseX / (9 * eyeZ / 10));
paddleX = tan(thetaX) * (eyeZ - paddleZ);
const thetaY = atan(paddleMouseY / (9 * eyeZ / 10));
paddleY = tan(thetaY) * (eyeZ - paddleZ);
// -- Make sure paddle's X and Y are within bounds of the box
if(paddleX - PADDLE_WIDTH/2 < -BOX_WIDTH/2) {
paddleX = -BOX_WIDTH/2 + PADDLE_WIDTH/2;
}
if(paddleX + PADDLE_WIDTH/2 > BOX_WIDTH/2) {
paddleX = BOX_WIDTH/2 - PADDLE_WIDTH/2;
}
if(paddleY - PADDLE_HEIGHT/2 < -BOX_HEIGHT/2) {
paddleY = -BOX_HEIGHT/2 + PADDLE_HEIGHT/2;
}
if(paddleY + PADDLE_HEIGHT/2 > BOX_HEIGHT/2) {
paddleY = BOX_HEIGHT/2 - PADDLE_HEIGHT/2;
}
}
// -- Render the paddle
pg.translate(paddleX, paddleY, paddleZ);
pg.stroke(255);
pg.strokeWeight(10);
pg.noFill();
pg.box(PADDLE_WIDTH, PADDLE_HEIGHT, PADDLE_DEPTH);
pg.pop();
// Draw the ball
pg.push();
pg.fill(255);
// -- Move the ball as long as we're not on the Game Over screen
if(!gameOver) {
position = position.add(acceleration);
if(position.x - BALL_RADIUS < -(BOX_WIDTH/2) || position.x + BALL_RADIUS > BOX_WIDTH/2) {
acceleration.x *= -1;
}
if(position.y - BALL_RADIUS < -(BOX_HEIGHT/2) || position.y + BALL_RADIUS > BOX_HEIGHT/2) {
acceleration.y *= -1;
}
if(position.z - BALL_RADIUS < -(BOX_DEPTH/2)) {
acceleration.z *= -1;
} else if(position.z + BALL_RADIUS > BOX_DEPTH/2) {
// If currently in a game, check if ball hit the paddle
if(gameOn) {
// Check if the ball hit the paddle
if(position.x > paddleX - PADDLE_WIDTH/2
&& position.x < paddleX + PADDLE_WIDTH/2
&& position.y > paddleY - PADDLE_HEIGHT/2
&& position.y < paddleY + PADDLE_HEIGHT/2) {
// Ball hit the paddle; bounce it and increase score
score++;
acceleration.z *= -1;
} else {
// Ball missed the paddle; game over
setTimeout(() => {
gameOn = false;
gameOver = true;
}, 250);
}
} else {
// If on the main menu, just keep bouncing
acceleration.z *= -1;
}
}
}
pg.translate(position.x, position.y, position.z);
pg.sphere(BALL_RADIUS);
pg.pop();
// Render text and buttons (main menu, game over screen, or score if game is in progress)
// gameOn = game is in progress
// gameOver = game just ended (show game over screen)
// neither is true = show main menu
/*
When starting a game, toggle gameOn = true, gameOver = false, and add "in-game" class to <main>
When game is over, toggle gameOn = false, gameOver = true, and remove "in-game" class from <main>
When returning to main menu, toggle gameOn = false, gameOver = false
*/
if(gameOn) {
// Render score
pg.push();
pg.fill(255);
pg.textFont(fontValorax, 30);
pg.text(`Score: ${score}`, -BOX_WIDTH/2, -BOX_HEIGHT/2, BOX_WIDTH, 200);
pg.pop();
} else if(gameOver) {
// Render Game Over screen
pg.push();
pg.fill(255);
pg.textFont(fontValorax, 60);
pg.textAlign(CENTER);
pg.text("Game Over", -BOX_WIDTH/2, -BOX_HEIGHT/2, BOX_WIDTH, 300);
pg.text(`Final Score: ${score}`, -BOX_WIDTH/2, -BOX_HEIGHT/2 + 100, BOX_WIDTH, 300);
const nMouseX = mouseX - windowWidth / 2;
const nMouseY = mouseY - windowHeight / 2;
if(nMouseX > -160 && nMouseX < 160
&& nMouseY > 80 && nMouseY < 240) {
pg.fill(166);
}
pg.textFont(fontValorax, 50);
pg.text(">>Restart<<", -80, 200, 160, 160);
pg.pop();
} else {
// Render Main Menu
pg.push();
pg.fill(255);
pg.textFont(fontValorax, 75);
pg.textAlign(CENTER);
// -- Render game title
pg.text("Curveball 3D", -BOX_WIDTH/2, -BOX_HEIGHT/2, BOX_WIDTH, BOX_HEIGHT);
// -- Render button to start game
const nMouseX = mouseX - windowWidth / 2;
const nMouseY = mouseY - windowHeight / 2;
// I don't even understand these numbers
if(nMouseX > -160 && nMouseX < 160
&& nMouseY > 80 && nMouseY < 240) {
pg.fill(166);
}
pg.textFont(fontValorax, 50);
pg.text(">>Play<<", -80, 200, 160, 160);
pg.pop();
}
}
function mouseClicked() {
const nMouseX = mouseX - windowWidth / 2;
const nMouseY = mouseY - windowHeight / 2;
if(nMouseX > -160 && nMouseX < 160
&& nMouseY > 80 && nMouseY < 240) {
if(gameOver || (!gameOver && !gameOn)) {
gameOn = true;
gameOver = false;
score = 0;
position = createVector(0, 0, 0);
acceleration = createVector(5, 5, 12);
}
}
}