โ† Home    Full Blog

Documentation

Code Comments ยท Mini-Lesson Documentation ยท Code Highlights

๐Ÿ“‹ Table of Contents

  1. Code Comments
  2. Mini-Lesson Documentation
  3. Code Highlights

๐Ÿ“ Code Comments

Every custom class and method in the game has JSDoc documentation with @class, @extends, @param, and @returns tags. Inline comments explain non-obvious logic throughout. Comment density exceeds the 10% requirement across all source files.

JSDoc class documentation โ€” PeppaBossEnemy

/**
 * PeppaBossEnemy โ€” custom boss class extending the Enemy base.
 * Adds health system, defeat state, invulnerability window,
 * movement AI, and hitbox debug visualization.
 *
 * @class PeppaBossEnemy
 * @extends Enemy
 * @example
 * // Instantiated automatically by the engine via this.classes array
 * const boss = new PeppaBossEnemy(sprite_data_enemy, gameEnv);
 */
class PeppaBossEnemy extends Enemy {

  /**
   * @param {Object} data              - Sprite and config data
   * @param {number} data.health       - Starting HP for this boss
   * @param {number} data.moveSpeed    - Pixels per frame toward player
   * @param {Object} gameEnv           - Shared game environment reference
   */
  constructor(data = null, gameEnv = null) {
    super(data, gameEnv);
    /** @type {number} Current health points */
    this.health = data?.health ?? 3;
    /** @type {boolean} Whether boss is currently in defeat state */
    this.isDefeated = false;
    /** @type {boolean} Whether boss can currently receive damage */
    this.isVulnerable = true;
  }
}

JSDoc method documentation โ€” takeDamage()

/**
 * Applies damage to the boss. Respects invulnerability window and defeat state.
 * Starts the 300ms invulnerability cooldown after each successful hit.
 *
 * @param {number} amount - HP to subtract (default 1)
 * @returns {boolean} true if this hit caused defeat, false otherwise
 */
takeDamage(amount = 1) {
  if (this.isDefeated) return true;
  if (!this.isVulnerable) return false;
  this.health = Math.max(0, this.health - amount);
  this.isVulnerable = false;
  this.lastDamagedAt = Date.now();
  if (this.health === 0) this.isDefeated = true;
  return this.isDefeated;
}

/**
 * Moves boss toward a target object each frame using normalized direction.
 * @param {Object} target - game object with .position, .width, .height
 * @returns {boolean} true if movement occurred, false if already at target
 */
moveTowardPlayer(target) {
  const dx = (target.position.x + target.width  / 2) - (this.position.x + this.width  / 2);
  const dy = (target.position.y + target.height / 2) - (this.position.y + this.height / 2);
  const distance = Math.hypot(dx, dy);
  if (distance > 1) {
    this.position.x += (dx / distance) * this.moveSpeed;
    this.position.y += (dy / distance) * this.moveSpeed;
    return true;
  }
  return false;
}
JSDoc comments use the /** */ syntax and tags like @param {type} name - description and @returns {type}. These are recognized by IDEs to show autocomplete hints and type information. The @example tag shows how to instantiate the class. Inline /** @type {type} */ comments before properties document instance variables for anyone reading the code later.

Strategic inline comment pattern

// Strategic console.log placement โ€” prefixed tags make logs easy to filter
console.log(`[PeppaBossEnemy] Spawned: ${data?.id} with ${this.health} HP`);
console.log(`[Boss] Hit! HP: ${this.health}/${this.maxHealth}`);
console.log(`[Boss] Invulnerable, ignoring damage.`);
console.log(`[Boss] Defeated!`);
console.log(`[Hitbox] pos=(${Math.round(this.position.x)},${Math.round(this.position.y)})`);
console.log(`[Leaderboard] Score saved:`, data);
console.error(`[Leaderboard] POST failed:`, error.message);

๐Ÿ“– Mini-Lesson Documentation

The rubric requires a comic/visual post with embedded runtime game demo in the personal portfolio. This entire blog โ€” eight separate pages with annotated code snippets, concept explanations, live code runners, and a playable embedded game โ€” is that mini-lesson.

What makes this a mini-lesson: Each page teaches one concept area by showing real code from the game, explaining WHY the code is structured the way it is (not just what it does), and letting readers run modified versions themselves via the embedded code runners.

What each page teaches

PageConcept TaughtInteractive Element
Object-Oriented ProgrammingClass hierarchy, inheritance, constructor chaining, method overridingCode runner โ€” build your own class
Control Structuresfor/forEach loops, conditionals, nested conditionsAnnotated live game code
Data TypesNumbers, Strings, Booleans, Arrays, ObjectsType reference table with game examples
OperatorsMath, string, boolean expressionsAABB collision walkthrough
Input/OutputKeyboard events, Canvas API, API calls, async/await, JSONLive iTunes API demo โ€” plays real audio
DocumentationJSDoc, mini-lesson structure, code highlightsThis page
DebuggingConsole, Hitbox, Sources, Network, Application, ElementsDevTools debugging walkthrough
Testing & VerificationGameplay testing, integration testing, API error handlingCode runner โ€” iTunes API practice
โ–ถ Play the Game (Embedded Demo)

The game itself is the runtime demo referenced throughout every page.

Mini-Lesson Structure Practice

Each page in this blog is a mini-lesson. Run this to see how the lesson catalog is structured as data โ€” try adding a new page entry.

Lines: 1Characters: 0
Output
Click โ–ถ Run to execute...

โœจ Code Highlights

The rubric requires annotated key code snippets demonstrating OOP, APIs, and collision. Every page in this blog uses the same annotation pattern โ€” a blue left-border callout box under each code block that explains WHY the code is written the way it is, not just what it does.

Annotation pattern โ€” used on every page

This blue callout box is the annotation format used throughout. Each one answers: what non-obvious decision was made here, why was it necessary, and what would break if this were done differently. This is the "Code Highlights" rubric requirement โ€” annotating key snippets with explanations.

Key annotated snippets across the blog

SnippetPageWhy It's Highlighted
super(data, gameEnv) constructor chaining OOP Explains why omitting super() breaks the entire sprite loading chain
AABB collision detection (4 && conditions) Operators Shows how all four edge checks must pass simultaneously for a hit
Backwards for loop with splice() Control Structures Explains why forward iteration misses elements after removal
async/await + try/catch for iTunes API Input/Output Shows the full async I/O pattern: fetch โ†’ parse โ†’ destructure โ†’ use
moveTowardPlayer() normalized vector OOP Explains normalization โ€” why dividing by distance keeps speed constant
takeDamage() invulnerability window OOP Three nested conditions โ€” each prevents a different damage exploit

Code Highlights Practice

Run this to see the backwards-loop annotation in action. Try changing it to a forward loop and see what gets skipped.

Lines: 1Characters: 0
Output
Click โ–ถ Run to execute...

โ–ถ Documentation Practice

Run the code below to see documented classes in action. The JSDoc comments describe each method โ€” try adding your own @param comments or a new documented method.

JSDoc & Code Comments Practice

Modify or extend the documented class below. Notice how JSDoc tags (@param, @returns, @class) describe types and behavior without repeating what the code already says.

Output will appear here...