convert spaces to tabs

master
Aevann 2023-04-27 20:20:11 +02:00
parent cff0edd197
commit 2864575068
1 changed files with 196 additions and 196 deletions

View File

@ -1,199 +1,199 @@
// based on oneko.js from https://github.com/adryd325/oneko.js, licensed under MIT, with art from https://twitter.com/_Anunnery // based on oneko.js from https://github.com/adryd325/oneko.js, licensed under MIT, with art from https://twitter.com/_Anunnery
(function oneko() { (function oneko() {
const nekoEl = document.createElement("div"); const nekoEl = document.createElement("div");
let nekoPosX = 32; let nekoPosX = 32;
let nekoPosY = 32; let nekoPosY = 32;
let mousePosX = 0; let mousePosX = 0;
let mousePosY = 0; let mousePosY = 0;
let frameCount = 0; let frameCount = 0;
let idleTime = 0; let idleTime = 0;
let idleAnimation = null; let idleAnimation = null;
let idleAnimationFrame = 0; let idleAnimationFrame = 0;
const nekoSpeed = 10; const nekoSpeed = 10;
const spriteSets = { const spriteSets = {
idle: [[-3, -3]], idle: [[-3, -3]],
alert: [[-7, -3]], alert: [[-7, -3]],
scratchSelf: [ scratchSelf: [
[-5, 0], [-5, 0],
[-6, 0], [-6, 0],
[-7, 0], [-7, 0],
], ],
scratchWallN: [ scratchWallN: [
[0, 0], [0, 0],
[0, -1], [0, -1],
], ],
scratchWallS: [ scratchWallS: [
[-7, -1], [-7, -1],
[-6, -2], [-6, -2],
], ],
scratchWallE: [ scratchWallE: [
[-2, -2], [-2, -2],
[-2, -3], [-2, -3],
], ],
scratchWallW: [ scratchWallW: [
[-4, 0], [-4, 0],
[-4, -1], [-4, -1],
], ],
tired: [[-3, -2]], tired: [[-3, -2]],
sleeping: [ sleeping: [
[-2, 0], [-2, 0],
[-2, -1], [-2, -1],
], ],
N: [ N: [
[-1, -2], [-1, -2],
[-1, -3], [-1, -3],
], ],
NE: [ NE: [
[0, -2], [0, -2],
[0, -3], [0, -3],
], ],
E: [ E: [
[-3, 0], [-3, 0],
[-3, -1], [-3, -1],
], ],
SE: [ SE: [
[-5, -1], [-5, -1],
[-5, -2], [-5, -2],
], ],
S: [ S: [
[-6, -3], [-6, -3],
[-7, -2], [-7, -2],
], ],
SW: [ SW: [
[-5, -3], [-5, -3],
[-6, -1], [-6, -1],
], ],
W: [ W: [
[-4, -2], [-4, -2],
[-4, -3], [-4, -3],
], ],
NW: [ NW: [
[-1, 0], [-1, 0],
[-1, -1], [-1, -1],
], ],
}; };
function create() { function create() {
nekoEl.id = "oneko"; nekoEl.id = "oneko";
nekoEl.style.width = "32px"; nekoEl.style.width = "32px";
nekoEl.style.height = "32px"; nekoEl.style.height = "32px";
nekoEl.style.position = "fixed"; nekoEl.style.position = "fixed";
nekoEl.style.pointerEvents = "none"; nekoEl.style.pointerEvents = "none";
nekoEl.style.backgroundImage = "url('/i/oneko.gif')"; nekoEl.style.backgroundImage = "url('/i/oneko.gif')";
nekoEl.style.imageRendering = "pixelated"; nekoEl.style.imageRendering = "pixelated";
nekoEl.style.left = "16px"; nekoEl.style.left = "16px";
nekoEl.style.top = "16px"; nekoEl.style.top = "16px";
document.body.appendChild(nekoEl); document.body.appendChild(nekoEl);
document.onmousemove = (event) => { document.onmousemove = (event) => {
mousePosX = event.clientX; mousePosX = event.clientX;
mousePosY = event.clientY; mousePosY = event.clientY;
}; };
window.onekoInterval = setInterval(frame, 100); window.onekoInterval = setInterval(frame, 100);
} }
function setSprite(name, frame) { function setSprite(name, frame) {
const sprite = spriteSets[name][frame % spriteSets[name].length]; const sprite = spriteSets[name][frame % spriteSets[name].length];
nekoEl.style.backgroundPosition = `${sprite[0] * 32}px ${sprite[1] * 32}px`; nekoEl.style.backgroundPosition = `${sprite[0] * 32}px ${sprite[1] * 32}px`;
} }
function resetIdleAnimation() { function resetIdleAnimation() {
idleAnimation = null; idleAnimation = null;
idleAnimationFrame = 0; idleAnimationFrame = 0;
} }
function idle() { function idle() {
idleTime += 1; idleTime += 1;
// every ~ 20 seconds // every ~ 20 seconds
if (idleTime > 10 && true && idleAnimation == null) { if (idleTime > 10 && true && idleAnimation == null) {
let avalibleIdleAnimations = ["sleeping", "scratchSelf"]; let avalibleIdleAnimations = ["sleeping", "scratchSelf"];
if (nekoPosX < 32) { if (nekoPosX < 32) {
avalibleIdleAnimations.push("scratchWallW"); avalibleIdleAnimations.push("scratchWallW");
} }
if (nekoPosY < 32) { if (nekoPosY < 32) {
avalibleIdleAnimations.push("scratchWallN"); avalibleIdleAnimations.push("scratchWallN");
} }
if (nekoPosX > window.innerWidth - 32) { if (nekoPosX > window.innerWidth - 32) {
avalibleIdleAnimations.push("scratchWallE"); avalibleIdleAnimations.push("scratchWallE");
} }
if (nekoPosY > window.innerHeight - 32) { if (nekoPosY > window.innerHeight - 32) {
avalibleIdleAnimations.push("scratchWallS"); avalibleIdleAnimations.push("scratchWallS");
} }
idleAnimation = idleAnimation =
avalibleIdleAnimations[ avalibleIdleAnimations[
Math.floor(Math.random() * avalibleIdleAnimations.length) Math.floor(Math.random() * avalibleIdleAnimations.length)
]; ];
} }
switch (idleAnimation) { switch (idleAnimation) {
case "sleeping": case "sleeping":
if (idleAnimationFrame < 8) { if (idleAnimationFrame < 8) {
setSprite("tired", 0); setSprite("tired", 0);
break; break;
} }
setSprite("sleeping", Math.floor(idleAnimationFrame / 4)); setSprite("sleeping", Math.floor(idleAnimationFrame / 4));
if (idleAnimationFrame > 192) { if (idleAnimationFrame > 192) {
resetIdleAnimation(); resetIdleAnimation();
} }
break; break;
case "scratchWallN": case "scratchWallN":
case "scratchWallS": case "scratchWallS":
case "scratchWallE": case "scratchWallE":
case "scratchWallW": case "scratchWallW":
case "scratchSelf": case "scratchSelf":
setSprite(idleAnimation, idleAnimationFrame); setSprite(idleAnimation, idleAnimationFrame);
if (idleAnimationFrame > 9) { if (idleAnimationFrame > 9) {
resetIdleAnimation(); resetIdleAnimation();
} }
break; break;
default: default:
setSprite("idle", 0); setSprite("idle", 0);
return; return;
} }
idleAnimationFrame += 1; idleAnimationFrame += 1;
} }
function frame() { function frame() {
frameCount += 1; frameCount += 1;
const diffX = nekoPosX - mousePosX; const diffX = nekoPosX - mousePosX;
const diffY = nekoPosY - mousePosY; const diffY = nekoPosY - mousePosY;
const distance = Math.sqrt(diffX ** 2 + diffY ** 2); const distance = Math.sqrt(diffX ** 2 + diffY ** 2);
if (distance < nekoSpeed || distance < 48) { if (distance < nekoSpeed || distance < 48) {
idle(); idle();
return; return;
} }
idleAnimation = null; idleAnimation = null;
idleAnimationFrame = 0; idleAnimationFrame = 0;
if (idleTime > 1) { if (idleTime > 1) {
setSprite("alert", 0); setSprite("alert", 0);
// count down after being alerted before moving // count down after being alerted before moving
idleTime = Math.min(idleTime, 7); idleTime = Math.min(idleTime, 7);
idleTime -= 1; idleTime -= 1;
return; return;
} }
direction = diffY / distance > 0.5 ? "N" : ""; direction = diffY / distance > 0.5 ? "N" : "";
direction += diffY / distance < -0.5 ? "S" : ""; direction += diffY / distance < -0.5 ? "S" : "";
direction += diffX / distance > 0.5 ? "W" : ""; direction += diffX / distance > 0.5 ? "W" : "";
direction += diffX / distance < -0.5 ? "E" : ""; direction += diffX / distance < -0.5 ? "E" : "";
setSprite(direction, frameCount); setSprite(direction, frameCount);
nekoPosX -= (diffX / distance) * nekoSpeed; nekoPosX -= (diffX / distance) * nekoSpeed;
nekoPosY -= (diffY / distance) * nekoSpeed; nekoPosY -= (diffY / distance) * nekoSpeed;
nekoPosX = Math.min(Math.max(16, nekoPosX), window.innerWidth - 16); nekoPosX = Math.min(Math.max(16, nekoPosX), window.innerWidth - 16);
nekoPosY = Math.min(Math.max(16, nekoPosY), window.innerHeight - 16); nekoPosY = Math.min(Math.max(16, nekoPosY), window.innerHeight - 16);
nekoEl.style.left = `${nekoPosX - 16}px`; nekoEl.style.left = `${nekoPosX - 16}px`;
nekoEl.style.top = `${nekoPosY - 16}px`; nekoEl.style.top = `${nekoPosY - 16}px`;
} }
create(); create();
})(); })();