Skip to content

Commit bc39fb5

Browse files
committed
session controller added.
1 parent ff010d4 commit bc39fb5

File tree

3 files changed

+124
-34
lines changed

3 files changed

+124
-34
lines changed

Audio/AudioController.cs

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,20 @@ private enum AudioAction {
2121
RESTART
2222
}
2323

24-
private struct AudioJob {
24+
[System.Serializable]
25+
public class AudioObject {
26+
public AudioType type;
27+
public AudioClip clip;
28+
}
29+
30+
[System.Serializable]
31+
public class AudioTrack
32+
{
33+
public AudioSource source;
34+
public AudioObject[] audio;
35+
}
36+
37+
private class AudioJob {
2538
public AudioAction action;
2639
public AudioType type;
2740
public bool fade;
@@ -78,24 +91,46 @@ private void Dispose() {
7891
}
7992

8093
private void AddJob(AudioJob _job) {
81-
// cancel the job if one exists with the same type
82-
if (m_JobTable.ContainsKey(_job.type)) {
83-
Log("Stopping job on ["+_job.type+"] for new operation: "+_job.action);
84-
IEnumerator _runningJob = (IEnumerator)m_JobTable[_job.type];
85-
StopCoroutine(_runningJob);
86-
m_JobTable.Remove(_job.type);
87-
}
88-
89-
if (GetAudioTrack(_job.type, _job.action.ToString()) == null) {
90-
return;
91-
}
94+
// cancel any job that might be using this job's audio source
95+
RemoveConflictingJobs(_job.type);
9296

9397
IEnumerator _jobRunner = RunAudioJob(_job);
9498
m_JobTable.Add(_job.type, _jobRunner);
9599
StartCoroutine(_jobRunner);
96100
Log("Starting job on ["+_job.type+"] with operation: "+_job.action);
97101
}
98102

103+
private void RemoveJob(AudioType _type) {
104+
if (!m_JobTable.ContainsKey(_type)) {
105+
Log("Trying to stop a job ["+_type+"] that is not running.");
106+
return;
107+
}
108+
IEnumerator _runningJob = (IEnumerator)m_JobTable[_type];
109+
StopCoroutine(_runningJob);
110+
m_JobTable.Remove(_type);
111+
}
112+
113+
private void RemoveConflictingJobs(AudioType _type) {
114+
// cancel the job if one exists with the same type
115+
if (m_JobTable.ContainsKey(_type)) {
116+
RemoveJob(_type);
117+
}
118+
119+
// cancel jobs that share the same audio track
120+
AudioType _conflictAudio = AudioType.None;
121+
foreach (DictionaryEntry _entry in m_JobTable) {
122+
AudioType _audioType = (AudioType)_entry.Key;
123+
AudioTrack _audioTrackInUse = GetAudioTrack(_audioType, "Get Audio Track In Use");
124+
AudioTrack _audioTrackNeeded = GetAudioTrack(_type, "Get Audio Track Needed");
125+
if (_audioTrackInUse.source == _audioTrackNeeded.source) {
126+
_conflictAudio = _audioType;
127+
}
128+
}
129+
if (_conflictAudio != AudioType.None) {
130+
RemoveJob(_conflictAudio);
131+
}
132+
}
133+
99134
private IEnumerator RunAudioJob(AudioJob _job) {
100135
yield return new WaitForSeconds(_job.delay);
101136

Audio/AudioTrack.cs

Lines changed: 0 additions & 20 deletions
This file was deleted.

Session/SessionController.cs

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,88 @@
11

22
using UnityEngine;
3+
using UnityCore.Menu;
34

45
namespace UnityCore {
5-
6+
67
namespace Session {
78

89
public class SessionController : MonoBehaviour
910
{
11+
public static SessionController instance;
12+
13+
private long m_SessionStartTime;
14+
private bool m_IsPaused;
15+
private GameController m_Game;
16+
private float m_FPS;
17+
18+
public long sessionStartTime {
19+
get {
20+
return m_SessionStartTime;
21+
}
22+
}
23+
24+
public float fps {
25+
get {
26+
return m_FPS;
27+
}
28+
}
1029

30+
#region Unity Functions
31+
private void Awake() {
32+
Configure();
33+
}
34+
35+
private void OnApplicationFocus(bool _focus) {
36+
if (_focus) {
37+
// Open a window to unpause the game
38+
PageController.instance.TurnPageOn(PageType.PausePopup);
39+
} else {
40+
// Flag the game paused
41+
m_IsPaused = true;
42+
}
43+
}
44+
45+
private void Update() {
46+
if (m_IsPaused) return;
47+
m_Game.OnUpdate();
48+
m_FPS = Time.frameCount / Time.time;
49+
}
50+
#endregion
51+
52+
#region Public Functions
53+
public void InitializeGame(GameController _game) {
54+
m_Game = _game;
55+
m_Game.OnInit();
56+
}
57+
58+
public void UnPause() {
59+
m_IsPaused = false;
60+
}
61+
#endregion
62+
63+
#region Private Functions
64+
/// <summary>
65+
/// Initialize the singleton pattern!
66+
/// </summary>
67+
private void Configure() {
68+
if (!instance) {
69+
instance = this;
70+
StartSession();
71+
DontDestroyOnLoad(gameObject);
72+
} else {
73+
Destroy(gameObject);
74+
}
75+
}
76+
77+
private void StartSession() {
78+
m_SessionStartTime = EpochSeconds();
79+
}
80+
81+
private long EpochSeconds() {
82+
var _epoch = new System.DateTimeOffset(System.DateTime.UtcNow);
83+
return _epoch.ToUnixTimeSeconds();
84+
}
85+
#endregion
1186
}
1287
}
13-
}
88+
}

0 commit comments

Comments
 (0)