The Level System has been refactored into a modular architecture following SOLID object-oriented principles. The monolithic LevelManager class has been broken down into smaller, focused classes, each with a single responsibility. This modular approach improves maintainability, testability, and extensibility of the level system.
The modular level system consists of the following components:
- GroundFactory: Creates and manages the ground
- PlatformFactory: Creates and manages static platforms
- MovingPlatformController: Handles moving platform behavior
- CollectibleManager: Manages collectibles
- LevelCompletionManager: Handles level completion logic
- LevelTransitionController: Manages level transitions
- LevelLoader: Loads level data and initializes level elements
- LevelManager: Main coordinator class (facade pattern)
The original LevelManager class has been updated to serve as a wrapper that maintains backward compatibility while delegating to the new modular implementation.
Responsible for creating and managing the ground:
// Key methods
constructor(scene, world, eventSystem);
createGround(config);
removeGround();
getGround();
getBodyToSpriteMap();Responsible for creating and managing static platforms:
// Key methods
constructor(scene, world, eventSystem);
createPlatforms(platformConfigs);
removePlatforms();
getPlatforms();
getBodyToSpriteMap();Responsible for creating and managing moving platforms:
// Key methods
constructor(scene, world, eventSystem);
createMovingPlatforms(movingPlatformConfigs);
removeMovingPlatforms();
updateMovingPlatforms(delta);
getMovingPlatforms();
getBodyToSpriteMap();Responsible for creating and managing collectibles:
// Key methods
constructor(scene, world, eventSystem);
createCollectibles(collectibleConfigs);
removeCollectibles();
collectItem(collectibleId);
handleCollectibleCollision(bodyHandle);
areAllCollectiblesCollected();
getCollectibles();
getBodyToSpriteMap();Responsible for handling level completion logic:
// Key methods
constructor(scene, world, eventSystem, collectibleManager);
setCurrentLevelId(levelId);
createCompletionTrigger(triggerConfig);
removeCompletionTrigger();
checkLevelCompletion(playerPosition, playerBody);
handleTriggerCollision(bodyHandle, playerPosition);
completeLevel();
getCompletionTrigger();
getBodyToSpriteMap();Responsible for managing level transitions:
// Key methods
constructor(scene, eventSystem);
startTransitionToNextLevel(fromLevelId);
startTransitionToGameOver(fromLevelId);
startTransition(fromLevelId, toLevelId);
handleLevelLoaded(levelId);
isInTransition();
getTransitionState();Responsible for loading level data and initializing level elements:
// Key methods
constructor(scene, eventSystem, managers);
loadLevel(levelId);
initializeLevel(levelConfig);
clearLevel();
getCurrentLevelId();
getCurrentLevelConfig();Serves as a facade for the level system, coordinating the other specialized classes:
// Key methods
constructor(scene, world, eventSystem);
loadLevel(levelId);
nextLevel();
resetLevel();
update(delta);
getBodyToSpriteMap();
getPlatforms();
getGround();
getCurrentLevelId();
getCurrentLevelConfig();Maintains backward compatibility with the original LevelManager while delegating to the new modular implementation:
// Key methods
constructor(scene, world, eventSystem);
createGround(width, height, y);
createPlatforms(platformConfigs);
getPlatforms();
getGround();
getBodyToSpriteMap();
loadLevel(levelId);
nextLevel();
resetLevel();
update(delta);The Game scene should be updated to use the new LevelManager. The existing code should continue to work without changes due to the backward compatibility wrapper, but to take full advantage of the new features, the Game scene should be updated to use the new methods:
// In Game.js create method
this.levelManager = new LevelManager(this, this.physicsManager.getWorld(), this.eventSystem);
// Load the first level
this.levelManager.loadLevel('level1');
// Register level body-sprite mappings with physics manager
this.physicsManager.registerBodySpriteMap(this.levelManager.getBodyToSpriteMap());// Load a specific level
this.levelManager.loadLevel('level1');
// Load the next level
this.levelManager.nextLevel();
// Reset the current level
this.levelManager.resetLevel();// Get all platforms (static and moving)
const platforms = this.levelManager.getPlatforms();
// Get the ground
const ground = this.levelManager.getGround();
// Get the body-to-sprite mapping
const bodyToSpriteMap = this.levelManager.getBodyToSpriteMap();
// Get the current level ID
const currentLevelId = this.levelManager.getCurrentLevelId();
// Get the current level configuration
const levelConfig = this.levelManager.getCurrentLevelConfig();// In the update method
update(time, delta) {
// Update the level manager
this.levelManager.update(delta);
// Rest of the update logic
// ...
}- Update Game Scene: Modify the Game scene to use the new level loading methods.
- Create Level Selection UI: Implement a level selection screen using the new level system.
- Add More Levels: Create additional levels using the LevelData.js structure.
- Implement Collectible Effects: Add special effects and gameplay impacts for collectibles.
- Add Level Completion UI: Create a level completion screen with stats and next level button.
- Implement Level Persistence: Save completed levels and player progress.
The modular level system provides a solid foundation for these future enhancements, making them easier to implement and maintain.