Skip to content
Merged
Show file tree
Hide file tree
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
139 changes: 4 additions & 135 deletions packages/client/src/components/MapPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { Box, Button, HStack, Stack, Text, VStack } from '@chakra-ui/react';
import { useComponentValue } from '@latticexyz/react';
import { encodeEntity } from '@latticexyz/store-sync/recs';
import { useCallback, useState } from 'react';
import { BiSolidNavigation } from 'react-icons/bi';
import {
IoIosArrowDropdownCircle,
Expand All @@ -11,144 +8,16 @@ import {
} from 'react-icons/io';
import { TbDirectionArrows } from 'react-icons/tb';

import { useCharacter } from '../contexts/CharacterContext';
import { useMUD } from '../contexts/MUDContext';
import { useToast } from '../hooks/useToast';
import { useMapNavigation } from '../contexts/MapNavigationContext';

const SAFE_ZONE_AREA = {
topLeft: { x: 0, y: 4 },
bottomRight: { x: 4, y: 0 },
};

export const MapPanel = (): JSX.Element => {
const { renderError, renderSuccess } = useToast();
const {
burnerBalance,
components: { Position, Spawned },
delegatorAddress,
systemCalls: { move, spawn },
} = useMUD();
const { character } = useCharacter();

const [isSpawning, setIsSpawning] = useState(false);
const [isMoving, setIsMoving] = useState(false);

const position = useComponentValue(
Position,
encodeEntity(
{ characterId: 'uint256' },
{ characterId: BigInt(character?.characterId ?? 0) },
),
);

const isSpawned = !!useComponentValue(
Spawned,
encodeEntity(
{ characterId: 'uint256' },
{ characterId: BigInt(character?.characterId ?? 0) },
),
)?.spawned;

const onSpawn = useCallback(async () => {
try {
setIsSpawning(true);

if (burnerBalance === '0') {
throw new Error(
'Insufficient funds. Please top off your session account.',
);
}

if (!delegatorAddress) {
throw new Error('Missing delegation.');
}

if (!character) {
throw new Error('Character not found.');
}

const success = await spawn(character.characterId);

if (!success) {
throw new Error('Contract call failed');
}

renderSuccess('Spawned!');
} catch (e) {
renderError(e, 'Failed to roll stats.');
} finally {
setIsSpawning(false);
}
}, [
burnerBalance,
character,
delegatorAddress,
renderError,
renderSuccess,
spawn,
]);

const onMove = useCallback(
async (direction: 'up' | 'down' | 'left' | 'right') => {
try {
setIsMoving(true);

if (!delegatorAddress) {
throw new Error('Burner not found');
}

if (!position) {
throw new Error('Position not found');
}

if (!character) {
throw new Error('Character not found');
}

const { x, y } = position;

if (
(direction === 'up' && position.y === 9) ||
(direction === 'down' && position.y === 0) ||
(direction === 'left' && position.x === 0) ||
(direction === 'right' && position.x === 9)
) {
return;
}

let newX = x;
let newY = y;

switch (direction) {
case 'up':
newY = y + 1;
break;
case 'down':
newY = y - 1;
break;
case 'left':
newX = x - 1;
break;
case 'right':
newX = x + 1;
break;
default:
break;
}

const success = await move(character.characterId, newX, newY);

if (!success) {
throw new Error('Contract call failed');
}
} catch (e) {
renderError(e, 'Failed to move.');
} finally {
setIsMoving(false);
}
},
[character, delegatorAddress, move, position, renderError],
);
const { isRefreshing, isSpawned, isSpawning, onMove, onSpawn, position } =
useMapNavigation();

return (
<Stack
Expand Down Expand Up @@ -212,7 +81,7 @@ export const MapPanel = (): JSX.Element => {
</Stack>
</Box>
{isSpawned ? (
<NavigationCompass isMoving={isMoving} onMove={onMove} />
<NavigationCompass isMoving={isRefreshing} onMove={onMove} />
) : (
<Button
isLoading={isSpawning}
Expand Down
Loading