Skip to content

Commit

Permalink
fix: piece spawn pos
Browse files Browse the repository at this point in the history
  • Loading branch information
noyyyy committed Apr 9, 2024
1 parent 4c45ccd commit b4c9422
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand All @@ -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,
Expand Down
19 changes: 12 additions & 7 deletions packages/client/src/phaser/systems/local/placeSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<typeof LocalPiece.schema>(
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);
Expand Down
4 changes: 2 additions & 2 deletions packages/client/src/phaser/systems/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -36,7 +36,7 @@ export const prepare = (layer: PhaserLayer) => {
},
} = layer;

const { spawnPiece } = utils(layer);
const { spawnPiece } = pieceManage(layer);

// initialize and sync match status
defineSystemST<typeof Player.schema>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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,
Expand Down
26 changes: 0 additions & 26 deletions packages/client/src/ui/CreateAccount.tsx

This file was deleted.

56 changes: 0 additions & 56 deletions packages/client/src/ui/Spawn.tsx

This file was deleted.

0 comments on commit b4c9422

Please sign in to comment.