Game Development Reference
In-Depth Information
ByVal myIndex As Integer, ByVal theRoad As Road) As Vehicle
Dim i As Integer
Dim OtherGuy As Vehicle
For i = myIndex - 1 To 1 Step -1
'Behind me in the lane we are checking.
OtherGuy = CType(theRoad.ToyBox(i), Vehicle)
If OtherGuy.Lane = desiredLane Then
Return OtherGuy
End If
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.
Public Sub Think(ByVal myIndex As Integer, ByVal theRoad As Road)
'Find the best lane:
'Which lane is fastest for me?
'Look from right to left.
Dim newlane As Integer = myLane - 1
'No lane is this bad:
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
'Color based on speed changes.
If currentV = newspeed Then Me.Body.BackColor = Color.White
Search Nedrilad ::

Custom Search