// Searches for an ConstraintVehicleHandler instance in the Simulation.ForceEffects. private static ConstraintVehicleHandler GetHandler(Simulation simulation) { foreach (var forceEffect in simulation.ForceEffects) { ConstraintVehicleHandler handler = forceEffect as ConstraintVehicleHandler; if (handler != null) { return(handler); } } return(null); }
// Creates or gets the ConstraintVehicleHandler instance and adds a vehicle. internal static void Add(ConstraintVehicle vehicle) { var simulation = vehicle.Simulation; var handler = GetHandler(simulation); if (handler == null) { // The first vehicle: Add a new instance of this class to the simulation. handler = new ConstraintVehicleHandler(); simulation.ForceEffects.Add(handler); simulation.SubTimeStepFinished += handler.OnSubTimeStepFinished; } handler._vehicles.Add(vehicle); }
// Removes a vehicle. When the last vehicle is removed, the ConstraintVehicleHandler // instance is removed too. internal static void Remove(ConstraintVehicle vehicle) { Simulation simulation = vehicle.Simulation; ConstraintVehicleHandler handler = GetHandler(simulation); if (handler != null) { handler._vehicles.Remove(vehicle); // The last vehicle was removed. if (handler._vehicles.Count == 0) { simulation.ForceEffects.Remove(handler); simulation.SubTimeStepFinished -= handler.OnSubTimeStepFinished; } } }