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

Creating horror events using observer pattern setup #12

Open
wants to merge 38 commits into
base: Creating-Horror-Events-Using-Observer-Pattern-Setup
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
3a9e1a8
Removed All Events and Made Project Ready to Start
MalharDevasthali May 2, 2023
efaf2da
Potion Improved
MalharDevasthali May 2, 2023
9bee598
Cleaned PlayerController
MalharDevasthali May 2, 2023
76bc14e
Cleaned PlayerController
MalharDevasthali May 2, 2023
eab183c
Cleaned Camera
MalharDevasthali May 2, 2023
86eeb1e
LightSwitchTodo Setup
MalharDevasthali May 2, 2023
2019d02
Scripts Cleanup
MalharDevasthali May 2, 2023
14c8a77
Final Commit
MalharDevasthali May 2, 2023
991a1e8
Added Sound in MainMenu UI
MalharDevasthali May 2, 2023
b80def1
Added Light Switch Logic Using Delegate
MalharDevasthali May 6, 2023
2151d7c
Added Listerner in Player onLightSwitch
MalharDevasthali May 6, 2023
823019c
Added Listerner in Player onLightSwitch
MalharDevasthali May 6, 2023
de8ad2e
Removed getter
MalharDevasthali May 6, 2023
d3bbc42
Scene Changes
MalharDevasthali May 6, 2023
bb3d035
Final Commit
MalharDevasthali May 6, 2023
93702ae
Added Event Keyword
MalharDevasthali May 6, 2023
f22ab16
Converted event Delegate into event Action
MalharDevasthali May 6, 2023
5195b8d
Revert "Converted event Delegate into event Action"
MalharDevasthali May 6, 2023
0718b8d
Converted delegates into actions
MalharDevasthali May 6, 2023
d940963
Added Event Architecture
MalharDevasthali May 10, 2023
0694d29
Code Cleaning
MalharDevasthali May 10, 2023
818cbca
Added Bad Practice for Keys
MalharDevasthali May 10, 2023
7af302e
Code Cleanup
MalharDevasthali May 16, 2023
9db36ba
Setup
MalharDevasthali May 16, 2023
d4f36df
Setup for Chapter
MalharDevasthali May 17, 2023
e96e43a
Naming Improvements
MalharDevasthali May 22, 2023
f11fd00
Added EventController
Bharath200027 Oct 28, 2023
7f50271
Added EventService
Bharath200027 Oct 28, 2023
b1ab469
Added New Architecture for Events
Bharath200027 Oct 28, 2023
8e3d877
Added Generic Actions
Bharath200027 Oct 28, 2023
fdba271
Changes in KeyView Logic
Bharath200027 Oct 28, 2023
65daa2d
Added Listener for keys in PlayerController
Bharath200027 Oct 28, 2023
dd0909d
Added Camera Shake Event
Bharath200027 Oct 28, 2023
9127cab
Added GameUI effect
Bharath200027 Oct 28, 2023
55468f5
Added RatRush
Bharath200027 Oct 28, 2023
9a3d91c
Added PlayerSanity
Bharath200027 Oct 29, 2023
bc46fae
Added SkullDrop
Bharath200027 Oct 29, 2023
d8612ec
Added Potion Drink Event
Bharath200027 Oct 29, 2023
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/MainMenu.unity
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ NavMeshSettings:
debug:
m_Flags: 0
m_NavMeshData: {fileID: 0}
<<<<<<< HEAD
--- !u!1001 &466576513
=======
--- !u!1001 &457845157
>>>>>>> 160156fb4b631d530b8e2b29fa5e180dc6f4714d
PrefabInstance:
m_ObjectHideFlags: 0
serializedVersion: 2
Expand Down
12 changes: 9 additions & 3 deletions Assets/Scripts/Camera/CameraView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@ public class CameraView : MonoBehaviour
private void OnEnable()
{
EventService.Instance.OnLightsOffByGhostEvent.AddListener(Shake);
EventService.Instance.PlayerDeathEvent.AddListener(Shake);
EventService.Instance.OnPlayerDeathEvent.AddListener(Shake);
EventService.Instance.OnRatRush.AddListener(Shake);
EventService.Instance.OnSkullDrop.AddListener(Shake);

}

private void OnDisable()
{
EventService.Instance.OnLightsOffByGhostEvent.RemoveListener(Shake);
EventService.Instance.PlayerDeathEvent.RemoveListener(Shake);
EventService.Instance.OnPlayerDeathEvent.RemoveListener(Shake);
EventService.Instance.OnRatRush.RemoveListener(Shake);
EventService.Instance.OnSkullDrop.RemoveListener(Shake);

}

