Skip to content
This repository has been archived by the owner on Nov 26, 2024. It is now read-only.

Commit

Permalink
Add: スコアを表示して、resultに遷移するまでの機能
Browse files Browse the repository at this point in the history
  • Loading branch information
Sea10wood committed Aug 17, 2024
1 parent e0cfad4 commit 7214e84
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 8 deletions.
Binary file modified public/drink/bottle0.webp
Binary file not shown.
Binary file modified public/drink/bottle2.webp
Binary file not shown.
Binary file modified public/drink/bottle3.webp
Binary file not shown.
2 changes: 1 addition & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Shooter from "./pages/shooter";
import Yatai from "./pages/yatai";

const AppRoutes = () => {
const [score, setScore] = useState<number>(0);
const [score, setScore] = useState(0);

return (
<Routes>
Expand Down
6 changes: 3 additions & 3 deletions src/pages/result/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const Result = ({ score }: ResultProps) => {
"/drink/bottle3.webp",
];

const image = images[score];
const image = images[score] || images[0];

return (
<div>
Expand All @@ -38,11 +38,11 @@ const Result = ({ score }: ResultProps) => {
/>
</div>
<div className={styles["get-text"]}>
<p>${score}本倒した!</p>
<p>{score}本倒した!</p>
</div>
<div className={styles["share-btn"]}>
<a
href="https://twitter.com/intent/tweet?text=Webの射的で遊んだよ.${score}本倒した!%20%23virtualnatsumatsuri"
href={`https://twitter.com/intent/tweet?text=Webの射的で遊んだよ${score}本倒した!%20%23virtualnatsumatsuri`}
target="_blank"
rel="noopener noreferrer"
>
Expand Down
18 changes: 14 additions & 4 deletions src/pages/shooter/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { type KeyboardEventHandler, useEffect, useState } from "react";
import { useNavigate } from "react-router-dom";
import { DefaultButton } from "../../components/ui/Button";
import { Modal } from "../../components/ui/Modal";
import { ShooterButton } from "../../components/ui/ShooterButton";
Expand All @@ -21,6 +22,7 @@ const Shooter = ({ setScore }: ShooterProps) => {
const { sendData } = useSocketSender();
const { onMessage } = useSocketReceiver();
const uuid = useUUIDStore((state) => state.uuid);
const navigate = useNavigate();

const initialImages = [
"/2D_material/cork.webp",
Expand All @@ -46,12 +48,19 @@ const Shooter = ({ setScore }: ShooterProps) => {
useEffect(() => {
onMessage((data) => {
if (data.message_type === MessageType.Hit && data.id === uuid) {
setLocalScore((prevScore) => prevScore + 1);
setScore(score);
const newScore = score + 1;
setLocalScore(newScore);
setScore(newScore);
}
});
}, [onMessage, uuid, score, setScore]);

useEffect(() => {
if (images.length === 0) {
navigate("/result", { state: { score } });
}
}, [images, navigate, score]);

const handleClick = () => {
const audio = new Audio("/sound/cork_sound.mp3");
audio.play().catch((error) => {
Expand All @@ -77,8 +86,9 @@ const Shooter = ({ setScore }: ShooterProps) => {
</div>
<div className={style.cork}>
{images.length > 0 ? (
images.map((src) => (
<img key={src} src={src} alt="コルクの残量を表示しています" />
images.map((src, i) => (
// biome-ignore lint/suspicious/noArrayIndexKey: <explanation>
<img key={i} src={src} alt="コルクの残量を表示しています" />
))
) : (
<p>コルクがなくなりました!</p>
Expand Down

0 comments on commit 7214e84

Please sign in to comment.