From 213b15e79de2c856187885d7975f091076e2b373 Mon Sep 17 00:00:00 2001 From: claustra01 Date: Thu, 15 Aug 2024 16:55:15 +0900 Subject: [PATCH] add: send hit data --- src/hooks/useSocketSender.ts | 10 +++++++--- src/pages/shooter/index.tsx | 10 +++------- src/pages/yatai/index.tsx | 9 +++++++-- src/type/schema.ts | 1 + 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/hooks/useSocketSender.ts b/src/hooks/useSocketSender.ts index b3faac5..ca6eb51 100644 --- a/src/hooks/useSocketSender.ts +++ b/src/hooks/useSocketSender.ts @@ -1,12 +1,16 @@ import { useCallback } from "react"; -import { event_type, message_type, Schema } from "../type/schema"; import { useSocketRefStore } from "../store"; +import { type Schema, event_type, type message_type } from "../type/schema"; export const useSocketSender = () => { const socketRef = useSocketRefStore((state) => state.socketRef); const sendData = useCallback( - (mes_type: message_type, uuid: string, orientationDiff: { alpha: number, beta: number }) => { + ( + mes_type: message_type, + uuid: string, + orientationDiff: { alpha: number; beta: number }, + ) => { const data: Schema = { id: uuid, interval: 0, @@ -30,7 +34,7 @@ export const useSocketSender = () => { console.log(data); socketRef?.current?.send(JSON.stringify(data)); }, - [socketRef] + [socketRef], ); return { sendData }; diff --git a/src/pages/shooter/index.tsx b/src/pages/shooter/index.tsx index 77fb298..e858016 100644 --- a/src/pages/shooter/index.tsx +++ b/src/pages/shooter/index.tsx @@ -1,18 +1,14 @@ -import { - type KeyboardEventHandler, - useEffect, - useState, -} from "react"; +import { type KeyboardEventHandler, useEffect, useState } from "react"; import { DefaultButton } from "../../components/ui/Button"; import { Modal } from "../../components/ui/Modal"; import { ShooterButton } from "../../components/ui/ShooterButton"; import { useOrientation } from "../../hooks/useOrientation"; import { useSocketReceiver } from "../../hooks/useSocketReceiver"; +import { useSocketSender } from "../../hooks/useSocketSender"; import { useUUIDStore } from "../../store"; import { message_type } from "../../type/schema"; import { MessageType } from "../../type/shooting"; import style from "./index.module.css"; -import { useSocketSender } from "../../hooks/useSocketSender"; const Shooter = () => { const [isOpen, setIsOpen] = useState(true); @@ -42,7 +38,7 @@ const Shooter = () => { clearInterval(intervalId); } }; - }, [sendData]); + }, [uuid, orientationDiff, sendData]); useEffect(() => { onMessage((data) => { diff --git a/src/pages/yatai/index.tsx b/src/pages/yatai/index.tsx index 024f3ff..615286b 100644 --- a/src/pages/yatai/index.tsx +++ b/src/pages/yatai/index.tsx @@ -10,7 +10,8 @@ import type { } from "three"; import { randFloat } from "three/src/math/MathUtils.js"; import { useSocketReceiver } from "../../hooks/useSocketReceiver"; - +import { useSocketSender } from "../../hooks/useSocketSender"; +import { message_type } from "../../type/schema"; import { type ActionSchema, MessageType, @@ -50,6 +51,7 @@ const YataiStage = memo(() => { // 的 const Target = (props: ThreeElements["mesh"]) => { + const { sendData } = useSocketSender(); const { onMessage } = useSocketReceiver(); const position = props.position as [number, number, number]; @@ -69,8 +71,10 @@ const YataiStage = memo(() => { }, [onMessage]); // TODO: これらは一人用,いつかマルチプレイヤー対応する + const [uuid, setUuid] = useState(""); const [target, setTarget] = useState(undefined); const shotTarget = (data: ActionSchema) => { + setUuid(data.id); setTarget({ x: data.target.x, y: data.target.y }); }; @@ -86,8 +90,9 @@ const YataiStage = memo(() => { [randFloat(-2, 2), 4, 8], [randFloat(-1, 1), randFloat(-1, 1), randFloat(-1, 1)], ); + sendData(message_type.Hit, uuid, { alpha: 0, beta: 0 }); } - }, [target, position, api]); + }, [uuid, target, position, api, sendData]); return (