Commit
- 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,21 @@ | ||
using UnityEngine; | ||
using System; | ||
using System.Collections; | ||
|
||
namespace GameFoundations | ||
{ | ||
public enum MessagePriority | ||
{ | ||
Low, | ||
Medium, | ||
High | ||
} | ||
|
||
public interface IMessageEvent | ||
{ | ||
DateTime timeRaised { get; } | ||
float displayTime { get; } | ||
MessagePriority priority { get; } | ||
object message { get; } | ||
} | ||
} |
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,22 @@ | ||
using UnityEngine; | ||
using System; | ||
using System.Collections; | ||
|
||
namespace GameFoundations | ||
{ | ||
public class MessageEvent : GameEvent, IMessageEvent | ||
{ | ||
public DateTime timeRaised { private set; get; } | ||
public MessagePriority priority { private set; get; } | ||
public float displayTime { private set; get; } | ||
public object message { private set; get; } | ||
|
||
public MessageEvent (object message, float displayTime, MessagePriority priority) | ||
{ | ||
this.message = message; | ||
this.displayTime = displayTime; | ||
this.priority = priority; | ||
timeRaised = DateTime.Now; | ||
} | ||
} | ||
} |
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,103 @@ | ||
using UnityEngine; | ||
using UnityEngine.UI; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
|
||
namespace GameFoundations | ||
{ | ||
public class MessageQueue : MonoBehaviour | ||
{ | ||
public bool logToConsole = false; | ||
public bool prependDateTime = false; | ||
|
||
[Header ("Message Colours")] | ||
public Color | ||
highPriorityColour = Color.red; | ||
public Color normalPriorityColour = Color.black; | ||
public Color lowPriorityColour = Color.white; | ||
|
||
[Header ("Message Font Style")] | ||
public FontStyle | ||
highPriorityStyle = FontStyle.Bold; | ||
public FontStyle normalPriorityStyle = FontStyle.Normal; | ||
public FontStyle lowPriorityStyle = FontStyle.Normal; | ||
|
||
[Header ("Message Location")] | ||
public Vector2 | ||
queueLocation = new Vector2 (25, 25); | ||
public Vector2 messageSize = new Vector2 (200, 15); | ||
|
||
private static readonly GUIStyle LOW_PRIORTY = new GUIStyle (), NORMAL_PRIORITY = new GUIStyle (), HIGH_PRIORITY = new GUIStyle (); | ||
|
||
private List<IMessageEvent> _pending = new List<IMessageEvent> (); | ||
|
||
void OnEnable () | ||
{ | ||
if (_pending.Count > 0) { | ||
_pending.Clear (); | ||
} | ||
|
||
LOW_PRIORTY.normal.textColor = lowPriorityColour; | ||
LOW_PRIORTY.fontStyle = lowPriorityStyle; | ||
|
||
NORMAL_PRIORITY.normal.textColor = normalPriorityColour; | ||
NORMAL_PRIORITY.fontStyle = normalPriorityStyle; | ||
|
||
HIGH_PRIORITY.normal.textColor = highPriorityColour; | ||
HIGH_PRIORITY.fontStyle = highPriorityStyle; | ||
|
||
Events.instance.AddListener<MessageEvent> (OnMessage); | ||
} | ||
|
||
void OnDisable () | ||
{ | ||
Events.instance.RemoveListener<MessageEvent> (OnMessage); | ||
} | ||
|
||
void Update () | ||
{ | ||
for (int i = _pending.Count - 1; i>=0; i--) { | ||
if (Time.time > _pending [i].displayTime) | ||
_pending.RemoveAt (i); | ||
} | ||
} | ||
|
||
void OnMessage (IMessageEvent e) | ||
{ | ||
_pending.Add (e); | ||
|
||
if (logToConsole) { | ||
Debug.Log ("Message Recieved [" + System.DateTime.Now + "]: " + e.message.ToString ()); | ||
} | ||
} | ||
|
||
void OnGUI () | ||
{ | ||
float yPos = queueLocation.y; | ||
|
||
foreach (var m in _pending) { | ||
|
||
GUIStyle style = GetMessageStyle (m); | ||
|
||
string message = (prependDateTime) ? "[" + m.timeRaised + "]: " + m.message.ToString () : m.message.ToString (); | ||
|
||
GUI.Label (new Rect (queueLocation.x, yPos, messageSize.x, messageSize.y), message, style); | ||
|
||
yPos += messageSize.y; | ||
} | ||
} | ||
|
||
GUIStyle GetMessageStyle (IMessageEvent e) | ||
{ | ||
switch (e.priority) { | ||
case MessagePriority.Low: | ||
return LOW_PRIORTY; | ||
case MessagePriority.Medium: | ||
return NORMAL_PRIORITY; | ||
default: | ||
return HIGH_PRIORITY; | ||
} | ||
|
||
} | ||
} | ||
} |
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,24 @@ | ||
using UnityEngine; | ||
using System.Collections; | ||
using GameFoundations; | ||
|
||
public class MessageQueueTest : MonoBehaviour | ||
{ | ||
private int count = 1; | ||
|
||
void Update () | ||
{ | ||
if (Input.GetMouseButtonDown (0)) { | ||
MessagePriority priorty = MessagePriority.High; | ||
|
||
float random = Random.value; | ||
if (random < 0.333f) { | ||
priorty = MessagePriority.Low; | ||
} else if (random < 0.666f) { | ||
priorty = MessagePriority.Medium; | ||
} | ||
|
||
Events.instance.Raise (new MessageEvent ("Test Message " + count++, Time.time + 3f, priorty)); | ||
} | ||
} | ||
} |
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,71 @@ | ||
using UnityEngine; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
|
||
namespace GameFoundations | ||
{ | ||
public class Events | ||
{ | ||
private static Events _instance; | ||
public static Events instance { | ||
get { | ||
if (_instance == null) { | ||
_instance = new Events (); | ||
} | ||
|
||
return _instance; | ||
} | ||
} | ||
|
||
public delegate void EventDelegate<T> (T e) where T: GameEvent; | ||
private delegate void EventDelegate (GameEvent e); | ||
|
||
private Dictionary<System.Type, EventDelegate> delegates = new Dictionary<System.Type, EventDelegate> (); | ||
private Dictionary<System.Delegate, EventDelegate> delegateLookup = new Dictionary<System.Delegate, EventDelegate> (); | ||
|
||
public void AddListener<T> (EventDelegate<T> del) where T : GameEvent | ||
{ | ||
|
||
EventDelegate internalDelegate = (e) => del ((T)e); | ||
if (delegateLookup.ContainsKey (del) && delegateLookup [del] == internalDelegate) { | ||
return; | ||
} | ||
|
||
delegateLookup [del] = internalDelegate; | ||
|
||
EventDelegate tempDel; | ||
if (delegates.TryGetValue (typeof(T), out tempDel)) { | ||
delegates [typeof(T)] = tempDel += internalDelegate; | ||
} else { | ||
delegates [typeof(T)] = internalDelegate; | ||
} | ||
} | ||
|
||
public void RemoveListener<T> (EventDelegate<T> del) where T : GameEvent | ||
{ | ||
EventDelegate internalDelegate; | ||
if (delegateLookup.TryGetValue (del, out internalDelegate)) { | ||
EventDelegate tempDel; | ||
if (delegates.TryGetValue (typeof(T), out tempDel)) { | ||
tempDel -= internalDelegate; | ||
if (tempDel == null) { | ||
delegates.Remove (typeof(T)); | ||
} else { | ||
delegates [typeof(T)] = tempDel; | ||
} | ||
} | ||
|
||
delegateLookup.Remove (del); | ||
} | ||
} | ||
|
||
public void Raise (GameEvent e) | ||
{ | ||
EventDelegate del; | ||
if (delegates.TryGetValue (e.GetType (), out del)) { | ||
del.Invoke (e); | ||
} | ||
} | ||
|
||
} | ||
} |
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,10 @@ | ||
using UnityEngine; | ||
using System.Collections; | ||
|
||
namespace GameFoundations | ||
{ | ||
public abstract class GameEvent | ||
{ | ||
|
||
} | ||
} |
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,2 @@ | ||
m_EditorVersion: 5.4.1f1 | ||
m_StandardAssetsVersion: 0 |