Opponents controller
AI-controlled vehicle that extends the base physics controller with racing-specific behaviors and decision-making capabilities. 🧠
🚀 Core Features
| Feature | Description | Key Properties |
|---|---|---|
| AI Driving | Smart navigation using driving policy | drivingPolicy |
| Race Tracking | Lap counting and finishing logic | currentLap, finished |
| Performance Limits | Speed and acceleration caps | currentMaxSpeedForward |
| Cooldown System | Prevents input spamming | _lastRotation, _accelerationCooldown |
🏗️ Constructor
constructor( sprite: Sprite, startPosition: StartPosition, drivingPolicy: DrivingPolicyBase, nickname: string, traction: number)Parameters:
| Parameter | Type | Description |
|---|---|---|
sprite | Sprite | Visual representation asset 🖼️ |
startPosition | StartPosition | Initial position and angle 🏁 |
drivingPolicy | DrivingPolicyBase | AI decision-making logic 🧠 |
nickname | string | Opponent identifier 🏷️ |
traction | number | Surface grip coefficient (0-1) |
Initial Setup:
- Sets initial position with collision box offset (+30, +15)
- Configures speed limits (200 max speed, 80 acceleration)
- Links driving policy to controller
🔄 Update Loop
override update(deltaTime: number)Behavior Flow:
graph TD A[Update Physics] --> B[Get AI Decision] B --> C{Accelerate?} C -->|Yes| D[Apply Acceleration] B --> E{Rotate?} E -->|Yes| F[Apply Rotation] B --> G{Brake?} G -->|Yes| H[Apply Brake] H --> I[Check Race Completion]Key Actions:
- Updates cooldown timers ⏱️
- Gets driving decision from policy 🧠
- Applies movement commands ↔️
- Checks race completion status 🏁
🏆 Race Completion Logic
Finishing Conditions:
- Completes 3 laps (
currentLap >= 3) ✅ - Sets
finishedflag and records time 🕒 - Triggers results scene when all finish 🎬
if (this.currentLap >= 3) { this.finished = true; this.finishedTime = Scoreboard.instance.currentTime; // ...trigger results scene}⚙️ Technical Details
Cooldown System
| Action | Cooldown | Purpose |
|---|---|---|
| Rotation | 0.04s | Smooth turning 🌀 |
| Acceleration | 0.00s | Instant response ⚡ |
| Braking | 0.02s | Controlled stops 🛑 |
Position Offset
- Adds (+30, +15) to align collision box
- Matches
getCarCornersutility function
🎮 Usage Example
// Create opponentconst opponent = new OpponentController( carSprite, startPosition, new AggressiveDrivingPolicy(), "AI Racer", 0.85);
// Game loopfunction update(deltaTime) { opponent.update(deltaTime);}🛠️ Design Considerations
Inheritance
- Extends
PhysicsBasedControllerfor core physics - Adds AI-specific behaviors on top
Performance
- Cooldowns prevent over-processing
- Simple condition checks
- Efficient position calculations
💡 Customization Tips
-
Adjust Driving Policy:
this.drivingPolicy = new CustomDrivingPolicy(); -
Modify Speed Limits:
this.currentMaxSpeedForward = 250; // Faster AI -
Change Cooldowns:
this._rotationCooldown = 0.06; // Slower turns
🚦 Pro Tip: Use different driving policies to create varied AI difficulty levels!
📊 State Management
| State | Description |
|---|---|
finished | Race completion status |
currentLap | Progress tracking |
_lastRotation | Turn cooldown timer |
_lastAcceleration | Throttle cooldown timer |