Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lights turn off by ghost event setup #11

Open
wants to merge 8 commits into
base: Lights-Turn-Off-By-Ghost-Event-Setup
Choose a base branch
from
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/Scenes/GameScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -2240,6 +2240,10 @@ PrefabInstance:
propertyPath: soundService
value:
objectReference: {fileID: 203687011}
- target: {fileID: 2443645658748825555, guid: d2470b45bfcea9e4780d1866d7cd0f8c, type: 3}
propertyPath: soundType
value: 4
objectReference: {fileID: 0}
- target: {fileID: 3492344741786150290, guid: d2470b45bfcea9e4780d1866d7cd0f8c, type: 3}
propertyPath: gameUIView
value:
Expand Down
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.

10 changes: 8 additions & 2 deletions Assets/Scripts/Events/EventController.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

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

}

public class EventController<T>
Expand Down
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.

12 changes: 11 additions & 1 deletion Assets/Scripts/Events/LightsOffByGhostEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,15 @@

public class LightsOffByGhostEvent : MonoBehaviour
{

[SerializeField] private int keysRequiredToTrigger;
[SerializeField] private SoundType soundType;
private void OnTriggerEnter(Collider other)
{
if(other.GetComponent<PlayerView>() != null && keysRequiredToTrigger == GameService.Instance.GetPlayerController().KeysEquipped)
{
EventService.Instance.OnLightsOffByGhostEvent.InvokeEvent();
GameService.Instance.GetSoundView().PlaySoundEffects(soundType);
this.enabled = false;
}
}
}
8 changes: 3 additions & 5 deletions Assets/Scripts/Interactables/KeyView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ public class KeyView : MonoBehaviour, IInteractable

public void Interact()
{
int currentKeys = GameService.Instance.GetPlayerController().KeysEquipped;
currentKeys++;

int currentKey = GameService.Instance.GetPlayerController().KeysEquipped;
GameService.Instance.GetInstructionView().HideInstruction();
GameService.Instance.GetSoundView().PlaySoundEffects(SoundType.KeyPickUp);
EventService.Instance.OnKeyPickedUp.InvokeEvent(currentKeys);

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

public class LightSwitchView : MonoBehaviour, IInteractable
{
[SerializeField] private List<Light> lightsources = new List<Light>();
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);

Expand Down Expand Up @@ -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);
}
}
23 changes: 17 additions & 6 deletions Assets/Scripts/Player/PlayerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,21 @@ 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);
EventService.Instance.OnLightSwitchToggled.AddListener(LightSwitchToggled);
EventService.Instance.OnKeyPickedUp.AddListener(onKeysPickedUp);
EventService.Instance.OnLightsOffByGhostEvent.AddListener(onLightsTurnedOffByGhost);
}

~PlayerController()
{
EventService.Instance.OnLightSwitchToggled.RemoveListener(onLightSwitch);
~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);

public void Jump(Rigidbody playerRigidbody, Transform transform)
Expand Down Expand Up @@ -79,14 +83,21 @@ 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 onLightsTurnedOffByGhost() => PlayerState = PlayerState.InDark;


private void onKeysPickedUp(int keys)
{
KeysEquipped = keys;
Expand Down
6 changes: 6 additions & 0 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 @@ -25,3 +30,4 @@ public EventService()
OnLightsOffByGhostEvent = new EventController();
}
}

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.

11 changes: 9 additions & 2 deletions Assets/Scripts/UI/GameUIView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,29 @@ 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");

private void updateKeyText(int keys) => keysFoundText.SetText($"Keys Found: {keys}/ 3");
private void onQuitButtonClicked() => Application.Quit();
private void onTryAgainButtonClicked() => SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);



//Assignment - Call this method as a lister of LightsOffByGhostEvent
private void setRedVignette()
{
redVignette.enabled = true;
redVignette.canvasRenderer.SetAlpha(0.5f);
redVignette.CrossFadeAlpha(0, 5, false);
}

}