MainHub Lessons Game Overview
Let’s Go! Let’s Go! Let’s Go!

Object-Oriented Programming

Click a concept to learn more.

Inheritance Hierarchy

GameObject        ← Level 1: shared position and draw logic
  └── Character   ← Level 2: adds movement and gravity
        ├── Player  ← Level 3: handles user keyboard input
        └── Pirate    ← Level 3: handles hostile logic

Used In Code!

class Pirate extends Character {
    constructor(data, gameEnv) {
        super(data, gameEnv);  // passes setup data to parent class
        this.type = "Pirate";
        this.isHostile = true; // boolean flag
    }

    handleCollision(other, direction) { // 2 parameters
        if (other instanceof Player) {       // condition
            if (this.distanceTo(other) < 50) { // nested condition
                this.reaction("hostile");
            }
        }
    }

    update() {
        super.update(); // calls parent update, then adds wolf behavior
        this.checkProximity();
    }
}

🏴‍☠️ ## Explanation of the Pirate Class


🔷 1. Class Declaration

class Pirate extends Character {
  • Pirate is a new class.
  • It extends Character, meaning it inherits all the properties and methods from the Character class.
  • This is inheritance, one of the core pillars of OOP.

🔷 2. Constructor

constructor(data, gameEnv) {
    super(data, gameEnv);  // passes setup data to parent class
    this.type = "Pirate";
    this.isHostile = true; // boolean flag
}

What happens here:

  • The constructor runs when a new Pirate is created.
  • super(data, gameEnv) calls the parent Character constructor so the Pirate gets:
    • position
    • sprite
    • movement logic
    • collision setup
    • any other shared character features

Then you add Pirate‑specific properties:

  • this.type = "Pirate"
    Helps identify the object in the game.

  • this.isHostile = true
    Marks this character as an enemy.

This is encapsulation — the Pirate stores its own data and behavior.


🔷 3. Collision Handling

handleCollision(other, direction) {
    if (other instanceof Player) {       // condition
        if (this.distanceTo(other) < 50) { // nested condition
            this.reaction("hostile");
        }
    }
}

What this does:

  1. This method runs whenever the Pirate collides with something.
  2. other instanceof Player
    Checks if the Pirate collided with the player.
  3. this.distanceTo(other) < 50
    Makes sure the player is close enough to trigger aggression.
  4. this.reaction("hostile")
    Tells the Pirate to react aggressively — maybe an animation, sound, or attack.

This is polymorphism — the Pirate overrides the parent’s collision behavior with its own.


🔷 4. Update Loop

update() {
    super.update(); // calls parent update, then adds pirate behavior
    this.checkProximity();
}

What this does:

  • super.update()
    Runs the normal character update logic (movement, animation, physics).
  • this.checkProximity()
    Adds Pirate‑specific behavior each frame — likely checking if the player is near.

This is a common OOP pattern:
extend the parent behavior without replacing it.


🧠 In Summary

Feature What It Demonstrates
extends Character Inheritance
super() Using parent class setup
handleCollision() override Polymorphism
Pirate‑specific properties Encapsulation
update() override Custom behavior layered on top of parent logic