Decided to use an event system for my pet project. And now I am struggling with that :).
The whole idea is the following:
I have a spaceship with two separate engines. User can control both of them separately.
When user holds down a specific key on the keyboard - the engine starts working - particle system is enabled, the sound is enabled.
InputKeyboard has two events: OnEngineActivated and OnEngineDeactivated. EngineSound and EngineParticle components are subscribed to these events. They are responsible for turning on particles and sound respectively.
After that, I wanted to add a new feature called 'Damaged Engine'. The idea behind this is damaged engine works with breaks that means have to turn off particles and sound during this break, then after break turn on particles and sound again.
Decided to choose interfaces and events for that again I have a controller let's say this is EngineController. It has two states: EngineWorkable and EngineDamaged.
public class EngineController {
private IEngineWork engineRightWork;
private readonly IEngineWork engineDamaged;
private readonly EngineRightCollision collision;
public EngineController(GameObject gameObject, float speed) {
Rigidbody2D rigidbody2D = gameObject.GetComponent<Rigidbody2D>();
engineDamaged = new EngineDamaged(rigidbody2D, speed);
engineRightWork = new EngineWorkable(rigidbody2D, speed);
collision = gameObject.GetComponent<EngineRightCollision>();
}
public void OnEnable() {
collision.OnEngineRightFirstDamaged += OnEngineFirstDamaged;
}
public void OnDisable() {
collision.OnEngineRightFirstDamaged -= OnEngineFirstDamaged;
}
public void FixedUpdate() {
engineRightWork.FixedUpdate();
}
private void OnEngineFirstDamaged() {
engineRightWork = engineDamaged;
}
...
}
There are two implementations for the working engine.
There is a collision detection system with the environment and this collision class produces event OnEngineRightFirstDamaged. That means when this event has happened I switch to another implementation - DamagedEngine. You can see it on the code above.
It works perfectly. But I have a big issue with DamagedEngine implementation. As I said before, I have to turn off particles and sound during the break of working. I have separated implementation for DamagedEngine but How I should solve the problem with turning off particles and sound when DamagedEngine implementation will start pause for the working engine.
The first idea and the last that comes to my mind is to add a new event to DamagedEngine. Let's say
public class EngineDamaged : IEngineWork
{
private readonly float speed;
private readonly Rigidbody2D rigidbody;
private readonly Transform rigidbodyTransform;
public event Action OnEngineDamaged;
.....
}
Basically this event has the same behavior as OnEngineDeactivated from InputKeyboard. And this event is already tested. With new event OnEngineDamaged I should add it to all related classes such as EngineSound, EngineParticle, later maybe something else will appear. Which definitely is not a good idea. But probably I should use an existing one.
Maybe should use a sort of event manager but I am not sure and do not know how to implement it in that case. And would not like to use Singleton for that goal.
Can you guys advise me on how to do this part? Or maybe I should choose another implementation of all processes of the working engine. But which?
Aucun commentaire:
Enregistrer un commentaire