What purpose does creating a separate ScreenShake
and ScreenShakeActions
with all the addedd “complexity” of a singleton serve? In my mind the effect (ScreenShake) and to react to events and trigger it (ScreenShakeAction) can happily live in the same class, I can’t really find a strong argument for the need to separate them.
I know just becuse something works (my code example later) it doesn’t mean it’s “right”, but I was just wondering if there’s any conscious design decision I might have missed in the video why this implementation was done using thesingleton pattern and separate classes?
Wouldn’t it be easier, simpler and more readable to do something like this? Any pitfalls, downsides to this implementation?
using UnityEngine;
public class ScreenShake : MonoBehaviour {
private CinemachineImpulseSource cinemachineImpulseSource;
private void Awake() {
cinemachineImpulseSource = GetComponent<CinemachineImpulseSource>();
}
private void Start() {
ShootAction.OnAnyShoot += ShootAction_OnAnyShoot;
}
private void ShootAction_OnAnyShoot(object sender, ShootAction.OnShootEventArgs e) {
Shake(5f);
}
public void Shake(float intensity = 1f) {
cinemachineImpulseSource.GenerateImpulse(intensity);
}
}