| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- (function () {
- 'use strict';
- const petImg = document.getElementById('pet-img');
- const petEl = document.getElementById('pet');
- const bubble = document.getElementById('bubble');
- let currentState = 'idle';
- let currentAnimation = null;
- let config = null;
- let bubbleTimer = null;
- const DEFAULT_IMAGE = 'data:image/svg+xml;utf8,' +
- encodeURIComponent('<svg xmlns="http://www.w3.org/2000/svg" width="120" height="140">' +
- '<text y="100" font-size="90" text-anchor="middle">🦜</text></svg>');
- const BUBBLE_TEXTS = {
- happy: '完成了!继续保持~',
- busy: '有任务在等你呢~',
- worried: '任务要过期啦,快去看看!',
- excited: '打卡成功,太棒了!',
- remind: '该喝水啦~',
- tired: '有点累了呢…',
- sad: '要照顾好自己哦…',
- waitingForInteraction: '来和我互动一下吧!',
- miniGame: '我们一起玩吧!',
- sleeping: 'zZ…',
- custom: '自定义状态'
- };
- window.cfcPetAPI = {
- notifyState: function (stateJson) {
- const payload = typeof stateJson === 'string' ? JSON.parse(stateJson) : stateJson;
- setState(payload.state, payload.reason || null, payload.data || null);
- },
- setCustomState: function (stateName, dataJson) {
- const data = typeof dataJson === 'string' ? JSON.parse(dataJson) : dataJson;
- setState(stateName, 'custom', data);
- },
- loadConfig: function (configJson) {
- config = typeof configJson === 'string' ? JSON.parse(configJson) : configJson;
- applyImage(config);
- },
- showBubble: function (text, durationMs) {
- bubble.textContent = text;
- bubble.style.display = 'block';
- clearTimeout(bubbleTimer);
- if (durationMs) bubbleTimer = setTimeout(hideBubble, durationMs);
- },
- hideBubble: hideBubble,
- getState: function () { return currentState; }
- };
- function hideBubble() { bubble.style.display = 'none'; }
- function applyImage(cfg) {
- if (!cfg || !cfg.default_image) return;
- const src = cfg.image_base_url + cfg.default_image;
- petImg.src = src;
- petImg.onerror = function () { petImg.src = DEFAULT_IMAGE; };
- }
- function setState(stateName, reason, data) {
- currentState = stateName;
- if (currentAnimation) { petEl.classList.remove(currentAnimation); currentAnimation = null; }
- let animClass = null;
- if (config && config.actions && config.actions[stateName]) {
- animClass = 'anim-' + config.actions[stateName];
- } else {
- animClass = 'anim-' + stateName;
- }
- const styleEl = document.getElementById('pet-animations');
- const sheet = styleEl ? styleEl.sheet : null;
- const hasClass = sheet && Array.from(sheet.cssRules).some(r => r.selectorText === '.' + animClass);
- if (hasClass) { petEl.classList.add(animClass); currentAnimation = animClass; }
- if (reason === 'drink_water') {
- bubble.textContent = '该喝水啦~';
- bubble.style.display = 'block';
- clearTimeout(bubbleTimer);
- bubbleTimer = setTimeout(hideBubble, 8000);
- } else if (reason === 'welcome') {
- bubble.textContent = '我来了!';
- bubble.style.display = 'block';
- clearTimeout(bubbleTimer);
- bubbleTimer = setTimeout(hideBubble, 3000);
- } else if (BUBBLE_TEXTS[stateName]) {
- bubble.textContent = BUBBLE_TEXTS[stateName];
- bubble.style.display = 'block';
- clearTimeout(bubbleTimer);
- bubbleTimer = setTimeout(hideBubble, 5000);
- }
- }
- petImg.src = DEFAULT_IMAGE;
- setInterval(function () {
- if (document.getElementById('pet-img')) {
- petImg.classList.add('anim-blink_eye');
- setTimeout(() => petImg.classList.remove('anim-blink_eye'), 200);
- }
- }, 3500);
- })();
|