breakout71/src/getLevelBackground.ts

18 lines
518 B
TypeScript
Raw Normal View History

import { RawLevel } from "./types";
2025-03-13 14:14:00 +01:00
2025-03-16 17:45:29 +01:00
import _backgrounds from "./data/backgrounds.json";
2025-03-13 14:14:00 +01:00
const backgrounds = _backgrounds as string[];
export function getLevelBackground(level: RawLevel) {
return backgrounds[hashCode(level.name) % backgrounds.length];
2025-03-14 16:13:43 +01:00
}
export function hashCode(string: string) {
let hash = 0;
for (let i = 0; i < string.length; i++) {
let code = string.charCodeAt(i);
hash = (hash << 5) - hash + code;
hash = hash & hash; // Convert to 32bit integer
}
return Math.abs(hash);
}