pet-pose.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // 姿态渲染层:监听 locomotion.rs 的 pet://gait / pet://perch,把动作映射到 #pet-body 上的 .pose-* 类。
  2. // 与 #pet 上的情绪动画(pet-engine.js 管理 .anim-*)分层叠加,互不覆盖。
  3. (function () {
  4. 'use strict';
  5. var petBody = document.getElementById('pet-body');
  6. if (!petBody) return;
  7. var POSES = { idle: 'pose-idle', walk: 'pose-walk', hop: 'pose-hop', fly: 'pose-fly', perch: 'pose-perch' };
  8. var currentPose = null;
  9. var lastDir = 'right';
  10. function apply(kind, direction) {
  11. if (direction === 'left' || direction === 'right') lastDir = direction;
  12. var cls = POSES[kind] || 'pose-idle'; // 未知 kind / idle → 呼吸待机
  13. if (currentPose !== cls) {
  14. if (currentPose) petBody.classList.remove(currentPose);
  15. currentPose = cls;
  16. void petBody.offsetWidth; // 强制回流,确保动画可重播
  17. petBody.classList.add(cls);
  18. }
  19. // 朝向:left → 水平镜像(fly/perch 的 direction 为 none,沿用上次朝向)
  20. petBody.classList.toggle('pose-face-left', lastDir === 'left');
  21. }
  22. // 初始即进入呼吸待机(Rust 侧初始 gait 不会 emit,故此处兜底)
  23. apply('idle');
  24. try {
  25. var listen = window.__TAURI__.event.listen;
  26. listen('pet://gait', function (e) {
  27. var p = (e && e.payload) || {};
  28. apply(p.kind, p.direction);
  29. });
  30. listen('pet://perch', function (e) {
  31. window.__petPerched = !!(e.payload && e.payload.active);
  32. // 站手瞬间 gait 事件已带 perch;此处兜底确保姿态出现
  33. if (window.__petPerched) apply('perch');
  34. });
  35. } catch (err) {
  36. console.log('[pet-pose] tauri events unavailable', err);
  37. }
  38. })();