private void Start()
Expand Down Expand Up @@ -76,4 +82,4 @@ private void stopCoroutine(Coroutine coroutine)
StopCoroutine(coroutine);
coroutine = null;
}
}
}
6 changes: 5 additions & 1 deletion Assets/Scripts/Events.meta

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

27 changes: 16 additions & 11 deletions Assets/Scripts/Events/EventController.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
using System;

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

public class EventController
{
public event Action baseEvent;
public Action baseEvent;
public void AddListener(Action Listener) => baseEvent += Listener;

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

public void InvokeEvent() => baseEvent?.Invoke();
public void AddListener(Action listener) => baseEvent += listener;
public void RemoveListener(Action listener) => baseEvent -= listener;

}

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

7 changes: 3 additions & 4 deletions Assets/Scripts/Events/LightsOffByGhostEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
public class LightsOffByGhostEvent : MonoBehaviour
{
[SerializeField] private int keysRequiredToTrigger;
[SerializeField] private SoundType soundToPlay;

[SerializeField] private SoundType soundType;
private void OnTriggerEnter(Collider other)
{
if (other.GetComponent<PlayerView>() != null && GameService.Instance.GetPlayerController().KeysEquipped == keysRequiredToTrigger)
if(other.GetComponent<PlayerView>() != null && keysRequiredToTrigger == GameService.Instance.GetPlayerController().KeysEquipped)
{
EventService.Instance.OnLightsOffByGhostEvent.InvokeEvent();
GameService.Instance.GetSoundView().PlaySoundEffects(soundToPlay);
GameService.Instance.GetSoundView().PlaySoundEffects(soundType);
GetComponent<Collider>().enabled = false;
}
}
Expand Down
15 changes: 0 additions & 15 deletions Assets/Scripts/Events/PlayerEscapedEvent.cs

This file was deleted.

11 changes: 0 additions & 11 deletions Assets/Scripts/Events/PlayerEscapedEvent.cs.meta

This file was deleted.

1 change: 1 addition & 0 deletions Assets/Scripts/Events/RatRushEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ private void OnTriggerEnter(Collider other)
if (other.GetComponent<PlayerView>() != null)
{
onRatRush();
EventService.Instance.OnRatRush.InvokeEvent();
GameService.Instance.GetSoundView().PlaySoundEffects(soundToPlay);
GetComponent<Collider>().enabled = false;
}
Expand Down
1 change: 1 addition & 0 deletions Assets/Scripts/Events/SkullDropEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ private void OnTriggerEnter(Collider other)
if (other.GetComponent<PlayerView>() != null && GameService.Instance.GetPlayerController().KeysEquipped >= keysRequiredToTrigger)
{
OnSkullDrop();
EventService.Instance.OnSkullDrop.InvokeEvent();
GameService.Instance.GetSoundView().PlaySoundEffects(soundToPlay);
GetComponent<Collider>().enabled = false;
}
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/Interactables/KeyView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ public void Interact()
EventService.Instance.OnKeyPickedUp.InvokeEvent(++currentKeys);
gameObject.SetActive(false);
}
}
}
42 changes: 28 additions & 14 deletions Assets/Scripts/Interactables/LightSwitchView.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using UnityEngine;

Expand All @@ -6,28 +7,29 @@ public class LightSwitchView : MonoBehaviour, IInteractable
[SerializeField] private List<Light> lightsources = new List<Light>();
[SerializeField] private SoundType soundType;
private SwitchState currentState;
public static event Action OnLightSwitchToggled;

private void OnEnable()
{
EventService.Instance.OnLightSwitchToggled.AddListener(onLightsToggled);
EventService.Instance.OnLightsOffByGhostEvent.AddListener(onLightsOffByGhostEvent);
EventService.Instance.OnLightSwitchToggled.AddListener(onLightSwitch);
EventService.Instance.OnLightsOffByGhostEvent.AddListener(onLightsTurnedOffByGhost);
}

private void OnDisable()
{
EventService.Instance.OnLightSwitchToggled.RemoveListener(onLightsToggled);
EventService.Instance.OnLightsOffByGhostEvent.RemoveListener(onLightsOffByGhostEvent);
EventService.Instance.OnLightsOffByGhostEvent.RemoveListener(onLightsTurnedOffByGhost);
}

private void Start()
{
currentState = SwitchState.Off;
}

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


public void Interact()
{
GameService.Instance.GetInstructionView().HideInstruction();
EventService.Instance.OnLightSwitchToggled.InvokeEvent();
}

