• 简体中文
  • CastLineup

    按脚本编排多个角色的一场戏。

    来自 @aora-bot/surfaces,不属于 @aora-bot/react:pnpm add @aora-bot/surfaces @aora-bot/react,并在 React 样式之后引入 @aora-bot/surfaces/styles.css。

    1/4, enter
    import { AgentPortrait, CharacterStageProvider } from '@aora-bot/react';
    import { PortraitSlot, CastLineup, useCastScene, type CastStep } from '@aora-bot/surfaces';
    
    const members = [
      { id: 'lead', shape: 'blob', color: 'blue', bobPx: 4 },
      { id: 'helper', shape: 'hex', color: 'green', bobPx: 3, bobMs: 2600 },
      { id: 'scout', shape: 'star', color: 'orange', sizePx: 56 },
    ] as const;
    
    const steps: CastStep[] = [
      { id: 'enter', positions: { lead: { x: 0, y: -40, state: 'waking' } }, durationMs: 1400 },
      {
        id: 'meet',
        positions: { lead: { x: -60, y: 0 }, helper: { x: 60, y: 10, state: 'happy' } },
        durationMs: 1600,
      },
      {
        id: 'work',
        positions: {
          lead: { x: -80, y: -20, state: 'thinking' },
          helper: { x: 0, y: 30, state: 'working' },
          scout: { x: 80, y: -30, state: 'searching' },
        },
        durationMs: 2000,
      },
      { id: 'land', positions: { lead: { x: 110, y: 90, scale: 0.7, state: 'happy' } }, durationMs: 1800 },
    ];
    
    export default function Demo() {
      const scene = useCastScene(steps, { autoplay: true, loop: true });
      const landed = scene.step?.id === 'land';
      return (
        <CharacterStageProvider>
          <div className="site-demo site-demo--column">
            <CastLineup members={members} steps={steps} stepIndex={scene.stepIndex} widthPx={320} heightPx={260}>
              <div style={{ position: 'absolute', right: 16, bottom: 16 }}>
                <PortraitSlot arrived={landed} sizePx={56} label="你的智能体">
                  <AgentPortrait agent={{ id: 'lead', shape: 'blob', color: 'blue' }} sizePx={56} state="happy" />
                </PortraitSlot>
              </div>
            </CastLineup>
            <div className="site-controls">
              <button className="site-btn" onClick={scene.prev}>
                ‹
              </button>
              <button className="site-btn" onClick={scene.playing ? scene.pause : scene.play}>
                {scene.playing ? '暂停' : '播放'}
              </button>
              <button className="site-btn" onClick={scene.next}>
                ›
              </button>
              <span className="site-demo__label">
                {scene.stepIndex + 1}/{steps.length}, {scene.step?.id}
              </span>
            </div>
          </div>
        </CharacterStageProvider>
      );
    }

    脚本就是数据

    一场戏由一组 members 和一组 steps 组成。每一步指定每个成员的位置,以及这一步停留多久。位置包括相对舞台中心的像素坐标 x、y,可选的 scale,以及这一步要采用的可选 state。某一步的 positions 中没有列出的成员,在这一步就是下场:会缩小并淡出,而不是直接消失。

    成员和步骤都是普通数据,编排由宿主自己编写,组件本身并不知道这场戏的内容。引导流程可以重新排列场次,而不需要改动任何移动逻辑。

    没有帧循环的弹簧

    移动用的是 CSS transition,缓动曲线则由引擎自带的弹簧采样,生成 linear() 函数。真正的弹簧需要帧循环,而为四个只是在滑行的成员单独开一个帧循环,会和它们各自已在为脸运行的动画争抢资源。对解析解采样可以得到同一条曲线(包括过冲),代价只是一个字符串。spring 调节刚度(omega)和阻尼(zeta),moveMs 是过渡时长。

    某个成员的 transform 结束时会触发 onArrive(memberId, stepIndex),每个成员每一步触发一次。宿主可以在角色落地的那一刻触发下一件事,不必靠估算时间的定时器。

    useCastScene

    这个 Hook 只决定何时切换,不决定在哪里。它保存当前的 stepIndex,playing 时在每一步的 durationMs 之后前进,到最后一步停下,设置了 loop 时则循环。next、prev、play、pause、goTo 用于控制播放;onStep 在每次切换时报告。autoplay 让它在挂载时就开始播放。

    CastFigure 与 PortraitSlot

    CastFigure 是嵌套在两层容器里的一个角色:外层负责移动(位置、缩放、透明度共用同一个 transition),内层负责原地起伏(bobPx、bobMs),所以成员可以一边滑过舞台,一边原地漂浮。请为每个成员设置不同的 bobMs,避免整群角色同步起伏。成员都是 StagedMark,所以上层需要有 CharacterStageProvider。

    PortraitSlot 是角色最后飞入的那个空圆:场景播放期间显示一圈虚线轮廓,提示「这里应该放点什么」;arrived 为 true 后,交叉淡入到 children。它由两层构成,所以过渡期间不会出现空白。它不关心自己的位置,用普通 CSS 定位即可,放在舞台里或旁边的表单里都行。

    API

    没有找到 API 元数据: CastLineup — pnpm --filter @aora-bot/docs api

    CastFigure

    没有找到 API 元数据: CastFigure — pnpm --filter @aora-bot/docs api

    PortraitSlot

    没有找到 API 元数据: PortraitSlot — pnpm --filter @aora-bot/docs api

    接下来