🚀 add xp share

This commit is contained in:
Chase Manning 2023-11-26 18:07:00 +00:00
commit 83c14c1ed3
2 changed files with 129 additions and 56 deletions

View file

@ -1,6 +1,6 @@
=========== ===========
- Text cuttoff with attacks - Implement moon stone
=========== ===========

View file

@ -11,6 +11,7 @@ import {
recoverFromFainting, recoverFromFainting,
resetActivePokemon, resetActivePokemon,
selectActivePokemon, selectActivePokemon,
selectActivePokemonIndex,
selectName, selectName,
selectPokemon, selectPokemon,
selectPokemonEncounter, selectPokemonEncounter,
@ -18,6 +19,7 @@ import {
setActivePokemon, setActivePokemon,
updatePokemon, updatePokemon,
updatePokemonEncounter, updatePokemonEncounter,
updateSpecificPokemon,
} from "../state/gameSlice"; } from "../state/gameSlice";
import usePokemonMetadata from "../app/use-pokemon-metadata"; import usePokemonMetadata from "../app/use-pokemon-metadata";
import Frame from "./Frame"; import Frame from "./Frame";
@ -547,6 +549,7 @@ const PokemonEncounter = () => {
const startMenuOpen = useSelector(selectStartMenu); const startMenuOpen = useSelector(selectStartMenu);
const pokeballThrowing = useSelector(selectPokeballThrowing); const pokeballThrowing = useSelector(selectPokeballThrowing);
const trainer = useSelector(selectTrainerEncounter); const trainer = useSelector(selectTrainerEncounter);
const activePokemonIndex = useSelector(selectActivePokemonIndex);
// 0 = intro animation started // 0 = intro animation started
// 1 = intro animation finished // 1 = intro animation finished
@ -603,6 +606,11 @@ const PokemonEncounter = () => {
const [stage, setStage] = useState(-1); const [stage, setStage] = useState(-1);
const [trainerPokemonIndex, setTrainerPokemonIndex] = useState(0); const [trainerPokemonIndex, setTrainerPokemonIndex] = useState(0);
const [outroIndex, setOutroIndex] = useState(0); const [outroIndex, setOutroIndex] = useState(0);
const [involvedPokemon, setInvolvedPokemon] = useState<number[]>([0]);
const [processingInvolvedPokemon, setProcessingInvolvedPokemon] = useState(0);
const processingPokemon =
pokemon[involvedPokemon[processingInvolvedPokemon] || 0];
const processingMetadata = usePokemonMetadata(processingPokemon?.id || null);
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);
@ -613,6 +621,31 @@ const PokemonEncounter = () => {
const isThrowingEnemyPokeball = stage >= 34 && stage <= 38 && isTrainer; const isThrowingEnemyPokeball = stage >= 34 && stage <= 38 && isTrainer;
const endEncounter_ = (exitBattle = false) => { const endEncounter_ = (exitBattle = false) => {
if (processingInvolvedPokemon < involvedPokemon.length - 1) {
const nextIndex = processingInvolvedPokemon + 1;
if (enemy) {
dispatch(
updateSpecificPokemon({
index: involvedPokemon[nextIndex],
pokemon: {
...pokemon[involvedPokemon[nextIndex]],
xp:
pokemon[involvedPokemon[nextIndex]].xp +
Math.round(
getXp(enemy.id, enemy.level) / involvedPokemon.length
),
},
})
);
}
setProcessingInvolvedPokemon(nextIndex);
setStage(21);
return;
}
setInvolvedPokemon([activePokemonIndex]);
setProcessingInvolvedPokemon(0);
// Ending encounter
if (exitBattle) { if (exitBattle) {
setTrainerPokemonIndex(0); setTrainerPokemonIndex(0);
dispatch(endEncounter()); dispatch(endEncounter());
@ -628,6 +661,7 @@ const PokemonEncounter = () => {
encounterPokemon(getPokemonEncounter(newPokemon.id, newPokemon.level)) encounterPokemon(getPokemonEncounter(newPokemon.id, newPokemon.level))
); );
setTrainerPokemonIndex(newIndex); setTrainerPokemonIndex(newIndex);
console.log("Hrowing pokeball at enemy");
throwPokeballAtEnemy(49); throwPokeballAtEnemy(49);
return; return;
} }
@ -811,6 +845,8 @@ const PokemonEncounter = () => {
} }
if (stage === 2) { if (stage === 2) {
setInvolvedPokemon([activePokemonIndex]);
setProcessingInvolvedPokemon(0);
if (isTrainer) { if (isTrainer) {
setStage(46); setStage(46);
setTimeout(() => { setTimeout(() => {
@ -839,9 +875,16 @@ const PokemonEncounter = () => {
setStage(21); setStage(21);
if (enemy) { if (enemy) {
dispatch( dispatch(
updatePokemon({ updateSpecificPokemon({
...active, index: involvedPokemon[processingInvolvedPokemon],
xp: active.xp + getXp(enemy.id, enemy.level), pokemon: {
...processingPokemon,
xp:
processingPokemon.xp +
Math.round(
getXp(enemy.id, enemy.level) / involvedPokemon.length
),
},
}) })
); );
} }
@ -849,15 +892,18 @@ const PokemonEncounter = () => {
if (stage === 21) { if (stage === 21) {
const { level, leveledUp, remainingXp } = getLevelData( const { level, leveledUp, remainingXp } = getLevelData(
active.level, processingPokemon.level,
active.xp processingPokemon.xp
); );
if (leveledUp) { if (leveledUp) {
dispatch( dispatch(
updatePokemon({ updateSpecificPokemon({
...active, index: involvedPokemon[processingInvolvedPokemon],
level, pokemon: {
xp: remainingXp, ...processingPokemon,
level,
xp: remainingXp,
},
}) })
); );
setStage(22); setStage(22);
@ -867,12 +913,12 @@ const PokemonEncounter = () => {
} }
const isEvolving = const isEvolving =
activeMetadata && processingMetadata &&
activeMetadata.evolution && processingMetadata.evolution &&
active.level >= activeMetadata.evolution.level; processingPokemon.level >= processingMetadata.evolution.level;
if (stage === 22) { if (stage === 22) {
const move = getLearnedMove(active); const move = getLearnedMove(processingPokemon);
const hasFourMoves = active.moves.length === 4; const hasFourMoves = active.moves.length === 4;
if (move && !hasFourMoves) { if (move && !hasFourMoves) {
setStage(29); setStage(29);
@ -909,12 +955,15 @@ const PokemonEncounter = () => {
} }
if (stage === 29) { if (stage === 29) {
const move = getLearnedMove(active); const move = getLearnedMove(processingPokemon);
if (!move) throw new Error("No move found"); if (!move) throw new Error("No move found");
dispatch( dispatch(
updatePokemon({ updateSpecificPokemon({
...active, index: involvedPokemon[processingInvolvedPokemon],
moves: [...active.moves, move], pokemon: {
...processingPokemon,
moves: [...processingPokemon.moves, move],
},
}) })
); );
@ -997,26 +1046,35 @@ const PokemonEncounter = () => {
if (stage === 12) return "Got away safely!"; if (stage === 12) return "Got away safely!";
if (stage === 20) if (stage === 20)
return `Enemy ${enemyMetadata.name.toUpperCase()} fainted!`; return `Enemy ${enemyMetadata.name.toUpperCase()} fainted!`;
if (stage === 21) if (stage === 21) {
return `${activeMetadata.name.toUpperCase()} gained ${getXp( if (!processingMetadata) throw new Error("No processing metadata found");
enemy.id, return `${processingMetadata.name.toUpperCase()} gained ${Math.round(
enemy.level getXp(enemy.id, enemy.level) / involvedPokemon.length
)} EXP. points!`; )} EXP. points!`;
if (stage === 22) }
return `${activeMetadata.name.toUpperCase()} grew to level ${ if (stage === 22) {
getLevelData(active.level, active.xp).level if (!processingMetadata) throw new Error("No processing metadata found");
return `${processingMetadata.name.toUpperCase()} grew to level ${
getLevelData(processingPokemon.level, processingPokemon.xp).level
}!`; }!`;
}
if (stage === 24) return `${activeMetadata.name.toUpperCase()} fainted!`; if (stage === 24) return `${activeMetadata.name.toUpperCase()} fainted!`;
if (stage === 26) return `${name} is out of usable POKéMON!`; if (stage === 26) return `${name} is out of usable POKéMON!`;
if (stage === 27) return `${name} blacked out!`; if (stage === 27) return `${name} blacked out!`;
if (stage === 29) if (stage === 29) {
return `${activeMetadata.name.toUpperCase()} learned ${getLearnedMove( if (!processingMetadata) throw new Error("No processing metadata found");
active const move = getLearnedMove(processingPokemon);
)}!`; if (!move) throw new Error("No move found");
if (stage === 30) return `${processingMetadata.name.toUpperCase()} learned ${move.id}!`;
return `${activeMetadata.name.toUpperCase()} is trying to learn ${getLearnedMove( }
active if (stage === 30) {
)}.`; if (!processingMetadata) throw new Error("No processing metadata found");
const move = getLearnedMove(processingPokemon);
if (!move) throw new Error("No move found");
return `${processingMetadata.name.toUpperCase()} is trying to learn ${
move.id
}.`;
}
if (stage === 31) return `But it cannot learn more than 4 moves`; if (stage === 31) return `But it cannot learn more than 4 moves`;
if (stage === 32) return `Choose a move you would like to forget`; if (stage === 32) return `Choose a move you would like to forget`;
if (stage === 42) return `Darn! The POKéMON broke free!`; if (stage === 42) return `Darn! The POKéMON broke free!`;
@ -1365,6 +1423,7 @@ const PokemonEncounter = () => {
close={() => setStage(11)} close={() => setStage(11)}
switchAction={(index) => { switchAction={(index) => {
dispatch(setActivePokemon(index)); dispatch(setActivePokemon(index));
setInvolvedPokemon([...involvedPokemon, index]);
throwPokeball(); throwPokeball();
}} }}
/> />
@ -1375,14 +1434,20 @@ const PokemonEncounter = () => {
close={() => setStage(11)} close={() => setStage(11)}
/> />
<Evolution <Evolution
pokemonId={active.id} pokemonId={processingPokemon.id}
show={stage === 23} show={stage === 23}
close={() => { close={() => {
if (!activeMetadata.evolution) throw new Error("No evolution"); if (!processingMetadata)
throw new Error("No processing metadata");
if (!processingMetadata.evolution)
throw new Error("No evolution");
dispatch( dispatch(
updatePokemon({ updateSpecificPokemon({
...active, index: involvedPokemon[processingInvolvedPokemon],
id: activeMetadata.evolution.pokemon, pokemon: {
...processingPokemon,
id: processingMetadata.evolution.pokemon,
},
}) })
); );
endEncounter_(); endEncounter_();
@ -1395,6 +1460,7 @@ const PokemonEncounter = () => {
switchAction={(index) => { switchAction={(index) => {
if (pokemon[index].hp <= 0) return; if (pokemon[index].hp <= 0) return;
dispatch(setActivePokemon(index)); dispatch(setActivePokemon(index));
setInvolvedPokemon([...involvedPokemon, index]);
throwPokeball(); throwPokeball();
}} }}
/> />
@ -1405,8 +1471,8 @@ const PokemonEncounter = () => {
padding={isMobile ? "100px" : "40vw"} padding={isMobile ? "100px" : "40vw"}
show={stage === 33} show={stage === 33}
menuItems={[ menuItems={[
...active.moves.map((m) => { ...processingPokemon.moves.map((m) => {
const newMove = getLearnedMove(active); const newMove = getLearnedMove(processingPokemon);
if (!newMove) if (!newMove)
return { return {
label: "Error", label: "Error",
@ -1416,21 +1482,27 @@ const PokemonEncounter = () => {
label: m.id, label: m.id,
action: () => { action: () => {
const isEvolving = const isEvolving =
activeMetadata && processingMetadata &&
activeMetadata.evolution && processingMetadata.evolution &&
active.level >= activeMetadata.evolution.level; processingPokemon.level >=
processingMetadata.evolution.level;
if (isEvolving) { if (isEvolving) {
setStage(23); setStage(23);
} else { } else {
endEncounter_(); endEncounter_();
} }
dispatch( dispatch(
updatePokemon({ updateSpecificPokemon({
...active, index: involvedPokemon[processingInvolvedPokemon],
moves: [ pokemon: {
...active.moves.filter((move) => move.id !== m.id), ...processingPokemon,
newMove, moves: [
], ...processingPokemon.moves.filter(
(move) => move.id !== m.id
),
newMove,
],
},
}) })
); );
}, },
@ -1438,12 +1510,13 @@ const PokemonEncounter = () => {
return item; return item;
}), }),
{ {
label: getLearnedMove(active)?.id || "Error", label: getLearnedMove(processingPokemon)?.id || "Error",
action: () => { action: () => {
const isEvolving = const isEvolving =
activeMetadata && processingMetadata &&
activeMetadata.evolution && processingMetadata.evolution &&
active.level >= activeMetadata.evolution.level; processingPokemon.level >=
processingMetadata.evolution.level;
if (isEvolving) { if (isEvolving) {
setStage(23); setStage(23);
} else { } else {
@ -1454,9 +1527,9 @@ const PokemonEncounter = () => {
]} ]}
close={() => { close={() => {
const isEvolving = const isEvolving =
activeMetadata && processingMetadata &&
activeMetadata.evolution && processingMetadata.evolution &&
active.level >= activeMetadata.evolution.level; processingPokemon.level >= processingMetadata.evolution.level;
if (isEvolving) { if (isEvolving) {
setStage(23); setStage(23);
} else { } else {