diff --git a/Assets/Scripts/Events.meta b/Assets/Scripts/Events.meta new file mode 100644 index 00000000..c6217d50 --- /dev/null +++ b/Assets/Scripts/Events.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 9848fe8237deaf542bdfbaf041b885ae +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..758995a5 --- /dev/null +++ b/Assets/Scripts/Events/EventController.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class EventController : MonoBehaviour +{ + public Action baseEvent; + + public void AddListener(Action Listener) => baseEvent += Listener; + + public void RemoveListener(Action Listener) => baseEvent -= Listener; + + public void InvokeEvent() => baseEvent?.Invoke(); + + // Start is called before the first frame update + void Start() + { + + } + + // Update is called once per frame + void Update() + { + + } +} diff --git a/Assets/Scripts/Events/EventController.cs.meta b/Assets/Scripts/Events/EventController.cs.meta new file mode 100644 index 00000000..a3d23e89 --- /dev/null +++ b/Assets/Scripts/Events/EventController.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 2349e3744fb7a514c8f4dad3859ae586 +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..c0c147ea 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 lightToggledAction; - private void OnEnable() => OnLightSwitchToggled += onLightSwitch; + private void OnEnable() => EventService.Instance.lightToggledAction.AddListener(onLightSwitch); - private void OnDisable() => OnLightSwitchToggled -= onLightSwitch; + private void OnDisable() => EventService.Instance.lightToggledAction.RemoveListener(onLightSwitch); private void Start() => currentState = SwitchState.Off; - public void Interact() => OnLightSwitchToggled?.Invoke(); + public void Interact() => EventService.Instance.lightToggledAction.InvokeEvent(); private void toggleLights() { diff --git a/Assets/Scripts/Player/PlayerController.cs b/Assets/Scripts/Player/PlayerController.cs index 8254b215..6f94141b 100644 --- a/Assets/Scripts/Player/PlayerController.cs +++ b/Assets/Scripts/Player/PlayerController.cs @@ -25,14 +25,15 @@ public PlayerController(PlayerView playerView, PlayerScriptableObject playerScri this.playerScriptableObject = playerScriptableObject; this.playerScriptableObject.KeysEquipped = 0; - LightSwitchView.OnLightSwitchToggled += onLightSwitch; + playerState = PlayerState.InDark; + EventService.Instance.lightToggledAction.AddListener(LightSwitchToggled); } - ~PlayerController() - { - LightSwitchView.OnLightSwitchToggled -= onLightSwitch; + ~PlayerController() { + EventService.Instance.lightToggledAction.RemoveListener(LightSwitchToggled); } + public void Interact() => IsInteracted = Input.GetKeyDown(KeyCode.E) ? true : (Input.GetKeyUp(KeyCode.E) ? false : IsInteracted); public void Jump(Rigidbody playerRigidbody, Transform transform) @@ -77,12 +78,15 @@ private void calculatePositionRotation(Rigidbody playerRigidbody, Transform tran rotation = playerRigidbody.rotation * Quaternion.Euler(lookRotation); position = (transform.position) + (velocity * movement) * Time.fixedDeltaTime; } - - private void onLightSwitch() + private void LightSwitchToggled() { if (PlayerState == PlayerState.InDark) + { PlayerState = PlayerState.None; + } else + { PlayerState = PlayerState.InDark; + } } } diff --git a/Assets/Scripts/Service/EventService.cs b/Assets/Scripts/Service/EventService.cs new file mode 100644 index 00000000..cca34762 --- /dev/null +++ b/Assets/Scripts/Service/EventService.cs @@ -0,0 +1,37 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class EventService +{ + private static EventService instance; + public static EventService Instance + { + get + { + if(instance == null) + { + instance = new EventService(); + } + return instance; + } + } + + public EventController lightToggledAction { get; private set; } + + public EventService() + { + lightToggledAction = new EventController(); + } + // Start is called before the first frame update + void Start() + { + + } + + // Update is called once per frame + void Update() + { + + } +} diff --git a/Assets/Scripts/Service/EventService.cs.meta b/Assets/Scripts/Service/EventService.cs.meta new file mode 100644 index 00000000..b2b3c2a4 --- /dev/null +++ b/Assets/Scripts/Service/EventService.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e31cd314a935b6d4f87c18700006968c +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: