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