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

Commit

Permalink
refactor: replace to socket hook in yatai component
Browse files Browse the repository at this point in the history
  • Loading branch information
claustra01 committed Aug 15, 2024
1 parent 09951cc commit 29da0d3
Showing 1 changed file with 10 additions and 25 deletions.
35 changes: 10 additions & 25 deletions src/pages/yatai/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
type Target,
} from "../../type/shooting";
import styles from "./index.module.css";
import { useSocketReciever } from "../../hooks/useSocketReciever";

const YataiStage = memo(() => {
// 土台
Expand Down Expand Up @@ -49,6 +50,7 @@ const YataiStage = memo(() => {

// 的
const Target = (props: ThreeElements["mesh"]) => {
const { onMessage } = useSocketReciever();
const position = props.position as [number, number, number];

const args: [number, number, number] = [0.7, 2, 0.7];
Expand All @@ -58,21 +60,13 @@ const YataiStage = memo(() => {
args: args,
}));

const socketRef = useSocketRefStore((state) => state.socketRef);

useEffect(() => {
const onMessage = (event: MessageEvent) => {
const data = JSON.parse(event.data);
onMessage((data) => {
if (data.message_type === MessageType.Action) {
shotTarget(data);
}
};
const currentSocketRef = socketRef?.current;
currentSocketRef?.addEventListener("message", onMessage);
return () => {
currentSocketRef?.removeEventListener("message", onMessage);
};
}, [socketRef]);
});
}, [onMessage]);

// TODO: これらは一人用,いつかマルチプレイヤー対応する
const [target, setTarget] = useState<Target | undefined>(undefined);
Expand Down Expand Up @@ -154,25 +148,16 @@ const YataiStage = memo(() => {
});

const TargetOverlay = () => {
const socketRef = useSocketRefStore((state) => state.socketRef);
const { onMessage } = useSocketReciever();

useEffect(() => {
const onMessage = (event: MessageEvent) => {
const data = JSON.parse(event.data);
// サーバーから受け取ったデータを使った処理
if (data.message_type === MessageType.Pointer) {
// aimTarget(data);
}
onMessage((data) => {
// ここも本来はPointerSchemaになる
if (data.message_type === MessageType.Action) {
shotTarget(data);
}
};
const currentSocketRef = socketRef?.current;
currentSocketRef?.addEventListener("message", onMessage);
return () => {
currentSocketRef?.removeEventListener("message", onMessage);
};
}, [socketRef]);
});
}, [onMessage]);

// TODO: これらは一人用,いつかマルチプレイヤー対応する
const [aim, setAim] = useState<Target | undefined>(undefined);
Expand Down

0 comments on commit 29da0d3

Please sign in to comment.