1
0

Refactor mouse hold logic to include movement tracking and joy animation conditions

This commit is contained in:
2025-05-26 04:10:23 +02:00
parent 1c6ce0eec7
commit ecb5ddfa09

View File

@@ -168,6 +168,7 @@ _bindMouseHold() {
closeEyes(); closeEyes();
clearTimeout(this._blinkTimeout); // Blinzeln pausieren clearTimeout(this._blinkTimeout); // Blinzeln pausieren
// Bewegung tracken
function onMove(ev) { function onMove(ev) {
if (!mouseDown) return; if (!mouseDown) return;
if (!isMouseNearCat(ev)) { if (!isMouseNearCat(ev)) {
@@ -187,11 +188,47 @@ _bindMouseHold() {
const onMoveBound = onMove.bind(this); const onMoveBound = onMove.bind(this);
function onUp() { // Nach 4 Sekunden wird entschieden, unabhängig vom Mauszustand
const heldFor = Date.now() - mouseDownAt; holdTimer = setTimeout(() => {
cleanup(); if (!mouseDown) return; // Schon abgebrochen
// (1) Joy-Animation wenn erfüllt // Joy-Bedingung
if (moveDist >= MOVE_THRESHOLD) {
cleanup();
joyActive = true;
this._runJoyAnimation(() => {
joyActive = false;
reopenEyes();
this._startBlinking();
});
mouseDown = false;
} else {
// Trostpreis: Augen auf, dann mouth_open
cleanup();
mouseDown = false;
reopenEyes();
const heldFor = Date.now() - mouseDownAt;
const mouthOpenTime = Math.max(heldFor - 1000, 0);
if (mouthOpenTime > 0) {
this.img.src = this.images.mouthOpen || this.images.default;
this._startBlinking();
setTimeout(() => {
if (!joyActive && !this._isSpeaking) this.img.src = this.images.default;
}, mouthOpenTime);
}
else {
this._startBlinking();
}
}
}, 4000);
// Beim Loslassen: Falls früher losgelassen, normale Logik
function onUp() {
if (!mouseDown) return;
cleanup();
const heldFor = Date.now() - mouseDownAt;
if (heldFor >= 4000) return; // already handled by timer above
// Joy: wenn beide Bedingungen erfüllt
if (heldFor >= 4000 && moveDist >= MOVE_THRESHOLD) { if (heldFor >= 4000 && moveDist >= MOVE_THRESHOLD) {
joyActive = true; joyActive = true;
this._runJoyAnimation(() => { this._runJoyAnimation(() => {
@@ -200,23 +237,21 @@ _bindMouseHold() {
this._startBlinking(); this._startBlinking();
}); });
} }
// (2) Trostpreis: Mouth open wenn >1s gehalten, aber keine Joy-Bedingung erfüllt // Trostpreis: siehe oben
else if (heldFor > 1000) { else if (heldFor > 1000) {
reopenEyes();
const mouthOpenTime = Math.max(heldFor - 1000, 0); const mouthOpenTime = Math.max(heldFor - 1000, 0);
if (mouthOpenTime > 0) { if (mouthOpenTime > 0) {
this.img.src = this.images.mouthOpen || this.images.default; this.img.src = this.images.mouthOpen || this.images.default;
// BLINKEN IST AKTIV (also _startBlinking sofort!)
this._startBlinking(); this._startBlinking();
setTimeout(() => { setTimeout(() => {
if (!joyActive && !this._isSpeaking) this.img.src = this.images.default; if (!joyActive && !this._isSpeaking) this.img.src = this.images.default;
}, mouthOpenTime); }, mouthOpenTime);
} else { }
reopenEyes(); else {
this._startBlinking(); this._startBlinking();
} }
} } else {
// (3) Weniger als 1 Sekunde? Nichts besonderes.
else {
reopenEyes(); reopenEyes();
this._startBlinking(); this._startBlinking();
} }
@@ -235,22 +270,6 @@ _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(() => {
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;
this._runJoyAnimation(() => {
joyActive = false;
reopenEyes();
this._startBlinking();
});
}
}, 4000);
}); });
} }