Overview
MyPacman extends MyCharacter to create the player’s character with distinctive mouth-opening animation, death animation, and input buffering for smooth direction changes.
Inheritance: THREE.Object3D → MyCharacter → MyPacman
Constructor
Initial position of Pac-Man in grid coordinates
Radius of the Pac-Man sphere
Initial direction with
x and y properties (values: -1, 0, or 1)Properties
status
"alive"
dirBuffer
validRotationX
{x: -1, y: 1} means can go left or right).
validRotationY
{x: -1, y: 1} means can go up or down).
sphereGeom
circleGeom
material
MyMaterial.YELLOW).
upSphere
upCircle
downSphere
downCircle
upHalf
downHalf
moveAnimation
Methods
setNeighbors(neighbors)
Sets the valid movement directions at Pac-Man’s current grid position. Called by the game logic when Pac-Man enters a new cell. Parameters:neighbors(Array<number>) - Array of 4 values:[left, right, up, down]where each is -1, 0, or 1
rotateBuffer(dir)
Buffers a direction change request that will be applied when valid. Parameters:dir(THREE.Vector2) - Desired direction
checkRotation()
Checks if the buffered direction is valid and applies it if possible. Called every frame byupdate().
die()
Initiates the death animation sequence. Stops mouth animation and transitions status to “dying”.startDeathAnimation()
Plays the death animation by gradually closing Pac-Man’s mouth from 0° to 180° over 1.5 seconds.crearNuevo(size, rot)
Updates Pac-Man’s geometry during the death animation to progressively close the mouth. Parameters:size(number) - Sphere radiusrot(number) - Current rotation angle in radians (0 to π)
dispose()
Cleans up geometries and materials to free memory.update()
Updates Pac-Man state each frame: moves, checks for valid direction changes, and updates animations.Usage Example
Animation Details
Mouth Animation
- Duration: 200ms per cycle
- Range: 0° to 45° (π/4 radians)
- Type: Yoyo (ping-pong) with infinite repeat
- Effect: Creates chomping motion
Death Animation
- Duration: 1500ms (1.5 seconds)
- Range: 0° to ~180° (3.12414 radians)
- Type: One-time, smooth closing
- Completion: Status changes to “dead”
Direction Buffering
The direction buffering system allows players to input direction changes slightly before reaching an intersection:- Player presses a direction key →
rotateBuffer()stores the input - Each frame,
checkRotation()verifies if the buffered direction is valid - If valid and not opposite to current direction, Pac-Man snaps to grid and rotates
- Buffer is cleared after successful rotation
Notes
- Pac-Man is rendered using two hemispheres and two circles to create the iconic shape
- The mouth opens and closes continuously during normal movement
- Valid directions must be set each frame based on maze geometry
- The character automatically snaps to grid positions when changing direction
- Status should be checked before applying game logic to handle death state