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

Created Non-Monobehavior script EventController #32

Open
wants to merge 3 commits into
base: Adding-Event-Architecture-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
13 changes: 13 additions & 0 deletions .idea/.idea.AGD_ObserverPattern_EventfulEscape/.idea/.gitignore

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

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

6 changes: 6 additions & 0 deletions .idea/.idea.AGD_ObserverPattern_EventfulEscape/.idea/vcs.xml

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

3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"dotnet.preferCSharpExtension": true
}
8 changes: 8 additions & 0 deletions Assets/Scripts/Events.meta

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

21 changes: 21 additions & 0 deletions Assets/Scripts/Events/EventController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;

public class EventController
{
public Action baseEvent;

public void AddListener(Action listener)
{
baseEvent += listener;
}

public void RemoveListener(Action listener)
{
baseEvent -= listener;
}

public void InvokeEvent()
{
baseEvent?.Invoke();
}
}
11 changes: 11 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.

8 changes: 4 additions & 4 deletions Assets/Scripts/Interactables/LightSwitchView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ public class LightSwitchView : MonoBehaviour, IInteractable
{
[SerializeField] private List<Light> lightsources = new List<Light>();
private SwitchState currentState;
public static event Action OnLightSwitchToggled;
//public static event Action OnLightSwitchToggled;

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

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

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

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

private void toggleLights()
{
Expand Down
4 changes: 2 additions & 2 deletions Assets/Scripts/Player/PlayerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ public PlayerController(PlayerView playerView, PlayerScriptableObject playerScri

this.playerScriptableObject = playerScriptableObject;
this.playerScriptableObject.KeysEquipped = 0;
LightSwitchView.OnLightSwitchToggled += onLightSwitch;
EventService.Instance.OnLightSwitchToggled.AddListener(onLightSwitch);
playerState = PlayerState.InDark;
}

~PlayerController()
{
LightSwitchView.OnLightSwitchToggled -= onLightSwitch;
EventService.Instance.OnLightSwitchToggled.RemoveListener(onLightSwitch);
}
public void Interact() => IsInteracted = Input.GetKeyDown(KeyCode.E) ? true : (Input.GetKeyUp(KeyCode.E) ? false : IsInteracted);

Expand Down
28 changes: 28 additions & 0 deletions Assets/Scripts/Service/EventService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

using Unity.VisualScripting;

public class EventService
{
private static EventService _instance;

public static EventService Instance
{
get
{
if (_instance == null)
{
_instance = new EventService();
}
return _instance;
}
}

public EventController OnLightSwitchToggled{get; private set;}
public EventService()
{
OnLightSwitchToggled = new EventController();
}



}// class
11 changes: 11 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.

2 changes: 1 addition & 1 deletion Packages/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"com.unity.2d.spriteshape": "7.0.7",
"com.unity.collab-proxy": "2.0.1",
"com.unity.feature.development": "1.0.1",
"com.unity.ide.rider": "3.0.18",
"com.unity.ide.rider": "3.0.34",
"com.unity.ide.visualstudio": "2.0.17",
"com.unity.ide.vscode": "1.2.5",
"com.unity.test-framework": "1.1.31",
Expand Down
12 changes: 6 additions & 6 deletions Packages/packages-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
"depth": 1,
"source": "registry",
"dependencies": {
"com.unity.burst": "1.5.1",
"com.unity.2d.sprite": "1.0.0",
"com.unity.mathematics": "1.1.0",
"com.unity.modules.uielements": "1.0.0",
"com.unity.burst": "1.5.1"
"com.unity.modules.uielements": "1.0.0"
},
"url": "https://packages.unity.com"
},
Expand All @@ -30,9 +30,9 @@
"depth": 0,
"source": "registry",
"dependencies": {
"com.unity.mathematics": "1.1.0",
"com.unity.2d.common": "6.0.6",
"com.unity.2d.path": "5.0.2",
"com.unity.2d.common": "6.0.6",
"com.unity.mathematics": "1.1.0",
"com.unity.modules.physics2d": "1.0.0"
},
"url": "https://packages.unity.com"
Expand Down Expand Up @@ -82,7 +82,7 @@
}
},
"com.unity.ide.rider": {
"version": "3.0.18",
"version": "3.0.34",
"depth": 0,
"source": "registry",
"dependencies": {
Expand Down Expand Up @@ -162,9 +162,9 @@
"depth": 0,
"source": "registry",
"dependencies": {
"com.unity.modules.audio": "1.0.0",
"com.unity.modules.director": "1.0.0",
"com.unity.modules.animation": "1.0.0",
"com.unity.modules.audio": "1.0.0",
"com.unity.modules.particlesystem": "1.0.0"
},
"url": "https://packages.unity.com"
Expand Down