Game Level: The Maze (GameLevelMaze)

Overview

The Maze is the second game level challenging players to navigate through walls while avoiding Ghost enemies. This level demonstrates complex game mechanics including maze generation, enemy AI, and collision systems.

File Location

_projects/games/castle-game/levels/GameLevelMaze.js

Quick Concept

try/catch error handling runs a block of code and catches exceptions if they occur. It keeps the game from crashing by allowing recovery or fallback behavior when operations fail. Use try/catch around risky setup steps like loading level assets or parsing configuration.

Level Features

  • Maze Layout: 19+ DeathBarrier instances defining walls
  • Enemy AI: Ghost class enemies with pathfinding
  • Navigation Challenge: Player must reach exit without collision
  • Level Progression: Completion leads to fortress level
  • Increasing Difficulty: Ghost behavior adjusts based on player proximity

Configuration

  • Canvas Setup: Dimensions and difficulty parameters
  • Maze Path: Array of wall configurations
  • Enemy Spawning: Ghost placement and behavior settings

Game Objects

  • Player (from previous level)
  • Ghost enemies (multiple instances with AI)
  • Maze walls (DeathBarrier instances)
  • Exit portal (level completion trigger)
  • Collectibles (coins for points)

Key Methods

  • initMaze() - Creates maze wall layout
  • spawnEnemies() - Places Ghost enemies strategically
  • update() - Updates maze state and enemy AI
  • draw() - Renders maze, enemies, and collectibles
  • checkMazeCompletion() - Detects level completion

Technical Implementation

  • Array of configuration objects for maze layout
  • Multiple Ghost instances with independent AI
  • Collision detection with multiple barriers
  • Nested conditionals for complex state logic
  • Try/catch error handling for robustness

Code Example

// Reset grace period when the level starts
DeathBarrier.resetLevelStartTime();

// Ghost configuration (excerpt from GameLevelMaze.js)
const ghostData = {
    id: 'Ghost',
    greeting: false,
    src: path + "/images/projects/castle-game/ghost.png",
    SCALE_FACTOR: 12,
    ANIMATION_RATE: 20,
    INIT_POSITION: { x: 0.8 * width, y: 0.2 * height },
    pixels: { width: 3000, height: 1000 },
    orientation: { rows: 2, columns: 6 },
    hitbox: { widthPercentage: 0.15, heightPercentage: 0.2 },
    followSpeedFactor: 0.4,
    followStopDistance: 12
};

// Example of the level instantiation array (excerpt)
this.classes = [
    { class: GameEnvBackground, data: bgData },
    { class: Player, data: sprite_data_mc },
    { class: Npc, data: mortyData },
    { class: Npc, data: sprite_data_invis },
    { class: Ghost, data: ghostData },
    { class: DeathBarrier, data: dbarrier_1 },
    { class: DeathBarrier, data: dbarrier_2 },
    { class: DeathBarrier, data: dbarrier_3 },
    { class: DeathBarrier, data: dbarrier_4 },
    { class: DeathBarrier, data: dbarrier_5 },
    { class: DeathBarrier, data: dbarrier_6 },
    { class: DeathBarrier, data: dbarrier_7 },
    { class: DeathBarrier, data: dbarrier_8 },
    { class: DeathBarrier, data: dbarrier_9 },
    { class: DeathBarrier, data: dbarrier_10 },
    { class: DeathBarrier, data: dbarrier_11 },
    { class: DeathBarrier, data: dbarrier_12 },
    { class: DeathBarrier, data: dbarrier_13 },
    { class: DeathBarrier, data: dbarrier_14 },
    { class: DeathBarrier, data: dbarrier_15 },
    { class: DeathBarrier, data: dbarrier_16 },
    { class: DeathBarrier, data: dbarrier_17 },
    { class: DeathBarrier, data: dbarrier_18 },
    { class: DeathBarrier, data: dbarrier_19 }
];