# 随状态变化的角色反应

猫睡着时按铃，它会抬起头、睁开眼。它醒着时再按同一个铃，它会睁大眼睛、竖起耳朵。

## 试一试

先让猫睡觉，然后按两次铃。第一次会叫醒它，第二次会让它竖耳倾听。

## 交互细节

猫有熟睡、醒着休息、竖耳倾听三种状态。倾听时再次按铃，姿势保持不变。用“让猫睡觉”和“让猫醒着坐好”设置起始状态。

## 各平台的操作方式

- iOS · 轻点“让猫睡觉”，再轻点两次铃。观察猫每次的眼睛和耳朵。
- Android · 让猫睡觉后点按两次同一个铃。也可以让猫醒着坐好，再试一次。
- PC · 按 Tab 移至铃按钮，再按两次回车。用“让猫睡觉”或“让猫醒着坐好”重新开始。

## 适用场景

让游戏角色或互动插画根据当前状态对同一输入作出不同反应。

## 实现要求

> 根据猫当前的状态响应按铃操作。睡着时让它醒来，醒着时让它竖起耳朵，并允许重新选择起始状态。

## 来源与致谢

[Rive · Transition conditions](<https://rive.app/docs/editor/state-machine/transitions#transition-conditions>)

## 独立 HTML 示例

将以下代码保存为 HTML 文件，用浏览器打开即可离线体验。

```html
<!doctype html>
<html lang="zh-Hans" data-motion-locales="inline">
<head><script>if(new URLSearchParams(location.search).get('reduce')==='1')document.documentElement.classList.add('motion-reduced');</script>
<meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">
<title>서점 고양이와 종</title>
<style>
*{box-sizing:border-box}:root{font:15px/1.45 system-ui,sans-serif;color:#3e302b;background:#f5eee1;color-scheme:light}
body{margin:0}main{width:min(780px,100%);margin:auto;padding:clamp(12px,3vw,24px)}
header,.readout,.controls{display:flex;align-items:center;flex-wrap:wrap;gap:6px 12px}header,.readout{justify-content:space-between}
h1{margin:0;font-size:clamp(22px,4vw,34px);line-height:1.12;letter-spacing:-.055em}header small{color:#695747}
.vignette{border:1px solid #ae9985;border-radius:13px;overflow:hidden;margin-top:12px;background:#f1c190}
svg{display:block;width:100%;height:auto}.readout{padding:9px 14px;background:#493e46;color:#faf1de;font-size:14px}.readout b{font-weight:650}.readout span{color:#f0d3b4}
.controls{margin-top:12px;gap:7px}button{min-height:44px;border:1px solid #ad9882;border-radius:7px;padding:7px 13px;background:#fff8ed;color:#45342b;cursor:pointer;font:600 14px system-ui}button:focus-visible{outline:3px solid #307b74;outline-offset:2px}
#bell{background:#514153;color:#fff6df;border-color:#514153}.hint{font-size:13px;margin:8px 0 0;color:#665548}
#cat-head,#ear-a,#ear-b,#tail,#paw{transition:transform .32s ease}#cat-head{transform-origin:302px 131px}#ear-a{transform-origin:275px 96px}#ear-b{transform-origin:325px 96px}#tail{transform-origin:185px 168px}#paw{transform-origin:283px 160px}
[data-state="sleep"] #cat-head{transform:translate(-25px,35px) rotate(13deg)}[data-state="sleep"] #ear-a{transform:rotate(-15deg)}[data-state="sleep"] #ear-b{transform:rotate(12deg)}
[data-state="awake"] #cat-head{transform:translate(0,2px)}[data-state="awake"] #ear-a{transform:rotate(-12deg)}[data-state="awake"] #ear-b{transform:rotate(14deg)}
[data-state="alert"] #cat-head{transform:translate(5px,-10px) rotate(-4deg)}[data-state="alert"] #tail{transform:rotate(-10deg)}[data-state="alert"] #paw{transform:translate(4px,-2px) rotate(-7deg)}
.eyes,.sleep-mark,.listen-mark{display:none}[data-state="sleep"] #closed,[data-state="sleep"] .sleep-mark,[data-state="awake"] #open,[data-state="alert"] #wide,[data-state="alert"] .listen-mark{display:inline}
#ring-lines{opacity:0}#bell-art.strike #ring-lines{animation:rings .48s ease-out}#bell-art.strike #bell-cap{animation:chime .48s ease-in-out;transform-origin:413px 178px}
@keyframes rings{0%,20%{opacity:1}100%{opacity:0}}@keyframes chime{0%,100%{transform:rotate(0deg)}30%{transform:rotate(-5deg)}65%{transform:rotate(4deg)}}
@media(prefers-reduced-motion:reduce){*,*::before,*::after{animation:none!important;transition:none!important}}
.motion-reduced *{animation:none!important;transition:none!important}
@media(max-width:385px){button{padding-inline:10px}.readout{padding:8px 10px}}
</style>
</head><body><main>
<header><h1 id="title">서점 고양이와 종</h1><small data-i="kind">상태에 따른 반응</small></header>
<section id="scene" class="vignette" data-state="sleep" aria-labelledby="title">
<svg viewBox="0 0 560 220" role="img" aria-labelledby="drawing"><title id="drawing">창가의 고양이와 계산대 종</title>
<defs><linearGradient id="wall" x2="1" y2="1"><stop stop-color="#f6ddac"/><stop offset="1" stop-color="#de9b78"/></linearGradient><pattern id="grain" width="9" height="9" patternUnits="userSpaceOnUse"><circle cx="2" cy="4" r=".7" fill="#937450" opacity=".2"/></pattern><clipPath id="pane"><rect x="24" y="0" width="127" height="129" rx="1"/></clipPath></defs>
<rect width="560" height="220" fill="url(#wall)"/><rect width="560" height="220" fill="url(#grain)"/>
<g clip-path="url(#pane)"><rect x="24" width="127" height="129" fill="#b8d4c1"/><circle cx="123" cy="34" r="15" fill="#fae5ab"/><path d="M0 121Q61 66 117 95T195 85V145H0Z" fill="#769d90"/><path d="M0 116Q60 94 103 119T177 101V143H0Z" fill="#506f68"/></g>
<path d="M21 0V131H154V0M86 0V130M23 60H152" fill="none" stroke="#c29f7d" stroke-width="6"/><path d="M14 133H160" stroke="#a98669" stroke-width="5" stroke-linecap="round"/>
<g transform="translate(450,10)"><path d="M0 0H110V80H0Z" fill="#c38b71"/><path d="M3 60H110V65H3Z" fill="#866355"/><path d="M12 17H22V59H12Z" fill="#726e72"/><path d="M24 24H35V59H24Z" fill="#d4b57f"/><path d="M37 13H49V59H37Z" fill="#818d81"/><path d="M52 20L62 17L73 57L63 60Z" fill="#a15e49"/></g>
<path d="M0 174Q128 163 266 169Q410 160 560 168V220H0Z" fill="#a36f58"/><path d="M0 186Q270 175 560 179V220H0Z" fill="#765244"/><path d="M0 193Q220 185 560 187" fill="none" stroke="#c19370" stroke-width="2"/>
<ellipse cx="246" cy="182" rx="106" ry="11" fill="#593d36" opacity=".43"/>
<g id="cat">
<path id="tail" d="M198 160C162 151 149 160 142 145C134 129 143 123 143 117C142 109 134 109 128 116C107 141 125 170 149 176C166 182 190 183 210 176Z" fill="#ad593d" stroke="#694536" stroke-width="2.5"/>
<path d="M160 161C159 119 188 103 220 105C253 103 285 118 299 139L305 175Q293 184 257 183H188Q158 182 160 161Z" fill="#cc7955" stroke="#694536" stroke-width="2.5"/>
<path d="M186 117Q191 129 201 134M201 111Q206 122 215 126M219 111Q223 119 231 124" fill="none" stroke="#a25b44" stroke-width="5" stroke-linecap="round"/>
<path d="M182 165Q195 144 215 147Q235 146 243 161" stroke="#ab6148" stroke-width="3" fill="none"/>
<path id="paw" d="M275 140Q270 153 271 167Q268 176 254 178Q250 187 274 184L297 181Q306 176 302 158Z" fill="#df9a71" stroke="#704735" stroke-width="2"/>
<g id="cat-head">
<g id="ear-a"><path d="M268 106L262 72Q265 66 271 72L290 88Z" fill="#c57a56" stroke="#694536" stroke-width="2.3"/><path d="M269 93L266 76L281 89Z" fill="#e7b094"/></g>
<g id="ear-b"><path d="M311 89L330 68Q335 65 335 75L334 105Z" fill="#c57a56" stroke="#694536" stroke-width="2.3"/><path d="M318 90L330 76L329 96Z" fill="#e7b094"/></g>
<path d="M264 105C267 91 282 85 298 85C319 83 338 95 340 110C344 130 331 145 309 148C290 152 269 142 261 126Q257 115 264 105Z" fill="#d88a62" stroke="#694536" stroke-width="2.4"/>
<path d="M285 89L292 99M297 86L300 97M310 87L308 96" fill="none" stroke="#a55c44" stroke-width="3.5" stroke-linecap="round"/>
<path d="M275 126Q285 117 298 123Q310 117 324 124Q326 136 306 142Q286 146 275 132Z" fill="#f1caa2"/>
<g id="closed" class="eyes" fill="none" stroke="#473630" stroke-width="2.4" stroke-linecap="round"><path d="M273 114Q279 121 285 115M310 112Q317 119 324 112"/></g>
<g id="open" class="eyes" fill="#46372f"><ellipse cx="281" cy="114" rx="3.1" ry="4"/><ellipse cx="318" cy="112" rx="3.1" ry="4"/></g>
<g id="wide" class="eyes" fill="#fcdd99" stroke="#715440" stroke-width="1.3"><ellipse cx="282" cy="113" rx="6.4" ry="6.8"/><ellipse cx="318" cy="111" rx="6.4" ry="6.8"/><g fill="#3a352c" stroke="none"><ellipse cx="284" cy="113" rx="2.1" ry="5"/><ellipse cx="320" cy="111" rx="2.1" ry="5"/></g></g>
<path d="M294 124Q300 121 306 123L301 129Z" fill="#8a5748"/><path d="M301 129Q300 134 294 133M301 129Q305 134 309 131M276 128L254 127M278 132L259 136M322 124L344 119M323 129L346 131" fill="none" stroke="#745345" stroke-width="1.4" stroke-linecap="round"/>
</g></g>
<g class="sleep-mark" fill="none" stroke="#836c73" stroke-width="2.4" stroke-linejoin="round"><path d="M322 91H330L323 99H332M336 70H349L337 83H352"/></g>
<g class="listen-mark" fill="none" stroke="#876549" stroke-width="2.5" stroke-linecap="round"><path d="M353 75L364 68M357 85L370 82"/></g>
<g id="bell-art"><ellipse cx="413" cy="183" rx="31" ry="5" fill="#58433c" opacity=".6"/><path d="M385 177Q412 170 441 177L441 182Q412 188 384 181Z" fill="#574443"/><g id="bell-cap"><path d="M389 174Q391 156 411 152Q429 150 435 173Z" fill="#e2b560" stroke="#82633e" stroke-width="2"/><path d="M395 168Q398 160 406 158" fill="none" stroke="#f5d991" stroke-width="3" stroke-linecap="round"/><path d="M412 152V145M407 145H417" stroke="#7f6540" stroke-width="3" stroke-linecap="round"/></g><g id="ring-lines" fill="none" stroke="#735d49" stroke-width="2.4" stroke-linecap="round"><path d="M387 153Q380 148 382 140M440 152Q447 146 444 139"/></g></g>
</svg>
<div class="readout"><span data-i="current">현재 상태</span><b id="state" role="status"></b></div>
</section>
<div class="controls"><button id="bell" data-i="bell">종 울리기</button><button id="sleep" data-i="sleep">재우기</button><button id="rest" data-i="rest">편히 앉히기</button></div>
<p class="hint" data-i="hint">잠든 고양이에게 종을 한 번 울려 보세요. 눈을 뜬 뒤 같은 종을 한 번 더 눌러 귀를 살펴보세요.</p>
</main><script>
'use strict';
const params=new URLSearchParams(location.search);
if(params.get('reduce')==='1')document.documentElement.classList.add('motion-reduced');
const copy={
 ko:{title:'서점 고양이와 종',kind:'상태에 따른 반응',current:'현재 상태',bell:'종 울리기',sleep:'재우기',rest:'편히 앉히기',asleep:'잠들어 있음',awake:'깨어 있음',alert:'귀를 세워 듣는 중',drawing:'창가의 고양이와 계산대 종',hint:'잠든 고양이에게 종을 한 번 울려 보세요. 눈을 뜬 뒤 같은 종을 한 번 더 눌러 귀를 살펴보세요.'},
 en:{title:'The bookshop cat',kind:'State-dependent reaction',current:'Current state',bell:'Ring the bell',sleep:'Put to sleep',rest:'Settle awake',asleep:'Asleep',awake:'Awake at rest',alert:'Listening with ears up',drawing:'A bookshop cat by the window beside a counter bell',hint:'Ring once while the cat sleeps. When its eyes open, ring the same bell again and watch the ears.'},
 es:{title:'El gato de la librería',kind:'Reacción según el estado',current:'Estado actual',bell:'Tocar el timbre',sleep:'Dormir al gato',rest:'Sentarlo despierto',asleep:'Dormido',awake:'Despierto y tranquilo',alert:'Escuchando con las orejas erguidas',drawing:'Un gato junto a la ventana de la librería y un timbre de mostrador',hint:'Toca el timbre mientras el gato duerme. Cuando abra los ojos, vuelve a tocar el mismo timbre y fíjate en las orejas.'},
 'zh-Hans':{title:'书店里的猫',kind:'随状态变化的反应',current:'当前状态',bell:'按一下铃',sleep:'让猫睡觉',rest:'让猫醒着坐好',asleep:'正在熟睡',awake:'醒着休息',alert:'竖起耳朵倾听',drawing:'书店窗边的小猫与柜台铃',hint:'小猫睡着时按一下铃。它睁眼后再次按铃，观察耳朵的变化。'}
};
const requested=params.get('lang')||document.documentElement.lang, lang=Object.hasOwn(copy,requested)?requested:'ko', t=copy[lang];
document.documentElement.lang=lang;document.title=t.title;document.getElementById('title').textContent=t.title;
for(const el of document.querySelectorAll('[data-i]'))el.textContent=t[el.dataset.i];
const scene=document.getElementById('scene'),svgTitle=document.getElementById('drawing'), stateText=document.getElementById('state'), bellArt=document.getElementById('bell-art');
function setState(state){scene.dataset.state=state;const description=t[state==='sleep'?'asleep':state];stateText.textContent=description;svgTitle.textContent=t.drawing+' · '+description;}
const next={sleep:'awake',awake:'alert',alert:'alert'};
document.getElementById('bell').addEventListener('click',()=>{
  setState(next[scene.dataset.state]);
  bellArt.classList.remove('strike');void bellArt.getBoundingClientRect();bellArt.classList.add('strike');
});
document.getElementById('sleep').addEventListener('click',()=>setState('sleep'));
document.getElementById('rest').addEventListener('click',()=>setState('awake'));setState('sleep');
</script></body></html>
```
