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

feat:EventTrigger of LightsTurnOffByGhostEvent #22

Open
wants to merge 3 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
2 changes: 1 addition & 1 deletion Assets/Art/Prefabs/House.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -13297,7 +13297,7 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
keysRequiredToTrigger: 1
soundToPlay: 4
soundType: 4
--- !u!1 &7927258282318190006
GameObject:
m_ObjectHideFlags: 0
Expand Down
11 changes: 11 additions & 0 deletions Assets/Scripts/Events/LightsOffByGhostEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,16 @@

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;
}
}
}
35 changes: 33 additions & 2 deletions Assets/Scripts/Interactables/LightSwitchView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,17 @@ public class LightSwitchView : MonoBehaviour, IInteractable
[SerializeField] private List<Light> lightsources = new List<Light>();
private SwitchState currentState;

private void OnEnable() => EventService.Instance.OnLightSwitchToggled.AddListener(onLightSwitch);
private void OnEnable()
{
EventService.Instance.OnLightSwitchToggled.AddListener(onLightSwitch);
EventService.Instance.OnLightsOffByGhostEvent.AddListener(onLightTurnedOffByGhost);
}

private void OnDisable() => EventService.Instance.OnLightSwitchToggled.RemoveListener(onLightSwitch);
private void OnDisable()
{
EventService.Instance.OnLightSwitchToggled.RemoveListener(onLightSwitch);
EventService.Instance.OnLightsOffByGhostEvent.RemoveListener(onLightTurnedOffByGhost);
}

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

Expand Down Expand Up @@ -38,11 +46,34 @@ private void toggleLights()
lightSource.enabled = lights;
}
}

private void setLights(bool light)
{
foreach (Light lightSource in lightsources)
{
lightSource.enabled = light;
}
if(light)
{
currentState = SwitchState.On;
}
else
{
currentState = SwitchState.Off;
}
}

private void onLightSwitch()
{
toggleLights();
GameService.Instance.GetSoundView().PlaySoundEffects(SoundType.SwitchSound);
GameService.Instance.GetInstructionView().HideInstruction();
}

private void onLightTurnedOffByGhost()
{
setLights(false);
GameService.Instance.GetSoundView().PlaySoundEffects(SoundType.SwitchSound);
GameService.Instance.GetInstructionView().ShowInstruction(InstructionType.LightsOff);
}
}
9 changes: 9 additions & 0 deletions Assets/Scripts/Player/PlayerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@ public PlayerController(PlayerView playerView, PlayerScriptableObject playerScri
playerState = PlayerState.InDark;

EventService.Instance.OnLightSwitchToggled.AddListener(onLightSwitch);
EventService.Instance.OnLightsOffByGhostEvent.AddListener(onLightsTurnedOffByGhost);
EventService.Instance.OnKeyPickedUp.AddListener(onKeysPickedUp);
}

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

Expand Down Expand Up @@ -87,6 +90,12 @@ private void onLightSwitch()
else
PlayerState = PlayerState.InDark;
}

private void onLightsTurnedOffByGhost()
{
playerState = PlayerState.InDark;
}

private void onKeysPickedUp(int keys)
{
KeysEquipped = keys;
Expand Down
8 changes: 6 additions & 2 deletions Assets/Scripts/UI/GameUIView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ 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);
EventService.Instance.OnLightsOffByGhostEvent.RemoveListener(setRedVignette);
}
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");
Expand Down
12 changes: 6 additions & 6 deletions Packages/packages-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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": {},
Expand All @@ -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": {
Expand Down
4 changes: 2 additions & 2 deletions ProjectSettings/ProjectVersion.txt
Original file line number Diff line number Diff line change
@@ -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)