Reset this lesson?

Your edits to this lesson are deleted and its starting files load again.

Pokémon-6 rewards winning. Defeating a wild Pokémon earns experience, which fills a bar under the player’s health. A full bar levels the Pokémon up, raising its stats.

Experience

Each Pokémon keeps a running total of all the experience it has ever earned, currentExperience. Each level starts at a total given by experienceFromLevel() in src/objects/Pokemon.js. This game uses the Medium Fast curve from the real games, where reaching a level takes that level cubed:

src/objects/Pokemon.js
173
experienceFromLevel(level) {
174
return level === 1 ? 0 : level * level * level;
175
}

Level 2 starts at 8, level 3 at 27, level 4 at 64, and level 10 at 1,000, so each level takes more experience than the one before it. The constructor sets three fields from the curve:

src/objects/Pokemon.js
70
this.targetExperience = this.experienceFromLevel(level + 1);
71
this.currentExperience = this.experienceFromLevel(level);
72
this.levelExperience = this.experienceFromLevel(level);
  • levelExperience is where the current level starts.
  • targetExperience is where the next level starts.
  • currentExperience begins at the start of the level.

The bar only shows progress through the current level. getLevelProgress() takes away levelExperience, so a level 3 Pokémon with 40 experience is 13 out of 37 along, which is the same as 40 - 27 out of 64 - 27.

Defeating a Pokémon awards experience with calculateExperienceToAward(). Each species in pokemonDefinitions has a baseExperience, so a new species needs one too, and a higher level gives more of it.

Victory

processVictory() in src/objects/BattleTurn.js now awards experience and checks for a level-up once the opponent has fainted:

src/objects/BattleTurn.js
200
await this.processExperience();
201
await this.processLevelUp();

processExperience() says how much was earned, adds it with gainExperience(), and awaits animateExperience() on the player’s panel, which fills the bar with the experience sound playing. If the bar fills all the way, animateExperience() in src/user-interface/battle/PlayerPanel.js finishes with a ding, and only resolves once the ding ends:

src/user-interface/battle/PlayerPanel.js
53
sounds.play(SoundName.ExperienceGain);
54
await this.experienceBar.setValue(this.pokemon.getLevelProgress());
55
sounds.stop(SoundName.ExperienceGain);
56
57
if (this.pokemon.canLevelUp()) {
58
await sounds.playAsync(SoundName.ExperienceFull);
59
}

Levelling Up

processLevelUp() levels up for as long as the Pokémon has enough experience:

src/objects/BattleTurn.js
232
while (playerPokemon.canLevelUp()) {
233
await this.levelUp();
234
}

levelUp() in src/objects/Pokemon.js adds 1 to level. It also has to move levelExperience and targetExperience along to the new level and recalculate the stats, or canLevelUp() stays true. You write that part in the challenge below.

It’s a loop, not an if, because one battle against a much stronger Pokémon can pass two levels at once. Each pass plays the level-up jingle, shows the message, and slides the bar into the new level:

src/objects/BattleTurn.js
243
const jingle = game.sounds.playAsync(SoundName.LevelUp);
244
245
playerPokemon.levelUp();
246
247
await Promise.all([
248
jingle,
249
this.battle.showMessage(
250
`${playerPokemon.name} grew to LV. ${playerPokemon.level}!`,
251
),
252
]);
253
254
await this.battle.panels.player.animateNextLevel();

Promise.all() waits for both the jingle and the message, so pressing Enter early doesn’t cut the jingle off, and the next level-up can’t start over it.

The Experience Bar

ExperienceProgressBar in src/user-interface/battle/ExperienceProgressBar.js is a ProgressBar, like the health bar, so it slides with the setValue() you wrote in Pokémon-5. Its max is the experience the current level needs, targetExperience - levelExperience, and its value is getLevelProgress(), which stops at a full bar.

After a level-up, animateNextLevel() in src/user-interface/battle/PlayerPanel.js empties the bar, sets its max to the new level’s size, and slides it again:

src/user-interface/battle/PlayerPanel.js
68
animateNextLevel() {
69
const { experienceBar, healthBar, pokemon } = this;
70
71
healthBar.max = pokemon.health;
72
experienceBar.value = 0;
73
experienceBar.max = pokemon.targetExperience - pokemon.levelExperience;
74
75
return this.animateExperience();
76
}

So the bar fills, empties, and slides on into the new level with whatever experience was left over. The health bar’s max follows the Pokémon’s new maximum health too. The stats screen shows the experience as an EXP row.

Give a level 5 Charmander some experience. 25 stays inside the level. 250 is enough for two levels, so the while loop in processLevelUp() runs twice. Your own game levels up once per win. Your starter begins at level 2, 15 experience into the 19 that level 3 needs. A level 1 wild Pokémon is worth 8 experience, so one win is enough:

Level up

Win a battle, and your Pokémon levels up again every time you press Enter, because levelUp() in src/objects/Pokemon.js only raises the level. targetExperience never moves, so canLevelUp() stays true forever. That battle never ends, so reload the preview once you’ve seen it.

Under the TODO, move the level’s start and end to the new level, then recalculate the stats for it:

  1. Set this.levelExperience to the experience the new level starts at, from this.experienceFromLevel().
  2. Set this.targetExperience to the experience the level after it starts at.
  3. Recalculate the stats with this.calculateStats().

Then win a battle, which is enough for your level 2 starter to level up, and check its stats with Escape. The Challenge tab gets a ✓ once you’re back on the map with a level-up behind you.

this.level++ runs first, so this.level is already the new level when your lines run.

The constructor in src/objects/Pokemon.js sets levelExperience and targetExperience from level the same way. Copy those two lines and use this.level in place of level.

Replace the TODO with:

this.levelExperience = this.experienceFromLevel(this.level);
this.targetExperience = this.experienceFromLevel(this.level + 1);
this.calculateStats();

📚 References

Powered by WebContainers
Files
Preparing Environment