diff --git a/Assets/Scripts/Events.meta b/Assets/Scripts/Events.meta new file mode 100644 index 00000000..65c784a8 --- /dev/null +++ b/Assets/Scripts/Events.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 4209025db0a88254a977c96fde6fab7b +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Events/EventController.cs b/Assets/Scripts/Events/EventController.cs new file mode 100644 index 00000000..1de5f89a --- /dev/null +++ b/Assets/Scripts/Events/EventController.cs @@ -0,0 +1,11 @@ +using System; + +public class EventController +{ + public Action baseEvent; + + public void AddListener(Action listener) => baseEvent += listener; + public void RemoveListener(Action listener) => baseEvent -= listener; + + public void InvokeEvent() => baseEvent?.Invoke(); +} diff --git a/Assets/Scripts/Events/EventController.cs.meta b/Assets/Scripts/Events/EventController.cs.meta new file mode 100644 index 00000000..090ac01d --- /dev/null +++ b/Assets/Scripts/Events/EventController.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: bae5943617aeb9045a3e8821b6ae2805 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Interactables/LightSwitchView.cs b/Assets/Scripts/Interactables/LightSwitchView.cs index 68129e47..5a48c142 100644 --- a/Assets/Scripts/Interactables/LightSwitchView.cs +++ b/Assets/Scripts/Interactables/LightSwitchView.cs @@ -7,15 +7,15 @@ public class LightSwitchView : MonoBehaviour, IInteractable { [SerializeField] private List lightsources = new List(); private SwitchState currentState; - public static event Action OnLightSwitchToggled; + //public static event Action OnLightSwitchToggled; - private void OnEnable() => OnLightSwitchToggled += onLightSwitch; + private void OnEnable() => EventService.Instance.OnLightSwitchToggled.AddListener(onLightSwitch); - private void OnDisable() => OnLightSwitchToggled -= onLightSwitch; + private void OnDisable() => EventService.Instance.OnLightSwitchToggled.RemoveListener(onLightSwitch); private void Start() => currentState = SwitchState.Off; - public void Interact() => OnLightSwitchToggled?.Invoke(); + public void Interact() => EventService.Instance.OnLightSwitchToggled.InvokeEvent(); private void toggleLights() { diff --git a/Assets/Scripts/Player/PlayerController.cs b/Assets/Scripts/Player/PlayerController.cs index 8254b215..794cb9bc 100644 --- a/Assets/Scripts/Player/PlayerController.cs +++ b/Assets/Scripts/Player/PlayerController.cs @@ -25,13 +25,13 @@ public PlayerController(PlayerView playerView, PlayerScriptableObject playerScri this.playerScriptableObject = playerScriptableObject; this.playerScriptableObject.KeysEquipped = 0; - LightSwitchView.OnLightSwitchToggled += onLightSwitch; + EventService.Instance.OnLightSwitchToggled.AddListener(onLightSwitch); playerState = PlayerState.InDark; } ~PlayerController() { - LightSwitchView.OnLightSwitchToggled -= onLightSwitch; + EventService.Instance.OnLightSwitchToggled.RemoveListener(onLightSwitch); } public void Interact() => IsInteracted = Input.GetKeyDown(KeyCode.E) ? true : (Input.GetKeyUp(KeyCode.E) ? false : IsInteracted); diff --git a/Assets/Scripts/Service/EventService.cs b/Assets/Scripts/Service/EventService.cs new file mode 100644 index 00000000..14855310 --- /dev/null +++ b/Assets/Scripts/Service/EventService.cs @@ -0,0 +1,24 @@ + +public class EventService +{ + private static EventService instance; + public static EventService Instance + { + get + { + if(instance == null) + { + instance = new EventService(); + } + + return instance; + } + } + + public EventController OnLightSwitchToggled { get; private set; } + + public EventService() + { + OnLightSwitchToggled = new EventController(); + } +} diff --git a/Assets/Scripts/Service/EventService.cs.meta b/Assets/Scripts/Service/EventService.cs.meta new file mode 100644 index 00000000..2dfa10c3 --- /dev/null +++ b/Assets/Scripts/Service/EventService.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 74ab5340cb3f21a4291afb40306e95d1 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: