This commit is contained in:
CutieCat2804 2024-10-19 17:25:01 +02:00
commit d1dae6aba9
5 changed files with 10 additions and 21 deletions

BIN
bun.lockb

Binary file not shown.

View File

@ -40,6 +40,7 @@
"pixi.js": "^7.0.0",
"pixi.js-legacy": "^7.4.2",
"prom-client": "^15.1.3",
"random-seed": "^0.3.0",
"react": "^18.3.1",
"react-confetti-boom": "^1.0.0",
"react-dom": "^18.3.1",
@ -49,11 +50,11 @@
"zod": "^3.23.8"
},
"devDependencies": {
"vite-imagetools": "^7.0.4",
"tailwindcss": "^4.0.0-alpha.26",
"@eslint/compat": "^1.2.0",
"@eslint/js": "^9.12.0",
"@tailwindcss/vite": "next",
"@types/bun": "latest",
"@types/random-seed": "^0.3.5",
"@types/react": "^18.3.11",
"@types/react-dom": "^18.3.1",
"@vitejs/plugin-react-swc": "^3.7.1",
@ -62,9 +63,10 @@
"eslint-plugin-react": "^7.37.1",
"eslint-plugin-react-hooks": "5.0.0",
"globals": "^15.11.0",
"tailwindcss": "^4.0.0-alpha.26",
"typescript": "^5.6.3",
"typescript-eslint": "^8.8.1",
"vite": "^5.4.8",
"@tailwindcss/vite": "next"
"vite-imagetools": "^7.0.4"
}
}

View File

@ -3,21 +3,6 @@ export const pickRandom = <T>(arr: T[]) => {
return arr[index];
};
function bashHashStr(str: string) {
let hash = 5381,
i = str.length;
while (i) {
hash = (hash * 33) ^ str.charCodeAt(--i);
}
return hash >>> 0;
}
export const hashStr = (str: string) => {
return Number(`0.${bashHashStr(str)}`);
};
export const weightedPickRandom = <T>(
arr: T[],
getWeight: (item: T) => number = () => 1,

View File

@ -42,7 +42,8 @@ import "@pixi/canvas-sprite-tiling";
import "@pixi/canvas-sprite";
import "@pixi/canvas-text";
import { themes } from "../themes";
import { hashStr, weightedPickRandom } from "../../shared/utils";
import { weightedPickRandom } from "../../shared/utils";
import gen from "random-seed";
interface BoardProps {
className?: string;
@ -280,10 +281,11 @@ const Tile = ({
const resolveSprite = useCallback(
(lt: LoadedTexture) => {
if (Array.isArray(lt)) {
const rng = gen.create(game.uuid + ";" + x + ";" + y);
return weightedPickRandom(
lt,
(i) => i.weight,
(tw) => hashStr(game.uuid + ";" + x + ";" + y) * tw,
(tw) => rng.floatBetween(0, tw),
).sprite;
}
return lt;

View File

@ -51,7 +51,7 @@ export const mainWithSpecials = (
const [main, ...specials] = sprites;
return [
{ weight: 1, sprite: main },
...specials.map((sprite) => ({ weight: 0.3, sprite })),
...specials.map((sprite) => ({ weight: 0.05, sprite })),
];
};