Central timing and progression system that manages race statistics, lap tracking, and player/opponent results with millisecond precision.
🏁 Core Responsibilities
| Function | Description |
|---|
| Lap Timing | Tracks split/lap/finish times |
| Checkpoint Validation | Verifies proper track navigation |
| Race State Management | Handles start/finish logic |
| Results Aggregation | Compiles final rankings |
| UI Coordination | Updates race progress displays |
📊 Properties
Timing Data
| Property | Type | Description |
|---|
currentPlayerStartTime | Date | null | Race start timestamp |
lapTimeList | (Date | null)[] | Array of lap timestamps (max 3) |
currentLapTime | number | Current lap duration (ms) |
Progress Tracking
| Property | Type | Range | Description |
|---|
_currentLap | number | 1-3 | Active lap number |
currentCheckpoint | number | 0-N | Next checkpoint ID |
clearCheckpointsCounter | number | ≥0 | Valid checkpoints passed |
Results
| Property | Type | Description |
|---|
playerResults | Array<{nickname: string, time: number}> | Final standings |
⏱️ Timing Methods
currentTime
get currentTime(): number {
return this.currentPlayerStartTime
? new Date().getTime() - this.currentPlayerStartTime.getTime()
: 0;
}
Lap Management
| Method | Returns | Description |
|---|
resetCurrentTime() | void | Clears all timing data |
currentLap | number | Gets/sets current lap |
currentLapTime | number | Current lap duration |
🎮 Core Game Loop
update(gameScene: GameScene)
graph TD
A[Check Position] --> B{Checkpoint Reached?}
B -->|Yes| C[Advance Checkpoint]
C --> D{Lap Complete?}
D -->|Yes| E[Increment Lap]
E --> F[Play Lap Sound]
D -->|No| G[Continue Tracking]
B -->|No| G
F --> H{Race Complete?}
H -->|Yes| I[Record Results]
I --> J[Transition to Results]
Key Behaviors:
- Checkpoint validation radius: 150 units
- Lap transition sound:
audioLap
- Race completion sound:
audioFinish
- Requires all opponents to finish before showing results
🏆 Results Management
Final Ranking Logic
- Stores player time in
playerResults
- Sorts entries by fastest time
- Triggers podium animation
- Unlocks achievements if applicable
Data Structure:
interface RaceResult {
nickname: string; // Player/opponent name
time: number; // Milliseconds
vehicle?: string; // Optional vehicle type
}
🔊 Audio Feedback
| Event | Sound | Trigger Condition |
|---|
| Lap Complete | audioLap | currentCheckpoint === 0 |
| Race Finish | audioFinish | _currentLap > totalLaps |
| New Best Lap | audioBestLap | If lap time < stored best |
🛠️ Usage Example
// Initialize
Scoreboard.instance.resetCurrentTime();
// Game loop
function updateRace() {
Scoreboard.instance.update(currentScene);
// Display lap time
hud.setLapTime(
Scoreboard.instance.currentLap,
formatTime(Scoreboard.instance.currentLapTime)
);
}
// Finish handling
if (Scoreboard.instance.currentLap > 3) {
showPodiumAnimation();
}
🧠 Design Considerations
Singleton Pattern
- Ensures global race state access
- Prevents timing conflicts
- Simplifies UI coordination
Checkpoint System
- Prevents lap skipping
- Validates proper track navigation
- Enables position tracking
- Lightweight calculations
- Event-driven audio triggers
- Minimal object allocation