From 19d32b28f4888407a5c06ed84a751a2c35691d2b Mon Sep 17 00:00:00 2001 From: marcinzelent Date: Sat, 27 Apr 2019 18:00:48 +0200 Subject: Added messaging --- nGJ2019/Assets/Scripts/EventTimeline.cs | 28 +++++++++++++++++++++++++- nGJ2019/Assets/Scripts/Messenger.cs | 33 +++++++++++++++++++++++++++++++ nGJ2019/Assets/Scripts/ObstacleSpawner.cs | 6 ++++++ 3 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 nGJ2019/Assets/Scripts/Messenger.cs (limited to 'nGJ2019/Assets/Scripts') diff --git a/nGJ2019/Assets/Scripts/EventTimeline.cs b/nGJ2019/Assets/Scripts/EventTimeline.cs index 21b0bdb..b864ae7 100644 --- a/nGJ2019/Assets/Scripts/EventTimeline.cs +++ b/nGJ2019/Assets/Scripts/EventTimeline.cs @@ -17,15 +17,30 @@ public class EventTimeline this.type = type; } } + + public class MessageEvent + { + public float time; + public String message; + + public MessageEvent(float time, String message) + { + this.time = time; + this.message = message; + } + } public event Action OnSpawnEvent; + public event Action OnMessageEvent; private List futureEvents; + private List futureMessages; private float currentTime; public EventTimeline() { futureEvents = new List(); + futureMessages = new List(); currentTime = 0; } @@ -34,17 +49,28 @@ public class EventTimeline futureEvents.Add(new SpawnEvent(time, height, type)); futureEvents.Sort((x,y) => x.time.CompareTo(y.time)); } + + public void Add(float time, String message) + { + futureMessages.Add(new MessageEvent(time, message)); + futureMessages.Sort((x,y) => x.time.CompareTo(y.time)); + } public void timeTick(float deltaTime) { currentTime += deltaTime; - while(futureEvents.Count > 0 && currentTime > futureEvents[0].time) + while(futureEvents.Count > 0 && currentTime > futureEvents[0].time && futureMessages.Count > 0 && currentTime > futureMessages[0].time) { SpawnEvent e = futureEvents[0]; futureEvents.RemoveAt(0); if(OnSpawnEvent != null) OnSpawnEvent(e); + + MessageEvent m = futureMessages[0]; + futureMessages.RemoveAt(0); + if(OnMessageEvent != null) + OnMessageEvent(m); } } } diff --git a/nGJ2019/Assets/Scripts/Messenger.cs b/nGJ2019/Assets/Scripts/Messenger.cs new file mode 100644 index 0000000..ca2ffe1 --- /dev/null +++ b/nGJ2019/Assets/Scripts/Messenger.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.UI; + +public class Messenger : MonoBehaviour +{ + public Text MainText; + public ObstacleSpawner Spawner; + private EventTimeline timeline; + + // Start is called before the first frame update + void Start() + { + timeline = Spawner.GetEventTimeline(); + + timeline.Add(1, "Test"); + + timeline.OnMessageEvent += MessageOnEvent; + } + + // Update is called once per frame + void Update() + { + + } + + void MessageOnEvent(EventTimeline.MessageEvent e) + { + MainText.text = e.message; + } +} diff --git a/nGJ2019/Assets/Scripts/ObstacleSpawner.cs b/nGJ2019/Assets/Scripts/ObstacleSpawner.cs index c285352..f438e85 100644 --- a/nGJ2019/Assets/Scripts/ObstacleSpawner.cs +++ b/nGJ2019/Assets/Scripts/ObstacleSpawner.cs @@ -8,6 +8,7 @@ public class ObstacleSpawner : MonoBehaviour public GameObject caveWallsPrefab, rockTopPrefab, rockBottomPrefab, narrowPassagePrefab, rockJawsPrefab; public LevelScrolling scrolling; + public Messenger messenger; public float spawnLine; @@ -63,4 +64,9 @@ public class ObstacleSpawner : MonoBehaviour Gizmos.color = Color.green; Gizmos.DrawLine(new Vector3(spawnLine, -10, 0), new Vector3(spawnLine, 10, 0)); } + + public EventTimeline GetEventTimeline() + { + return timeline; + } } -- cgit v1.2.3