Skip to content

Commit

Permalink
fix: the map offset
Browse files Browse the repository at this point in the history
  • Loading branch information
Vivomo committed Nov 23, 2023
1 parent 577fa1d commit bb52bdb
Show file tree
Hide file tree
Showing 7 changed files with 1,909 additions and 1,926 deletions.
7 changes: 5 additions & 2 deletions packages/client/src/components/PIXIAPP/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { IPlayer } from '@/components/PIXIPlayers/Player';
import GameContext from '@/context';
import { ICoordinate } from '@/components/MapCell';
import { CellType } from '@/constants';
import { bfs, getDistance, isDelivery, isMovable, triggerOffsetUpdate } from '@/utils/map';
import { bfs, calculateOffset, getDistance, isDelivery, isMovable } from '@/utils/map';
import {
createPathInterpolator,
getPlayersCache,
Expand Down Expand Up @@ -69,6 +69,9 @@ const PIXIAPP = () => {
}
} else {
renderPlayersArr.push({ ...player });
if (player.addr === curAddr) {
setOffset(calculateOffset(player));
}
console.log(`load player ${player.name}`)
}
});
Expand Down Expand Up @@ -101,7 +104,7 @@ const PIXIAPP = () => {
return;
}
if (movingPlayer.addr === curPlayer?.addr) {
setOffset(triggerOffsetUpdate(linePath[index], movingPlayer, simpleMapData, offset));
setOffset(calculateOffset(linePath[index]));
}
updatePlayerPosition(movingPlayer, linePath[index]);
movingPlayer.action = 'run';
Expand Down
26 changes: 15 additions & 11 deletions packages/client/src/config/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,6 @@ export const DELIVERY = {
y: 5
};

export const MapConfig = {
visualWidth: 24,
visualHeight: 16,
cellSize: 48,
spriteCellSize: 16,
}

export const LimitSpace = {
x: ~~(MapConfig.visualWidth / 2),
y: ~~(MapConfig.visualHeight / 2)
}

const MAP_CFG = [
[101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101],
Expand Down Expand Up @@ -81,3 +70,18 @@ const MAP_CFG = [
[101, 101, 101, 101, 101, 101, 100, 100, 100, 100, 100, 100, 100, 100, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101],
[101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101]]
export default MAP_CFG;


export const MapConfig = {
visualWidth: 24,
visualHeight: 16,
cellSize: 48,
spriteCellSize: 16,
width: MAP_CFG[0].length,
height: MAP_CFG.length,
}

export const LimitSpace = {
x: ~~(MapConfig.visualWidth / 2),
y: ~~(MapConfig.visualHeight / 2)
}
38 changes: 7 additions & 31 deletions packages/client/src/utils/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,37 +408,13 @@ export const triggerVertexUpdate = (cur, before, mapData, vertexCoordinate) => {
});
};

export const triggerOffsetUpdate = (cur, before, mapData, offset) => {
const xDegree = cur.x - before.x;
const yDegree = cur.y - before.y;
if (xDegree > 0) {
const limitExceeded = cur.x > LimitSpace.x;
const lessBoundary = cur.x + LimitSpace.x < mapData[0].length - 1;
if (limitExceeded && lessBoundary) {
offset.x = (LimitSpace.x - cur.x) * cellSize;
}
} else if (xDegree < 0>) {
const limitExceeded = cur.x > LimitSpace.x;
if (limitExceeded) {
offset.x = (LimitSpace.x - cur.x ) * cellSize;
}
} else if (yDegree > 0) {
const limitExceeded = cur.y > LimitSpace.y;
const lessBoundary = cur.y + LimitSpace.y < mapData[0].length - 1;
if (limitExceeded && lessBoundary) {
offset.y = (LimitSpace.y - cur.y) * cellSize;
}
} else if (yDegree < 0) {
const limitExceeded = cur.y > LimitSpace.y;
if (limitExceeded) {
offset.y = (LimitSpace.y - cur.y) * cellSize;
}
}

return({
...offset,
});
};
export const calculateOffset = (coordinate: ICoordinate) => {
const { x, y } = coordinate;
const { visualWidth, visualHeight, width, height, cellSize } = MapConfig;
const offsetX = Math.max(0, Math.min(x - visualWidth / 2, width - visualWidth));
const offsetY = Math.max(0, Math.min(y - visualHeight / 2, height - visualHeight));
return { x: -offsetX * cellSize, y: -offsetY * cellSize };
}

export const getDistance = (p1, p2) => {
return Math.sqrt(Math.pow(p2.x - p1.x, 2) + Math.pow(p2.y - p1.y, 2));
Expand Down
8 changes: 4 additions & 4 deletions packages/contracts/out/IWorld.sol/IWorld.json

Large diffs are not rendered by default.

2,096 changes: 1,048 additions & 1,048 deletions packages/contracts/out/Loot.sol/MLoot.json

Large diffs are not rendered by default.

Loading

0 comments on commit bb52bdb

Please sign in to comment.