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

衝突判定の追加 #33

Merged
merged 3 commits into from
Aug 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 41 additions & 12 deletions src/pages/yatai/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,47 @@ const YataiStage = memo(() => {
args: args,
}));

// 弾が当たった時はこれを呼び出す
const handleHit = () => {
api.applyImpulse(
[randFloat(-2, 2), 4, 8],
[randFloat(-1, 1), randFloat(-1, 1), randFloat(-1, 1)],
);
const socketRef = useSocketRefStore((state) => state.socketRef);

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

// TODO: これらは一人用,いつかマルチプレイヤー対応する
const [target, setTarget] = useState<Target | undefined>(undefined);
const shotTarget = (data: ActionSchema) => {
setTarget({ x: data.target.x, y: data.target.y });
};

useEffect(() => {
if (!target) return;
if (
target.x * 2 >
(props.position as [number, number, number])[0] - args[0] / 2 &&
target.x * 2 <
(props.position as [number, number, number])[0] + args[0] / 2 &&
target.y * 2 >
(props.position as [number, number, number])[1] - args[1] / 2 - 2 &&
target.y * 2 <
(props.position as [number, number, number])[1] + args[1] / 2 - 2
) {
api.applyImpulse(
[randFloat(-2, 2), 4, 8],
[randFloat(-1, 1), randFloat(-1, 1), randFloat(-1, 1)],
);
}
}, [target, props.position, api]);

return (
<mesh
ref={
Expand All @@ -78,7 +111,6 @@ const YataiStage = memo(() => {
{...props}
castShadow
receiveShadow
onPointerOver={() => handleHit()}
>
<boxGeometry args={[...args]} />
<meshStandardMaterial color={"yellow"} />
Expand Down Expand Up @@ -153,18 +185,15 @@ const TargetOverlay = () => {
// setAim({ x, y });
// };

const [target, setTarget] = useState<Target | undefined>(undefined);
// const [target, setTarget] = useState<Target | undefined>(undefined);
const shotTarget = (data: ActionSchema) => {
const x = window.innerWidth / 2 + data.target.x * 300;
const y = window.innerHeight / 2 + data.target.y * 300;
// TODO: エイム実装ができたらここのsetAimは削除する
setAim({ x, y });
setTarget({ x, y });
// setTarget({ x, y });
};

// DEBUG: 後で消す
console.log(target);

return (
<div
className={styles.target}
Expand Down