From f11fd005429bdc111240eeb104ddb67ad0458307 Mon Sep 17 00:00:00 2001 From: Bharath G Date: Sat, 28 Oct 2023 15:44:14 +0530 Subject: [PATCH 1/6] Added EventController --- Assets/Scripts/Events.meta | 8 ++++++ Assets/Scripts/Events/EventController.cs | 27 +++++++++++++++++++ Assets/Scripts/Events/EventController.cs.meta | 11 ++++++++ .../Scripts/Interactables/LightSwitchView.cs | 8 +++--- Assets/Scripts/Player/PlayerController.cs | 14 +++++----- 5 files changed, 57 insertions(+), 11 deletions(-) create mode 100644 Assets/Scripts/Events.meta create mode 100644 Assets/Scripts/Events/EventController.cs create mode 100644 Assets/Scripts/Events/EventController.cs.meta 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..5126c596 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() => lightToggledAction += onLightSwitch; - private void OnDisable() => OnLightSwitchToggled -= onLightSwitch; + private void OnDisable() => lightToggledAction -= onLightSwitch; private void Start() => currentState = SwitchState.Off; - public void Interact() => OnLightSwitchToggled?.Invoke(); + public void Interact() => lightToggledAction?.Invoke(); private void toggleLights() { diff --git a/Assets/Scripts/Player/PlayerController.cs b/Assets/Scripts/Player/PlayerController.cs index 8254b215..220bc7cf 100644 --- a/Assets/Scripts/Player/PlayerController.cs +++ b/Assets/Scripts/Player/PlayerController.cs @@ -25,14 +25,11 @@ public PlayerController(PlayerView playerView, PlayerScriptableObject playerScri this.playerScriptableObject = playerScriptableObject; this.playerScriptableObject.KeysEquipped = 0; - LightSwitchView.OnLightSwitchToggled += onLightSwitch; + playerState = PlayerState.InDark; + LightSwitchView.lightToggledAction += LightSwitchToggled; } - ~PlayerController() - { - LightSwitchView.OnLightSwitchToggled -= onLightSwitch; - } public void Interact() => IsInteracted = Input.GetKeyDown(KeyCode.E) ? true : (Input.GetKeyUp(KeyCode.E) ? false : IsInteracted); public void Jump(Rigidbody playerRigidbody, Transform transform) @@ -77,12 +74,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; + } } } From 7f502711db4cf117346f3681b321fc97bf36a074 Mon Sep 17 00:00:00 2001 From: Bharath G Date: Sat, 28 Oct 2023 16:13:21 +0530 Subject: [PATCH 2/6] Added EventService --- Assets/Scripts/Service/EventService.cs | 37 +++++++++++++++++++++ Assets/Scripts/Service/EventService.cs.meta | 11 ++++++ 2 files changed, 48 insertions(+) create mode 100644 Assets/Scripts/Service/EventService.cs create mode 100644 Assets/Scripts/Service/EventService.cs.meta diff --git a/Assets/Scripts/Service/EventService.cs b/Assets/Scripts/Service/EventService.cs new file mode 100644 index 00000000..146e461f --- /dev/null +++ b/Assets/Scripts/Service/EventService.cs @@ -0,0 +1,37 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class EventService : MonoBehaviour +{ + 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: From b1ab4691fc952ac39c07ef7bcc46990124830a07 Mon Sep 17 00:00:00 2001 From: Bharath G Date: Sat, 28 Oct 2023 16:28:27 +0530 Subject: [PATCH 3/6] Added New Architecture for Events --- Assets/Scripts/Interactables/LightSwitchView.cs | 6 +++--- Assets/Scripts/Player/PlayerController.cs | 6 +++++- Assets/Scripts/Service/EventService.cs | 2 +- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/Assets/Scripts/Interactables/LightSwitchView.cs b/Assets/Scripts/Interactables/LightSwitchView.cs index 5126c596..c0c147ea 100644 --- a/Assets/Scripts/Interactables/LightSwitchView.cs +++ b/Assets/Scripts/Interactables/LightSwitchView.cs @@ -9,13 +9,13 @@ public class LightSwitchView : MonoBehaviour, IInteractable private SwitchState currentState; public static event Action lightToggledAction; - private void OnEnable() => lightToggledAction += onLightSwitch; + private void OnEnable() => EventService.Instance.lightToggledAction.AddListener(onLightSwitch); - private void OnDisable() => lightToggledAction -= onLightSwitch; + private void OnDisable() => EventService.Instance.lightToggledAction.RemoveListener(onLightSwitch); private void Start() => currentState = SwitchState.Off; - public void Interact() => lightToggledAction?.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 220bc7cf..6f94141b 100644 --- a/Assets/Scripts/Player/PlayerController.cs +++ b/Assets/Scripts/Player/PlayerController.cs @@ -27,7 +27,11 @@ public PlayerController(PlayerView playerView, PlayerScriptableObject playerScri this.playerScriptableObject.KeysEquipped = 0; playerState = PlayerState.InDark; - LightSwitchView.lightToggledAction += LightSwitchToggled; + EventService.Instance.lightToggledAction.AddListener(LightSwitchToggled); + } + + ~PlayerController() { + EventService.Instance.lightToggledAction.RemoveListener(LightSwitchToggled); } 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 index 146e461f..cca34762 100644 --- a/Assets/Scripts/Service/EventService.cs +++ b/Assets/Scripts/Service/EventService.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using UnityEngine; -public class EventService : MonoBehaviour +public class EventService { private static EventService instance; public static EventService Instance From fdba27165e23780688ab0b7ee2eb127b22dba2dd Mon Sep 17 00:00:00 2001 From: Bharath G Date: Sat, 28 Oct 2023 17:31:58 +0530 Subject: [PATCH 4/6] Changes in KeyView Logic --- Assets/Scripts/Interactables/KeyView.cs | 3 +-- Assets/Scripts/Service/EventService.cs | 1 + Assets/Scripts/UI/GameUIView.cs | 13 ++++++++----- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/Assets/Scripts/Interactables/KeyView.cs b/Assets/Scripts/Interactables/KeyView.cs index cad52cfa..a49e2fec 100644 --- a/Assets/Scripts/Interactables/KeyView.cs +++ b/Assets/Scripts/Interactables/KeyView.cs @@ -9,8 +9,7 @@ public void Interact() GameService.Instance.GetInstructionView().HideInstruction(); GameService.Instance.GetSoundView().PlaySoundEffects(SoundType.KeyPickUp); currentKey++; - gameUIView.UpdateKeyText(); - + EventService.Instance.OnKeyPickedUp.InvokeEvent(currentKey); gameObject.SetActive(false); } } diff --git a/Assets/Scripts/Service/EventService.cs b/Assets/Scripts/Service/EventService.cs index c20632d0..f24283a5 100644 --- a/Assets/Scripts/Service/EventService.cs +++ b/Assets/Scripts/Service/EventService.cs @@ -25,6 +25,7 @@ public static EventService Instance public EventService() { lightToggledAction = new EventController(); + OnKeyPickedUp = new EventController(); } } diff --git a/Assets/Scripts/UI/GameUIView.cs b/Assets/Scripts/UI/GameUIView.cs index b4468eee..175e016f 100644 --- a/Assets/Scripts/UI/GameUIView.cs +++ b/Assets/Scripts/UI/GameUIView.cs @@ -21,13 +21,16 @@ public class GameUIView : MonoBehaviour private void OnEnable() { - tryAgainButton.onClick.AddListener(OnTryAgainButtonClicked); - quitButton.onClick.AddListener(OnQuitButtonClicked); + tryAgainButton.onClick.AddListener(onTryAgainButtonClicked); + quitButton.onClick.AddListener(onQuitButtonClicked); + EventService.Instance.OnKeyPickedUp.AddListener(updateKeyText); } + + private void OnDisable() => EventService.Instance.OnKeyPickedUp.RemoveListener(updateKeyText); public void UpdateInsanity(float playerSanity) => insanityImage.rectTransform.localScale = new Vector3(1, playerSanity, 1); - public void UpdateKeyText() => keysFoundText.SetText($"Keys Found: {GameService.Instance.GetPlayerController().KeysEquipped}/3"); + private void updateKeyText(int keys) => keysFoundText.SetText($"Keys Found: {keys}/3"); - private void OnQuitButtonClicked() => Application.Quit(); - private void OnTryAgainButtonClicked() => SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex); + private void onQuitButtonClicked() => Application.Quit(); + private void onTryAgainButtonClicked() => SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex); } From 65daa2de2a5d28dbc338b96683ff613bcd78ed91 Mon Sep 17 00:00:00 2001 From: Bharath G Date: Sat, 28 Oct 2023 17:41:45 +0530 Subject: [PATCH 5/6] Added Listener for keys in PlayerController --- Assets/Scripts/Player/PlayerController.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Assets/Scripts/Player/PlayerController.cs b/Assets/Scripts/Player/PlayerController.cs index 20f92faf..f7fab7e0 100644 --- a/Assets/Scripts/Player/PlayerController.cs +++ b/Assets/Scripts/Player/PlayerController.cs @@ -27,11 +27,13 @@ public PlayerController(PlayerView playerView, PlayerScriptableObject playerScri playerState = PlayerState.InDark; - EventService.Instance.lightToggledAction.AddListener(LightSwitchToggled); + EventService.Instance.lightToggledAction.AddListener(LightSwitchToggled); + EventService.Instance.OnKeyPickedUp.AddListener(onKeysPickedUp); } ~PlayerController() { EventService.Instance.lightToggledAction.RemoveListener(LightSwitchToggled); + EventService.Instance.OnKeyPickedUp.RemoveListener(onKeysPickedUp); } @@ -90,4 +92,9 @@ private void LightSwitchToggled() PlayerState = PlayerState.InDark; } } + + private void onKeysPickedUp(int keys) + { + KeysEquipped = keys; + } } From 9127cab423a1d8cb5bf84d95118ce904f2fe52c3 Mon Sep 17 00:00:00 2001 From: Bharath G Date: Sat, 28 Oct 2023 22:15:54 +0530 Subject: [PATCH 6/6] Added GameUI effect --- .../Scripts/Interactables/LightSwitchView.cs | 28 ++++++++++++++++++- Assets/Scripts/Player/PlayerController.cs | 5 ++++ Assets/Scripts/UI/GameUIView.cs | 7 ++++- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/Assets/Scripts/Interactables/LightSwitchView.cs b/Assets/Scripts/Interactables/LightSwitchView.cs index 3407df47..3b219e8c 100644 --- a/Assets/Scripts/Interactables/LightSwitchView.cs +++ b/Assets/Scripts/Interactables/LightSwitchView.cs @@ -8,7 +8,11 @@ public class LightSwitchView : MonoBehaviour, IInteractable private SwitchState currentState; public static event Action OnLightSwitchToggled; - private void OnEnable() => EventService.Instance.OnLightSwitchToggled.AddListener(onLightSwitch); + private void OnEnable() + { + EventService.Instance.OnLightSwitchToggled.AddListener(onLightSwitch); + EventService.Instance.OnLightsOffByGhostEvent.AddListener(onLightsTurnedOffByGhost); + } private void OnDisable() => EventService.Instance.OnLightSwitchToggled.RemoveListener(onLightSwitch); @@ -39,10 +43,32 @@ private void toggleLights() } } + private void setLights(bool lights) + { + foreach (Light lightSource in lightsources) + { + lightSource.enabled = lights; + } + if (lights) + { + currentState = SwitchState.On; + } + else + { + currentState = SwitchState.Off; + } + } + private void onLightSwitch() { toggleLights(); GameService.Instance.GetSoundView().PlaySoundEffects(SoundType.SwitchSound); GameService.Instance.GetInstructionView().HideInstruction(); } + private void onLightsTurnedOffByGhost() + { + setLights(false); + GameService.Instance.GetSoundView().PlaySoundEffects(SoundType.SwitchSound); + GameService.Instance.GetInstructionView().ShowInstruction(InstructionType.LightsOff); + } } diff --git a/Assets/Scripts/Player/PlayerController.cs b/Assets/Scripts/Player/PlayerController.cs index 181ca313..75a7f5e0 100644 --- a/Assets/Scripts/Player/PlayerController.cs +++ b/Assets/Scripts/Player/PlayerController.cs @@ -30,11 +30,13 @@ public PlayerController(PlayerView playerView, PlayerScriptableObject playerScri EventService.Instance.OnLightSwitchToggled.AddListener(LightSwitchToggled); EventService.Instance.OnKeyPickedUp.AddListener(onKeysPickedUp); + EventService.Instance.OnLightsOffByGhostEvent.AddListener(onLightsTurnedOffByGhost); } ~PlayerController() { EventService.Instance.OnLightSwitchToggled.RemoveListener(LightSwitchToggled); EventService.Instance.OnKeyPickedUp.RemoveListener(onKeysPickedUp); + EventService.Instance.OnLightsOffByGhostEvent.RemoveListener(onLightsTurnedOffByGhost); } public void Interact() => IsInteracted = Input.GetKeyDown(KeyCode.E) ? true : (Input.GetKeyUp(KeyCode.E) ? false : IsInteracted); @@ -93,6 +95,9 @@ private void LightSwitchToggled() } } + private void onLightsTurnedOffByGhost() => PlayerState = PlayerState.InDark; + + private void onKeysPickedUp(int keys) { KeysEquipped = keys; diff --git a/Assets/Scripts/UI/GameUIView.cs b/Assets/Scripts/UI/GameUIView.cs index 450090a0..221cc665 100644 --- a/Assets/Scripts/UI/GameUIView.cs +++ b/Assets/Scripts/UI/GameUIView.cs @@ -24,9 +24,14 @@ private void OnEnable() tryAgainButton.onClick.AddListener(onTryAgainButtonClicked); quitButton.onClick.AddListener(onQuitButtonClicked); EventService.Instance.OnKeyPickedUp.AddListener(updateKeyText); + EventService.Instance.OnLightsOffByGhostEvent.AddListener(setRedVignette); } - private void OnDisable() => EventService.Instance.OnKeyPickedUp.RemoveListener(updateKeyText); + private void OnDisable() + { + EventService.Instance.OnKeyPickedUp.RemoveListener(updateKeyText); + EventService.Instance.OnLightsOffByGhostEvent?.RemoveListener(setRedVignette); + } public void UpdateInsanity(float playerSanity) => insanityImage.rectTransform.localScale = new Vector3(1, playerSanity, 1); private void updateKeyText(int keys) => keysFoundText.SetText($"Keys Found: {keys}/3");