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
4 changes: 4 additions & 0 deletions Assets/Scripts/Events.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 21 additions & 3 deletions Assets/Scripts/Events/EventController.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

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);

}

public class EventController
{
public Action baseEvent;
public void AddListener(Action listener) => baseEvent += listener;
public void RemoveListener(Action listener) => baseEvent -= listener;

public void AddListener(Action Listener) => baseEvent += Listener;

public void RemoveListener(Action Listener) => baseEvent -= Listener;

public void InvokeEvent() => baseEvent?.Invoke();
}

}
4 changes: 4 additions & 0 deletions Assets/Scripts/Events/EventController.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Assets/Scripts/Interactables/KeyView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ public class KeyView : MonoBehaviour, IInteractable
[SerializeField] GameUIView gameUIView;
public void Interact()
{
int currentKey = GameService.Instance.GetPlayerController().KeysEquipped;
GameService.Instance.GetInstructionView().HideInstruction();
GameService.Instance.GetSoundView().PlaySoundEffects(SoundType.KeyPickUp);
GameService.Instance.GetPlayerController().KeysEquipped++;
gameUIView.UpdateKeyText();

currentKey++;
EventService.Instance.OnKeyPickedUp.InvokeEvent(currentKey);
gameObject.SetActive(false);
}
}
8 changes: 4 additions & 4 deletions Assets/Scripts/Interactables/LightSwitchView.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
using System;
using System.Collections.Generic;
using UnityEditor.MPE;
using UnityEngine;
using static LightSwitchView;

public class LightSwitchView : MonoBehaviour, IInteractable
{
[SerializeField] private List<Light> lightsources = new List<Light>();
private SwitchState currentState;
public static event Action lightToggledAction;

private void OnEnable() => EventService.Instance.OnLightSwitchToggled.AddListener(onLightSwitch);
private void OnEnable() => EventService.Instance.lightToggledAction.AddListener(onLightSwitch);

private void OnDisable() => EventService.Instance.OnLightSwitchToggled.RemoveListener(onLightSwitch);
private void OnDisable() => EventService.Instance.lightToggledAction.RemoveListener(onLightSwitch);

private void Start() => currentState = SwitchState.Off;

public void Interact() => EventService.Instance.OnLightSwitchToggled.InvokeEvent();
public void Interact() => EventService.Instance.lightToggledAction.InvokeEvent();

private void toggleLights()
{
Expand Down
26 changes: 19 additions & 7 deletions Assets/Scripts/Player/PlayerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,19 @@ public PlayerController(PlayerView playerView, PlayerScriptableObject playerScri
this.playerView.SetController(this);
this.playerScriptableObject = playerScriptableObject;
this.playerScriptableObject.KeysEquipped = 0;
playerState = PlayerState.InDark;

EventService.Instance.OnLightSwitchToggled.AddListener(onLightSwitch);

playerState = PlayerState.InDark;
EventService.Instance.lightToggledAction.AddListener(LightSwitchToggled);
EventService.Instance.OnKeyPickedUp.AddListener(onKeysPickedUp);
}

~PlayerController()
{
EventService.Instance.OnLightSwitchToggled.RemoveListener(onLightSwitch);
~PlayerController() {
EventService.Instance.lightToggledAction.RemoveListener(LightSwitchToggled);
EventService.Instance.OnKeyPickedUp.RemoveListener(onKeysPickedUp);
}


public void Interact() => IsInteracted = Input.GetKeyDown(KeyCode.E) ? true : (Input.GetKeyUp(KeyCode.E) ? false : IsInteracted);

public void Jump(Rigidbody playerRigidbody, Transform transform)
Expand Down Expand Up @@ -77,12 +81,20 @@ 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;
}
}

private void onKeysPickedUp(int keys)
{
KeysEquipped = keys;
}
}
12 changes: 10 additions & 2 deletions Assets/Scripts/Service/EventService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class EventService
{
private static EventService instance;
Expand All @@ -14,10 +19,13 @@ public static EventService Instance
}
}

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

public EventService()
{
OnLightSwitchToggled = new EventController();
lightToggledAction = new EventController();
OnKeyPickedUp = new EventController<int>();
}
}

4 changes: 4 additions & 0 deletions Assets/Scripts/Service/EventService.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions Assets/Scripts/UI/GameUIView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}