This lesson was updated. Reset to the new version?
This browser isn't keeping your edits, so they're lost when you leave the page.
Pokémon-7 finishes the base game. It adds a title screen, music that changes as you cross from Pallet Town to Route 1, the flashing encounter transitions from the real games, and leaves that fly up when you walk through tall grass. ENCOUNTER_CHANCE in src/config/game.js drops from 0.3 to 0.1, so an encounter comes one step in ten on tall grass.
The Title Screen
main.js now pushes a TitleScreenState instead of a PlayState:
49game.services.stateStack.push(new TitleScreenState(game.services));TitleScreenState in src/states/TitleScreenState.js draws the title image and plays the title music. Enter calls play(), which should swap the title screen for the game and show the welcome message over the town. That swap is the challenge.
Browsers only allow sound to start after the player has clicked or pressed a key on the page, so the title music starts the first time you press one.
Transition Effects
Every TransitionState from Pokémon-4 used a fade effect. TransitionState.run() in src/states/TransitionState.js now takes an effect too, and passes it on with the duration and colour. The plain fade is still the default:
96static run(game, type, options) {97 const {98 effect = TransitionEffect.Fade,99 duration = DEFAULT_DURATION,100 colour = Colour.White,101 } = options;render() hands the effect and the tweened progress to drawTransition() in src/services/transitions.js. Each effect is a function that covers part of the screen. At a progress of 0 it draws nothing, and at 1 it totally covers the screen. Covering tweens from 0 to 1, and uncovering from 1 to 0, so every effect can also run backwards. wipeRight() is the simplest:
49function wipeRight(context, progress) {50 context.fillRect(0, 0, CANVAS_WIDTH * progress, CANVAS_HEIGHT);51}There are twelve effects in all, including wipes from each side, a radial sweep like a clock hand, stripes, and a shutter. Hold covers the screen whatever the progress, for a pause between other effects.
Pick an effect and move progress to see what it draws. While it plays, progress goes up as a cover, then back down as an uncover:
A wild battle in the real games opens with two black flashes and then a random effect. coverForEncounter() plays that as a sequence of covers and uncovers, each one awaited:
140static async coverForEncounter(game) {141 const flash = { duration: 0.2, colour: Colour.Black };142
143 await TransitionState.cover(game, flash);144 await TransitionState.uncover(game, flash);145 await TransitionState.cover(game, flash);146 await TransitionState.uncover(game, flash);147 await TransitionState.cover(game, {148 effect: pickRandomElement(ENCOUNTER_EFFECTS),149 duration: 0.6,150 colour: Colour.Black,151 });152 await TransitionState.cover(game, {153 effect: TransitionEffect.Hold,154 duration: 0.95,155 colour: Colour.Black,156 });157}startBattle() in src/states/PlayState.js calls it in place of the first cover(), and uncovers the battle with the shutter. The way back to the map is still a plain fade.
Music
game.music from lib/Music.js plays one track at a time and loops it. A track in the real games usually has an intro that plays once, then a part that repeats forever. Each file in assets/sounds/music/ holds the intro followed by one pass of the loop, and loopStart in src/config/music.js is where the intro ends, in seconds:
11export const Track = {12 Title: { sound: SoundName.Title, loopStart: 5 },13 Tutorial: { sound: SoundName.Tutorial, loopStart: 12.66 },14 PalletTown: { sound: SoundName.PalletTown, loopStart: 1.5 },15 Route: { sound: SoundName.Route },16 WildBattle: { sound: SoundName.Battle, loopStart: 13.603 },17 WildVictory: { sound: SoundName.BattleVictory, loopStart: 2.3 },18};Music plays the file through the Web Audio API, which can loop between two exact points in a sound. When the file reaches its end, it jumps back to loopStart instead of the start, so the intro never plays again. Route 1 has no loopStart, so its whole file loops.
Battles play the wild battle track, and processVictory() in src/objects/BattleTurn.js switches to the victory fanfare. The level-up jingle uses music.playJingle(), which pauses the track, plays the jingle, and picks the track back up where it left off.
Music by Area
Each area in areas in src/config/areas.js, from Pokémon-4, now has a music track as well as its encounters:
22export const areas = {23 overworld: [24 // Route 125 {26 music: Track.Route,27 bounds: { x: 0, y: 0, width: 24, height: 40 },28 encounters: ROUTE_1_ENCOUNTERS,29 },30 // Pallet Town31 { music: Track.PalletTown },32 ],33};update() in src/states/PlayState.js plays the area’s track every frame:
124update(dt) {125 this.game.music.play(this.area.music);126 this.map.update(dt);127}Music.play() does nothing when asked for the track that’s already playing, so this only changes the music when the area does. It also puts the town’s music back after a battle. exit() in src/states/battle/BattleState.js stops the battle music, so the screen uncovers in silence, and the town’s track starts once PlayState updates again with the battle off the stack. Nothing has to remember to restart it.
Rustling Grass
Walking into tall grass flattens it and throws up leaves around the player’s feet. The leaves’ sprites are in grass-animation.png, which neither tileset has, so tilesets in src/config/tiles.js adds it as a sheet whose ids follow on from the others. Only the code places these tiles, so Tiled never needs to know about them.
move() in src/objects/GameEntity.js calls stepOnGrass() before the step, which swaps the tile for the flattened one with setTileId(). When the step ends, startGrassAnimation() puts the tall grass back and starts the leaves, an Animation that plays once:
256startGrassAnimation(x, y) {257 this.map.bottomLayer.setTileId(x, y, GRASS_TILE);258 this.grassAnimations.push({259 x,260 y,261 animation: new Animation(262 GRASS_ANIMATION_TILES,263 GRASS_ANIMATION_INTERVAL,264 1,265 ),266 });267}render() in src/objects/GameEntity.js draws the leaves and the character sorted by y, as GameMap sorts the characters, so the grass covers the character’s legs. A finished animation stays on its last sprite while the character stands on its tile, then disappears once they walk off.
Healing in the Field
P in PlayerIdleState in src/states/player/PlayerIdleState.js heals the party with healWithMessage(), so you can keep battling without fainting first. healWithMessage() pauses the music while the heal sound plays, and resumes it after.
Start the game
Pressing Enter on the title screen shows the welcome message over the title, because play() in src/states/TitleScreenState.js never changes the screen.
Replace the TODO with a change of screen, with the same cover, change, and uncover as Pokémon-4’s battles:
- Make the player first, as a
new Player(game)in aplayerconstant. - Cover the screen with
TransitionState.cover(), then stop the title music withgame.music.stop(). - Pop the title screen off
game.stateStack, then push anew PlayState(game)with{ mapId: START_MAP, player }. - Uncover the screen with
TransitionState.uncover().
Await the cover and the uncover. pop() comes before push() here, because nothing should come back to the title screen. Then press Enter on the title screen. The Challenge tab gets a ✓ once you’ve read the welcome message to the end and are on the map.
Player, PlayState, TransitionState and START_MAP are already imported at the top of src/states/TitleScreenState.js.
If the town appears before the screen is covered, the await in front of TransitionState.cover(game) is missing. If the welcome message shows while the screen is still covered, the await in front of TransitionState.uncover(game) is missing.
Replace the TODO with:
const player = new Player(game);
await TransitionState.cover(game);game.music.stop();game.stateStack.pop();game.stateStack.push(new PlayState(game), { mapId: START_MAP, player });await TransitionState.uncover(game);