From 2439397d93a5cd5e392680e37be920c9561170e1 Mon Sep 17 00:00:00 2001 From: Aditya Singh Date: Tue, 27 Feb 2024 15:51:45 +0530 Subject: [PATCH 1/3] feat: Implementation of Generic EventController --- Assets/Scripts/Events/EventController.cs | 7 +++++++ Assets/Scripts/Interactables/KeyView.cs | 4 ++++ Assets/Scripts/Service/EventService.cs | 1 + Packages/packages-lock.json | 12 ++++++------ ProjectSettings/ProjectVersion.txt | 4 ++-- 5 files changed, 20 insertions(+), 8 deletions(-) diff --git a/Assets/Scripts/Events/EventController.cs b/Assets/Scripts/Events/EventController.cs index da1926b4..9a3b0829 100644 --- a/Assets/Scripts/Events/EventController.cs +++ b/Assets/Scripts/Events/EventController.cs @@ -7,4 +7,11 @@ public class EventController public void RemoveListener(Action listener) => baseEvent -= listener; public void InvokeEvent() => baseEvent?.Invoke(); } +public class EventController +{ + public Action baseEvent; + public void AddListener(Action listener) => baseEvent += listener; + public void RemoveListener(Action listener) => baseEvent -= listener; + public void InvokeEvent(T type) => baseEvent?.Invoke(type); +} diff --git a/Assets/Scripts/Interactables/KeyView.cs b/Assets/Scripts/Interactables/KeyView.cs index 9e5b0238..419537e8 100644 --- a/Assets/Scripts/Interactables/KeyView.cs +++ b/Assets/Scripts/Interactables/KeyView.cs @@ -5,11 +5,15 @@ public class KeyView : MonoBehaviour, IInteractable [SerializeField] GameUIView gameUIView; public void Interact() { + int currentKeys = GameService.Instance.GetPlayerController().KeysEquipped; + GameService.Instance.GetInstructionView().HideInstruction(); GameService.Instance.GetSoundView().PlaySoundEffects(SoundType.KeyPickUp); GameService.Instance.GetPlayerController().KeysEquipped++; gameUIView.UpdateKeyText(); + currentKeys++; + gameObject.SetActive(false); } } diff --git a/Assets/Scripts/Service/EventService.cs b/Assets/Scripts/Service/EventService.cs index fb22c2b5..ee954e54 100644 --- a/Assets/Scripts/Service/EventService.cs +++ b/Assets/Scripts/Service/EventService.cs @@ -15,6 +15,7 @@ public static EventService Instance } public EventController OnLightSwitchToggled { get; private set; } + public EventController OnKeyPickedUp { get; private set; } public EventService() { diff --git a/Packages/packages-lock.json b/Packages/packages-lock.json index 3d6c323f..a7a5887e 100644 --- a/Packages/packages-lock.json +++ b/Packages/packages-lock.json @@ -72,13 +72,13 @@ "depth": 0, "source": "builtin", "dependencies": { - "com.unity.ide.visualstudio": "2.0.17", - "com.unity.ide.rider": "3.0.18", + "com.unity.ide.visualstudio": "2.0.16", + "com.unity.ide.rider": "3.0.15", "com.unity.ide.vscode": "1.2.5", "com.unity.editorcoroutines": "1.0.0", - "com.unity.performance.profile-analyzer": "1.2.2", + "com.unity.performance.profile-analyzer": "1.1.1", "com.unity.test-framework": "1.1.31", - "com.unity.testtools.codecoverage": "1.2.2" + "com.unity.testtools.codecoverage": "1.0.1" } }, "com.unity.ide.rider": { @@ -114,7 +114,7 @@ "url": "https://packages.unity.com" }, "com.unity.performance.profile-analyzer": { - "version": "1.2.2", + "version": "1.1.1", "depth": 1, "source": "registry", "dependencies": {}, @@ -139,7 +139,7 @@ "url": "https://packages.unity.com" }, "com.unity.testtools.codecoverage": { - "version": "1.2.2", + "version": "1.0.1", "depth": 1, "source": "registry", "dependencies": { diff --git a/ProjectSettings/ProjectVersion.txt b/ProjectSettings/ProjectVersion.txt index bca3d022..1f883d75 100644 --- a/ProjectSettings/ProjectVersion.txt +++ b/ProjectSettings/ProjectVersion.txt @@ -1,2 +1,2 @@ -m_EditorVersion: 2021.3.21f1 -m_EditorVersionWithRevision: 2021.3.21f1 (1b156197d683) +m_EditorVersion: 2021.3.10f1 +m_EditorVersionWithRevision: 2021.3.10f1 (1c7d0df0160b) From b4733cc6f5ae89608c1c2d7d1b721ecd967db909 Mon Sep 17 00:00:00 2001 From: Aditya Singh Date: Tue, 27 Feb 2024 16:26:17 +0530 Subject: [PATCH 2/3] fix:Improving UI Logic --- Assets/Scripts/Interactables/KeyView.cs | 2 +- Assets/Scripts/Service/EventService.cs | 2 +- Assets/Scripts/UI/GameUIView.cs | 14 ++++++++------ 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/Assets/Scripts/Interactables/KeyView.cs b/Assets/Scripts/Interactables/KeyView.cs index 419537e8..b861638d 100644 --- a/Assets/Scripts/Interactables/KeyView.cs +++ b/Assets/Scripts/Interactables/KeyView.cs @@ -10,10 +10,10 @@ public void Interact() GameService.Instance.GetInstructionView().HideInstruction(); GameService.Instance.GetSoundView().PlaySoundEffects(SoundType.KeyPickUp); GameService.Instance.GetPlayerController().KeysEquipped++; - gameUIView.UpdateKeyText(); currentKeys++; + EventService.Instance.OnKeyPickedUp.InvokeEvent(currentKeys); gameObject.SetActive(false); } } diff --git a/Assets/Scripts/Service/EventService.cs b/Assets/Scripts/Service/EventService.cs index ee954e54..e865c6fe 100644 --- a/Assets/Scripts/Service/EventService.cs +++ b/Assets/Scripts/Service/EventService.cs @@ -1,4 +1,3 @@ - public class EventService { private static EventService instance; @@ -20,5 +19,6 @@ public static EventService Instance public EventService() { OnLightSwitchToggled = new EventController(); + OnKeyPickedUp = new EventController(); } } diff --git a/Assets/Scripts/UI/GameUIView.cs b/Assets/Scripts/UI/GameUIView.cs index b4468eee..f5f97670 100644 --- a/Assets/Scripts/UI/GameUIView.cs +++ b/Assets/Scripts/UI/GameUIView.cs @@ -21,13 +21,15 @@ 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); } - 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 OnQuitButtonClicked() => Application.Quit(); - private void OnTryAgainButtonClicked() => SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex); + private void OnDisable() => EventService.Instance.OnKeyPickedUp.RemoveListener(updateKeyText); + public void UpdateInsanity(float playerSanity) => insanityImage.rectTransform.localScale = new Vector3(1, playerSanity, 1); + private void updateKeyText(int keys) => keysFoundText.SetText($"Keys Found: {keys}/3"); + private void onQuitButtonClicked() => Application.Quit(); + private void onTryAgainButtonClicked() => SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex); } From 629eac710cf9938c444152fcc6737eddd760a959 Mon Sep 17 00:00:00 2001 From: Aditya Singh Date: Tue, 27 Feb 2024 17:04:25 +0530 Subject: [PATCH 3/3] feat:Adding OnKeyPickedUp Listner in PlayerController --- Assets/Scripts/Player/PlayerController.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Assets/Scripts/Player/PlayerController.cs b/Assets/Scripts/Player/PlayerController.cs index a7febc8b..ccb12cf6 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.OnLightSwitchToggled.AddListener(onLightSwitch); + EventService.Instance.OnKeyPickedUp.AddListener(onkeyPickedUp); } ~PlayerController() { EventService.Instance.OnLightSwitchToggled.RemoveListener(onLightSwitch); + EventService.Instance.OnKeyPickedUp.RemoveListener(onkeyPickedUp); } public void Interact() => IsInteracted = Input.GetKeyDown(KeyCode.E) ? true : (Input.GetKeyUp(KeyCode.E) ? false : IsInteracted); @@ -85,4 +87,9 @@ private void onLightSwitch() else PlayerState = PlayerState.InDark; } + + private void onkeyPickedUp(int keys) + { + KeysEquipped = keys; + } }