This document describes the modular structure after breaking up the large viewWebGPU.ts and game.ts files.
src/
├── webgpu/ # WebGPU rendering subsystems
│ ├── shaders.ts # Shader definitions (519 lines)
│ ├── geometry.ts # 3D geometry generators (87 lines)
│ ├── themes.ts # Color themes (83 lines)
│ ├── particles.ts # Particle system (104 lines)
│ └── effects.ts # Visual effects (166 lines)
├── game/ # Game logic subsystems
│ ├── pieces.ts # Tetromino pieces (106 lines)
│ ├── rotation.ts # SRS rotation system (65 lines)
│ ├── collision.ts # Collision detection (52 lines)
│ └── scoring.ts # Scoring system (55 lines)
├── viewWebGPU.ts # Main renderer (1251 lines, down from 2167)
├── game.ts # Main game logic (437 lines, down from 622)
├── controller.ts # Input handling
└── sound.ts # Sound effects
Exports all WGSL shader code:
PostProcessShaders()- Post-processing effects (chromatic aberration, shockwave)ParticleShaders()- Particle rendering shadersGridShader()- Grid overlay shaderBackgroundShaders()- Animated background shaderShaders()- Main block rendering shader with lighting
Exports geometry data generators:
CubeData()- Cube mesh (positions, normals, UVs)FullScreenQuadData()- Full-screen quad for post-processingGridData()- Grid line positions
Exports theme system:
ThemeColorsinterface - Theme color definitionThemesinterface - Collection of themesthemes- Theme configurations (pastel, neon, future)
Exports particle system:
Particleinterface - Single particle definitionParticleSystemclass - Manages particle lifecycleemitParticles()- Emit particles with random velocitiesemitParticlesRadial()- Emit particles in specific directionupdateParticles()- Update all particles (physics, lifetime)getParticleData()- Generate GPU buffer data
Exports visual effects system:
VisualEffectsclass - Manages screen effects and video backgroundsupdateEffects()- Update effect timerstriggerFlash()- Flash effect for line clearstriggerShake()- Screen shake effecttriggerShockwave()- Shockwave ripple effectupdateVideoForLevel()- Switch background videogetClearColors()- Get current screen clear colorgetShakeOffset()- Get camera shake offset
Exports piece management:
Pieceinterface - Tetromino piece definitionPieceGeneratorclass - Creates and manages piecescreatePieceByType()- Create specific piece typecreatePiece()- Create next piece from baggenerateBag()- Shuffle piece bag (7-bag system)resetPiecePosition()- Reset piece to spawn position
Exports SRS rotation system:
SRS_KICKS_JLSTZ- Wall kick data for J, L, S, T, Z piecesSRS_KICKS_I- Wall kick data for I piecerotatePieceBlocks()- Rotate piece blocks matrixgetWallKicks()- Get wall kick offsets for rotation
Exports collision detection:
CollisionDetectorclass - Handles collision checkshasCollision()- Check if piece collidesgetGhostY()- Calculate ghost piece Y positionupdatePlayfield()- Update playfield reference
Exports scoring system:
ScoringSystemclass - Manages score and line clearingclearLines()- Clear completed linesupdateScore()- Update score based on lines clearedlevelproperty - Current level (calculated from lines)reset()- Reset score and lines
Uses subsystems:
particleSystem: ParticleSystem- Particle effectsvisualEffects: VisualEffects- Screen effects and video- Imports
themesfromwebgpu/themes.ts - Imports shader functions from
webgpu/shaders.ts - Imports geometry functions from
webgpu/geometry.ts
Uses subsystems:
pieceGenerator: PieceGenerator- Piece creationcollisionDetector: CollisionDetector- Collision checksscoringSystem: ScoringSystem- Score and line management- Imports rotation functions from
game/rotation.ts
- Separation of Concerns: Each module has a single, clear responsibility
- Easier Testing: Modules can be tested independently
- Better Maintainability: Smaller files are easier to understand
- Reusability: Subsystems can be reused in other contexts
- Expandability: New features can be added without bloating existing files
- Type Safety: Interfaces clearly define contracts between modules
With this modular structure, it's now easier to:
- Add new visual effects (extend
VisualEffectsclass) - Add new particle effects (extend
ParticleSystemclass) - Add new piece types (extend
PieceGeneratorclass) - Add new themes (add to
themesobject) - Add new shaders (add to
webgpu/shaders.ts) - Implement different rotation systems (create new rotation module)
- Add different scoring systems (create new scoring module)