aboutsummaryrefslogtreecommitdiff
blob: b864ae77f1eb655059975dc73acea0018bf73433 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System;
using System.Collections;
using System.Collections.Generic;

public class EventTimeline
{
	public class SpawnEvent
	{
		public float time;
		public float height;
		public ObstacleType type;
		
		public SpawnEvent(float time, float height, ObstacleType type)
		{
			this.time = time;
			this.height = height;
			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<SpawnEvent> OnSpawnEvent;
    public event Action<MessageEvent> OnMessageEvent;
	
	private List<SpawnEvent> futureEvents;
    private List<MessageEvent> futureMessages;
	private float currentTime;
	
	public EventTimeline()
	{
		futureEvents = new List<SpawnEvent>();
        futureMessages = new List<MessageEvent>();
		currentTime = 0;
	}
	
	public void Add(float time, float height, ObstacleType type)
	{
		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 && 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);
		}
	}
}