[pongwars] duels feature thing

This commit is contained in:
dogeystamp 2024-02-23 20:38:03 -05:00
parent 020f3c35a9
commit 5401694e7b
Signed by: dogeystamp
GPG Key ID: 7225FE3592EFFA38

View File

@ -145,6 +145,10 @@
const numSquaresY = canvas.height / squareSize; const numSquaresY = canvas.height / squareSize;
let squares = []; let squares = [];
// matrix of "timestamps" where each square was claimed
let squareTaken = [];
var squareTime = 0;
document.addEventListener("keydown", (event) => { document.addEventListener("keydown", (event) => {
const key = parseInt(event.key) const key = parseInt(event.key)
if (isNaN(key)) return if (isNaN(key)) return
@ -165,14 +169,18 @@
dx: 0, dx: 0,
dy: 0, dy: 0,
score: 0, score: 0,
// how many consecutive contested tiles it has hit
conflict: 0,
})) }))
// base noise pattern (failsafe in case the grid doesn't cover some part) // base noise pattern (failsafe in case the grid doesn't cover some part)
for (let i = 0; i < numSquaresX; i++) { for (let i = 0; i < numSquaresX; i++) {
squares[i] = []; squares[i] = [];
squareTaken[i] = [];
for (let j = 0; j < numSquaresY; j++) { for (let j = 0; j < numSquaresY; j++) {
const t = randInt(0, teams.length-1); const t = randInt(0, teams.length-1);
squares[i][j] = t; squares[i][j] = t;
squareTaken[i][j] = squareTime;
} }
} }
@ -292,6 +300,31 @@
if (i >= 0 && i < numSquaresX && j >= 0 && j < numSquaresY) { if (i >= 0 && i < numSquaresX && j >= 0 && j < numSquaresY) {
if (squares[i][j] !== color) { if (squares[i][j] !== color) {
const foreign = squares[i][j]; const foreign = squares[i][j];
if (!state[foreign].elim) {
// in the event that the ball gets entangled with another on a contested square really long,
// have them duel and one of them will win (the one with more territory is more likely to win)
if (squareTime == squareTaken[i][j]) {
state[color].conflict++;
if (state[color].conflict >= 100 && randInt(0, 1) == 1) {
const ratio = state[color].score / (state[foreign].score + state[color].score);
console.log(`${teams[color].name} and ${teams[foreign].name} start a duel (territory ratio ${ratio.toFixed(2)})`);
const val = Math.random();
if (val == ratio) {
console.log(`nobody wins the duel`);
} else if (val < ratio) {
console.log(`${teams[color].name} vanquishes ${teams[foreign].name}`);
state[foreign].elim = true;
} else if (val > ratio) {
console.log(`${teams[foreign].name} vanquishes ${teams[color].name}`);
state[color].elim = true;
}
state[color].conflict = 0;
state[foreign].conflict = 0;
}
} else {
state[color].conflict = 0;
}
}
if (state[foreign].elim) { if (state[foreign].elim) {
console.log(`${teams[color].name} claims the remaining territory of ${teams[foreign].name}`) console.log(`${teams[color].name} claims the remaining territory of ${teams[foreign].name}`)
for (let ai = 0; ai < numSquaresX; ai++) { for (let ai = 0; ai < numSquaresX; ai++) {
@ -301,6 +334,7 @@
} }
} }
squareTaken[i][j] = squareTime;
squares[i][j] = color; squares[i][j] = color;
// Determine bounce direction based on the angle // Determine bounce direction based on the angle
@ -369,6 +403,9 @@
ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.clearRect(0, 0, canvas.width, canvas.height);
drawSquares(); drawSquares();
squareTime++;
squareTime %= 100000;
for (let i = 0; i < teams.length; i++) { for (let i = 0; i < teams.length; i++) {
if (state[i].elim) continue if (state[i].elim) continue
const t = state[i]; const t = state[i];