Game Development Reference
In-Depth Information
Dim i As Integer
Dim OtherGuy As Vehicle
For i = myIndex - 1 To 1 Step -1
'Behind me in the lane we are checking.
If OtherGuy.Lane = desiredLane Then
Return OtherGuy
End If
Next
Return Nothing
End Function
Now that the AI can get answers about the cars around it and the capabilities of
the vehicle itself, it is time for the AI to do some thinking. The AI has two parts.
The first part picks the best speed from the available choices. It depends on the
part that computes the best speed in a given lane.
'The next two are where the AI lives.
'Find the best lane:
'Which lane is fastest for me?
'Look from right to left.
Dim newlane As Integer = myLane - 1
Dim newspeed As Integer = -100
Dim i As Integer
'Go through up to three lanes.
For i = myLane - 1 To myLane + 1
'Don't let cars below lane 1.
If i > 0 Then
Dim otherspeed As Integer = SpeedInLane(i, myIndex, theRoad)
If otherspeed > newspeed Then
newspeed = otherspeed
newlane = i
End If
End If
Next
'Color based on speed changes.
If currentV = newspeed Then Me.Body.BackColor = Color.White