-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6129533
commit 4557f59
Showing
4 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { useEffect, useState } from "react"; | ||
import { SoundNames } from "./sounds"; | ||
|
||
function useAssetsPreloader() { | ||
const [done, setDone] = useState<boolean>(false); | ||
const [error, setError] = useState<boolean>(false); | ||
const [loaded, setLoadedCount] = useState<number>(0); | ||
const [total, setTotal] = useState<number>(0); | ||
|
||
// Other | ||
const assets: string[] = [ | ||
"cards/cardback.png", | ||
"cards/cardback-au.png", | ||
"blackheart.svg", | ||
"skull.svg", | ||
"wave.svg", | ||
"whiteheart.svg", | ||
]; | ||
|
||
// Sounds | ||
for (const sound of SoundNames) { | ||
assets.push(`/sounds/${sound}.mp3`); | ||
assets.push(`/sounds/${sound}.ogg`); | ||
} | ||
|
||
// Cards | ||
for (const s of ["A", "C", "D", "H", "I", "S"]) { | ||
for (let i = 2; i <= 14; i++) { | ||
assets.push(`/cards/${s}-${i}.png`); | ||
} | ||
} | ||
|
||
useEffect(() => { | ||
setTotal(assets.length); | ||
|
||
for (const asset of assets) { | ||
if (hasSuffix(asset, [".png", ".svg"])) { | ||
loadImages(asset); | ||
} | ||
|
||
if (hasSuffix(asset, [".mp3", ".ogg"])) { | ||
loadSound(asset); | ||
} | ||
} | ||
}, []); | ||
|
||
const incrementLoadedCount = () => { | ||
setLoadedCount((prev) => { | ||
if (prev + 1 === assets.length) { | ||
setDone(true); | ||
} | ||
return prev + 1; | ||
}); | ||
}; | ||
|
||
const loadImages = async (path: string) => { | ||
const img = new Image(); | ||
img.src = path; | ||
img.onload = () => { | ||
incrementLoadedCount(); | ||
}; | ||
img.onerror = () => { | ||
setError(true); | ||
}; | ||
}; | ||
|
||
const loadSound = async (path: string) => { | ||
const audio = new Audio(path); | ||
audio.oncanplaythrough = () => { | ||
incrementLoadedCount(); | ||
}; | ||
audio.onerror = () => { | ||
setError(true); | ||
}; | ||
}; | ||
|
||
return { done, error, loaded, total }; | ||
} | ||
|
||
const hasSuffix = (str: string, substr: string[]) => { | ||
return substr.some((s) => str.includes(s)); | ||
}; | ||
|
||
export { useAssetsPreloader }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters