Code Comments ยท Mini-Lesson Documentation ยท Code Highlights
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.
/**
* 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;
}
}
/**
* 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;
}
/** */ 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 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);
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.
| Page | Concept Taught | Interactive Element |
|---|---|---|
| Object-Oriented Programming | Class hierarchy, inheritance, constructor chaining, method overriding | Code runner โ build your own class |
| Control Structures | for/forEach loops, conditionals, nested conditions | Annotated live game code |
| Data Types | Numbers, Strings, Booleans, Arrays, Objects | Type reference table with game examples |
| Operators | Math, string, boolean expressions | AABB collision walkthrough |
| Input/Output | Keyboard events, Canvas API, API calls, async/await, JSON | Live iTunes API demo โ plays real audio |
| Documentation | JSDoc, mini-lesson structure, code highlights | This page |
| Debugging | Console, Hitbox, Sources, Network, Application, Elements | DevTools debugging walkthrough |
| Testing & Verification | Gameplay testing, integration testing, API error handling | Code runner โ iTunes API practice |
The game itself is the runtime demo referenced throughout every page.
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.
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.
| Snippet | Page | Why 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 |
Run this to see the backwards-loop annotation in action. Try changing it to a forward loop and see what gets skipped.
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.
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.