Overview
MyMaze extends THREE.Object3D and generates procedurally created Pac-Man mazes using a unique Tetris-piece-based algorithm. It manages maze tiles, collectibles, teleports, and provides collision detection and pathfinding utilities.
Constructor
The size of each maze tile in world units (typically
MyConstant.BOX_SIZE)Properties
Total count of collectible dots and pills remaining in the maze
Procedurally generated shader material for maze walls with random colors and patterns
2D array representing the maze structure:
0= Wall1= Empty space2= Dot3= Power pill4= Teleport
Array of positions for teleport tiles (always come in pairs)
Array of all walkable positions (used for random ghost targeting)
Methods
tetrisGenerator()
Generates a 10x5 Tetris-style pattern using procedural piece placement.Array<Array<[number, number]>> - 10x5 grid where each cell contains [up, right] indicating walls
Details:
- Uses Tetris-like pieces from
MyPiecedefinitions - Ensures ghost spawn area remains clear (rows 3-4, columns 0-1)
- Randomly selects valid pieces that fit available space
- Returns pattern suitable for 3x3 expansion
tetris3x3Generator()
Expands the Tetris pattern from 10x5 to 28x15 by scaling each cell to 3x3.Array<Array<number>> - 28x15 grid of 0s and 1s (walls and spaces)
Details:
- Calls
tetrisGenerator()and expands each cell - Clears the ghost spawn box area
- Adjusts dimensions to exactly 28x15 for symmetry
mazeGenerator()
Generates the complete 31x28 maze with symmetry, teleports, and collectibles.Array<Array<number>> - Complete 31x28 maze data array
Details:
- Generates half maze using
tetris3x3Generator() - Mirrors horizontally for symmetry
- Adds border walls
- Places teleport portals on edges
- Fills walkable spaces with dots (value 2)
- Adds 4 power pills at strategic corners
- Keeps center area clear for ghost spawn
pieceGenerator(row, col, map)
Selects a valid Tetris piece for a specific position in the pattern.Current row in the 10x5 Tetris grid
Current column in the 10x5 Tetris grid
Availability map tracking which cells are already occupied
Array<[number, number, [number, number]]> - Selected piece with relative positions
getRandomValidPosition()
Returns a random walkable position from the maze.THREE.Vector2 - Random valid position in maze coordinates
Usage: Used by ghosts for random wandering in scared mode
getDotNumber()
Returns the current count of remaining collectibles.number - Total dots and pills remaining
Usage: Used to detect level completion (when returns 0)
clearColor(material)
Resets all tiles with a specific material back to invisible.The material to clear from maze tiles
MyConstant.SHOW_PATH is enabled
getOtherTeleport(pos, dir)
Finds the exit teleport position when entering a teleport tile.Current character position in maze coordinates
Movement direction vector
THREE.Vector2 - Position of the paired teleport exit
collisionType(pos, dir)
Returns the tile type in the direction of movement.Current position in maze coordinates
Direction vector to check
number - Tile type (0-4) in the specified direction
getTileType(pos)
Returns the type of tile at a specific position.Position in maze coordinates to check
number - Tile type: 0=wall, 1=empty, 2=dot, 3=pill, 4=teleport
getNeighbors(pos)
Returns walkability information for all four cardinal directions.Position in maze coordinates
Array<number> - Four values [right, left, down, up] where:
1or-1= walkable in that direction0= wall (blocked)
removeDot(pos)
Removes a collectible from the maze and decrements the dot counter.Position of the dot/pill to remove
- Removes the dot/pill visual from the tile
- Changes tile type to empty (1)
- Decrements
dotNumber
checkCollision(hitbox, pos, dir)
Checks if a character’s hitbox would collide with walls in the movement direction.Character’s collision box
Current position in maze coordinates
Intended movement direction
boolean - true if collision detected, false if path is clear
Details:
- Checks 3 tiles in movement direction (handles width of characters)
- Only checks tiles with
has_hitboxproperty - Uses Three.js Box3 intersection tests
createShaderMaterial()
Generates a unique procedural shader material for maze walls.THREE.ShaderMaterial - Custom shader with random colors and noise patterns
Details:
- Randomizes hue for base color
- Generates random noise parameters
- Creates border effect with configurable width and blur
- Uses vertex and fragment shaders from DOM
update()
Updates animated maze elements each frame.- Updates teleport portal animations
- Called every frame from scene update loop
dispose()
Cleans up all maze tile resources and geometries.- Calls
dispose()on all child tiles - Releases geometry and material memory
- Should be called before creating a new maze
Example Usage
Maze Generation Algorithm
The maze generation uses a unique three-stage process:- Tetris Stage (10x5): Places Tetris-like pieces randomly while respecting constraints
- 3x3 Expansion (28x15): Expands each 1x1 cell to a 3x3 block for proper scale
- Mirroring & Finishing (31x28): Mirrors horizontally, adds borders, teleports, and collectibles
- Every maze is unique and playable
- Symmetric layout (classic Pac-Man style)
- Proper pathfinding graph for ghost AI
- Guaranteed solvability
Tile Types Reference
| Value | Type | Description |
|---|---|---|
| 0 | Wall | Solid obstacle with collision |
| 1 | Empty | Walkable space (no collectible) |
| 2 | Dot | Small collectible (10 points) |
| 3 | Pill | Power pill (50 points, scares ghosts) |
| 4 | Teleport | Portal to opposite side of maze |