2025-03-19 21:58:50 +01:00
|
|
|
import { Ball, GameState, PerkId, PerksMap } from "./types";
|
|
|
|
import { icons, upgrades } from "./loadGameData";
|
2025-03-14 11:59:49 +01:00
|
|
|
|
|
|
|
export function getMajorityValue(arr: string[]): string {
|
2025-03-15 10:34:01 +01:00
|
|
|
const count: { [k: string]: number } = {};
|
|
|
|
arr.forEach((v) => (count[v] = (count[v] || 0) + 1));
|
|
|
|
// Object.values inline polyfill
|
|
|
|
const max = Math.max(...Object.keys(count).map((k) => count[k]));
|
|
|
|
return sample(Object.keys(count).filter((k) => count[k] == max));
|
2025-03-14 11:59:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export function sample<T>(arr: T[]): T {
|
2025-03-15 10:34:01 +01:00
|
|
|
return arr[Math.floor(arr.length * Math.random())];
|
2025-03-14 11:59:49 +01:00
|
|
|
}
|
|
|
|
|
2025-03-15 10:34:01 +01:00
|
|
|
export function sumOfKeys(obj: { [key: string]: number } | undefined | null) {
|
|
|
|
if (!obj) return 0;
|
|
|
|
return Object.values(obj)?.reduce((a, b) => a + b, 0) || 0;
|
2025-03-14 15:49:04 +01:00
|
|
|
}
|
|
|
|
|
2025-03-15 10:34:01 +01:00
|
|
|
export const makeEmptyPerksMap = (upgrades: { id: PerkId }[]) => {
|
|
|
|
const p = {} as any;
|
|
|
|
upgrades.forEach((u) => (p[u.id] = 0));
|
|
|
|
return p as PerksMap;
|
|
|
|
};
|
2025-03-16 14:29:14 +01:00
|
|
|
|
|
|
|
export function brickCenterX(gameState: GameState, index: number) {
|
|
|
|
return (
|
2025-03-16 17:45:29 +01:00
|
|
|
gameState.offsetX +
|
|
|
|
((index % gameState.gridSize) + 0.5) * gameState.brickWidth
|
2025-03-16 14:29:14 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function brickCenterY(gameState: GameState, index: number) {
|
|
|
|
return (Math.floor(index / gameState.gridSize) + 0.5) * gameState.brickWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getRowColIndex(gameState: GameState, row: number, col: number) {
|
|
|
|
if (
|
2025-03-16 17:45:29 +01:00
|
|
|
row < 0 ||
|
|
|
|
col < 0 ||
|
|
|
|
row >= gameState.gridSize ||
|
|
|
|
col >= gameState.gridSize
|
2025-03-16 14:29:14 +01:00
|
|
|
)
|
|
|
|
return -1;
|
|
|
|
return row * gameState.gridSize + col;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getPossibleUpgrades(gameState: GameState) {
|
|
|
|
return upgrades
|
2025-03-16 17:45:29 +01:00
|
|
|
.filter((u) => gameState.totalScoreAtRunStart >= u.threshold)
|
|
|
|
.filter((u) => !u?.requires || gameState.perks[u?.requires]);
|
2025-03-16 14:29:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export function max_levels(gameState: GameState) {
|
|
|
|
return 7 + gameState.perks.extra_levels;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function pickedUpgradesHTMl(gameState: GameState) {
|
|
|
|
let list = "";
|
|
|
|
for (let u of upgrades) {
|
|
|
|
for (let i = 0; i < gameState.perks[u.id]; i++)
|
|
|
|
list += icons["icon:" + u.id] + " ";
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function currentLevelInfo(gameState: GameState) {
|
|
|
|
return gameState.runLevels[
|
2025-03-16 17:45:29 +01:00
|
|
|
gameState.currentLevel % gameState.runLevels.length
|
|
|
|
];
|
2025-03-16 14:29:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export function isTelekinesisActive(gameState: GameState, ball: Ball) {
|
2025-03-19 21:58:50 +01:00
|
|
|
return gameState.perks.telekinesis && ball.vy < 0;
|
2025-03-19 20:14:55 +01:00
|
|
|
}
|
|
|
|
export function isYoyoActive(gameState: GameState, ball: Ball) {
|
2025-03-19 21:58:50 +01:00
|
|
|
return gameState.perks.yoyo && ball.vy > 0;
|
2025-03-16 14:29:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export function findLast<T>(
|
2025-03-16 17:45:29 +01:00
|
|
|
arr: T[],
|
|
|
|
predicate: (item: T, index: number, array: T[]) => boolean,
|
2025-03-16 14:29:14 +01:00
|
|
|
) {
|
|
|
|
let i = arr.length;
|
|
|
|
while (--i)
|
|
|
|
if (predicate(arr[i], i, arr)) {
|
|
|
|
return arr[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function distance2(
|
2025-03-16 17:45:29 +01:00
|
|
|
a: { x: number; y: number },
|
|
|
|
b: { x: number; y: number },
|
2025-03-16 14:29:14 +01:00
|
|
|
) {
|
|
|
|
return Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function distanceBetween(
|
2025-03-16 17:45:29 +01:00
|
|
|
a: { x: number; y: number },
|
|
|
|
b: { x: number; y: number },
|
2025-03-16 14:29:14 +01:00
|
|
|
) {
|
|
|
|
return Math.sqrt(distance2(a, b));
|
2025-03-16 17:45:29 +01:00
|
|
|
}
|
2025-03-17 19:47:16 +01:00
|
|
|
|
2025-03-19 21:58:50 +01:00
|
|
|
export function clamp(value: number, min: number, max: number) {
|
|
|
|
return Math.max(min, Math.min(value, max));
|
2025-03-19 21:58:08 +01:00
|
|
|
}
|
2025-03-17 19:47:16 +01:00
|
|
|
export function defaultSounds() {
|
2025-03-18 14:16:12 +01:00
|
|
|
return {
|
|
|
|
aboutToPlaySound: {
|
|
|
|
wallBeep: { vol: 0, x: 0 },
|
|
|
|
comboIncreaseMaybe: { vol: 0, x: 0 },
|
|
|
|
comboDecrease: { vol: 0, x: 0 },
|
|
|
|
coinBounce: { vol: 0, x: 0 },
|
|
|
|
explode: { vol: 0, x: 0 },
|
|
|
|
lifeLost: { vol: 0, x: 0 },
|
|
|
|
coinCatch: { vol: 0, x: 0 },
|
|
|
|
colorChange: { vol: 0, x: 0 },
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2025-03-19 20:14:55 +01:00
|
|
|
|
|
|
|
export function shouldPierceByColor(
|
2025-03-19 21:58:50 +01:00
|
|
|
gameState: GameState,
|
|
|
|
vhit: number | undefined,
|
|
|
|
hhit: number | undefined,
|
|
|
|
chit: number | undefined,
|
2025-03-19 20:14:55 +01:00
|
|
|
) {
|
2025-03-19 21:58:50 +01:00
|
|
|
if (!gameState.perks.pierce_color) return false;
|
|
|
|
if (
|
|
|
|
typeof vhit !== "undefined" &&
|
|
|
|
gameState.bricks[vhit] !== gameState.ballsColor
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
typeof hhit !== "undefined" &&
|
|
|
|
gameState.bricks[hhit] !== gameState.ballsColor
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
typeof chit !== "undefined" &&
|
|
|
|
gameState.bricks[chit] !== gameState.ballsColor
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2025-03-20 21:02:51 +01:00
|
|
|
|
|
|
|
export function countBricksAbove(gameState: GameState, index: number) {
|
|
|
|
const col = index % gameState.gridSize;
|
|
|
|
const row = Math.floor(index / gameState.gridSize);
|
|
|
|
let count = 0;
|
|
|
|
for (let y = 0; y < row; y++) {
|
|
|
|
if (gameState.bricks[col + y * gameState.gridSize]) {
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
export function countBricksBelow(gameState: GameState, index: number) {
|
|
|
|
const col = index % gameState.gridSize;
|
|
|
|
const row = Math.floor(index / gameState.gridSize);
|
|
|
|
let count = 0;
|
|
|
|
for (let y = row + 1; y < gameState.gridSize; y++) {
|
|
|
|
if (gameState.bricks[col + y * gameState.gridSize]) {
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|