Thy classes should do one thing vs. this example

In a HighCohesion example TakeHit function do a lot of things. So it might be understood incorrectly. Maybe it’s good way to show it would be to make all three functions private and public TakeHit will execute functions instead of using components.
For this example it make no sense but in real code TakeDamage function checks if player is still alive, audio function checks if clip is exists and so on.

You’re right that a TakeHit/TakeDamage method has to do a couple of logical checks, including whether the character is alive at both the beginning and the end of the method…

For your audio effect, however, I would probably use an event to trigger a sound effect, which would be in a separate component… something like

public event System.Action OnDamageTaken;

and then in the TakeHit method, call

OnDamageTaken?.Invoke();

Then your sound effect component would subscribe to this event.
This keeps the responsibility for maintaining Health in one class and firing a sound effect in another.

Privacy & Terms