4

I'm trying to create a simple simulator in C#, which should simulate a very simple car engine. The simulator can either be on or off, and it will, for instance, have the following inputs (there will possibly come more in the future).

Use case:

In this example, the car engine will have the follow input parameter:

  • Thrust produced (0-100%)
  • Temperature (-50C to 300C)
  • NOS (enabled/disabled)
  • Driving mode (economy / normal / speed)
  • On/Off

These input parameter will alter the following two output parameter:

  • Thrust produced (0-150%)
  • Temperature (-50C to 300C)

If for instance the driving mode is set to economy, the maximum thrust which it can output will be e.g. 70%. NOS will also alternate the output somehow. The simulator will have an event handler, were the computed values for thrust and temperature will be announced.

Question:

I'm asking for some input on how this simulator easiest can be designed, so it can be extended with new input parameter later on. Any good design pattern you would recommend or articles? Feel free to ask questions or show your best solution :)

Thanks in advance

SOK
  • 515
  • 6
  • 21

2 Answers2

1

Given the car engine simulator is bunch of input and output parameters which depends on change to other parameters I'd recommend to use Observer pattern,

For example ThrustProduced parameter would be an observer of DrivingMode, and when the DrivingMode parameter changed, it has to notify it's observers including ThrustProduced, which then recalculates the thrust and limit it to 70% and then fire the event to notify that the thrust has changed.

Low Flying Pelican
  • 5,974
  • 1
  • 32
  • 43
1

You should implement several patterns which will make your life easier. If you are familiar with Unity game engine use its concepts. I suggest you to use this game engine if you aiming to build games.

First of all, use Game Loop and Update patterns.

In simulation your need several FixedUpdate events for calculation in one Update event for rendering frame. FixedUpdate should not depend on current FPS.

Then you can implement some kind of Bus like in real cars CAN-BUS is used. It can be done with Observer/Observable or Subscriber/Publisher. You can learn about difference here Difference between Observer, Pub/Sub, and Data Binding but I suggest you to use Subscriber/Publisher as it is like message queue.

To implement different modes (economy, racing) you probably need Strategy pattern.

Also Component pattern will help to make things by analogy of devices and sensors in real car. Each component object reads messages from bus queue and sends events to bus. Some components like ECU or EMS can use current strategy to send commands for injectors.

State pattern will help you to maintain different states of components. For example, engine cycle state.

Your question is very broad, but I suggest you to choose some real-life concept and make code design by its analogy.

Please refer to this great book to read more about mentioned patterns: http://gameprogrammingpatterns.com/contents.html

Community
  • 1
  • 1
Dzianis Yafimau
  • 2,034
  • 1
  • 27
  • 38