mirror of
https://github.com/chase-manning/pokemon-js.git
synced 2025-09-08 16:57:14 +00:00
🎨 move evolution to ui slice
This commit is contained in:
parent
83c14c1ed3
commit
fd36e7ef8b
5 changed files with 68 additions and 52 deletions
src
|
@ -5,6 +5,9 @@ import usePokemonMetadata from "../app/use-pokemon-metadata";
|
||||||
import useEvent from "../app/use-event";
|
import useEvent from "../app/use-event";
|
||||||
import { Event } from "../app/emitter";
|
import { Event } from "../app/emitter";
|
||||||
import PixelImage from "../styles/PixelImage";
|
import PixelImage from "../styles/PixelImage";
|
||||||
|
import { useDispatch, useSelector } from "react-redux";
|
||||||
|
import { selectEvolution } from "../state/uiSlice";
|
||||||
|
import { selectPokemon, updateSpecificPokemon } from "../state/gameSlice";
|
||||||
|
|
||||||
const StyledEvolution = styled.div`
|
const StyledEvolution = styled.div`
|
||||||
position: absolute;
|
position: absolute;
|
||||||
|
@ -239,25 +242,36 @@ const TextContainer = styled.div`
|
||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
interface Props {
|
const Evolution = () => {
|
||||||
show: boolean;
|
const dispatch = useDispatch();
|
||||||
close: () => void;
|
const pokemonIndex = useSelector(selectEvolution);
|
||||||
pokemonId: number;
|
const allPokemon = useSelector(selectPokemon);
|
||||||
}
|
const evolvingPokemon = allPokemon[pokemonIndex || 0];
|
||||||
|
const evolvingId = evolvingPokemon ? evolvingPokemon.id : null;
|
||||||
const Evolution = ({ show, close, pokemonId }: Props) => {
|
const metadata = usePokemonMetadata(evolvingId);
|
||||||
const metadata = usePokemonMetadata(pokemonId);
|
|
||||||
const evolvesMetadata = usePokemonMetadata(
|
const evolvesMetadata = usePokemonMetadata(
|
||||||
metadata?.evolution?.pokemon || null
|
metadata?.evolution?.pokemon || null
|
||||||
);
|
);
|
||||||
|
const show = pokemonIndex !== null;
|
||||||
|
|
||||||
const [evolved, setEvolved] = useState(false);
|
const [evolved, setEvolved] = useState(false);
|
||||||
|
|
||||||
useEvent(Event.A, () => {
|
useEvent(Event.A, () => {
|
||||||
if (evolved) {
|
if (!show) return;
|
||||||
setEvolved(false);
|
if (!evolved) return;
|
||||||
close();
|
if (!metadata) throw new Error("No metadata for evolution");
|
||||||
}
|
if (!metadata.evolution) throw new Error("No evolving pokemon");
|
||||||
|
|
||||||
|
setEvolved(false);
|
||||||
|
dispatch(
|
||||||
|
updateSpecificPokemon({
|
||||||
|
index: pokemonIndex,
|
||||||
|
pokemon: {
|
||||||
|
...evolvingPokemon,
|
||||||
|
id: metadata.evolution.pokemon,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!show) return null;
|
if (!show) return null;
|
||||||
|
|
|
@ -32,6 +32,7 @@ import TextThenAction from "./TextThenAction";
|
||||||
import LearnMove from "../app/LearnMove";
|
import LearnMove from "../app/LearnMove";
|
||||||
import QuestHandler from "./QuestHandler";
|
import QuestHandler from "./QuestHandler";
|
||||||
import ConfirmationMenu from "./ConfirmationMenu";
|
import ConfirmationMenu from "./ConfirmationMenu";
|
||||||
|
import Evolution from "./Evolution";
|
||||||
|
|
||||||
const Container = styled.div`
|
const Container = styled.div`
|
||||||
position: absolute;
|
position: absolute;
|
||||||
|
@ -122,6 +123,7 @@ const Game = () => {
|
||||||
<LearnMove />
|
<LearnMove />
|
||||||
<PlayerMenu />
|
<PlayerMenu />
|
||||||
<ActionOnPokemon />
|
<ActionOnPokemon />
|
||||||
|
<Evolution />
|
||||||
<ConfirmationMenu />
|
<ConfirmationMenu />
|
||||||
<LoadScreen />
|
<LoadScreen />
|
||||||
<TitleScreen />
|
<TitleScreen />
|
||||||
|
|
|
@ -44,9 +44,11 @@ import ballRight from "../assets/battle/ball-right.png";
|
||||||
import Menu, { MenuItemType } from "./Menu";
|
import Menu, { MenuItemType } from "./Menu";
|
||||||
import PokemonList from "./PokemonList";
|
import PokemonList from "./PokemonList";
|
||||||
import {
|
import {
|
||||||
|
selectEvolution,
|
||||||
selectItemsMenu,
|
selectItemsMenu,
|
||||||
selectPokeballThrowing,
|
selectPokeballThrowing,
|
||||||
selectStartMenu,
|
selectStartMenu,
|
||||||
|
showEvolution,
|
||||||
showItemsMenu,
|
showItemsMenu,
|
||||||
showTextThenAction,
|
showTextThenAction,
|
||||||
stopThrowingPokeball,
|
stopThrowingPokeball,
|
||||||
|
@ -57,7 +59,6 @@ import { MoveMetadata } from "../app/move-metadata";
|
||||||
import processMove, { MoveResult } from "../app/move-helper";
|
import processMove, { MoveResult } from "../app/move-helper";
|
||||||
import getXp from "../app/xp-helper";
|
import getXp from "../app/xp-helper";
|
||||||
import getLevelData, { getLearnedMove } from "../app/level-helper";
|
import getLevelData, { getLearnedMove } from "../app/level-helper";
|
||||||
import Evolution from "./Evolution";
|
|
||||||
import MoveSelect from "./MoveSelect";
|
import MoveSelect from "./MoveSelect";
|
||||||
import catchesPokemon from "../app/pokeball-helper";
|
import catchesPokemon from "../app/pokeball-helper";
|
||||||
import { PokemonEncounterType, PokemonInstance } from "../state/state-types";
|
import { PokemonEncounterType, PokemonInstance } from "../state/state-types";
|
||||||
|
@ -573,7 +574,6 @@ const PokemonEncounter = () => {
|
||||||
// 20 = they fainted
|
// 20 = they fainted
|
||||||
// 21 = gained xp
|
// 21 = gained xp
|
||||||
// 22 = leveled up
|
// 22 = leveled up
|
||||||
// 23 = Evolving
|
|
||||||
// 24 = active fainted
|
// 24 = active fainted
|
||||||
// 25 = select new pokemon
|
// 25 = select new pokemon
|
||||||
// 26 = out of pokemon
|
// 26 = out of pokemon
|
||||||
|
@ -611,6 +611,7 @@ const PokemonEncounter = () => {
|
||||||
const processingPokemon =
|
const processingPokemon =
|
||||||
pokemon[involvedPokemon[processingInvolvedPokemon] || 0];
|
pokemon[involvedPokemon[processingInvolvedPokemon] || 0];
|
||||||
const processingMetadata = usePokemonMetadata(processingPokemon?.id || null);
|
const processingMetadata = usePokemonMetadata(processingPokemon?.id || null);
|
||||||
|
const pokemonEvolving = useSelector(selectEvolution);
|
||||||
|
|
||||||
const [alertText, setAlertText] = useState<string | null>(null);
|
const [alertText, setAlertText] = useState<string | null>(null);
|
||||||
const [clickableNotice, setClickableNotice] = useState<string | null>(null);
|
const [clickableNotice, setClickableNotice] = useState<string | null>(null);
|
||||||
|
@ -839,6 +840,7 @@ const PokemonEncounter = () => {
|
||||||
|
|
||||||
useEvent(Event.A, () => {
|
useEvent(Event.A, () => {
|
||||||
if (startMenuOpen) return;
|
if (startMenuOpen) return;
|
||||||
|
if (pokemonEvolving !== null) return;
|
||||||
|
|
||||||
if (clickableNotice) {
|
if (clickableNotice) {
|
||||||
setClickableNotice(null);
|
setClickableNotice(null);
|
||||||
|
@ -924,9 +926,10 @@ const PokemonEncounter = () => {
|
||||||
setStage(29);
|
setStage(29);
|
||||||
} else if (move && hasFourMoves) {
|
} else if (move && hasFourMoves) {
|
||||||
setStage(30);
|
setStage(30);
|
||||||
} else if (isEvolving) {
|
|
||||||
setStage(23);
|
|
||||||
} else {
|
} else {
|
||||||
|
if (isEvolving) {
|
||||||
|
dispatch(showEvolution(involvedPokemon[processingInvolvedPokemon]));
|
||||||
|
}
|
||||||
endEncounter_();
|
endEncounter_();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -968,10 +971,9 @@ const PokemonEncounter = () => {
|
||||||
);
|
);
|
||||||
|
|
||||||
if (isEvolving) {
|
if (isEvolving) {
|
||||||
setStage(23);
|
dispatch(showEvolution(involvedPokemon[processingInvolvedPokemon]));
|
||||||
} else {
|
|
||||||
endEncounter_();
|
|
||||||
}
|
}
|
||||||
|
endEncounter_();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (stage === 30) {
|
if (stage === 30) {
|
||||||
|
@ -1433,26 +1435,6 @@ const PokemonEncounter = () => {
|
||||||
select={(move: string) => processBattle(move)}
|
select={(move: string) => processBattle(move)}
|
||||||
close={() => setStage(11)}
|
close={() => setStage(11)}
|
||||||
/>
|
/>
|
||||||
<Evolution
|
|
||||||
pokemonId={processingPokemon.id}
|
|
||||||
show={stage === 23}
|
|
||||||
close={() => {
|
|
||||||
if (!processingMetadata)
|
|
||||||
throw new Error("No processing metadata");
|
|
||||||
if (!processingMetadata.evolution)
|
|
||||||
throw new Error("No evolution");
|
|
||||||
dispatch(
|
|
||||||
updateSpecificPokemon({
|
|
||||||
index: involvedPokemon[processingInvolvedPokemon],
|
|
||||||
pokemon: {
|
|
||||||
...processingPokemon,
|
|
||||||
id: processingMetadata.evolution.pokemon,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
);
|
|
||||||
endEncounter_();
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
{stage === 25 && (
|
{stage === 25 && (
|
||||||
<PokemonList
|
<PokemonList
|
||||||
text="Bring out which POKéMON?"
|
text="Bring out which POKéMON?"
|
||||||
|
@ -1487,10 +1469,13 @@ const PokemonEncounter = () => {
|
||||||
processingPokemon.level >=
|
processingPokemon.level >=
|
||||||
processingMetadata.evolution.level;
|
processingMetadata.evolution.level;
|
||||||
if (isEvolving) {
|
if (isEvolving) {
|
||||||
setStage(23);
|
dispatch(
|
||||||
} else {
|
showEvolution(
|
||||||
endEncounter_();
|
involvedPokemon[processingInvolvedPokemon]
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
endEncounter_();
|
||||||
dispatch(
|
dispatch(
|
||||||
updateSpecificPokemon({
|
updateSpecificPokemon({
|
||||||
index: involvedPokemon[processingInvolvedPokemon],
|
index: involvedPokemon[processingInvolvedPokemon],
|
||||||
|
@ -1518,10 +1503,11 @@ const PokemonEncounter = () => {
|
||||||
processingPokemon.level >=
|
processingPokemon.level >=
|
||||||
processingMetadata.evolution.level;
|
processingMetadata.evolution.level;
|
||||||
if (isEvolving) {
|
if (isEvolving) {
|
||||||
setStage(23);
|
dispatch(
|
||||||
} else {
|
showEvolution(involvedPokemon[processingInvolvedPokemon])
|
||||||
endEncounter_();
|
);
|
||||||
}
|
}
|
||||||
|
endEncounter_();
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
]}
|
]}
|
||||||
|
@ -1531,10 +1517,11 @@ const PokemonEncounter = () => {
|
||||||
processingMetadata.evolution &&
|
processingMetadata.evolution &&
|
||||||
processingPokemon.level >= processingMetadata.evolution.level;
|
processingPokemon.level >= processingMetadata.evolution.level;
|
||||||
if (isEvolving) {
|
if (isEvolving) {
|
||||||
setStage(23);
|
dispatch(
|
||||||
} else {
|
showEvolution(involvedPokemon[processingInvolvedPokemon])
|
||||||
endEncounter_();
|
);
|
||||||
}
|
}
|
||||||
|
endEncounter_();
|
||||||
}}
|
}}
|
||||||
bottom="0"
|
bottom="0"
|
||||||
right="0"
|
right="0"
|
||||||
|
|
|
@ -87,10 +87,10 @@ const StartMenu = () => {
|
||||||
updateSpecificPokemon({
|
updateSpecificPokemon({
|
||||||
index: 0,
|
index: 0,
|
||||||
pokemon: {
|
pokemon: {
|
||||||
id: 6,
|
id: 1,
|
||||||
level: 100,
|
level: 15,
|
||||||
xp: 0,
|
xp: 0,
|
||||||
hp: getPokemonStats(6, 100).hp,
|
hp: getPokemonStats(3, 100).hp,
|
||||||
moves: [
|
moves: [
|
||||||
{ id: "scratch", pp: 35 },
|
{ id: "scratch", pp: 35 },
|
||||||
{ id: "growl", pp: 40 },
|
{ id: "growl", pp: 40 },
|
||||||
|
|
|
@ -40,6 +40,7 @@ interface UiState {
|
||||||
learningMove: LearningMoveType | null;
|
learningMove: LearningMoveType | null;
|
||||||
blackScreen: boolean;
|
blackScreen: boolean;
|
||||||
confirmationMenu: ConfimationMenuType | null;
|
confirmationMenu: ConfimationMenuType | null;
|
||||||
|
evolution: number | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const initialState: UiState = {
|
const initialState: UiState = {
|
||||||
|
@ -60,6 +61,7 @@ const initialState: UiState = {
|
||||||
learningMove: null,
|
learningMove: null,
|
||||||
blackScreen: false,
|
blackScreen: false,
|
||||||
confirmationMenu: null,
|
confirmationMenu: null,
|
||||||
|
evolution: null,
|
||||||
};
|
};
|
||||||
|
|
||||||
export const uiSlice = createSlice({
|
export const uiSlice = createSlice({
|
||||||
|
@ -165,6 +167,12 @@ export const uiSlice = createSlice({
|
||||||
hideConfirmationMenu: (state) => {
|
hideConfirmationMenu: (state) => {
|
||||||
state.confirmationMenu = null;
|
state.confirmationMenu = null;
|
||||||
},
|
},
|
||||||
|
showEvolution: (state, action: PayloadAction<number>) => {
|
||||||
|
state.evolution = action.payload;
|
||||||
|
},
|
||||||
|
hideEvolution: (state) => {
|
||||||
|
state.evolution = null;
|
||||||
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -199,6 +207,8 @@ export const {
|
||||||
setBlackScreen,
|
setBlackScreen,
|
||||||
showConfirmationMenu,
|
showConfirmationMenu,
|
||||||
hideConfirmationMenu,
|
hideConfirmationMenu,
|
||||||
|
showEvolution,
|
||||||
|
hideEvolution,
|
||||||
} = uiSlice.actions;
|
} = uiSlice.actions;
|
||||||
|
|
||||||
export const selectText = (state: RootState) => state.ui.text;
|
export const selectText = (state: RootState) => state.ui.text;
|
||||||
|
@ -241,7 +251,8 @@ export const selectMenuOpen = (state: RootState) =>
|
||||||
state.ui.pokeMartMenu ||
|
state.ui.pokeMartMenu ||
|
||||||
state.ui.textThenAction !== null ||
|
state.ui.textThenAction !== null ||
|
||||||
state.ui.learningMove !== null ||
|
state.ui.learningMove !== null ||
|
||||||
state.ui.confirmationMenu !== null;
|
state.ui.confirmationMenu !== null ||
|
||||||
|
state.ui.evolution !== null;
|
||||||
|
|
||||||
export const selectStartMenuSubOpen = (state: RootState) =>
|
export const selectStartMenuSubOpen = (state: RootState) =>
|
||||||
state.ui.itemsMenu || state.ui.playerMenu;
|
state.ui.itemsMenu || state.ui.playerMenu;
|
||||||
|
@ -264,4 +275,6 @@ export const selectBlackScreen = (state: RootState) => state.ui.blackScreen;
|
||||||
export const selectConfirmationMenu = (state: RootState) =>
|
export const selectConfirmationMenu = (state: RootState) =>
|
||||||
state.ui.confirmationMenu;
|
state.ui.confirmationMenu;
|
||||||
|
|
||||||
|
export const selectEvolution = (state: RootState) => state.ui.evolution;
|
||||||
|
|
||||||
export default uiSlice.reducer;
|
export default uiSlice.reducer;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue