feat: bgm、点击和三连音效

This commit is contained in:
streakingman 2022-09-17 21:16:33 +08:00
parent d90bb9b717
commit 6906dec65e
6 changed files with 51 additions and 2 deletions

Binary file not shown.

BIN
public/sound-disco.mp3 Normal file

Binary file not shown.

BIN
public/sound-triple.mp3 Normal file

Binary file not shown.

View File

@ -90,3 +90,12 @@
top: 0; top: 0;
left: 0; left: 0;
} }
.bgm-button {
position: fixed;
left: 0;
top: 0;
padding: 4px;
width: 36px;
height: 36px;
}

View File

@ -1,8 +1,8 @@
import React, { import React, {
FC, FC,
MouseEventHandler, MouseEventHandler,
startTransition,
useEffect, useEffect,
useRef,
useState, useState,
} from 'react'; } from 'react';
@ -143,6 +143,20 @@ const App: FC = () => {
const [finished, setFinished] = useState<boolean>(false); const [finished, setFinished] = useState<boolean>(false);
const [tipText, setTipText] = useState<string>(''); const [tipText, setTipText] = useState<string>('');
const [animating, setAnimating] = useState<boolean>(false); const [animating, setAnimating] = useState<boolean>(false);
const bgmRef = useRef<HTMLAudioElement>(null);
const [bgmOn, setBgmOn] = useState<boolean>(false);
const [once, setOnce] = useState<boolean>(false);
const tapSoundRef = useRef<HTMLAudioElement>(null);
const tripleSoundRef = useRef<HTMLAudioElement>(null);
const levelUpSoundRef = useRef<HTMLAudioElement>(null);
useEffect(() => {
if (bgmOn) {
bgmRef.current?.play();
} else {
bgmRef.current?.pause();
}
}, [bgmOn]);
// 队列区排序 // 队列区排序
useEffect(() => { useEffect(() => {
@ -264,11 +278,22 @@ const App: FC = () => {
// 点击item // 点击item
const clickSymbol = async (idx: number) => { const clickSymbol = async (idx: number) => {
if (finished || animating) return; if (finished || animating) return;
if (!once) {
setBgmOn(true);
setOnce(true);
}
const updateScene = scene.slice(); const updateScene = scene.slice();
const symbol = updateScene[idx]; const symbol = updateScene[idx];
if (symbol.isCover || symbol.status !== 0) return; if (symbol.isCover || symbol.status !== 0) return;
symbol.status = 1; symbol.status = 1;
if (tapSoundRef.current) {
tapSoundRef.current.currentTime = 0;
tapSoundRef.current.play();
}
let updateQueue = queue.slice(); let updateQueue = queue.slice();
updateQueue.push(symbol); updateQueue.push(symbol);
@ -285,7 +310,13 @@ const App: FC = () => {
updateQueue = updateQueue.filter((sb) => sb.icon !== symbol.icon); updateQueue = updateQueue.filter((sb) => sb.icon !== symbol.icon);
for (const sb of filterSame) { for (const sb of filterSame) {
const find = updateScene.find((i) => i.id === sb.id); const find = updateScene.find((i) => i.id === sb.id);
if (find) find.status = 2; if (find) {
find.status = 2;
if (tripleSoundRef.current) {
tripleSoundRef.current.currentTime = 0.55;
tripleSoundRef.current.play();
}
}
} }
} }
@ -372,6 +403,14 @@ const App: FC = () => {
<button onClick={restart}></button> <button onClick={restart}></button>
</div> </div>
)} )}
<button className="bgm-button" onClick={() => setBgmOn(!bgmOn)}>
{bgmOn ? '🔊' : '🔈'}
<audio ref={bgmRef} loop src="/sound-disco.mp3" />
</button>
<audio ref={tapSoundRef} src="/sound-button-click.mp3" />
<audio ref={tripleSoundRef} src="/sound-triple.mp3" />
</> </>
); );
}; };

View File

@ -44,6 +44,7 @@ button {
cursor: pointer; cursor: pointer;
transition: border-color 0.25s; transition: border-color 0.25s;
word-break: keep-all; word-break: keep-all;
outline: none;
} }
button:hover { button:hover {