update oneko.js

pull/1/head
maia arson crimew 2023-03-04 02:46:39 +01:00
parent 8c77b05fba
commit 32d05fc840
1 changed files with 171 additions and 139 deletions

View File

@ -12,156 +12,188 @@
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]],
scratch: [ scratchSelf: [
[-5, 0], [-5, 0],
[-6, 0], [-6, 0],
[-7, 0], [-7, 0],
], ],
tired: [[-3, -2]], scratchWallN: [
sleeping: [ [0, 0],
[-2, 0], [0, -1],
[-2, -1], ],
], scratchWallS: [
N: [ [-7, -1],
[-1, -2], [-6, -2],
[-1, -3], ],
], scratchWallE: [
NE: [ [-2, -2],
[0, -2], [-2, -3],
[0, -3], ],
], scratchWallW: [
E: [ [-4, 0],
[-3, 0], [-4, -1],
[-3, -1], ],
], tired: [[-3, -2]],
SE: [ sleeping: [
[-5, -1], [-2, 0],
[-5, -2], [-2, -1],
], ],
S: [ N: [
[-6, -3], [-1, -2],
[-7, -2], [-1, -3],
], ],
SW: [ NE: [
[-5, -3], [0, -2],
[-6, -1], [0, -3],
], ],
W: [ E: [
[-4, -2], [-3, 0],
[-4, -3], [-3, -1],
], ],
NW: [ SE: [
[-1, 0], [-5, -1],
[-1, -1], [-5, -2],
], ],
S: [
[-6, -3],
[-7, -2],
],
SW: [
[-5, -3],
[-6, -1],
],
W: [
[-4, -2],
[-4, -3],
],
NW: [
[-1, 0],
[-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('/img/maia_oneko.gif')"; nekoEl.style.backgroundImage = "url('/img/maia_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 ${ nekoEl.style.backgroundPosition = `${sprite[0] * 32}px ${sprite[1] * 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 ( if (idleTime > 10 && true && idleAnimation == null) {
idleTime > 10 && let avalibleIdleAnimations = ["sleeping", "scratchSelf"];
Math.floor(Math.random() * 200) == 0 && if (nekoPosX < 32) {
idleAnimation == null avalibleIdleAnimations.push("scratchWallW");
) {
idleAnimation = ["sleeping", "scratch"][
Math.floor(Math.random() * 2)
];
} }
if (nekoPosY < 32) {
avalibleIdleAnimations.push("scratchWallN");
}
if (nekoPosX > window.innerWidth - 32) {
avalibleIdleAnimations.push("scratchWallE");
}
if (nekoPosY > window.innerHeight - 32) {
avalibleIdleAnimations.push("scratchWallS");
}
idleAnimation =
avalibleIdleAnimations[
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 "scratch": case "scratchWallN":
setSprite("scratch", idleAnimationFrame); case "scratchWallS":
if (idleAnimationFrame > 9) { case "scratchWallE":
resetIdleAnimation(); case "scratchWallW":
} case "scratchSelf":
break; setSprite(idleAnimation, idleAnimationFrame);
default: if (idleAnimationFrame > 9) {
setSprite("idle", 0); resetIdleAnimation();
return; }
} break;
idleAnimationFrame += 1; default:
setSprite("idle", 0);
return;
}
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;
nekoEl.style.left = `${nekoPosX - 16}px`; nekoPosX = Math.min(Math.max(16, nekoPosX), window.innerWidth - 16);
nekoEl.style.top = `${nekoPosY - 16}px`; nekoPosY = Math.min(Math.max(16, nekoPosY), window.innerHeight - 16);
nekoEl.style.left = `${nekoPosX - 16}px`;
nekoEl.style.top = `${nekoPosY - 16}px`;
} }
create(); create();
})(); })();