Compare commits

...

5 Commits

Author SHA1 Message Date
513d6c230c [pongwars]: add territory claim message 2024-01-30 19:08:16 -05:00
170aa8cee3 [pongwars] fix speed limit
- also make canvas bigger
2024-01-30 19:07:24 -05:00
e093f44e54 [pongwars]: add territory claim 2024-01-30 17:02:41 -05:00
2b6e9ec330 [pongwars]: misc improvements
- use tab instead of space
- warp out of bound balls
2024-01-30 15:35:49 -05:00
32dba49a50 [pongwars] fix user input slowing down balls 2024-01-30 14:23:50 -05:00

View File

@ -21,7 +21,7 @@
#container {
display: flex;
width: 100%;
max-width: 750px;
max-width: 1000px;
align-items: center;
flex-direction: column;
height: 100%;
@ -60,7 +60,7 @@
<body>
<div id="container">
<canvas id="pongCanvas" width="800" height="800"></canvas>
<canvas id="pongCanvas" width="1200" height="800"></canvas>
<div id="score"></div>
<p id="made">
made by <a href="https://koenvangilst.nl">Koen van Gilst</a> | source on
@ -93,7 +93,7 @@
{
name: "blue",
color: "#5555ff",
backgroundColor: "#0000aa",
backgroundColor: "#200199",
x: 768,
y: 256,
dx: -8,
@ -164,7 +164,6 @@
var state = teams.map((team) => ({
elim: false,
boost: 1,
boostEnabled: false,
}))
@ -234,6 +233,23 @@
function updateSquareAndBounce(x, y, dx, dy, color) {
if (state[color].elim) return
if (Math.max(Math.abs(dx), Math.abs(dy)) < 0.02) state[color].elim = true
if (Math.min(x, y) < 0 || isNaN(x) || isNaN(y)) {
console.warn(`warped ${teams[color].name}`)
teams[color].x = canvas.width * 0.1;
teams[color].y = canvas.height * 0.1;
dx = 4;
dy = 4;
}
if (x > canvas.width || y > canvas.height) {
console.warn(`warped ${teams[color].name}`)
teams[color].x = canvas.width * 0.9;
teams[color].y = canvas.height * 0.9;
dx = -4;
dy = -4;
}
let updatedDx = dx;
let updatedDy = dy;
@ -247,6 +263,16 @@
if (i >= 0 && i < numSquaresX && j >= 0 && j < numSquaresY) {
if (squares[i][j] !== color) {
const foreign = squares[i][j];
if (state[foreign].elim) {
console.log(`${teams[color].name} claims the remaining territory of ${teams[foreign].name}`)
for (let ai = 0; ai < numSquaresX; ai++) {
for (let aj = 0; aj < numSquaresY; aj++) {
if (squares[ai][aj] == foreign) squares[ai][aj] = color;
}
}
}
squares[i][j] = color;
// Determine bounce direction based on the angle
@ -259,12 +285,14 @@
updatedDx += randomNum(-0.15, mix(0.153, 0.15, suddenDeathCoeff));
updatedDy += randomNum(-0.15, 0.15);
const nTeams = state.map(t => !t.elim).filter(Boolean).length
const coeff = Math.min((teams[color].score/(squares.length**2/nTeams/mix(1.2, 4, suddenDeathCoeff))), 1.00);
const coeff = Math.min((teams[color].score/(numSquaresX*numSquaresY/nTeams/mix(1.2, 4, suddenDeathCoeff))), 1.00);
updatedDx *= coeff;
updatedDy *= coeff;
const speedLim = mix(100, 18, suddenDeathCoeff)
updatedDx = clamp(-speedLim, speedLim, updatedDx);
updatedDy = clamp(-speedLim, speedLim, updatedDy);
const speedLim = mix(30, 18, suddenDeathCoeff)
const norm = (updatedDx**2 + updatedDy**2)**(1/2)
const scalar = Math.min(speedLim/norm, 1)
updatedDx *= scalar;
updatedDy *= scalar;
}
}
@ -274,8 +302,10 @@
const ct = Math.cos(theta);
const st = Math.sin(theta);
if (state[color].boostEnabled) {
updatedDx = (ct * updatedDx - st * updatedDy)*1.01
updatedDy = (st * updatedDx + ct * updatedDy)*1.01
const rotDx = (ct * updatedDx - st * updatedDy);
const rotDy = (st * updatedDx + ct * updatedDy);
updatedDx = rotDx;
updatedDy = rotDy;
}
return { dx: updatedDx, dy: updatedDy };