Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Assets/Scripts/Events/EventController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,11 @@ public class EventController
public void InvokeEvent() => baseEvent?.Invoke();
}

public class EventController<T>
{
public Action<T> baseEvent;
public void AddListener(Action<T> listener) => baseEvent += listener;
public void RemoveListener(Action<T> listener) => baseEvent -= listener;
public void InvokeEvent(T type) => baseEvent?.Invoke(type);
}

8 changes: 5 additions & 3 deletions Assets/Scripts/Interactables/KeyView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
7 changes: 7 additions & 0 deletions Assets/Scripts/Player/PlayerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -85,4 +87,9 @@ private void onLightSwitch()
else
PlayerState = PlayerState.InDark;
}

private void onKeysPickedUp(int keys)
{
KeysEquipped = keys;
}
}
2 changes: 2 additions & 0 deletions Assets/Scripts/Service/EventService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ public static EventService Instance
}

public EventController OnLightSwitchToggled { get; private set; }
public EventController<int> OnKeyPickedUp { get; private set; }

public EventService()
{
OnLightSwitchToggled = new EventController();
OnKeyPickedUp = new EventController<int>();
}
}
6 changes: 5 additions & 1 deletion Assets/Scripts/UI/GameUIView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down