Skip to content

Commit

Permalink
game-events: API simplification, GameEventArgs abstraction was delete…
Browse files Browse the repository at this point in the history
…d. SubscribeWithDelay was introduced. Test scene.
  • Loading branch information
nubick committed Aug 5, 2017
1 parent 0e2d1b7 commit 1641819
Show file tree
Hide file tree
Showing 20 changed files with 292 additions and 152 deletions.
Binary file added sources/Assets/Scenes/GameEvents.unity
Binary file not shown.
8 changes: 8 additions & 0 deletions sources/Assets/Scenes/GameEvents.unity.meta

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

9 changes: 9 additions & 0 deletions sources/Assets/Scripts/Tests/GameEvents.meta

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

26 changes: 26 additions & 0 deletions sources/Assets/Scripts/Tests/GameEvents/EventsNotificator.cs
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.

30 changes: 30 additions & 0 deletions sources/Assets/Scripts/Tests/GameEvents/MetagameEvents.cs
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.

33 changes: 33 additions & 0 deletions sources/Assets/Scripts/Tests/GameEvents/UIManager.cs
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.

46 changes: 0 additions & 46 deletions sources/Assets/Scripts/Utils/GameEvents/CommandMonoBehaviour.cs

This file was deleted.

66 changes: 66 additions & 0 deletions sources/Assets/Scripts/Utils/GameEvents/EventsListener.cs
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.

36 changes: 36 additions & 0 deletions sources/Assets/Scripts/Utils/GameEvents/Extensions.cs
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);
}
}
}
12 changes: 12 additions & 0 deletions sources/Assets/Scripts/Utils/GameEvents/Extensions.cs.meta

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

Loading

0 comments on commit 1641819

Please sign in to comment.