content system now has a event system

This commit is contained in:
Harrison Deng 2018-10-31 23:57:36 -05:00
parent 46dc862aed
commit 0e4a4564cf

View File

@ -11,8 +11,11 @@ using System.Threading.Tasks;
namespace RhythmBullet.Zer01HD.Utilities.ContentSystem
{
public delegate void ContentUpdate(String fileName, float completed);
public class ContentSystem
{
public event ContentUpdate UpdateEvent;
Thread thread;
readonly ContentManager contentManager;
readonly Queue<LoadableContent> queue;
@ -64,7 +67,7 @@ namespace RhythmBullet.Zer01HD.Utilities.ContentSystem
}
else
{
Debug.WriteLine("Did not queue asset due to asset with same name being loaded: " + assetName);
throw new InvalidOperationException("Did not queue asset due to asset with same name being loaded: " + assetName);
}
}
}
@ -84,12 +87,16 @@ namespace RhythmBullet.Zer01HD.Utilities.ContentSystem
private void LoadBatch()
{
int totalTasks = queue.Count;
int tasksCompleted = 0;
while (queue.Count != 0)
{
lock (queue)
{
LoadableContent content = queue.Dequeue();
Load(content.assetName, content.type);
tasksCompleted++;
OnProgress(content.assetName, (float)tasksCompleted / totalTasks);
}
}
}
@ -142,5 +149,10 @@ namespace RhythmBullet.Zer01HD.Utilities.ContentSystem
return queue.Count == 0;
}
}
protected virtual void OnProgress(string fileName, float progress)
{
UpdateEvent?.Invoke(fileName, progress);
}
}
}