Skip to content

Latest commit

 

History

History
242 lines (184 loc) · 6.42 KB

File metadata and controls

242 lines (184 loc) · 6.42 KB

Modular Level System Implementation

Table of Contents

Overview

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.

Implementation Details

The modular level system consists of the following components:

  1. GroundFactory: Creates and manages the ground
  2. PlatformFactory: Creates and manages static platforms
  3. MovingPlatformController: Handles moving platform behavior
  4. CollectibleManager: Manages collectibles
  5. LevelCompletionManager: Handles level completion logic
  6. LevelTransitionController: Manages level transitions
  7. LevelLoader: Loads level data and initializes level elements
  8. 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.

Class Structure

GroundFactory

Responsible for creating and managing the ground:

// Key methods
constructor(scene, world, eventSystem);
createGround(config);
removeGround();
getGround();
getBodyToSpriteMap();

PlatformFactory

Responsible for creating and managing static platforms:

// Key methods
constructor(scene, world, eventSystem);
createPlatforms(platformConfigs);
removePlatforms();
getPlatforms();
getBodyToSpriteMap();

MovingPlatformController

Responsible for creating and managing moving platforms:

// Key methods
constructor(scene, world, eventSystem);
createMovingPlatforms(movingPlatformConfigs);
removeMovingPlatforms();
updateMovingPlatforms(delta);
getMovingPlatforms();
getBodyToSpriteMap();

CollectibleManager

Responsible for creating and managing collectibles:

// Key methods
constructor(scene, world, eventSystem);
createCollectibles(collectibleConfigs);
removeCollectibles();
collectItem(collectibleId);
handleCollectibleCollision(bodyHandle);
areAllCollectiblesCollected();
getCollectibles();
getBodyToSpriteMap();

LevelCompletionManager

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();

LevelTransitionController

Responsible for managing level transitions:

// Key methods
constructor(scene, eventSystem);
startTransitionToNextLevel(fromLevelId);
startTransitionToGameOver(fromLevelId);
startTransition(fromLevelId, toLevelId);
handleLevelLoaded(levelId);
isInTransition();
getTransitionState();

LevelLoader

Responsible for loading level data and initializing level elements:

// Key methods
constructor(scene, eventSystem, managers);
loadLevel(levelId);
initializeLevel(levelConfig);
clearLevel();
getCurrentLevelId();
getCurrentLevelConfig();

LevelManager (Modular)

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();

LevelManager (Wrapper)

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);

Integration with Game Scene

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());

How to Use

Loading a Level

// Load a specific level
this.levelManager.loadLevel('level1');

// Load the next level
this.levelManager.nextLevel();

// Reset the current level
this.levelManager.resetLevel();

Accessing Level Elements

// 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();

Updating the Level

// In the update method
update(time, delta) {
    // Update the level manager
    this.levelManager.update(delta);

    // Rest of the update logic
    // ...
}

Next Steps

  1. Update Game Scene: Modify the Game scene to use the new level loading methods.
  2. Create Level Selection UI: Implement a level selection screen using the new level system.
  3. Add More Levels: Create additional levels using the LevelData.js structure.
  4. Implement Collectible Effects: Add special effects and gameplay impacts for collectibles.
  5. Add Level Completion UI: Create a level completion screen with stats and next level button.
  6. 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.