diff --git a/src/components/Evolution.tsx b/src/components/Evolution.tsx index 64dfc22..225d77b 100644 --- a/src/components/Evolution.tsx +++ b/src/components/Evolution.tsx @@ -6,7 +6,7 @@ import useEvent from "../app/use-event"; import { Event } from "../app/emitter"; import PixelImage from "../styles/PixelImage"; import { useDispatch, useSelector } from "react-redux"; -import { selectEvolution } from "../state/uiSlice"; +import { hideEvolution, selectEvolution } from "../state/uiSlice"; import { selectPokemon, updateSpecificPokemon } from "../state/gameSlice"; const StyledEvolution = styled.div` @@ -244,15 +244,13 @@ const TextContainer = styled.div` const Evolution = () => { const dispatch = useDispatch(); - const pokemonIndex = useSelector(selectEvolution); + const evolution = useSelector(selectEvolution); const allPokemon = useSelector(selectPokemon); - const evolvingPokemon = allPokemon[pokemonIndex || 0]; + const evolvingPokemon = allPokemon[evolution?.index || 0]; const evolvingId = evolvingPokemon ? evolvingPokemon.id : null; const metadata = usePokemonMetadata(evolvingId); - const evolvesMetadata = usePokemonMetadata( - metadata?.evolution?.pokemon || null - ); - const show = pokemonIndex !== null; + const evolvesMetadata = usePokemonMetadata(evolution?.evolveToId || null); + const show = evolution !== null; const [evolved, setEvolved] = useState(false); @@ -260,18 +258,18 @@ const Evolution = () => { if (!show) return; if (!evolved) return; if (!metadata) throw new Error("No metadata for evolution"); - if (!metadata.evolution) throw new Error("No evolving pokemon"); setEvolved(false); dispatch( updateSpecificPokemon({ - index: pokemonIndex, + index: evolution.index, pokemon: { ...evolvingPokemon, - id: metadata.evolution.pokemon, + id: evolution.evolveToId, }, }) ); + dispatch(hideEvolution()); }); if (!show) return null; diff --git a/src/components/PokemonEncounter.tsx b/src/components/PokemonEncounter.tsx index 7bd563e..0d806cf 100644 --- a/src/components/PokemonEncounter.tsx +++ b/src/components/PokemonEncounter.tsx @@ -621,7 +621,23 @@ const PokemonEncounter = () => { const isTrainer = !!trainer; const isThrowingEnemyPokeball = stage >= 34 && stage <= 38 && isTrainer; + const handleEvolution = () => { + if (!processingMetadata) return; + if (!processingMetadata.evolution) return; + if (processingPokemon.level < processingMetadata.evolution.level) return; + dispatch( + showEvolution({ + index: involvedPokemon[processingInvolvedPokemon], + evolveToId: processingMetadata.evolution.pokemon, + }) + ); + }; + const endEncounter_ = (exitBattle = false) => { + // Handle evolutions + handleEvolution(); + + // Handling switching to the next processing pokemon if (processingInvolvedPokemon < involvedPokemon.length - 1) { const nextIndex = processingInvolvedPokemon + 1; if (enemy) { @@ -914,11 +930,6 @@ const PokemonEncounter = () => { } } - const isEvolving = - processingMetadata && - processingMetadata.evolution && - processingPokemon.level >= processingMetadata.evolution.level; - if (stage === 22) { const move = getLearnedMove(processingPokemon); const hasFourMoves = active.moves.length === 4; @@ -927,9 +938,6 @@ const PokemonEncounter = () => { } else if (move && hasFourMoves) { setStage(30); } else { - if (isEvolving) { - dispatch(showEvolution(involvedPokemon[processingInvolvedPokemon])); - } endEncounter_(); } } @@ -970,9 +978,6 @@ const PokemonEncounter = () => { }) ); - if (isEvolving) { - dispatch(showEvolution(involvedPokemon[processingInvolvedPokemon])); - } endEncounter_(); } @@ -1463,18 +1468,6 @@ const PokemonEncounter = () => { const item: MenuItemType = { label: m.id, action: () => { - const isEvolving = - processingMetadata && - processingMetadata.evolution && - processingPokemon.level >= - processingMetadata.evolution.level; - if (isEvolving) { - dispatch( - showEvolution( - involvedPokemon[processingInvolvedPokemon] - ) - ); - } endEncounter_(); dispatch( updateSpecificPokemon({ @@ -1497,32 +1490,11 @@ const PokemonEncounter = () => { { label: getLearnedMove(processingPokemon)?.id || "Error", action: () => { - const isEvolving = - processingMetadata && - processingMetadata.evolution && - processingPokemon.level >= - processingMetadata.evolution.level; - if (isEvolving) { - dispatch( - showEvolution(involvedPokemon[processingInvolvedPokemon]) - ); - } endEncounter_(); }, }, ]} - close={() => { - const isEvolving = - processingMetadata && - processingMetadata.evolution && - processingPokemon.level >= processingMetadata.evolution.level; - if (isEvolving) { - dispatch( - showEvolution(involvedPokemon[processingInvolvedPokemon]) - ); - } - endEncounter_(); - }} + close={() => endEncounter_()} bottom="0" right="0" /> diff --git a/src/state/uiSlice.ts b/src/state/uiSlice.ts index 4229c0f..605be1a 100644 --- a/src/state/uiSlice.ts +++ b/src/state/uiSlice.ts @@ -22,6 +22,11 @@ interface ConfimationMenuType { cancel?: () => void; } +interface EvolutionType { + index: number; + evolveToId: number; +} + interface UiState { text: string[] | null; startMenu: boolean; @@ -40,7 +45,7 @@ interface UiState { learningMove: LearningMoveType | null; blackScreen: boolean; confirmationMenu: ConfimationMenuType | null; - evolution: number | null; + evolution: EvolutionType | null; } const initialState: UiState = { @@ -167,7 +172,7 @@ export const uiSlice = createSlice({ hideConfirmationMenu: (state) => { state.confirmationMenu = null; }, - showEvolution: (state, action: PayloadAction<number>) => { + showEvolution: (state, action: PayloadAction<EvolutionType>) => { state.evolution = action.payload; }, hideEvolution: (state) => {