Skip to content

Commit bdb34ba

Browse files
committed
Reduce allocations in RoadCars
1 parent 31bd46b commit bdb34ba

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

Source/RunActivity/Viewer3D/RoadCars.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -255,13 +255,13 @@ public void Update(ElapsedTime elapsedTime)
255255
}
256256

257257
// Calculate all the distances to items we need to stop at (level crossings, other cars).
258-
var stopDistances = new List<float>();
258+
var stopDistance = float.MaxValue;
259259
for (var crossing = NextCrossingIndex; crossing < crossings.Count; crossing++)
260260
{
261261
if (crossings[crossing].Item.CrossingGroup != null && crossings[crossing].Item.CrossingGroup.HasTrain)
262262
{
263263
// TODO: Stopping distance for level crossings!
264-
stopDistances.Add(crossings[crossing].Distance - RoadCarSpawner.StopDistance);
264+
stopDistance = Math.Min(stopDistance, crossings[crossing].Distance - RoadCarSpawner.StopDistance);
265265
break;
266266
}
267267
}
@@ -271,13 +271,13 @@ public void Update(ElapsedTime elapsedTime)
271271
if (spawnerIndex > 0)
272272
{
273273
if (!cars[spawnerIndex - 1].CarriesCamera)
274-
stopDistances.Add(cars[spawnerIndex - 1].Travelled - cars[spawnerIndex - 1].Length / 2);
274+
stopDistance = Math.Min(stopDistance, cars[spawnerIndex - 1].Travelled - cars[spawnerIndex - 1].Length / 2);
275275
else
276-
stopDistances.Add(cars[spawnerIndex - 1].Travelled - cars[spawnerIndex - 1].Length * 0.65f - 4 - cars[spawnerIndex - 1].Speed * 0.5f);
277-
}
276+
stopDistance = Math.Min(stopDistance, cars[spawnerIndex - 1].Travelled - cars[spawnerIndex - 1].Length * 0.65f - 4 - cars[spawnerIndex - 1].Speed * 0.5f);
277+
}
278278

279279
// Calculate whether we're too close to the minimum stopping distance (and need to slow down) or going too slowly (and need to speed up).
280-
var stopDistance = stopDistances.Count > 0 ? stopDistances.Min() - Travelled - Length / 2 : float.MaxValue;
280+
stopDistance = stopDistance - Travelled - Length / 2;
281281
var slowingDistance = BrakingFactor * Length;
282282
if (stopDistance < slowingDistance)
283283
Speed = SpeedMax * (float)Math.Sin((Math.PI / 2) * (stopDistance / slowingDistance));

0 commit comments

Comments
 (0)