private void toggleLights()
{
bool lights = false;
Expand All @@ -53,21 +55,33 @@ 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;

foreach (Light lightSource in lightsources)
{
lightSource.enabled = lights;
currentState = SwitchState.Off;
}
}
private void onLightsOffByGhostEvent()

private void onLightSwitch()
{
toggleLights();
GameService.Instance.GetSoundView().PlaySoundEffects(SoundType.SwitchSound);
GameService.Instance.GetInstructionView().HideInstruction();
}
private void onLightsTurnedOffByGhost()
{
GameService.Instance.GetSoundView().PlaySoundEffects(soundType);
setLights(false);
GameService.Instance.GetSoundView().PlaySoundEffects(SoundType.SwitchSound);
GameService.Instance.GetInstructionView().ShowInstruction(InstructionType.LightsOff);
}

private void onLightsToggled()
{
toggleLights();
Expand Down
3 changes: 3 additions & 0 deletions Assets/Scripts/Interactables/PotionView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

public class PotionView : MonoBehaviour, IInteractable
{

[SerializeField] SoundType soundType;
private int potionEffect = 20;

public void Interact()
{

GameService.Instance.GetInstructionView().HideInstruction();
GameService.Instance.GetSoundView().PlaySoundEffects(soundType);
EventService.Instance.OnPotionDrink.InvokeEvent(potionEffect);
gameObject.SetActive(false);
}
}
50 changes: 32 additions & 18 deletions Assets/Scripts/Player/PlayerController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController
{
public bool IsInteracted;

private PlayerView playerView;
private PlayerScriptableObject playerScriptableObject;
private float velocity;
Expand All @@ -9,30 +14,32 @@ public class PlayerController
private float mouseX;
private PlayerState playerState;

public bool IsInteracted;
public int KeysEquipped { get => playerScriptableObject.KeysEquipped; set => playerScriptableObject.KeysEquipped = value; }
public PlayerState PlayerState { get => playerState; private set => playerState = value; }


public PlayerController(PlayerView playerView, PlayerScriptableObject playerScriptableObject)
{
this.playerView = playerView;
this.playerView.SetController(this);
this.playerScriptableObject = playerScriptableObject;
this.playerScriptableObject.KeysEquipped = 0;


playerState = PlayerState.InDark;

EventService.Instance.OnLightsOffByGhostEvent.AddListener(onLightsOffByGhost);
EventService.Instance.OnLightSwitchToggled.AddListener(onLightsToggled);
EventService.Instance.OnKeyPickedUp.AddListener(OnKeyPickedUp);
EventService.Instance.PlayerEscapedEvent.AddListener(DisableControls);
EventService.Instance.OnLightSwitchToggled.AddListener(LightSwitchToggled);
EventService.Instance.OnKeyPickedUp.AddListener(onKeysPickedUp);
EventService.Instance.OnLightsOffByGhostEvent.AddListener(onLightsTurnedOffByGhost);
}

~PlayerController()
{
EventService.Instance.OnLightsOffByGhostEvent.RemoveListener(onLightsOffByGhost);
EventService.Instance.OnLightSwitchToggled.RemoveListener(onLightsToggled);
EventService.Instance.OnKeyPickedUp.RemoveListener(OnKeyPickedUp);
EventService.Instance.PlayerEscapedEvent.RemoveListener(DisableControls);
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 All @@ -47,7 +54,7 @@ public void Jump(Rigidbody playerRigidbody, Transform transform)

public void Move(Rigidbody playerRigidbody, Transform transform)
{
getInput();
GetInput();

Quaternion rotation;
Vector3 position;
Expand All @@ -60,14 +67,9 @@ public void Move(Rigidbody playerRigidbody, Transform transform)
public void KillPlayer()
{
PlayerState = PlayerState.Dead;
EventService.Instance.PlayerDeathEvent.InvokeEvent();
}

private void onLightsOffByGhost() => PlayerState = PlayerState.InDark;
private void OnKeyPickedUp(int keys) => KeysEquipped = keys;
private void DisableControls() => playerView.enabled = false;

private void getInput()
private void GetInput()
{
horizontalAxis = Input.GetAxis("Horizontal");
verticalAxis = Input.GetAxis("Vertical");
Expand All @@ -82,11 +84,23 @@ private void calculatePositionRotation(Rigidbody playerRigidbody, Transform tran
rotation = playerRigidbody.rotation * Quaternion.Euler(lookRotation);
position = (transform.position) + (velocity * movement) * Time.fixedDeltaTime;
}
private void onLightsToggled()
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;
}
}
}
Loading