Overview
MyScene extends THREE.Scene and serves as the main container for the Pac-Man 3D game. It manages the WebGL renderer, multiple camera views, game state transitions, and user input handling.
Constructor
The DOM selector for the canvas element where the scene will be rendered (e.g., “#WebGL-output”)
Properties
The WebGL renderer instance that handles all scene rendering
Free perspective camera with trackball controls for flexible viewing
Top-down orthographic-style camera positioned directly above the maze
Side view camera for an alternative perspective of the game
Current active camera index (1 = freeCam, 2 = topCam, 3 = sideCam)
Camera controls for the free camera, allowing orbit, zoom, and pan
The game instance containing maze, characters, and game logic
The main menu instance displayed before game starts
Current application state: “MENU” or “PACMAN”
Array of objects that can be selected with the mouse (e.g., menu buttons)
Keyboard input handler for game controls
Methods
createCamera()
Initializes all three camera perspectives with appropriate positions and orientations.- Creates freeCam positioned at (6, 10.5, 35)
- Creates topCam positioned directly above maze center
- Creates sideCam positioned at (30, 2, 2)
- Sets up TrackballControls for the free camera
createLights()
Sets up ambient and spotlight illumination for the scene.- Adds ambient light with color 0xccddee and intensity 0.35
- Adds spotlight at position (0, 35, 100) with intensity 0.5
createRenderer(myCanvas)
Creates and configures the WebGL renderer.DOM selector for the canvas element
THREE.WebGLRenderer
getCamera()
Returns the currently active camera based on thecamera property.
THREE.PerspectiveCamera - The active camera (freeCam, topCam, or sideCam)
setCameraAspect(ratio)
Updates the aspect ratio of the active camera and its projection matrix.The new aspect ratio (width / height)
onWindowResize()
Handles window resize events by updating camera aspect ratio and renderer size.changeCamera(cam)
Switches to a different camera view.Camera index: 1 for freeCam, 2 for topCam, 3 for sideCam
onKeyDown(event)
Handles keyboard input for camera switching and game controls.The keyboard event object
1- Switch to free camera2- Switch to top camera3- Switch to side camera- Other keys are passed to game controls
onMouseMove(event)
Handles mouse movement for object selection and highlighting.The mouse event object
- Uses raycasting to detect mouse hover over pickable objects
- Calls
select()on hovered objects anddeselect()on others
onMouseClick(event)
Handles mouse clicks for menu interaction and game start.The mouse event object
- When clicking menu items, removes menu and starts game
- Changes status to “PACMAN” and switches to top camera
update()
Main animation loop that updates and renders the scene.- Calls
requestAnimationFramefor continuous rendering - Updates camera controls
- Makes title face the camera
- Updates maze and game state
- Renders the scene with the active camera
Example Usage
Camera Controls
The free camera (camera 1) supports:- Rotate: Left mouse drag
- Zoom: Mouse wheel
- Pan: Right mouse drag