forked from vnglst/pong-wars
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.html
512 lines (432 loc) · 17 KB
/
graph.html
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Hexapong wars | swawa_yu</title>
<meta name="description"
content="The eternal battle between day and night, good and bad, on a hexagon map. Written in JavaScript with some HTML & CSS in one index.html." />
<link rel="author" href="https://swawa.dev" />
<meta name="theme-color" content="#172B36" />
<meta name="creator" content="swawa_yu" />
<link rel="icon" href="https://raw.githubusercontent.com/swawa-yu/icons/main/mel_icon.ico" type="image/x-icon">
<meta property="og:title" content="Hexapong Wars" />
<meta property="og:image" content="https://raw.githubusercontent.com/swawa-yu/hexapong-wars/main/image.png" />
<meta property="og:url" content="https://swawa-yu.github.io/hexapong-wars/">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:site" content="@swawa_yu">
<meta name="twitter:creator" content="@swawa_yu">
<meta name="twitter:title" content="Hexapong Wars">
<meta name="twitter:image" content="https://raw.githubusercontent.com/swawa-yu/hexapong-wars/main/image.png">
<style>
html {
height: 100%;
}
body {
height: 100%;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
background: linear-gradient(to bottom, #172b36 0%, #d9e8e3 100%);
}
#container {
display: flex;
align-items: center;
flex-direction: column;
width: min(70vh, 80%);
max-width: 600px;
height: 100%;
}
canvas {
display: block;
border-radius: 4px;
overflow: hidden;
width: 100%;
margin-top: auto;
margin-bottom: auto;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.2);
}
#score {
font-family: monospace;
margin-top: 30px;
font-size: 16px;
padding-left: 20px;
color: #172b36;
}
#made {
text-align: center;
line-height: 1.5;
font-family: monospace;
margin-top: auto;
margin-bottom: 20px;
font-size: 10px;
}
#made a {
color: #172b36;
}
</style>
</head>
<body>
<div id="container">
<canvas id="pongCanvas" width="800" height="800"></canvas>
<div id="score"></div>
<canvas id="graph" width="1200" height="150"></cancas>
<p id="made">
made by
<a href="https://swawa.dev">swawa_yu</a> |
source on
<a href="https://github.com/swawa-yu/hexapong-wars">GitHub</a>
</p>
</div>
</body>
<script>
const colorPalette = {
ArcticPowder: "#F1F6F4",
MysticMint: "#D9E8E3",
Forsythia: "#FFC801",
DeepSaffron: "#FF9932",
NocturnalExpedition: "#114C5A",
OceanicNoir: "#172B36",
};
const canvas = document.getElementById("pongCanvas");
const graph = document.getElementById("graph");
const ctx = canvas.getContext("2d");
console.log("graph", graph);
const ctxGraph = graph.getContext("2d", { willReadFrequently: true });
const scoreElement = document.getElementById("score");
const DAY_COLOR = colorPalette.MysticMint;
const DAY_BALL_COLOR = colorPalette.NocturnalExpedition;
const NIGHT_COLOR = colorPalette.NocturnalExpedition;
const NIGHT_BALL_COLOR = colorPalette.MysticMint;
const NEITHER_COLOR = colorPalette.ArcticPowder;
const WALL_COLOR = colorPalette.DeepSaffron;
const HEXAGON_SIZE = 10;
const BALL_RADIUS = HEXAGON_SIZE / 1.5;
const MID_SPEED = HEXAGON_SIZE / 2;
const MIN_SPEED = MID_SPEED / 1.5;
const MAX_SPEED = MID_SPEED * 1.5;
const HEXAGON_MAP_LEVEL = 18;
let dayScore = 0;
let nightScore = 0;
const hexagons = getHexagons(HEXAGON_MAP_LEVEL, HEXAGON_SIZE);
const numHexagons = hexagons.length;
const angle0 = Math.random() * Math.PI * 2;
const angle1 = Math.random() * Math.PI * 2;
const balls = [
{
x: -canvas.width / 4,
y: 0,
dx: MID_SPEED * Math.cos(angle0),
dy: MID_SPEED * Math.sin(angle0),
reverseColor: DAY_COLOR,
ballColor: DAY_BALL_COLOR,
},
{
x: canvas.width / 4,
y: 0,
dx: MID_SPEED * Math.cos(angle1),
dy: MID_SPEED * Math.sin(angle1),
reverseColor: NIGHT_COLOR,
ballColor: NIGHT_BALL_COLOR,
},
];
let iteration = 0;
function drawBall(ball) {
ctx.beginPath();
ctx.arc(ball.x + canvas.width / 2, ball.y + canvas.height / 2, BALL_RADIUS, 0, Math.PI * 2, false);
ctx.fillStyle = ball.ballColor;
ctx.fill();
ctx.closePath();
}
function isOverlapping(r, R, hexCenter, circleCenter) {
const [hx, hy] = hexCenter;
const [cx, cy] = circleCenter;
if (Math.sqrt((hx - cx) ** 2 + (hy - cy) ** 2) > r / Math.cos(Math.PI / 6) + R) {
return false;
}
const vertices = [];
for (let i = 0; i < 6; i++) {
const angle = (60 * i) * Math.PI / 180;
const vx = hx + r * Math.cos(angle + Math.PI / 6);
const vy = hy + r * Math.sin(angle + Math.PI / 6);
vertices.push([vx, vy]);
}
function isPointInHexagon(px, py) {
const translatedX = px - hx;
const translatedY = py - hy;
const q2 = Math.sqrt(3) * Math.abs(translatedY);
if (q2 > 3 * r) {
return false;
}
return Math.abs(translatedX) <= Math.min(r, 2 * r - q2);
}
if (isPointInHexagon(cx, cy)) {
return true;
}
function lineCircleIntersection(x1, y1, x2, y2, cx, cy, r) {
const acx = cx - x1;
const acy = cy - y1;
const abx = x2 - x1;
const aby = y2 - y1;
const ab_ab = abx * abx + aby * aby;
const ab_ac = abx * acx + aby * acy;
const t = ab_ac / ab_ab;
const hpx = x1 + t * abx;
const hpy = y1 + t * aby;
const hcx = hpx - cx;
const hcy = hpy - cy;
if (t >= 0 && t <= 1) {
return hcx * hcx + hcy * hcy <= r * r;
} else if (t < 0) {
return acx * acx + acy * acy <= r * r;
} else {
const bcx = cx - x2;
const bcy = cy - y2;
return bcx * bcx + bcy * bcy <= r * r;
}
}
for (let i = 0; i < 6; i++) {
const [x1, y1] = vertices[i];
const [x2, y2] = vertices[(i + 1) % 6];
if (lineCircleIntersection(x1, y1, x2, y2, cx, cy, R)) {
return true;
}
}
return false;
}
function forceValidPos(ball) {
// 壁でない六角形ののうち、近い方から3つ選び、それらの中心の平均に向ける
let fst = null;
let snd = null;
let trd = null;
let minDist = Infinity;
let sndDist = Infinity;
let trdDist = Infinity;
for (let i = 0; i < numHexagons - (HEXAGON_MAP_LEVEL + 1) * 6; i++) {
const hex = hexagons[i];
const dist = Math.sqrt((ball.x - hex.x) ** 2 + (ball.y - hex.y) ** 2);
if (dist < minDist) {
trd = snd;
trdDist = sndDist;
snd = fst;
sndDist = minDist;
fst = hex;
minDist = dist;
} else if (dist < sndDist) {
trd = snd;
trdDist = sndDist;
snd = hex;
sndDist = dist;
} else if (dist < trdDist) {
trd = hex;
trdDist = dist;
}
}
const speed = Math.sqrt(ball.dx ** 2 + ball.dy ** 2);
// 大きさを維持したまま、中心に向ける
const dx = (fst.x + snd.x + trd.x) / 3 - ball.x;
const dy = (fst.y + snd.y + trd.y) / 3 - ball.y;
const length = Math.sqrt(dx ** 2 + dy ** 2);
ball.dx = dx / length * speed;
ball.dy = dy / length * speed;
ball.x += dx;
ball.y += dy;
}
function checkCollision(ball) {
const collisionHexagons = [];
for (let i = 0; i < numHexagons; i++) {
const hex = hexagons[i];
if (isOverlapping(HEXAGON_SIZE, BALL_RADIUS, [hex.x, hex.y], [ball.x, ball.y])) {
collisionHexagons.push(hex);
}
}
collisionHexagons.sort((a, b) => {
return Math.sqrt((ball.x - a.x) ** 2 + (ball.y - a.y) ** 2) - Math.sqrt((ball.x - b.x) ** 2 + (ball.y - b.y) ** 2);
});
if (collisionHexagons.length === 0) {
console.log("error: ball not in any hexagons", ball);
forceValidPos(ball);
return;
}
if (collisionHexagons.length === 4) {
// ボールが小さければこうなることはない
console.log("error: ball in 4 hexagons", collisionHexagons);
}
if (collisionHexagons.length === 1) {
if (collisionHexagons[0].color === WALL_COLOR) {
console.log("error: ball in wall", collisionHexagons[0]);
forceValidPos(ball);
} else if (collisionHexagons[0].color === NEITHER_COLOR) {
collisionHexagons[0].color = ball.reverseColor;
} else if (collisionHexagons[0].color !== ball.reverseColor) {
collisionHexagons[0].color = ball.reverseColor;
}
}
if (collisionHexagons.length === 2) {
const current = collisionHexagons[0];
const next = collisionHexagons[1];
const nx = next.x - current.x;
const ny = next.y - current.y;
if (next.color === WALL_COLOR) {
reflect({ x: nx, y: ny }, ball);
} else if (next.color === NEITHER_COLOR) {
next.color = ball.reverseColor;
} else if (next.color !== ball.reverseColor) {
next.color = ball.reverseColor;
reflect({ x: nx, y: ny }, ball);
}
} else if (collisionHexagons.length === 3) {
const current = collisionHexagons[0];
const next = collisionHexagons[1];
const third = collisionHexagons[2];
const nx = next.x - current.x;
const ny = next.y - current.y;
const tx = third.x - current.x;
const ty = third.y - current.y;
if (next.color === WALL_COLOR) {
reflect({ x: nx, y: ny }, ball);
} else if (next.color === NEITHER_COLOR) {
next.color = ball.reverseColor;
} else if (next.color !== ball.reverseColor) {
next.color = ball.reverseColor;
reflect({ x: nx, y: ny }, ball);
}
if (isOverlapping(HEXAGON_SIZE, BALL_RADIUS, [third.x, third.y], [ball.x, ball.y])) {
if (third.color === WALL_COLOR) {
reflect({ x: tx, y: ty }, ball);
} else if (third.color === NEITHER_COLOR) {
third.color = ball.reverseColor;
} else if (third.color !== ball.reverseColor) {
third.color = ball.reverseColor;
reflect({ x: tx, y: ty }, ball);
}
}
}
function reflect(nv, ball) {
const nvLength = Math.sqrt(nv.x ** 2 + nv.y ** 2);
const nvUnit = { x: nv.x / nvLength, y: nv.y / nvLength };
const dot = ball.dx * nvUnit.x + ball.dy * nvUnit.y;
const reflectX = ball.dx - 2 * dot * nvUnit.x;
const reflectY = ball.dy - 2 * dot * nvUnit.y;
ball.dx = reflectX;
ball.dy = reflectY;
ball.x += ball.dx;
ball.y += ball.dy;
}
}
function addRandomness(ball) {
ball.dx += MID_SPEED * (Math.random() - 0.5) * 0.1;
ball.dy += MID_SPEED * (Math.random() - 0.5) * 0.1;
ball.dx = Math.min(Math.max(ball.dx, -MAX_SPEED), MAX_SPEED);
ball.dy = Math.min(Math.max(ball.dy, -MAX_SPEED), MAX_SPEED);
if (Math.abs(ball.dx) < MIN_SPEED)
ball.dx = ball.dx > 0 ? MIN_SPEED : -MIN_SPEED;
if (Math.abs(ball.dy) < MIN_SPEED)
ball.dy = ball.dy > 0 ? MIN_SPEED : -MIN_SPEED;
}
function getHexagons(level, size) {
const res = [];
for (let l = 0; l <= level; l++) {
if (l === 0) {
res.push({ i: res.length, x: 0, y: 0, color: NEITHER_COLOR });
} else {
for (let vertex = 0; vertex < 6; vertex++) {
const x = 2 * l * size * Math.cos(2 * Math.PI / 6 * vertex);
const y = 2 * l * size * Math.sin(2 * Math.PI / 6 * vertex);
for (let dir = 0; dir < l; dir++) {
const dx = 2 * dir * size * Math.cos(2 * Math.PI / 6 * vertex + 2 * Math.PI / 3);
const dy = 2 * dir * size * Math.sin(2 * Math.PI / 6 * vertex + 2 * Math.PI / 3);
res.push({ i: res.length, x: x + dx, y: y + dy, color: NEITHER_COLOR });
}
}
}
}
const l = level + 1;
for (let vertex = 0; vertex < 6; vertex++) {
const x = 2 * l * size * Math.cos(2 * Math.PI / 6 * vertex);
const y = 2 * l * size * Math.sin(2 * Math.PI / 6 * vertex);
for (let dir = 0; dir < l; dir++) {
const dx = 2 * dir * size * Math.cos(2 * Math.PI / 6 * vertex + 2 * Math.PI / 3);
const dy = 2 * dir * size * Math.sin(2 * Math.PI / 6 * vertex + 2 * Math.PI / 3);
res.push({ i: res.length, x: x + dx, y: y + dy, color: WALL_COLOR });
}
}
return res;
}
function drawHexagon(x, y, size, color) {
ctx.beginPath();
for (let p = 0; p < 6; p++) {
const t = 2 * Math.PI / 6 * p + Math.PI / 6;
ctx.lineTo(x + size * Math.cos(t) / Math.cos(Math.PI / 6) * 0.98, y + size * Math.sin(t) / Math.cos(Math.PI / 6)) * 0.98;
}
ctx.fillStyle = color;
ctx.fill();
ctx.closePath();
}
function drawHexMap() {
dayScore = 0;
nightScore = 0;
hexagons.forEach((hexagon) => {
if (hexagon.color === WALL_COLOR) return;
if (hexagon.color === NEITHER_COLOR) return;
drawHexagon(hexagon.x + canvas.width / 2, hexagon.y + canvas.height / 2, HEXAGON_SIZE, hexagon.color);
if (hexagon.color === DAY_COLOR) dayScore++;
if (hexagon.color === NIGHT_COLOR) nightScore++;
});
}
function drawGraph() {
const dayPart = graph.height * dayScore / (numHexagons - (HEXAGON_MAP_LEVEL + 1) * 6);
const nightPart = graph.height * nightScore / (numHexagons - (HEXAGON_MAP_LEVEL + 1) * 6);
console.log(dayPart, nightPart);
const imageData = ctxGraph.getImageData(1, 0, graph.width, graph.height);
ctxGraph.putImageData(imageData, 0, 0);
ctxGraph.strokeStyle = DAY_COLOR;
ctxGraph.beginPath();
ctxGraph.moveTo(graph.width, 0);
ctxGraph.lineTo(graph.width, dayPart);
ctxGraph.lineWidth = 2;
ctxGraph.stroke();
ctxGraph.strokeStyle = NIGHT_COLOR;
ctxGraph.beginPath();
ctxGraph.moveTo(graph.width, graph.height);
ctxGraph.lineTo(graph.width, graph.height - nightPart);
ctxGraph.lineWidth = 2;
ctxGraph.stroke();
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawHexMap();
scoreElement.textContent = `day ${dayScore} | night ${nightScore}`;
if (iteration % 10 === 0) {
drawGraph();
}
balls.forEach((ball) => {
checkCollision(ball);
// 現在の壁の六角形にいれば、適切な位置に移動させる
let minDist = Infinity;
let minHex = null;
for (let i = 0; i < numHexagons; i++) {
const hex = hexagons[i];
const dist = Math.sqrt((ball.x - hex.x) ** 2 + (ball.y - hex.y) ** 2);
if (dist < minDist) {
minDist = dist;
minHex = hex;
}
}
if (minHex.color === WALL_COLOR) {
forceValidPos(ball);
}
drawBall(ball);
ball.x += ball.dx;
ball.y += ball.dy;
addRandomness(ball);
});
iteration++;
if (iteration % 1_000 === 0) console.log("iteration", iteration);
requestAnimationFrame(draw);
}
requestAnimationFrame(draw);
</script>
</html>