Skip to content

Commit

Permalink
Merge pull request #37 from JollyGrin/feat/dice
Browse files Browse the repository at this point in the history
Feat/dice
  • Loading branch information
JollyGrin authored Sep 22, 2024
2 parents a653825 + fb26250 commit 4b0042b
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 7 deletions.
5 changes: 4 additions & 1 deletion src/components/molecules/CommandModal/Command.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import {
ActDrawDeckBottom,
ActDrawDeckTop,
ActionIds,
ActionRollDice,
ActScryX,
} from "./Commands";
import { PlayerDataProps } from "@/types/card";

export const Command = (props: GameStateActions) => {
export const Command = (props: GameStateActions & PlayerDataProps) => {
return (
<Flex direction="column" minW="800px" minH="500px">
<p
Expand All @@ -24,6 +26,7 @@ export const Command = (props: GameStateActions) => {
{({ action }) => {
const id = action.value as ActionIds;

if (id === "dice") return <ActionRollDice {...props} />;
if (id === "spawn_token")
return "To spawn a token, right click empty space on a grid";
if (id === "draw_deck_top") return <ActDrawDeckTop {...props} />;
Expand Down
59 changes: 55 additions & 4 deletions src/components/molecules/CommandModal/Commands.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,26 @@ import { GameStateActions } from "@/components/organisms/GameBoard";
import {
getCardImage,
GRIDS,
initGameData,
} from "@/components/organisms/GameBoard/constants";
import { Button } from "@/components/ui/button";
import { PlayerDataProps } from "@/types/card";
import {
actDeckMoveToTop,
actDrawDeck,
actDrawDeckBottom,
} from "@/utils/actions";
import { useRouter } from "next/router";
import { useState } from "react";
import { css } from "styled-system/css";
import { Box, Flex, VStack } from "styled-system/jsx";
import { input } from "styled-system/recipes";

export const actions = [
// {
// value: "d6",
// label: "Roll a D6",
// },
{
value: "dice",
label: "Roll dice",
},
{
value: "spawn_token",
label: "Spawn a token onto the grid",
Expand Down Expand Up @@ -48,6 +51,54 @@ export type ActionIds = (typeof actions)[number]["value"];
* These are displayed with if conditions to do actions without worrying about UI
* */

export const ActionRollDice = (props: GameStateActions & PlayerDataProps) => {
const { query } = useRouter();
const name = (query?.name ?? "") as string;
const myData = props.players?.[name].data ?? initGameData;

function rollD6() {
const roll = Math.floor(Math.random() * 6) + 1;
const dice = {
d6: roll,
d20: undefined,
};
props.setMyData({
...myData,
dice,
});
}

function rollD20() {
const roll = Math.floor(Math.random() * 20) + 1;
const dice = {
d6: undefined,
d20: roll,
};
props.setMyData({
...myData,
dice,
});
}

function clearDice() {
props.setMyData({
...myData,
dice: undefined,
});
}

return (
<VStack alignItems="start">
<p>Roll a dice</p>
<p>d6: {myData?.dice?.d6}</p>
<p>d20: {myData?.dice?.d20}</p>
<Button onClick={rollD6}>Roll d6</Button>
<Button onClick={rollD20}>Roll d20</Button>
<Button onClick={clearDice}>Clear Rolls</Button>
</VStack>
);
};

export const ActDrawDeckTop = (props: GameStateActions) => (
<VStack alignItems="start">
<p>Draw a card from the top of your deck</p>
Expand Down
3 changes: 2 additions & 1 deletion src/components/molecules/CommandModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import { Children } from "@/utils/helpers/types";
import { useKeyPress } from "@/utils/hooks/useKeyPress";
import { Command } from "./Command";
import { useEffect, useState } from "react";
import { PlayerDataProps } from "@/types/card";

export const CommandModalWrapper = ({
children,
...props
}: Children & GameStateActions) => {
}: Children & GameStateActions & PlayerDataProps) => {
const { isOpen, onClose } = useDisclosure();

return (
Expand Down
8 changes: 8 additions & 0 deletions src/components/organisms/GameBoard/Header/PlayerBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
GiPirateGrave as IconGrave,
GiHealthNormal as IconHealth,
} from "react-icons/gi";
import { LiaDiceD6Solid as IconD6 } from "react-icons/lia";
import { FaDiceD20 as IconD20 } from "react-icons/fa";

export const PlayerBox = ({
name,
Expand Down Expand Up @@ -91,6 +93,12 @@ export const PlayerBox = ({
<Stat Icon={IconDeck} value={length(GRIDS.DECK)} />
<Stat Icon={IconMap} value={length(GRIDS.ATLAS_DECK)} />
<Stat Icon={IconHand} value={length(GRIDS.HAND)} />
{player?.data?.dice?.d6 !== undefined && (
<Stat Icon={IconD6} value={player?.data?.dice?.d6} />
)}
{player?.data?.dice?.d20 !== undefined && (
<Stat Icon={IconD20} value={player?.data?.dice?.d20} />
)}
</Flex>
</HStack>
);
Expand Down
2 changes: 1 addition & 1 deletion src/components/organisms/GameBoard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const GameBoard = ({
});

return (
<CommandModalWrapper {...{ gridItems, setGridItems }}>
<CommandModalWrapper {...{ gridItems, setGridItems, ...playerDataProps }}>
<DndContext {...dragProps}>
<GameLayout
gridItems={gridItems}
Expand Down
4 changes: 4 additions & 0 deletions src/types/card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ export type PlayerData = {
life: number;
mana: number;
manaRemaining: number;
dice?: {
d6?: number;
d20?: number;
};
};
export type PlayerState = {
state: GameState;
Expand Down

0 comments on commit 4b0042b

Please sign in to comment.