Skip to content

Commit

Permalink
AppHub is now a Unity singleton, so it is dependency injectable and n…
Browse files Browse the repository at this point in the history
…othing needs to manually create/destroy it.

Added TaskManager this allows objects to be registered for update, render, etc.
Added tests for TaskManager.
  • Loading branch information
Ashley Davis committed Mar 30, 2015
1 parent 285f92c commit 4efa717
Show file tree
Hide file tree
Showing 37 changed files with 9,526 additions and 28 deletions.
27 changes: 1 addition & 26 deletions App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,6 @@ private enum LogsDirectoryStatus
/// </summary>
private static readonly string SystemReportsPath = "System";

/// <summary>
/// Name of the game object automatically added to the scene that handles events on behalf of the app.
/// </summary>
public static readonly string AppHubObjectName = "_AppHub";

/// <summary>
/// Accessor for the singleton app instance.
/// </summary>
Expand Down Expand Up @@ -162,7 +157,7 @@ public App()
singletonManager.InstantiateSingletons(factory);
singletonManager.Startup();

var appHub = InitAppHub();
var appHub = factory.ResolveDep<IAppHub>();
appHub.Shutdown = () => singletonManager.Shutdown();

// Initialize errors for unhandled promises.
Expand Down Expand Up @@ -241,26 +236,6 @@ private static SingletonManager InitFactory(RSG.Utils.ILogger logger, Factory fa
return singletonManager;
}

/// <summary>
/// Helper function to initalize the app hub.
/// </summary>
private static AppHub InitAppHub()
{
var appHubGO = GameObject.Find(AppHubObjectName);
if (appHubGO == null)
{
appHubGO = new GameObject(AppHubObjectName);
GameObject.DontDestroyOnLoad(appHubGO);
}

var appHub = appHubGO.GetComponent<AppHub>();
if (appHub == null)
{
appHub = appHubGO.AddComponent<AppHub>();
}
return appHub;
}

/// <summary>
/// Get the factory instance.
/// </summary>
Expand Down
44 changes: 42 additions & 2 deletions AppHub.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand All @@ -9,12 +10,51 @@ namespace RSG
/// <summary>
/// Hub of the app, notifies important events.
/// </summary>
public class AppHub : MonoBehaviour
public interface IAppHub
{
/// <summary>
/// Callback invoked on app shutdown.
/// </summary>
public Action Shutdown;
Action Shutdown { get; set; }
}

/// <summary>
/// Hub of the app, notifies important events.
/// </summary>
[UnitySingleton(typeof(IAppHub))]
public class AppHub : MonoBehaviour, IAppHub
{
[Dependency]
public ITaskManager TaskManager { get; set; }

/// <summary>
/// Callback invoked on app shutdown.
/// </summary>
public Action Shutdown { get; set; }

protected void Update()
{
TaskManager.Update(Time.deltaTime);
}

protected void LateUpdate()
{
TaskManager.LateUpdate(Time.deltaTime);
}

public void OnRenderObject()
{
TaskManager.Render();
}

private IEnumerator EndOfFrame()
{
while (true)
{
yield return new WaitForEndOfFrame();
TaskManager.EndOfFrame();
}
}

/// <summary>
/// Called when the application is quiting.
Expand Down
51 changes: 51 additions & 0 deletions IUpdatable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RSG
{
/// <summary>
/// Interface to an updatable object.
/// </summary>
public interface IUpdatable
{
/// <summary>
/// Update the object.
/// </summary>
void Update(float timeDelta);
}

/// <summary>
/// Interface to a late updatable object.
/// </summary>
public interface ILateUpdatable
{
/// <summary>
/// Update the object.
/// </summary>
void LateUpdate(float timeDelta);
}

/// <summary>
/// Inteface for an object that gets an update after the end of each frame
/// </summary>
public interface IEndOfFrameUpdatable
{
/// <summary>
/// End of frame is reached
/// </summary>
void EndOfFrame();
}

/// <summary>
/// Interface for an object that is renderable.
/// </summary>
public interface IRenderable
{
/// <summary>
/// Render the object.
/// </summary>
void Render();
}
}
2 changes: 2 additions & 0 deletions RSG.UnityApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
<Compile Include="SettingsLoader.cs" />
<Compile Include="FormattedException.cs" />
<Compile Include="GameObjectCreator.cs" />
<Compile Include="IUpdatable.cs" />
<Compile Include="LogSystemInfo.cs" />
<Compile Include="MultiLogger.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand All @@ -80,6 +81,7 @@
<Compile Include="SerilogLogger.cs" />
<Compile Include="SerilogSinkAttribute.cs" />
<Compile Include="SerilogUnitySink.cs" />
<Compile Include="TaskManager.cs" />
<Compile Include="UnityLogger.cs" />
<Compile Include="UnitySingletonAttribute.cs" />
</ItemGroup>
Expand Down
6 changes: 6 additions & 0 deletions RSG.UnityApp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ VisualStudioVersion = 12.0.31101.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RSG.UnityApp", "RSG.UnityApp.csproj", "{D33A6F6D-49C7-4CE2-A2A9-0BCDBBBB12EA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{BFBB18BE-A0EB-42AB-94AD-294099877103}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -15,6 +17,10 @@ Global
{D33A6F6D-49C7-4CE2-A2A9-0BCDBBBB12EA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D33A6F6D-49C7-4CE2-A2A9-0BCDBBBB12EA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D33A6F6D-49C7-4CE2-A2A9-0BCDBBBB12EA}.Release|Any CPU.Build.0 = Release|Any CPU
{BFBB18BE-A0EB-42AB-94AD-294099877103}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BFBB18BE-A0EB-42AB-94AD-294099877103}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BFBB18BE-A0EB-42AB-94AD-294099877103}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BFBB18BE-A0EB-42AB-94AD-294099877103}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Loading

0 comments on commit 4efa717

Please sign in to comment.