Successfully implemented level-based video background switching for the Tetris WebGPU game. Videos now automatically change as players progress through levels, enhancing the gaming experience with dynamic visual feedback.
-
Theme System Enhancement
- Replaced single
backgroundVideoproperty withlevelVideosarray - Each theme (pastel, neon, future) now supports 8 different videos
- Extracted video paths to
DEFAULT_LEVEL_VIDEOSconstant for maintainability
- Replaced single
-
Level Tracking
- Added
currentLevelproperty to View class - Added
currentVideoSrcproperty for reliable video source tracking - Detects level changes in
renderMainScreen()method
- Added
-
Video Management
- Created
updateVideoForLevel()method for handling video switches - Implemented smart video source comparison to avoid unnecessary reloads
- Added fallback support for missing videos (shader-based background)
- Created
-
Code Quality
- Added TypeScript type definitions (ThemeColors, Themes interfaces)
- Removed all @ts-ignore directives for better type safety
- Fixed code review feedback items
| Game Level | Video File | Lines Required |
|---|---|---|
| 0 | bg1.mp4 | 0-9 |
| 1 | bg2.mp4 | 10-19 |
| 2 | bg3.mp4 | 20-29 |
| 3 | bg4.mp4 | 30-39 |
| 4 | bg5.mp4 | 40-49 |
| 5 | bg6.mp4 | 50-59 |
| 6 | bg7.mp4 | 60-69 |
| 7+ | bg8.mp4 | 70+ |
Level calculation: level = Math.floor(lines * 0.1)
- Positioning: Videos are automatically sized and positioned within the Tetris gameboard inner area
- Sizing: Uses
object-fit: containto maintain aspect ratio - Playback: Videos loop continuously and play muted
- Switching: Changes occur seamlessly when level increases
- Fallback: Automatically uses shader-based background if videos are missing
-
src/viewWebGPU.ts (Main implementation)
- Added type definitions
- Modified theme structure
- Implemented video switching logic
- Enhanced level tracking
-
assets/video/README.md (Documentation)
- Updated with level-based video information
- Added video mapping table
- Documented fallback behavior
-
IMPLEMENTATION.md (Technical documentation)
- Detailed technical implementation guide
- Performance considerations
- Browser compatibility information
-
assets/video/create-placeholder-videos.sh (Helper script)
- Script to create test placeholder videos
- Uses ffmpeg to generate colored videos
✅ TypeScript compilation: Successful ✅ Vite build: Successful ✅ Code review: All issues resolved ✅ CodeQL security scan: 0 alerts
To fully test the implementation:
- Place video files (bg1.mp4 - bg8.mp4) in
assets/video/directory - Run
npm run devto start development server - Play the game and clear lines to advance levels
- Verify videos switch automatically at level transitions
The implementation expects 8 video files in assets/video/:
- bg1.mp4
- bg2.mp4
- bg3.mp4
- bg4.mp4
- bg5.mp4
- bg6.mp4
- bg7.mp4
- bg8.mp4
Use the provided create-placeholder-videos.sh script to generate test videos, or replace with actual video content.
- WebGPU: Chrome/Edge 113+, Safari Technology Preview
- HTML5 Video: All modern browsers
- Recommended formats: MP4 (H.264), WebM (VP9)
- Video switching only occurs when level changes (not on every frame)
- Videos are preloaded but only one plays at a time
- Fallback to shader background is automatic and seamless
- Video elements are positioned behind the WebGPU canvas (z-index: -1)
✅ CodeQL analysis completed with 0 security alerts ✅ No unsafe practices introduced ✅ All user inputs properly validated
Potential improvements for future consideration:
- Theme-specific video sets (different videos per theme)
- Custom video upload functionality
- Video preloading for smoother transitions
- Audio support (currently muted)
- Video effects/filters based on game events
The level-based video background feature has been successfully implemented with:
- Clean, maintainable code
- Proper TypeScript typing
- Comprehensive documentation
- Fallback mechanisms
- Zero security issues
The feature is ready for use once video files are placed in the assets/video/ directory.