2025-11-20 09:15:38 +00:00
|
|
|
import { ref, computed, onMounted, onUnmounted } from 'vue';
|
|
|
|
|
|
|
|
|
|
export function usePetSystem() {
|
|
|
|
|
// --- State ---
|
|
|
|
|
const stage = ref('egg'); // egg, baby, adult
|
|
|
|
|
const state = ref('idle'); // idle, sleep, eating, sick, dead, refuse
|
|
|
|
|
|
|
|
|
|
// --- Stats ---
|
|
|
|
|
const stats = ref({
|
|
|
|
|
hunger: 100, // 0-100 (0 = Starving)
|
|
|
|
|
happiness: 100, // 0-100 (0 = Depressed)
|
|
|
|
|
health: 100, // 0-100 (0 = Sick risk)
|
|
|
|
|
weight: 500, // grams
|
2025-11-22 03:23:02 +00:00
|
|
|
age: 1, // days (start at day 1)
|
2025-11-20 09:15:38 +00:00
|
|
|
poopCount: 0 // Number of poops on screen
|
|
|
|
|
});
|
|
|
|
|
|
2025-11-22 03:23:02 +00:00
|
|
|
const achievements = ref([
|
|
|
|
|
{ id: 'newbie', name: '新手飼主', desc: '養育超過 1 天', unlocked: false, icon: '🥚' },
|
|
|
|
|
{ id: 'veteran', name: '資深飼主', desc: '養育超過 7 天', unlocked: false, icon: '🏆' },
|
|
|
|
|
{ id: 'healthy', name: '健康寶寶', desc: '3歲且健康 > 90', unlocked: false, icon: '💪' },
|
|
|
|
|
{ id: 'happy', name: '快樂天使', desc: '3歲且快樂 > 90', unlocked: false, icon: '💖' }
|
|
|
|
|
]);
|
|
|
|
|
|
2025-11-20 09:15:38 +00:00
|
|
|
// --- Internal Timers ---
|
|
|
|
|
let gameLoopId = null;
|
2025-11-22 03:23:02 +00:00
|
|
|
let tickCount = 0;
|
2025-11-20 09:15:38 +00:00
|
|
|
const TICK_RATE = 3000; // 3 seconds per tick
|
2025-11-22 03:23:02 +00:00
|
|
|
const TICKS_PER_DAY = 20; // For testing: 1 minute = 1 day (usually 28800 for 24h)
|
2025-11-20 09:15:38 +00:00
|
|
|
|
|
|
|
|
const isCleaning = ref(false);
|
|
|
|
|
|
|
|
|
|
// --- Actions ---
|
|
|
|
|
|
|
|
|
|
function feed() {
|
|
|
|
|
if (state.value === 'sleep' || state.value === 'dead' || stage.value === 'egg' || isCleaning.value) return false;
|
|
|
|
|
|
|
|
|
|
if (state.value === 'sick' || stats.value.hunger >= 90) {
|
|
|
|
|
// Refuse food if sick or full
|
|
|
|
|
triggerState('refuse', 2000);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Eat
|
|
|
|
|
triggerState('eating', 3000); // Animation duration
|
|
|
|
|
stats.value.hunger = Math.min(100, stats.value.hunger + 20);
|
|
|
|
|
stats.value.weight += 50;
|
|
|
|
|
|
2025-11-20 16:00:13 +00:00
|
|
|
// Chance to poop after eating (降低機率)
|
|
|
|
|
if (Math.random() < 0.15) { // 從 0.3 降到 0.15
|
2025-11-20 09:15:38 +00:00
|
|
|
setTimeout(() => {
|
|
|
|
|
if (stats.value.poopCount < 4) {
|
|
|
|
|
stats.value.poopCount++;
|
|
|
|
|
}
|
|
|
|
|
}, 4000);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function play() {
|
|
|
|
|
if (state.value !== 'idle' || stage.value === 'egg' || isCleaning.value) return false;
|
|
|
|
|
|
|
|
|
|
stats.value.happiness = Math.min(100, stats.value.happiness + 15);
|
|
|
|
|
stats.value.weight -= 10; // Exercise burns calories
|
|
|
|
|
stats.value.hunger = Math.max(0, stats.value.hunger - 5);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function clean() {
|
|
|
|
|
if (stats.value.poopCount > 0 && !isCleaning.value) {
|
|
|
|
|
isCleaning.value = true;
|
|
|
|
|
|
|
|
|
|
// Delay removal for animation
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
stats.value.poopCount = 0;
|
|
|
|
|
stats.value.happiness += 10;
|
|
|
|
|
isCleaning.value = false;
|
|
|
|
|
}, 2000); // 2 seconds flush animation
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function sleep() {
|
|
|
|
|
if (isCleaning.value) return;
|
|
|
|
|
|
|
|
|
|
if (state.value === 'idle') {
|
|
|
|
|
state.value = 'sleep';
|
|
|
|
|
} else if (state.value === 'sleep') {
|
|
|
|
|
state.value = 'idle'; // Wake up
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --- Game Loop ---
|
|
|
|
|
function tick() {
|
|
|
|
|
if (state.value === 'dead' || stage.value === 'egg') return;
|
|
|
|
|
|
|
|
|
|
// Decrease stats naturally
|
2025-11-20 16:00:13 +00:00
|
|
|
// 目標:飢餓值約 30-60 分鐘下降 10%,快樂值約 20-40 分鐘下降 10%
|
|
|
|
|
// TICK_RATE = 3000ms (3秒), 600 ticks = 30分鐘
|
|
|
|
|
// 飢餓值每 tick -0.05 → 600 ticks = -30 (30分鐘下降30%)
|
|
|
|
|
// 快樂值每 tick -0.08 → 600 ticks = -48 (30分鐘下降48%)
|
|
|
|
|
|
2025-11-20 09:15:38 +00:00
|
|
|
if (state.value !== 'sleep') {
|
2025-11-20 16:00:13 +00:00
|
|
|
stats.value.hunger = Math.max(0, stats.value.hunger - 0.05);
|
|
|
|
|
stats.value.happiness = Math.max(0, stats.value.happiness - 0.08);
|
2025-11-20 09:15:38 +00:00
|
|
|
} else {
|
2025-11-20 16:00:13 +00:00
|
|
|
// Slower decay when sleeping (約 1/3 速度)
|
|
|
|
|
stats.value.hunger = Math.max(0, stats.value.hunger - 0.015);
|
|
|
|
|
stats.value.happiness = Math.max(0, stats.value.happiness - 0.025);
|
2025-11-20 09:15:38 +00:00
|
|
|
}
|
|
|
|
|
|
2025-11-20 16:00:13 +00:00
|
|
|
// Random poop generation (更低的機率:約 0.5% per tick)
|
|
|
|
|
// 平均約每 200 ticks = 10 分鐘拉一次
|
|
|
|
|
if (state.value !== 'sleep' && Math.random() < 0.005 && stats.value.poopCount < 4 && !isCleaning.value) {
|
2025-11-20 09:15:38 +00:00
|
|
|
stats.value.poopCount++;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-20 16:00:13 +00:00
|
|
|
// Health Logic (更溫和的健康下降)
|
|
|
|
|
// 便便影響健康:每個便便每 tick -0.1 health
|
2025-11-20 09:15:38 +00:00
|
|
|
if (stats.value.poopCount > 0) {
|
2025-11-20 16:00:13 +00:00
|
|
|
stats.value.health = Math.max(0, stats.value.health - (0.1 * stats.value.poopCount));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 飢餓影響健康:飢餓值低於 20 時開始影響健康
|
|
|
|
|
if (stats.value.hunger < 20) {
|
|
|
|
|
const hungerPenalty = (20 - stats.value.hunger) * 0.02; // 飢餓越嚴重,扣越多
|
|
|
|
|
stats.value.health = Math.max(0, stats.value.health - hungerPenalty);
|
2025-11-20 09:15:38 +00:00
|
|
|
}
|
|
|
|
|
|
2025-11-20 16:00:13 +00:00
|
|
|
// 不開心影響健康:快樂值低於 20 時開始影響健康(較輕微)
|
|
|
|
|
if (stats.value.happiness < 20) {
|
|
|
|
|
const happinessPenalty = (20 - stats.value.happiness) * 0.01;
|
|
|
|
|
stats.value.health = Math.max(0, stats.value.health - happinessPenalty);
|
2025-11-20 09:15:38 +00:00
|
|
|
}
|
|
|
|
|
|
2025-11-20 16:00:13 +00:00
|
|
|
// Sickness Check (更低的生病機率)
|
2025-11-20 09:15:38 +00:00
|
|
|
if (stats.value.health < 30 && state.value !== 'sick') {
|
2025-11-20 16:00:13 +00:00
|
|
|
if (Math.random() < 0.1) { // 從 0.3 降到 0.1
|
2025-11-20 09:15:38 +00:00
|
|
|
state.value = 'sick';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-20 16:00:13 +00:00
|
|
|
// Health Recovery (健康值可以緩慢恢復)
|
|
|
|
|
// 如果沒有便便、飢餓值和快樂值都高,健康值會緩慢恢復
|
|
|
|
|
if (stats.value.poopCount === 0 && stats.value.hunger > 50 && stats.value.happiness > 50 && stats.value.health < 100 && state.value !== 'sick') {
|
|
|
|
|
stats.value.health = Math.min(100, stats.value.health + 0.05);
|
2025-11-20 09:15:38 +00:00
|
|
|
}
|
|
|
|
|
|
2025-11-20 16:00:13 +00:00
|
|
|
// Death Check (移除死亡機制,依照之前的討論)
|
|
|
|
|
// if (stats.value.health === 0) {
|
|
|
|
|
// state.value = 'dead';
|
|
|
|
|
// }
|
|
|
|
|
|
2025-11-22 03:23:02 +00:00
|
|
|
// Evolution / Growth
|
|
|
|
|
tickCount++;
|
|
|
|
|
if (tickCount >= TICKS_PER_DAY) {
|
|
|
|
|
stats.value.age++;
|
|
|
|
|
tickCount = 0;
|
|
|
|
|
checkEvolution();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
checkAchievements();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function checkAchievements() {
|
|
|
|
|
if (!achievements.value[0].unlocked && stats.value.age >= 1) {
|
|
|
|
|
unlockAchievement(0);
|
|
|
|
|
}
|
|
|
|
|
if (!achievements.value[1].unlocked && stats.value.age >= 7) {
|
|
|
|
|
unlockAchievement(1);
|
|
|
|
|
}
|
|
|
|
|
if (!achievements.value[2].unlocked && stats.value.age >= 3 && stats.value.health >= 90) {
|
|
|
|
|
unlockAchievement(2);
|
|
|
|
|
}
|
|
|
|
|
if (!achievements.value[3].unlocked && stats.value.age >= 3 && stats.value.happiness >= 90) {
|
|
|
|
|
unlockAchievement(3);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function unlockAchievement(index) {
|
|
|
|
|
if (!achievements.value[index].unlocked) {
|
|
|
|
|
achievements.value[index].unlocked = true;
|
|
|
|
|
triggerState('happy', 2000); // Celebrate achievement
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function unlockAllAchievements() {
|
|
|
|
|
achievements.value.forEach(a => a.unlocked = true);
|
|
|
|
|
triggerState('happy', 2000);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function checkEvolution() {
|
|
|
|
|
// Simple evolution logic
|
|
|
|
|
if (stage.value === 'baby' && stats.value.age >= 3) {
|
|
|
|
|
stage.value = 'child';
|
|
|
|
|
triggerState('happy', 2000); // Celebrate
|
|
|
|
|
} else if (stage.value === 'child' && stats.value.age >= 7) {
|
|
|
|
|
stage.value = 'adult';
|
|
|
|
|
triggerState('happy', 2000);
|
|
|
|
|
}
|
2025-11-20 09:15:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --- Helpers ---
|
|
|
|
|
function triggerState(tempState, duration) {
|
|
|
|
|
const previousState = state.value;
|
|
|
|
|
state.value = tempState;
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
if (state.value === tempState) { // Only revert if state hasn't changed again
|
|
|
|
|
state.value = previousState === 'sleep' ? 'idle' : 'idle';
|
|
|
|
|
}
|
|
|
|
|
}, duration);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function hatchEgg() {
|
|
|
|
|
if (stage.value === 'egg') {
|
|
|
|
|
stage.value = 'baby'; // or 'adult' for now since we only have that sprite
|
|
|
|
|
// Let's map 'baby' to our 'adult' sprite for now, or just use 'adult'
|
|
|
|
|
stage.value = 'adult';
|
|
|
|
|
state.value = 'idle';
|
|
|
|
|
stats.value.hunger = 50;
|
|
|
|
|
stats.value.happiness = 50;
|
|
|
|
|
stats.value.health = 100;
|
|
|
|
|
stats.value.poopCount = 0;
|
|
|
|
|
isCleaning.value = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function reset() {
|
|
|
|
|
stage.value = 'egg';
|
|
|
|
|
state.value = 'idle';
|
|
|
|
|
isCleaning.value = false;
|
|
|
|
|
stats.value = {
|
|
|
|
|
hunger: 100,
|
|
|
|
|
happiness: 100,
|
|
|
|
|
health: 100,
|
|
|
|
|
weight: 500,
|
2025-11-22 03:23:02 +00:00
|
|
|
age: 1,
|
2025-11-20 09:15:38 +00:00
|
|
|
poopCount: 0
|
|
|
|
|
};
|
2025-11-22 03:23:02 +00:00
|
|
|
tickCount = 0;
|
2025-11-20 09:15:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --- Lifecycle ---
|
|
|
|
|
onMounted(() => {
|
|
|
|
|
gameLoopId = setInterval(tick, TICK_RATE);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
onUnmounted(() => {
|
|
|
|
|
if (gameLoopId) clearInterval(gameLoopId);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
stage,
|
|
|
|
|
state,
|
|
|
|
|
stats,
|
|
|
|
|
isCleaning,
|
|
|
|
|
feed,
|
|
|
|
|
play,
|
|
|
|
|
clean,
|
|
|
|
|
sleep,
|
|
|
|
|
hatchEgg,
|
2025-11-22 03:23:02 +00:00
|
|
|
reset,
|
|
|
|
|
achievements,
|
|
|
|
|
unlockAllAchievements
|
2025-11-20 09:15:38 +00:00
|
|
|
};
|
|
|
|
|
}
|