Overview
TheMyControls class handles keyboard input for controlling Pac-Man’s movement and accessing debug features. It maps key codes to game actions and provides a centralized manager for processing input events.
Constructor
MyControls()
Initializes the controls with default key mappings.
Key Mappings
The class defines the following key code mappings:Movement Controls
Left arrow key - Move Pac-Man left.
Up arrow key - Move Pac-Man up.
Right arrow key - Move Pac-Man right.
Down arrow key - Move Pac-Man down.
Camera Controls
Q key - Rotate camera left.
E key - Rotate camera right.
Action Keys
Space key - Trigger respawn (debug feature).
Enter key - Display renderer memory info in console (debug feature).
Methods
manager(keyCode, game, renderer)
Processes keyboard input and triggers appropriate game actions.
The key code of the pressed key (from keyboard event).
The game instance to control.
The THREE.js renderer instance (used for debug info).
Behavior
- Movement Keys: Converts arrow key input into a direction vector and buffers the rotation for Pac-Man
- Space Key: Triggers game respawn
- Enter Key: Logs renderer memory information to console
Usage Examples
Initialization
Processing Keyboard Events
Custom Key Mappings
Direction Vector Mapping
The controls convert key presses into 2D direction vectors:- Left Arrow:
dir = (-1, 0)- Move in negative X direction - Right Arrow:
dir = (1, 0)- Move in positive X direction - Up Arrow:
dir = (0, -1)- Move in negative Y direction - Down Arrow:
dir = (0, 1)- Move in positive Y direction
rotateBuffer() method to queue the next movement direction.
Control Flow
Key Code Reference
| Key | Code | Action |
|---|---|---|
| Q | 81 | Rotate camera left |
| E | 69 | Rotate camera right |
| ← | 37 | Move Pac-Man left |
| ↑ | 38 | Move Pac-Man up |
| → | 39 | Move Pac-Man right |
| ↓ | 40 | Move Pac-Man down |
| Space | 32 | Respawn (debug) |
| Enter | 13 | Show renderer info (debug) |
Source Location
File:~/workspace/source/pacman/src/MyControls.js:1
Related Classes
- MyScene - Initializes controls and handles keyboard events
- MyGame - Receives control commands for game actions
- MyCharacter - Processes buffered rotation from movement inputs