forked from j-be/ContentSpawner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScreenPlayPlayer.cs
128 lines (100 loc) · 3.2 KB
/
ScreenPlayPlayer.cs
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using UnityEngine;
using System.Collections.Generic;
public class RingList<T> {
private T[] _itemList;
private uint _i = 0;
public RingList(T[] itemList) {
_itemList = itemList;
}
public T getNext() {
_i ++;
if (_i >= _itemList.Length)
_i = 0;
return _itemList[_i];
}
}
public class ScreenPlayPlayer : MonoBehaviour {
private ScreenPlay _screenPlay;
public AudioSource[] _audioSources;
public GameObject _imageContainer;
public GameObject[] _imageSources;
public float _imageSyncOffset = -7.0f;
public string screenPlayName;
public bool _autostart = false;
private RingList<AudioSource> _audioSourcesRing;
private RingList<GameObject> _imageSourcesRing;
private List<Spawner> _spawners = new List<Spawner>();
public void Start() {
Spawner.init(this);
_screenPlay = ScreenPlay.loadFromFile(screenPlayName);
_audioSourcesRing = new RingList<AudioSource>(_audioSources);
_imageSourcesRing = new RingList<GameObject> (_imageSources);
instanciateSpawners();
if (_autostart)
play();
}
private AudioSource getNextAudioSource() {
return _audioSourcesRing.getNext();
}
private GameObject getNextImageSource() {
return _imageSourcesRing.getNext();
}
private Spawner getSpawnerForItem(ScreenPlayItem screenPlayItem) {
Spawner spawner = null;
switch (screenPlayItem.type) {
case ItemType.audio:
Debug.Log("New audio spawner: " + screenPlayItem);
spawner = new AudioSpawner()
.setAudioSource(getNextAudioSource())
.setMediaObject(Resources.Load(screenPlayItem.url) as AudioClip);
break;
case ItemType.image:
Debug.Log("New image spawner: " + screenPlayItem);
spawner = new ImageSpawner()
.setContainer(_imageContainer)
.setImageSource(getNextImageSource())
.setMediaObject(Resources.Load(screenPlayItem.url) as Texture);
break;
default:
Debug.Log("Unknown item: " + screenPlayItem);
break;
}
return spawner;
}
private void instanciateSpawners() {
float currentTime = 0f;
Spawner spawner = null;
foreach (ScreenPlayItem item in _screenPlay.getItems()) {
spawner = getSpawnerForItem(item);
//currentTime = getNextTimestamp(currentTime, item.delay);
// Get next timestamp and spawn time
float spawnTime = currentTime;
switch (item.delay.type) {
case DelayType.absolute:
currentTime = item.delay.seconds;
spawnTime = currentTime;
break;
case DelayType.relativeToPrevious:
currentTime += item.delay.seconds;
spawnTime = currentTime;
break;
case DelayType.relativeSynchronized:
currentTime += item.delay.seconds;
spawnTime = currentTime + _imageSyncOffset;
break;
default:
Debug.Log("Unknown delay type: " + item.delay);
break;
}
if (spawner != null) {
Debug.Log("Spawning " + item + " after " + spawnTime);
spawner.setDelay(spawnTime);
_spawners.Add(spawner);
}
}
}
public void play() {
foreach (Spawner spawner in _spawners)
spawner.spawnDelayed();
}
}