-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseGameLogicStore.js
More file actions
214 lines (173 loc) · 7.91 KB
/
useGameLogicStore.js
File metadata and controls
214 lines (173 loc) · 7.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import { create } from "zustand";
import { persist } from "zustand/middleware";
import { useCardStore } from "./useCardStore";
export const useGameLogicStore = create(
persist(
(set, get) => ({
allowSound: true,
toggleAllowSound: () => set((state) => ({ allowSound: !state.allowSound })),
allowCustomCursor: true,
toggleCustomCursor: () => set((state) => ({ allowCustomCursor: !state.allowCustomCursor })),
// return cursor styling
getCursorStyle: (type) => {
const { allowCustomCursor } = get();
return {
cursor: allowCustomCursor
? "none"
: type === "hover"
? "pointer"
: "default",
};
},
displayedCards: [],
setDisplayedCards: (value) => set({ displayedCards: value }),
resetDisplayedCards: () => set({ displayedCards: [] }),
level: 1,
levelUp: () => set((state) => ({ level: state.level + 1 })),
resetLevel: () => set({ level: 0 }),
//number of cards to display on the screen
cardsToDisplay: 3,
increaseCardsToDisplay: (numberOfCardsToDisplay) => set({ cardsToDisplay: numberOfCardsToDisplay }),
resetCardsToDisplay: () => set({ cardsToDisplay: 3 }),
gameOver: false,
setGameOver: (gameOver) => set({ gameOver }),
score: 0,
increaseScore: () => set((state) => ({ score: state.score + 1 })),
resetScore: () => set({ score: 0 }),
personalBest: 0,
increasePersonalBest: () => {
const { score, personalBest } = get();
if (score > personalBest) {
set({ personalBest: score });
}
},
// generate a set of unique cards with random suits and ranks
generateUniqueRandomCards: (numberOfCardsToGenerate) => set((state) => {
const { ranks, suits, suitType } = useCardStore.getState(); // Get state from `useCardStore`
const cards = [];
const uniqueCards = new Set();
while (cards.length < numberOfCardsToGenerate && uniqueCards.size < ranks.length * suits.length) {
const randomRank = ranks[Math.floor(Math.random() * ranks.length)];
const suitGroup = suits.find((suit) => suit.type === suitType);
if (!suitGroup) continue; // Skip if no suit group matches the style
const randomSuit = suitGroup.suits[Math.floor(Math.random() * suitGroup.suits.length)];
const uniqueID = `${randomSuit.name}-${randomRank}`;
if (!uniqueCards.has(uniqueID)) {
uniqueCards.add(uniqueID);
cards.push({
rank: randomRank,
suit: randomSuit.name,
id: uniqueID,
});
}
}
return { displayedCards: cards };
}),
clickedCards: [],
resetClickedCards: () => set({ clickedCards: [] }),
// play audio on click
playClickSound: () => {
const { allowSound } = get();
const clickSound = new Audio(`/audio/play.wav`);
allowSound && clickSound.play();
},
// play audio on card click
playCardClickSound: () => {
const { allowSound } = get();
const clickSound = new Audio(`/audio/click.wav`);
allowSound && clickSound.play();
},
// play audio on click (when same card is clicked twice)
playMistakeSound: () => {
const { allowSound } = get();
const clickSound = new Audio(`/audio/mistake.wav`);
allowSound && clickSound.play();
},
// play audio when new round starts
playNewRoundSound: () => {
const { allowSound } = get();
const clickSound = new Audio(`/audio/new-round.wav`);
allowSound && clickSound.play();
},
// pre load all sounds for a better experience
preLoadSoundEffects: () => {
const newRound = new Audio(`/audio/new-round.wav`);
const mistake = new Audio(`/audio/mistake.wav`);
const click = new Audio(`/audio/click.wav`);
const play = new Audio(`/audio/play.wav`);
// Ensure the audio is fully loaded before starting the game
const onAudioReady = (audio) => {
audio.removeEventListener('canplaythrough', () => onAudioReady(audio));
};
// Add event listeners for each audio file
[newRound, mistake, click, play].forEach(audio => {
audio.addEventListener('canplaythrough', () => onAudioReady(audio));
audio.load(); // Ensure audio is loaded
});
},
updateDisplayedCards: () => {
const { clickedCards, cardsToDisplay, playNewRoundSound, resetClickedCards, levelUp, increaseCardsToDisplay, generateUniqueRandomCards } = get();
if (clickedCards.length === cardsToDisplay) {
playNewRoundSound();
resetClickedCards();
generateUniqueRandomCards(cardsToDisplay + 1);
levelUp();
increaseCardsToDisplay(cardsToDisplay + 1);
}
},
// Add a card to clickedCards and check game over logic
addCard: (card) => set((state) => {
const {
increaseScore,
playCardClickSound,
playMistakeSound,
} = get();
const updatedClickedCards = new Set(state.clickedCards);
if (!updatedClickedCards.has(card)) {
updatedClickedCards.add(card);
increaseScore();
playCardClickSound();
return { clickedCards: Array.from(updatedClickedCards) }; // Convert Set to array
} else {
playMistakeSound();
return { gameOver: true };
}
}),
shuffleCards: () => set((state) => {
const currentCards = [...state.displayedCards];
// shuffle the array (Fisher-Yates)
for (let i = currentCards.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[currentCards[i], currentCards[j]] = [currentCards[j], currentCards[i]];
}
return { displayedCards: currentCards }; // return the updated state
}),
resetEverything: () => {
const {
resetScore,
resetClickedCards,
resetCardsToDisplay,
resetLevel,
resetDisplayedCards,
setGameOver,
generateUniqueRandomCards,
} = get();
resetScore();
resetLevel();
resetClickedCards();
resetCardsToDisplay();
resetDisplayedCards();
setGameOver(false);
generateUniqueRandomCards(3);
},
}),
{
name: "game-logic-store",
partialize: (state) => ({
personalBest: state.personalBest,
allowSound: state.allowSound,
allowCustomCursor: state.allowCustomCursor,
}),
}
)
);