Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

extensions/afk/src/cat.ts

Raw
export type CatMood =
	| "curious"
	| "thinking"
	| "watchful"
	| "angry"
	| "hungry"
	| "bored"
	| "pleased"
	| "napping"
	| "concerned";
const W = 32,
	H = 20;
export const colors = {
	outline: [100, 103, 133],
	fur: [237, 224, 207],
	light: [255, 245, 226],
	shadow: [199, 179, 171],
	pink: [238, 159, 174],
	ink: [59, 53, 76],
	gold: [235, 190, 111],
	mint: [155, 210, 181],
} as const;

export function sprite(
	mood: CatMood,
	t: number,
): Array<Array<keyof typeof colors | null>> {
	const pixels = Array.from({ length: H }, () =>
		Array<keyof typeof colors | null>(W).fill(null),
	);
	const put = (x: number, y: number, color: keyof typeof colors) => {
		x = Math.round(x);
		y = Math.round(y);
		const row = pixels[y];
		if (row && x >= 0 && x < W) row[x] = color;
	};
	const line = (
		x: number,
		y: number,
		n: number,
		color: keyof typeof colors,
	) => {
		for (let i = 0; i < n; i++) put(x + i, y, color);
	};
	const oval = (
		cx: number,
		cy: number,
		rx: number,
		ry: number,
		color: keyof typeof colors,
	) => {
		for (let y = Math.ceil(cy - ry); y <= cy + ry; y++) {
			for (let x = Math.ceil(cx - rx); x <= cx + rx; x++) {
				if (((x - cx) / rx) ** 2 + ((y - cy) / ry) ** 2 <= 1) put(x, y, color);
			}
		}
	};
	const blob = (cx: number, cy: number, rx: number, ry: number) => {
		oval(cx, cy, rx, ry, "outline");
		oval(cx, cy - 0.2, rx - 1, ry - 0.8, "fur");
	};
	const blink = t % 5.7 > 5.48 || (t > 1 && t % 11.3 < 0.16);
	const breath = Math.sin(t * 1.8) > 0.6 ? -1 : 0;
	const nap = mood === "napping";
	const low = mood === "bored" || nap;
	const hy = (low ? 11 : 8) + breath;
	const tilt = mood === "curious" && Math.sin(t * 1.5) > 0 ? 1 : 0;
	const tail = Math.round(Math.sin(t * (mood === "angry" ? 7 : 1.6)) * 1.4);

	// Giant curled tail, haunches, soft chest, cheek tufts.
	blob(24, 13 + tail, 5, 4);
	oval(25, 12 + tail, 2, 1.7, "light");
	blob(15, 14 + breath, 9, 4.5);
	oval(14, 13 + breath, 5, 3, "light");
	if (!nap) {
		for (const [x, flip] of [
			[7, 1],
			[19, -1],
		] as const) {
			const flat = mood === "angry" ? 2 : 0;
			const twitch = mood === "concerned" && flip < 0 ? 2 : 0;
			for (let row = 0; row < 6; row++) {
				const y = hy - 7 + row + flat + twitch;
				line(x - Math.floor(row / 2), y, row + 1, "outline");
				if (row > 1)
					line(x - Math.floor(row / 2) + 1, y, Math.max(1, row - 1), "pink");
			}
		}
	}
	blob(13, hy, 9, 5);
	oval(12, hy - 1, 6, 3, "light");
	for (const [x, y] of [
		[4, hy],
		[3, hy + 2],
		[5, hy + 3],
		[22, hy],
		[23, hy + 2],
		[21, hy + 3],
	] as const) {
		line(x, y, 2, "fur");
	}
	for (const ex of [9, 17]) {
		const ey = hy + (ex === 17 ? tilt : 0);
		if (blink || nap || mood === "bored") line(ex - 1, ey, 3, "ink");
		else if (mood === "pleased") {
			put(ex, ey - 1, "ink");
			put(ex - 1, ey, "ink");
			put(ex + 1, ey, "ink");
		} else {
			oval(
				ex,
				ey,
				mood === "hungry" ? 1.4 : 1,
				mood === "hungry" ? 1.7 : 1.1,
				"ink",
			);
			put(ex, ey - 1, "light");
		}
	}
	if (mood === "angry") {
		line(7, hy - 2, 2, "ink");
		put(9, hy - 1, "ink");
		line(18, hy - 2, 2, "ink");
		put(17, hy - 1, "ink");
	}
	if (mood === "concerned") {
		put(8, hy - 3, "ink");
		put(9, hy - 4, "ink");
		put(17, hy - 4, "ink");
		put(18, hy - 3, "ink");
	}
	line(6, hy + 2, 2, "pink");
	line(19, hy + 2, 2, "pink");
	put(13, hy + 1, "pink");
	put(13, hy + 2, "ink");
	put(12, hy + 3, "ink");
	put(14, hy + 3, "ink");
	// Paws have opposite timing, rather than moving the entire sprite up/down.
	const knead = mood === "pleased" ? Math.round(Math.sin(t * 7)) : 0;
	blob(9, 17 + knead, 3, 1.6);
	blob(18, 17 - knead, 3, 1.6);
	if (mood === "thinking") blob(18, hy + 4, 2, 1.8);
	if (mood === "angry" && t % 2 < 0.3) blob(19, 15, 2, 1.7);
	if (mood === "hungry") {
		line(24, 18, 7, "outline");
		line(25, 19, 5, "pink");
	}
	if (mood === "pleased") {
		put(27, 3, "pink");
		put(29, 3, "pink");
		line(27, 4, 3, "pink");
		put(28, 5, "pink");
	}
	if (mood === "curious" || mood === "thinking") {
		const dot = Math.floor(t * 2) % 3;
		put(25 + dot * 2, 3, "gold");
	}
	return pixels;
}