public WindscreenWiper(Windscreen windscreen, WiperPosition restPosition, WiperPosition holdPosition, double wipeSpeed, double holdTime) { RestPosition = restPosition; HoldPosition = holdPosition; MovementSpeed = wipeSpeed / 100.0; HoldTime = holdTime; Windscreen = windscreen; CurrentPosition = restPosition == WiperPosition.Left ? 100 : 0; CurrentSpeed = WiperSpeed.Off; }
/// <summary>Updates the windscreen wipers</summary> /// <param name="TimeElapsed">The time elapsed since the last call to this method</param> internal void Update(double TimeElapsed) { wiperTimer += TimeElapsed; if (CurrentSpeed == WiperSpeed.Off) { if (RestPosition == WiperPosition.Left && CurrentPosition == 100) { wiperTimer = 0; return; } if (RestPosition == WiperPosition.Right && CurrentPosition == 0) { wiperTimer = 0; return; } } //Move the wiper if (wiperTimer > MovementSpeed) { wiperTimer = 0; switch (currentDirection) { case WiperPosition.Left: if (CurrentPosition > 0) { CurrentPosition--; } break; case WiperPosition.Right: if (CurrentPosition < 100) { CurrentPosition++; } break; } soundTriggered = false; } switch (CurrentPosition) { //When the wiper is at either end of the travel, determine what to do next case 0: if (CurrentSpeed > 0) { if (HoldPosition == WiperPosition.Right && CurrentSpeed != WiperSpeed.Fast) { holdTimer += TimeElapsed; if (holdTimer > HoldTime) { holdTimer = 0; currentDirection = WiperPosition.Right; } } else { currentDirection = WiperPosition.Right; } } else { if (RestPosition == WiperPosition.Left) { if (HoldPosition == WiperPosition.Right) { holdTimer += TimeElapsed; if (holdTimer > HoldTime) { holdTimer = 0; currentDirection = WiperPosition.Right; } } else { currentDirection = WiperPosition.Right; } } } break; case 100: if (CurrentSpeed > 0) { if (HoldPosition == WiperPosition.Left && CurrentSpeed != WiperSpeed.Fast) { holdTimer += TimeElapsed; if (holdTimer > HoldTime) { holdTimer = 0; currentDirection = WiperPosition.Left; } } else { currentDirection = WiperPosition.Left; } } else { if (RestPosition == WiperPosition.Right) { if (HoldPosition == WiperPosition.Left) { holdTimer += TimeElapsed; if (holdTimer > HoldTime) { holdTimer = 0; currentDirection = WiperPosition.Left; } } else { currentDirection = WiperPosition.Left; } } } break; //Wiper has started moving, determine which sound effect to play case 1: if (currentDirection == WiperPosition.Right) { if (Windscreen.currentDrops / (double)Windscreen.RainDrops.Length > 0.8) { if (soundTriggered == false) { WetWipeSound.Play(Windscreen.Car, false); soundTriggered = true; } } else { if (soundTriggered == false) { DryWipeSound.Play(Windscreen.Car, false); soundTriggered = true; } } } break; case 99: if (currentDirection == WiperPosition.Left) { if (Windscreen.currentDrops / (double)Windscreen.RainDrops.Length > 0.8) { if (soundTriggered == false) { WetWipeSound.Play(Windscreen.Car, false); soundTriggered = true; } } else { if (soundTriggered == false) { DryWipeSound.Play(Windscreen.Car, false); soundTriggered = true; } } } break; } int dropToRemove = Windscreen.RainDrops.Length - 1 - Math.Min(Windscreen.RainDrops.Length - 1, (int)(CurrentPosition / (100.0 / Windscreen.RainDrops.Length))); if (Windscreen.RainDrops[dropToRemove].Visible) { Windscreen.RainDrops[dropToRemove].Visible = false; Windscreen.RainDrops[dropToRemove].IsSnowFlake = false; Windscreen.RainDrops[dropToRemove].RemainingLife = 0.5 * TrainManagerBase.RandomNumberGenerator.NextDouble() * Windscreen.DropLife; Windscreen.currentDrops--; } }