From b4c9422bcbde07ccab4759dc99d574613c76d92c Mon Sep 17 00:00:00 2001 From: Noy <59097122+noyyyy@users.noreply.github.com> Date: Wed, 10 Apr 2024 01:36:52 +0800 Subject: [PATCH] fix: piece spawn pos --- .../systems/local/coordOccupationSystem.ts | 4 +- .../src/phaser/systems/local/placeSystem.ts | 19 ++++--- packages/client/src/phaser/systems/prepare.ts | 4 +- .../utils/{index.ts => pieceManage.ts} | 9 ++- packages/client/src/ui/CreateAccount.tsx | 26 --------- packages/client/src/ui/Spawn.tsx | 56 ------------------- 6 files changed, 25 insertions(+), 93 deletions(-) rename packages/client/src/phaser/systems/utils/{index.ts => pieceManage.ts} (97%) delete mode 100644 packages/client/src/ui/CreateAccount.tsx delete mode 100644 packages/client/src/ui/Spawn.tsx diff --git a/packages/client/src/phaser/systems/local/coordOccupationSystem.ts b/packages/client/src/phaser/systems/local/coordOccupationSystem.ts index 9bfcb95..2552853 100644 --- a/packages/client/src/phaser/systems/local/coordOccupationSystem.ts +++ b/packages/client/src/phaser/systems/local/coordOccupationSystem.ts @@ -3,8 +3,8 @@ import { PhaserLayer } from "../.."; import { defineSystemST } from "../../../utils"; import { world } from "../../../dojo/generated/world"; import { Has, setComponent, updateComponent } from "@dojoengine/recs"; -import { utils } from "../utils"; import { getEntityIdFromKeys } from "@dojoengine/utils"; +import { logDebug } from "../../../ui/lib/utils"; export function coordOccupationSystem(layer: PhaserLayer) { const { @@ -28,6 +28,7 @@ export function coordOccupationSystem(layer: PhaserLayer) { BigInt(preV.x), BigInt(preV.y), ]); + logDebug(`unset occupi ${preV.x}, ${preV.y}`); updateComponent(LocalPieceOccupation, occupiedEntity, { x: preV.x, y: preV.y, @@ -42,6 +43,7 @@ export function coordOccupationSystem(layer: PhaserLayer) { BigInt(v.y), ]); + logDebug(`set occupi ${v.x}, ${v.y}`); setComponent(LocalPieceOccupation, occupiedEntity, { x: v.x, y: v.y, diff --git a/packages/client/src/phaser/systems/local/placeSystem.ts b/packages/client/src/phaser/systems/local/placeSystem.ts index 4cf0950..1d76e1b 100644 --- a/packages/client/src/phaser/systems/local/placeSystem.ts +++ b/packages/client/src/phaser/systems/local/placeSystem.ts @@ -3,8 +3,9 @@ import { PhaserLayer } from "../.."; import { defineSystemST, zeroEntity } from "../../../utils"; import { world } from "../../../dojo/generated/world"; import { Has, getComponentValueStrict } from "@dojoengine/recs"; -import { utils } from "../utils"; +import { pieceManage } from "../utils/pieceManage"; import { logDebug } from "../../../ui/lib/utils"; +import { GameStatusEnum } from "../../../dojo/types"; export function placeSystem(layer: PhaserLayer) { const { @@ -17,26 +18,30 @@ export function placeSystem(layer: PhaserLayer) { }, } = layer; - const { spawnPiece, removePieceOnBoard } = utils(layer); + const { spawnPiece, removePieceOnBoard } = pieceManage(layer); // follow local piece location defineSystemST( world, [Has(LocalPiece)], ({ entity, type, value: [v, preV] }) => { - logDebug("v:", v); - const status = getComponentValueStrict(GameStatus, zeroEntity); + logDebug("incoming LocalPiece change: ", v); if (v) { // only dynamic sync player's piece + const status = getComponentValueStrict(GameStatus, zeroEntity); if (v.owner === BigInt(address) && v.idx !== 0) { - console.log("place: ", v, v.owner, v.idx); - spawnPiece(v.owner, BigInt(v.idx)); + // only allow override on prepare + if (status.status == GameStatusEnum.Prepare) { + spawnPiece(v.owner, BigInt(v.idx), true); + } else { + spawnPiece(v.owner, BigInt(v.idx), false); + } } if (v.owner === 0n) { removePieceOnBoard(v.gid); } if (v.idx === 0) { - // wait for op render override remove + // wait 0.5s for op render override remove setTimeout(() => { if (v.idx === 0) { removePieceOnBoard(v.gid); diff --git a/packages/client/src/phaser/systems/prepare.ts b/packages/client/src/phaser/systems/prepare.ts index cb04352..8a2eea9 100644 --- a/packages/client/src/phaser/systems/prepare.ts +++ b/packages/client/src/phaser/systems/prepare.ts @@ -11,7 +11,7 @@ import { PhaserLayer } from ".."; import { getEntityIdFromKeys } from "@dojoengine/utils"; import { defineSystemST, zeroEntity } from "../../utils"; import { GameStatusEnum } from "../../dojo/types"; -import { utils } from "./utils"; +import { pieceManage } from "./utils/pieceManage"; import { getComponentValueUtilNotNull } from "../../ui/lib/utils"; export const prepare = (layer: PhaserLayer) => { @@ -36,7 +36,7 @@ export const prepare = (layer: PhaserLayer) => { }, } = layer; - const { spawnPiece } = utils(layer); + const { spawnPiece } = pieceManage(layer); // initialize and sync match status defineSystemST( diff --git a/packages/client/src/phaser/systems/utils/index.ts b/packages/client/src/phaser/systems/utils/pieceManage.ts similarity index 97% rename from packages/client/src/phaser/systems/utils/index.ts rename to packages/client/src/phaser/systems/utils/pieceManage.ts index 18533dc..127ba7a 100644 --- a/packages/client/src/phaser/systems/utils/index.ts +++ b/packages/client/src/phaser/systems/utils/pieceManage.ts @@ -13,7 +13,7 @@ import { zeroEntity } from "../../../utils"; import { isEqual } from "lodash"; import { logDebug } from "../../../ui/lib/utils"; -export const utils = (layer: PhaserLayer) => { +export const pieceManage = (layer: PhaserLayer) => { const { game, scenes: { @@ -59,6 +59,13 @@ export const utils = (layer: PhaserLayer) => { }); } + /** + * + * @param playerAddr + * @param index + * @param override whether to override previous one + * @returns + */ function spawnPiece(playerAddr: bigint, index: bigint, override = false) { const playerPiece = getComponentValue( LocalPlayerPiece, diff --git a/packages/client/src/ui/CreateAccount.tsx b/packages/client/src/ui/CreateAccount.tsx deleted file mode 100644 index 34e850e..0000000 --- a/packages/client/src/ui/CreateAccount.tsx +++ /dev/null @@ -1,26 +0,0 @@ -import { useUIStore } from "../store"; -import { Spawn } from "./Spawn"; -import { ClickWrapper } from "./clickWrapper"; - -export const CreateAccount = () => { - const loggedIn = useUIStore((state: any) => state.loggedIn); - return ( -
-
-

Auto Chess Battle

- {/*
Auto battle
*/} -
- -
- -
-
-
-
-
- ); -}; diff --git a/packages/client/src/ui/Spawn.tsx b/packages/client/src/ui/Spawn.tsx deleted file mode 100644 index 119a091..0000000 --- a/packages/client/src/ui/Spawn.tsx +++ /dev/null @@ -1,56 +0,0 @@ -import { useDojo } from "./hooks/useDojo"; -import { Button } from "./button"; -import { useUIStore } from "../store"; -import { useEffect } from "react"; -import { useComponentValue } from "@dojoengine/react"; -import { getEntityIdFromKeys } from "@dojoengine/utils"; -import { zeroEntity } from "../utils"; - -export const Spawn = () => { - const setLoggedIn = useUIStore((state: any) => state.setLoggedIn); - const { - account: { account, isDeploying }, - systemCalls: { spawn }, - clientComponents: { Player, GameStatus }, - } = useDojo(); - - useEffect(() => { - if (isDeploying) { - return; - } - - if (account) { - return; - } - }, [account, isDeploying]); - - const player = useComponentValue( - Player, - getEntityIdFromKeys([BigInt(account.address)]) - ); - - useEffect(() => { - if (player?.inMatch && player?.inMatch > 0) { - setLoggedIn(); - } - }, [player?.inMatch, setLoggedIn]); - - if (!account) { - return
Deploying...
; - } - - return ( -
- -
- ); -};