-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from JollyGrin/feat/pool-actions
Feat/pool actions
- Loading branch information
Showing
10 changed files
with
145 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,28 @@ | ||
type SorceryCardType = | ||
| "site" | ||
| "site" // in reality, only this one is relevant variation | ||
| "aura" | ||
| "avatar" | ||
| "artifact" | ||
| "minion" | ||
| "magic"; | ||
|
||
export type SorceryCard = { | ||
img: string; | ||
type: SorceryCardType; | ||
img: string; // used with CDN | ||
type: SorceryCardType; // site card is rotated sideways | ||
}; | ||
|
||
/** | ||
* Card props used for playtesting | ||
* */ | ||
type GameProps = { | ||
id: string; | ||
isTapped?: boolean; | ||
}; | ||
/** | ||
* Card type used for game playtesting | ||
* */ | ||
export type GameCard = SorceryCard & GameProps; | ||
|
||
export type GridItem = GameCard[]; | ||
|
||
export type GameState = GridItem[]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { GRIDS } from "@/components/organisms/GameBoard/constants"; | ||
import { GameState } from "@/types/card"; | ||
|
||
export function actDrawDeck(state: GameState) { | ||
// Create a shallow copy of the previous grid items array | ||
const newState = [...state]; | ||
|
||
// Make a copy of the deck and hand arrays, preserving their positions | ||
const newDeck = [...newState[GRIDS.DECK]]; | ||
const newHand = [...newState[GRIDS.HAND]]; | ||
|
||
// Pop a card from the deck and push it to the hand | ||
const card = newDeck.pop(); | ||
if (card) newHand.push(card); | ||
|
||
// Update the newState array with the updated deck and hand arrays | ||
newState[GRIDS.DECK] = newDeck; | ||
newState[GRIDS.HAND] = newHand; | ||
|
||
// Return the updated newState array to trigger a re-render | ||
return newState; | ||
} | ||
|
||
export function actDrawAtlas(state: GameState) { | ||
// Create a shallow copy of the previous grid items array | ||
const newState = [...state]; | ||
|
||
// Make a copy of the deck and hand arrays, preserving their positions | ||
const newDeck = [...newState[GRIDS.ATLAS_DECK]]; | ||
const newHand = [...newState[GRIDS.HAND]]; | ||
|
||
// Pop a card from the deck and push it to the hand | ||
const card = newDeck.pop(); | ||
if (card) newHand.push(card); | ||
|
||
// Update the newState array with the updated deck and hand arrays | ||
newState[GRIDS.ATLAS_DECK] = newDeck; | ||
newState[GRIDS.HAND] = newHand; | ||
|
||
// Return the updated newState array to trigger a re-render | ||
return newState; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { GameState } from "@/types/card"; | ||
import { DragEndEvent } from "@dnd-kit/core"; | ||
|
||
/** | ||
* Repositions a card within it's existing grid cell | ||
* Returns an updated game state | ||
* */ | ||
export function actMoveCardInCell(state: GameState, event: DragEndEvent) { | ||
const originIndex = parseInt(event.active.data.current?.gridIndex, 10); | ||
const updatedGrid = [...state]; | ||
const cardsInCell = [...updatedGrid[originIndex]]; // Copy the cards in the original cell | ||
|
||
const activeCardIndex = event.active?.data?.current?.index; // Index of the card being dragged | ||
const destinationCardIndex = event.over?.data?.current?.index; // Index where the card will be dropped | ||
|
||
// Remove the active card from its original position | ||
const [activeCard] = cardsInCell.splice(activeCardIndex, 1); | ||
|
||
// Insert the active card into the new position (destination) | ||
cardsInCell.splice(destinationCardIndex, 0, activeCard); | ||
|
||
// Update the original grid with the modified cards in the cell | ||
updatedGrid[originIndex] = cardsInCell; | ||
|
||
return updatedGrid; | ||
} | ||
|
||
/** | ||
* Repositions a card to another grid cell | ||
* Returns an updated game state | ||
* */ | ||
export function actMoveCardOutsideCell(state: GameState, event: DragEndEvent) { | ||
const originIndex = parseInt(event.active.data.current?.gridIndex, 10); | ||
const destinationIndex = event.over?.data?.current?.gridIndex; | ||
|
||
// Remove the active card from its original position | ||
const updatedGrid = [...state]; | ||
const [movedCard] = updatedGrid[originIndex].splice( | ||
event.active?.data?.current?.index, | ||
1, | ||
); | ||
// Place card in the destination area | ||
updatedGrid[destinationIndex]?.push(movedCard); | ||
|
||
return updatedGrid; | ||
} | ||
|
||
/** | ||
* Moves a card within a cell or to another grid cell | ||
* Returns an updated game state | ||
* If no action possible, will return the original state | ||
* * */ | ||
export function actMoveCard(state: GameState, event: DragEndEvent) { | ||
const { active, over } = event; | ||
|
||
if (over?.id === active.id) return state; // if self, do nothing | ||
|
||
if (over) { | ||
const originIndex = parseInt(active.data.current?.gridIndex, 10); | ||
const destinationIndex = over?.data?.current?.gridIndex; | ||
|
||
if (originIndex === destinationIndex) { | ||
return actMoveCardInCell(state, event); | ||
} | ||
|
||
return actMoveCardOutsideCell(state, event); | ||
} | ||
|
||
return state; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./deck"; |