Merge pull request 'Added background music' (#3) from san/idle_moloch:main into main
Some checks failed
CI / Foundry project (push) Has been cancelled

Reviewed-on: #3
This commit is contained in:
mic0 2024-10-26 19:00:25 +00:00
commit a45d74ebb2
3 changed files with 76 additions and 2 deletions

View File

@ -0,0 +1,52 @@
import { useState, useEffect, useRef, useCallback } from 'react';
import styles from '../styles/Background.module.css';
const MusicPlayer = ({ onReady }: { onReady: (unmute: () => void) => void }) => {
const [isMuted, setIsMuted] = useState(false);
const audioRef = useRef<HTMLAudioElement | null>(null);
const play = useCallback(() => {
if (audioRef.current) {
audioRef.current.play().catch(error => console.log("Audio play failed:", error));
}
}, []);
const unmute = useCallback(() => {
if (audioRef.current) {
audioRef.current.muted = false;
setIsMuted(false);
play();
}
}, [play]);
useEffect(() => {
if (audioRef.current) {
audioRef.current.volume = 0.5;
audioRef.current.loop = true;
audioRef.current.muted = true;
play();
onReady(unmute);
}
}, [play, onReady, unmute]);
const toggleMute = () => {
if (audioRef.current) {
audioRef.current.muted = !isMuted;
setIsMuted(!isMuted);
if (!isMuted) {
play();
}
}
};
return (
<>
<audio ref={audioRef} src="https://olive-fashionable-swallow-983.mypinata.cloud/ipfs/QmT74A6AVYTXywz7SGGuvqSqgoidV2SDCn9jGW5KEiRYtR" />
<button onClick={toggleMute} className={styles.musicButton}>
{isMuted ? '🔇' : '🔊'}
</button>
</>
);
};
export default MusicPlayer;

View File

@ -1,10 +1,19 @@
import React from "react" import React, { useCallback } from "react"
import styles from '../styles/Background.module.css'; import styles from '../styles/Background.module.css';
import Tower from "./Tower"; import Tower from "./Tower";
import Army from "./Army"; import Army from "./Army";
import MarchingBand from "./MarchingBand"; import MarchingBand from "./MarchingBand";
import MusicPlayer from "./MusicPlayer";
import { usePlayer } from "../providers/PlayerProvider";
const Scene = () => { const Scene = () => {
const { isRegistered } = usePlayer();
const handleMusicReady = useCallback((unmute: () => void) => {
if (isRegistered) {
unmute();
}
}, [isRegistered]);
return <div className={styles.frame}> return <div className={styles.frame}>
<div className={`${styles.air} ${styles.background_asset}`} /> <div className={`${styles.air} ${styles.background_asset}`} />
<div className={`${styles.clouds_small} ${styles.background_asset}`} /> <div className={`${styles.clouds_small} ${styles.background_asset}`} />
@ -15,7 +24,8 @@ const Scene = () => {
<MarchingBand /> <MarchingBand />
<div className={`${styles.bonfire} ${styles.background_asset}`} /> <div className={`${styles.bonfire} ${styles.background_asset}`} />
<Army /> <Army />
<MusicPlayer onReady={handleMusicReady} />
</div> </div>
} }
export default Scene export default Scene

View File

@ -113,6 +113,18 @@
bonfire 12s linear infinite, bonfire 12s linear infinite,
bonfire_skew 5s infinite linear; bonfire_skew 5s infinite linear;
} }
.musicButton {
position: absolute;
top: 30px;
left: 30px;
background-color: rgba(0, 0, 0, 0.5);
color: white;
border: none;
padding: 10px;
border-radius: 5px;
cursor: pointer;
z-index: 1000;
}
@keyframes scrollBackground { @keyframes scrollBackground {
0% { 0% {
background-position: 0 0; background-position: 0 0;