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.
Zelda-6 makes taking damage feel juicier. A hit knocks Link back, he blinks while he can’t be hurt again, and when his health runs out he dies and the dungeon starts over.
Link can’t die in this demo. Watch damageCooldown tick down while he blinks, then he walks back in for the next hit.
Taking Damage
In Zelda-4, takeDamage() only lowered Link’s health, and a Moblin drained it a point per frame. Now it does the whole job:
99takeDamage(amount, sourcePosition) {100 if (this.isDead) {101 return;102 }103
104 if (this.damageCooldown > 0) {105 return;106 }107
108 const nextHealth = Math.max(0, this.health - amount);109
110 if (nextHealth === this.health) {111 return;112 }113
114 this.health = nextHealth;115 this.damageCooldown = PLAYER_DAMAGE_COOLDOWN;116
117 if (this.health === 0) {118 this.stateMachine.change(PlayerStateName.Death);119 return;120 }121
122 this.services.sounds.play(SoundName.Hurt);123 this.stateMachine.change(PlayerStateName.Hurt, { sourcePosition });124}PlayState now passes the enemy’s position along with the damage, so Link knows which way to be knocked.
The Damage Cooldown
damageCooldown counts down in Player.update(). While it’s above zero, takeDamage() returns straight away, so a Moblin standing on Link hurts him once and then has to wait two seconds.
Link blinks for as long as the cooldown lasts. Player.isVisible turns the time since the hit into alternating on and off intervals:
140const invulnerableElapsed = PLAYER_DAMAGE_COOLDOWN - this.damageCooldown;141
142return Math.floor(invulnerableElapsed / PLAYER_BLINK_INTERVAL) % 2 === 0;Player.render() skips drawing on the off intervals. The blink is how the player knows Link can’t be hurt right now.
Turn on debug mode and watch damageCooldown in the panel count down after a hit.
The Hurt State
A hit interrupts whatever Link was doing, even a sword swing, by changing to PlayerHurtState. The state works out which way to slide from where the damage came from:
39enter(props) {40 super.enter(props);41 this.direction = getDirectionToward(42 this.player.position.x - props.sourcePosition.x,43 this.player.position.y - props.sourcePosition.y,44 );45 this.animation = new Animation([this.walkingFrames[this.player.facing][0]]);46
47 this.timer.after(PLAYER_KNOCKBACK_DURATION, () => {48 this.player.stateMachine.change(PlayerStateName.Idle);49 });50}Each frame it slides Link with move(), so knockback stops at walls like walking does. After a quarter of a second, its timer hands Link back to Idle. sourcePosition arrives as transition data which means the second argument to stateMachine.change() becomes the props passed to enter().
stateDiagram-v2 [*] --> Idle Idle --> Walk: direction held Walk --> Idle: no direction held Idle --> Sword: Z Walk --> Sword: Z Sword --> Idle: swing over Idle --> Hurt: hit Walk --> Hurt: hit Sword --> Hurt: hit Hurt --> Idle: knockback over Idle --> Death: health 0 Walk --> Death: health 0 Sword --> Death: health 0 Death --> [*]: new PlayState
PlayerEnterRoomState is left out of the diagram. Link can’t be hit during a pan, because PlayState doesn’t check collisions while the dungeon is panning.
Dying
When Link’s health reaches zero, PlayerDeathState plays the death sound and spins him around while the screen fades to white, then holds the white screen for a second:
52const fadeDuration = this.deathFrames.length * FRAME_INTERVAL;53
54this.timer.tween(this, { fadeOpacity: 1 }, fadeDuration);55this.timer.after(fadeDuration + WHITE_SCREEN_HOLD_DURATION, () => {56 this.game.stateMachine.add(StateName.Play, new PlayState(this.game));57 this.game.stateMachine.change(StateName.Play);58});The fade has to cover the whole room, but Link has to stay visible on top of it. PlayState leaves a dead Link out of the y-sorted actors and draws him last, after everything else, so his death state draws the white fade and then him.
Starting Over
Restarting registers a new PlayState under the same name. Changing to it runs the constructor again, which builds a fresh Dungeon, a fresh Player at full health, and room 1’s enemies, all from config.
Make Link blink
Link takes damage and can’t be hurt again for two seconds, but nothing shows it, because isVisible in src/objects/Player.js always returns true.
The two missing lines are the code block under The Damage Cooldown above. Use them in place of the final return true;.
Then let a Moblin hurt Link. The Challenge tab gets a ✓ once you’ve seen it work in the game.