-
-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
…d. SubscribeWithDelay was introduced. Test scene.
- Loading branch information
There are no files selected for viewing
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.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using UnityEngine; | ||
using Assets.Scripts.Utils.GameEvents; | ||
using UnityEngine.UI; | ||
|
||
namespace Assets.Scripts.Tests.GameEvents | ||
{ | ||
public class EventsNotificator : MonoBehaviour | ||
{ | ||
public Text NotificationText; | ||
public float FadeOutTime; | ||
|
||
public void Awake() | ||
{ | ||
this.Subscribe(MetagameEvents.NewGameStarted, () => ShowText("New Game Started")); | ||
this.Subscribe(MetagameEvents.CoinsGot, _ => ShowText("Got coins: " + _)); | ||
this.Subscribe(MetagameEvents.GameFinished, _ => ShowText("Game finished, reason: " + _.Reason + ", score: " + _.Score)); | ||
} | ||
|
||
private void ShowText(string text) | ||
{ | ||
NotificationText.text = text; | ||
NotificationText.canvasRenderer.SetAlpha(1f); | ||
NotificationText.CrossFadeAlpha(0f, FadeOutTime, true); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using Assets.Scripts.Utils.GameEvents; | ||
|
||
namespace Assets.Scripts.Tests.GameEvents | ||
{ | ||
public static class MetagameEvents | ||
{ | ||
public static GameEvent NewGameStarted = new GameEvent(); | ||
public static GameEvent<int> CoinsGot = new GameEvent<int>(); | ||
public static GameEvent<GameFinishedArgs> GameFinished = new GameEvent<GameFinishedArgs>(); | ||
} | ||
|
||
public class GameFinishedArgs | ||
{ | ||
public GameFinishedReason Reason { get; private set; } | ||
public int Score { get; private set; } | ||
|
||
public GameFinishedArgs(GameFinishedReason reason, int score = 0) | ||
{ | ||
Reason = reason; | ||
Score = score; | ||
} | ||
} | ||
|
||
public enum GameFinishedReason | ||
{ | ||
Win, | ||
Lose, | ||
Restart | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using Assets.Scripts.Utils.GameEvents; | ||
using UnityEngine; | ||
|
||
namespace Assets.Scripts.Tests.GameEvents | ||
{ | ||
public class UIManager : MonoBehaviour | ||
{ | ||
public void RaiseNewGameStarted() | ||
{ | ||
MetagameEvents.NewGameStarted.Publish(); | ||
} | ||
|
||
public void RaiseCoinsGotEvent() | ||
{ | ||
MetagameEvents.CoinsGot.Publish(Random.Range(10, 100)); | ||
} | ||
|
||
public void RaiseGameWinEvent() | ||
{ | ||
MetagameEvents.GameFinished.Publish(new GameFinishedArgs(GameFinishedReason.Win, Random.Range(1, 10))); | ||
} | ||
|
||
public void RaiseGameLoseEvent() | ||
{ | ||
MetagameEvents.GameFinished.Publish(new GameFinishedArgs(GameFinishedReason.Lose)); | ||
} | ||
|
||
public void RaiseGameRestartEvent() | ||
{ | ||
MetagameEvents.GameFinished.Publish(new GameFinishedArgs(GameFinishedReason.Restart)); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using System; | ||
using System.Collections; | ||
using UnityEngine; | ||
|
||
namespace Assets.Scripts.Utils.GameEvents | ||
{ | ||
public class EventsListener : MonoBehaviour | ||
{ | ||
private readonly int _subscriberId; | ||
private static int _counter; | ||
|
||
public EventsListener() | ||
{ | ||
_subscriberId = ++_counter; | ||
} | ||
|
||
public void Subscribe(GameEvent gameEvent, Action callback) | ||
{ | ||
gameEvent.Subscribe(_subscriberId, callback); | ||
} | ||
|
||
public void Subscribe<TEventArgs>(GameEvent<TEventArgs> gameEvent, Action<TEventArgs> callback) | ||
{ | ||
gameEvent.Subscribe(_subscriberId, callback); | ||
} | ||
|
||
public void SubscribeWithDelay(GameEvent gameEvent, Action callback, float delay) | ||
{ | ||
gameEvent.Subscribe(_subscriberId, () => | ||
{ | ||
StartCoroutine(ExecuteAfterDelay(callback, delay)); | ||
}); | ||
} | ||
|
||
public void SubscribeWithDelay<TEventArgs>(GameEvent<TEventArgs> gameEvent, Action<TEventArgs> callback, float delay) | ||
{ | ||
gameEvent.Subscribe(_subscriberId, eventArgs => | ||
{ | ||
StartCoroutine(ExecuteAfterDelay(callback, eventArgs, delay)); | ||
}); | ||
} | ||
|
||
private IEnumerator ExecuteAfterDelay(Action callback, float delay) | ||
{ | ||
yield return new WaitForSeconds(delay); | ||
callback(); | ||
} | ||
|
||
private IEnumerator ExecuteAfterDelay<TEventArgs>(Action<TEventArgs> callback, TEventArgs eventArgs, float delay) | ||
{ | ||
yield return new WaitForSeconds(delay); | ||
callback(eventArgs); | ||
} | ||
|
||
protected void UnSubscribe<TEventArgs>(GameEvent<TEventArgs> gameEvent) | ||
{ | ||
gameEvent.UnSubscribe(_subscriberId); | ||
} | ||
|
||
protected void UnSubscribe(GameEvent gameEvent) | ||
{ | ||
gameEvent.UnSubscribe(_subscriberId); | ||
} | ||
} | ||
|
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
using UnityEngine; | ||
|
||
namespace Assets.Scripts.Utils.GameEvents | ||
{ | ||
public static class Extensions | ||
{ | ||
public static void Subscribe(this MonoBehaviour monoBehaviour, GameEvent gameEvent, Action action) | ||
{ | ||
EventsListener eventsListener = monoBehaviour.GetComponent<EventsListener>() ?? | ||
monoBehaviour.gameObject.AddComponent<EventsListener>(); | ||
eventsListener.Subscribe(gameEvent, action); | ||
} | ||
|
||
public static void Subscribe<TEventArgs>(this MonoBehaviour monoBehaviour, GameEvent<TEventArgs> gameEvent, Action<TEventArgs> action) | ||
{ | ||
EventsListener eventsListener = monoBehaviour.GetComponent<EventsListener>() ?? | ||
monoBehaviour.gameObject.AddComponent<EventsListener>(); | ||
eventsListener.Subscribe(gameEvent, action); | ||
} | ||
|
||
public static void SubscribeWithDelay(this MonoBehaviour monoBehaviour, GameEvent gameEvent, Action action, float delay) | ||
{ | ||
EventsListener eventsListener = monoBehaviour.GetComponent<EventsListener>() ?? | ||
monoBehaviour.gameObject.AddComponent<EventsListener>(); | ||
eventsListener.SubscribeWithDelay(gameEvent, action, delay); | ||
} | ||
|
||
public static void SubscribeWithDelay<TEventArgs>(this MonoBehaviour monoBehaviour, GameEvent<TEventArgs> gameEvent, Action<TEventArgs> action, float delay) | ||
{ | ||
EventsListener eventsListener = monoBehaviour.GetComponent<EventsListener>() ?? | ||
monoBehaviour.gameObject.AddComponent<EventsListener>(); | ||
eventsListener.SubscribeWithDelay(gameEvent, action, delay); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.