1
0

Refactor and enhance mouse hold logic in animeCat.js

This commit is contained in:
2025-05-26 04:07:58 +02:00
parent 0d0824111f
commit 1c6ce0eec7

View File

@@ -132,13 +132,15 @@ window.AnimeCat = class AnimeCat {
} }
_bindMouseHold() { _bindMouseHold() {
let holdTimer = null;
let joyActive = false; let joyActive = false;
let mouseDown = false; let mouseDown = false;
let lastPos = null; let lastPos = null;
let moveDist = 0; let moveDist = 0;
let mouseDownAt = 0;
let holdTimer = null;
const CAT_TOLERANCE = 36; const CAT_TOLERANCE = 36;
const MOVE_THRESHOLD = 75;
const isMouseNearCat = (e) => { const isMouseNearCat = (e) => {
const rect = this.img.getBoundingClientRect(); const rect = this.img.getBoundingClientRect();
@@ -150,13 +152,9 @@ _bindMouseHold() {
); );
}; };
const closeEyes = () => { const closeEyes = () => { this.img.src = this.images.eyesClosed; };
this.img.src = this.images.eyesClosed;
};
const reopenEyes = () => { const reopenEyes = () => {
if (!joyActive && !this._isSpeaking) { if (!joyActive && !this._isSpeaking) this.img.src = this.images.default;
this.img.src = this.images.default;
}
}; };
this.img.addEventListener('mousedown', (e) => { this.img.addEventListener('mousedown', (e) => {
@@ -164,12 +162,11 @@ _bindMouseHold() {
if (!isMouseNearCat(e)) return; if (!isMouseNearCat(e)) return;
mouseDown = true; mouseDown = true;
mouseDownAt = Date.now();
lastPos = { x: e.clientX, y: e.clientY }; lastPos = { x: e.clientX, y: e.clientY };
moveDist = 0; moveDist = 0;
closeEyes(); closeEyes();
clearTimeout(this._blinkTimeout); // Blinzeln pausieren
// *** Blinken pausieren ***
clearTimeout(this._blinkTimeout);
function onMove(ev) { function onMove(ev) {
if (!mouseDown) return; if (!mouseDown) return;
@@ -177,7 +174,6 @@ _bindMouseHold() {
cleanup(); cleanup();
reopenEyes(); reopenEyes();
mouseDown = false; mouseDown = false;
// *** Nach Streichelspiel Blinken neu starten ***
this._startBlinking(); this._startBlinking();
return; return;
} }
@@ -192,11 +188,39 @@ _bindMouseHold() {
const onMoveBound = onMove.bind(this); const onMoveBound = onMove.bind(this);
function onUp() { function onUp() {
const heldFor = Date.now() - mouseDownAt;
cleanup(); cleanup();
if (!joyActive) reopenEyes();
// (1) Joy-Animation wenn erfüllt
if (heldFor >= 4000 && moveDist >= MOVE_THRESHOLD) {
joyActive = true;
this._runJoyAnimation(() => {
joyActive = false;
reopenEyes();
this._startBlinking();
});
}
// (2) Trostpreis: Mouth open wenn >1s gehalten, aber keine Joy-Bedingung erfüllt
else if (heldFor > 1000) {
const mouthOpenTime = Math.max(heldFor - 1000, 0);
if (mouthOpenTime > 0) {
this.img.src = this.images.mouthOpen || this.images.default;
// BLINKEN IST AKTIV (also _startBlinking sofort!)
this._startBlinking();
setTimeout(() => {
if (!joyActive && !this._isSpeaking) this.img.src = this.images.default;
}, mouthOpenTime);
} else {
reopenEyes();
this._startBlinking();
}
}
// (3) Weniger als 1 Sekunde? Nichts besonderes.
else {
reopenEyes();
this._startBlinking();
}
mouseDown = false; mouseDown = false;
// *** Nach Streichelspiel Blinken neu starten ***
this._startBlinking();
} }
const onUpBound = onUp.bind(this); const onUpBound = onUp.bind(this);
@@ -211,24 +235,22 @@ _bindMouseHold() {
window.addEventListener('mousemove', onMoveBound); window.addEventListener('mousemove', onMoveBound);
window.addEventListener('mouseup', onUpBound); window.addEventListener('mouseup', onUpBound);
// Joy-Animation Check nach 4s, aber nicht vorher auslösen
holdTimer = setTimeout(() => { holdTimer = setTimeout(() => {
if (moveDist >= 75) { if (moveDist >= MOVE_THRESHOLD && mouseDown) {
// "Streicheln" noch aktiv? Dann sofort Joy-Animation auslösen
mouseDown = false; // Simuliere Loslassen
window.removeEventListener('mousemove', onMoveBound);
window.removeEventListener('mouseup', onUpBound);
joyActive = true; joyActive = true;
cleanup();
this._runJoyAnimation(() => { this._runJoyAnimation(() => {
joyActive = false; joyActive = false;
reopenEyes(); reopenEyes();
// *** Nach Streichelspiel Blinken neu starten ***
this._startBlinking(); this._startBlinking();
}); });
} else {
cleanup();
reopenEyes();
// *** Nach Streichelspiel Blinken neu starten ***
this._startBlinking();
} }
mouseDown = false;
}, 4000); }, 4000);
}); });
} }