Hi, I want to refactor my game using SOLID principles. I’m trying to learn it, but lots of question comes to my mind.
Right now I’m wondering how one system can read values from other in a decoupled way. How to achieve that in the “best” way.
My game is a Truck Simulator. The truck have a Vehicle class (monovehaviour), and that Vehicle class have other subsystems (plain classes): Engine, Gearbox, Differential, Cockpit, Brake, Battery, OnboardComputer, Clutch, Chassis.
VehicleSpecs are just a scriptableobject that define vehicles data, like Brand, Model, Name, Price…
And every other subsystems also have their fixed specs in their own scriptableobjects: EngineSpecs, GearboxSpecs, and so on…
using UnityEngine;
public class Vehicle : MonoBehaviour
{
[SerializeField] private VehicleSpecs _specs;
private Engine _engine;
private Battery _gearbox;
private Differential _differential;
...
}
Questions:
1) How to make subsystems ask things between them? My Chassis class need to know things about the Engine class.
The approach Im thinking is this:
using UnityEngine;
public class Vehicle : MonoBehaviour
{
...
private void Update()
{
_engine.Run();
_chassis.Run(_engine);
}
}
Is this the Facade pattern? Shoul I send to Chassis all the engine object? or just send the variables that need? _chassis.Run(_engine) vs _chasis.Run(_engine.motorTorque, _engine.brakeTorque).
2) How to make subsystems ask things with other classes outside Vehicle? My Engine class need to know things about the Weather class. Wheater class is a world class that process and define things like the ambient temperature. My engine need to know ambient temperature for simulate heating parts.
The approach Im thinking is this:
Creating a ScriptableObject for store weather data of the world. Weather system have a reference to that scriptableobject and write to it. Engine have a reference to that scriptableobject and read from it.
But in this way, Engine class will have a variable called WeatherData weather. And I don’t pretty sure if engine should have a weather variable inside on it.
The thing Im sure:
I want be able to test the truck in empty scene without the need of a WeatherController on scene.