diff --git a/Assets/Scripts/Events/EventController.cs b/Assets/Scripts/Events/EventController.cs index da1926b4..616df86b 100644 --- a/Assets/Scripts/Events/EventController.cs +++ b/Assets/Scripts/Events/EventController.cs @@ -8,3 +8,11 @@ public class EventController 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..edb90f2a 100644 --- a/Assets/Scripts/Interactables/KeyView.cs +++ b/Assets/Scripts/Interactables/KeyView.cs @@ -2,14 +2,16 @@ 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++; + + EventService.Instance.OnKeyPickedUp.InvokeEvent(currentKeys); gameObject.SetActive(false); } } diff --git a/Assets/Scripts/Player/PlayerController.cs b/Assets/Scripts/Player/PlayerController.cs index a7febc8b..92ea3515 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(onKeysPickedUp); } ~PlayerController() { EventService.Instance.OnLightSwitchToggled.RemoveListener(onLightSwitch); + EventService.Instance.OnKeyPickedUp.RemoveListener(onKeysPickedUp); } 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 onKeysPickedUp(int keys) + { + KeysEquipped = keys; + } } diff --git a/Assets/Scripts/Service/EventService.cs b/Assets/Scripts/Service/EventService.cs index fb22c2b5..7c3d6db4 100644 --- a/Assets/Scripts/Service/EventService.cs +++ b/Assets/Scripts/Service/EventService.cs @@ -15,9 +15,11 @@ public static EventService Instance } public EventController OnLightSwitchToggled { get; private set; } + public EventController OnKeyPickedUp { get; private set; } public EventService() { OnLightSwitchToggled = new EventController(); + OnKeyPickedUp = new EventController(); } } diff --git a/Assets/Scripts/UI/GameUIView.cs b/Assets/Scripts/UI/GameUIView.cs index b4468eee..162cd397 100644 --- a/Assets/Scripts/UI/GameUIView.cs +++ b/Assets/Scripts/UI/GameUIView.cs @@ -23,9 +23,13 @@ private void OnEnable() { 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);