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

@ -14,11 +14,27 @@
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],
], ],
scratchWallN: [
[0, 0],
[0, -1],
],
scratchWallS: [
[-7, -1],
[-6, -2],
],
scratchWallE: [
[-2, -2],
[-2, -3],
],
scratchWallW: [
[-4, 0],
[-4, -1],
],
tired: [[-3, -2]], tired: [[-3, -2]],
sleeping: [ sleeping: [
[-2, 0], [-2, 0],
@ -57,6 +73,7 @@
[-1, -1], [-1, -1],
], ],
}; };
function create() { function create() {
nekoEl.id = "oneko"; nekoEl.id = "oneko";
nekoEl.style.width = "32px"; nekoEl.style.width = "32px";
@ -80,9 +97,7 @@
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() {
@ -94,13 +109,23 @@
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"][ if (nekoPosY < 32) {
Math.floor(Math.random() * 2) 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)
]; ];
} }
@ -115,8 +140,12 @@
resetIdleAnimation(); resetIdleAnimation();
} }
break; break;
case "scratch": case "scratchWallN":
setSprite("scratch", idleAnimationFrame); case "scratchWallS":
case "scratchWallE":
case "scratchWallW":
case "scratchSelf":
setSprite(idleAnimation, idleAnimationFrame);
if (idleAnimationFrame > 9) { if (idleAnimationFrame > 9) {
resetIdleAnimation(); resetIdleAnimation();
} }
@ -159,6 +188,9 @@
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);
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`;
} }