generated from nhcarrigan/template
feat: achievement sound
This commit is contained in:
@@ -2,6 +2,7 @@ import { writable, derived } from 'svelte/store';
|
||||
import { listen } from '@tauri-apps/api/event';
|
||||
import { invoke } from '@tauri-apps/api/core';
|
||||
import type { Achievement, AchievementUnlockedEvent, AchievementId } from '$lib/types/achievements';
|
||||
import { playAchievementSound } from '$lib/sounds/achievement';
|
||||
|
||||
interface AchievementState {
|
||||
achievements: Record<AchievementId, Achievement>;
|
||||
@@ -304,7 +305,7 @@ function createAchievementsStore() {
|
||||
|
||||
return {
|
||||
subscribe,
|
||||
unlockAchievement: (event: AchievementUnlockedEvent) => {
|
||||
unlockAchievement: (event: AchievementUnlockedEvent, playSound: boolean = true) => {
|
||||
update(state => {
|
||||
const achievement = state.achievements[event.achievement.id];
|
||||
if (achievement && !achievement.unlocked) {
|
||||
@@ -312,6 +313,15 @@ function createAchievementsStore() {
|
||||
achievement.unlockedAt = event.achievement.unlocked_at ? new Date(event.achievement.unlocked_at) : new Date();
|
||||
state.totalUnlocked++;
|
||||
state.lastUnlocked = achievement;
|
||||
|
||||
// Play achievement sound only for new unlocks, not when loading saved ones
|
||||
if (playSound) {
|
||||
try {
|
||||
playAchievementSound();
|
||||
} catch (error) {
|
||||
console.error('Failed to play achievement sound:', error);
|
||||
}
|
||||
}
|
||||
}
|
||||
return state;
|
||||
});
|
||||
@@ -386,9 +396,9 @@ export async function initAchievementsListener() {
|
||||
try {
|
||||
const savedAchievements = await invoke<AchievementUnlockedEvent[]>('load_saved_achievements');
|
||||
|
||||
// Update the store with saved achievements
|
||||
// Update the store with saved achievements (don't play sounds)
|
||||
for (const event of savedAchievements) {
|
||||
achievementsStore.unlockAchievement(event);
|
||||
achievementsStore.unlockAchievement(event, false);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to load saved achievements:', error);
|
||||
|
||||
Reference in New Issue
Block a user