Skip to content

Commit

Permalink
Merge pull request #2 from DennyScott/AdvancedMonobehaviours
Browse files Browse the repository at this point in the history
Advanced monobehaviours
  • Loading branch information
DennyScott authored Jul 22, 2016
2 parents 8f8819d + ef0ef50 commit bd6d83a
Show file tree
Hide file tree
Showing 11 changed files with 212 additions and 80 deletions.
48 changes: 0 additions & 48 deletions Zephyr/AdvancedMonoBehaviour/GameRunner.cs

This file was deleted.

6 changes: 0 additions & 6 deletions Zephyr/AdvancedMonoBehaviour/IStartable.cs

This file was deleted.

6 changes: 0 additions & 6 deletions Zephyr/AdvancedMonoBehaviour/IUpdateable.cs

This file was deleted.

2 changes: 1 addition & 1 deletion Zephyr/EventSystem/Core/EventManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Zephyr.MonoBehaviours;
using Zephyr.MonoBehaviourAdditions;

namespace Zephyr.EventSystem.Core
{
Expand Down
10 changes: 4 additions & 6 deletions Zephyr/EventSystem/Core/EventManagerModel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#if UNIT_TEST
using System;
#else
#if !UNIT_TEST
using UnityEngine;
#endif
using System.Collections.Generic;
Expand Down Expand Up @@ -370,17 +368,17 @@ public class Debug
{
public static void Log(string s)
{
Console.WriteLine(s);
System.Console.WriteLine(s);
}

public static void LogWarning(string s)
{
Console.WriteLine(s);
System.Console.WriteLine(s);
}

public static void LogError(string s)
{
Console.WriteLine(s);
System.Console.WriteLine(s);
}

public static bool isDebugBuild = true;
Expand Down
181 changes: 181 additions & 0 deletions Zephyr/MonoBehaviourAdditions/AdvanceMonobehvaiourRunner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
using System;
using UnityEngine;
using Zephyr.Singletons;

namespace Zephyr.MonoBehaviourAdditions
{
/// <summary>
/// In charge of running the update loop. This update loop is more efficient then the unity built update loop. Later this class should be extended to include high priority update items adnd such to cusomize the level of priority
/// items in the update loop have.
/// </summary>
public class AdvanceMonoBehvaiourRunner : SingletonAsComponent<AdvanceMonoBehvaiourRunner>
{
#region Constants

private const int UpdateableSize = 200;
private const int UpdateableScaleFactor = 2;

#endregion

#region Private Attributes

private IUpdateable[] _updateableObjects = new IUpdateable[UpdateableSize];
private int _index;
private float _timePassed;
private bool _isNeedingCleanup;

#endregion

#region Properties

public static AdvanceMonoBehvaiourRunner Instance
{
get { return (AdvanceMonoBehvaiourRunner) _Instance; }
set { _Instance = value; }
}

#endregion

#region Public Methods

/// <summary>
/// Registers an updateable object into the Update loop
/// </summary>
/// <param name="obj">The object to register into the update loop</param>
public void RegisterUpdateableObject(IUpdateable obj)
{
AddNewUpdateable(obj);
obj.OnAwake();
obj.OnStart();
}

/// <summary>
/// Unregisters an update-able component from the update loop
/// </summary>
/// <param name="obj">The object to remove from the Update loop</param>
public bool UnregisterUpdateableObject(IUpdateable obj)
{
for (var i = 0; i < _index; i++)
{
if (_updateableObjects[i] != obj) continue;

_updateableObjects[i] = null;
_isNeedingCleanup = true;
return true;
}
return false;
}


/// <summary>
/// Updates once a frame.
/// </summary>
public void Update()
{
var delta = Time.deltaTime;
for (var i = 0; i < Instance._updateableObjects.Length; i++)
if (_updateableObjects[i] != null)
_updateableObjects[i].OnUpdate(delta);

if (!_isNeedingCleanup) return;

_timePassed += delta;

if (_timePassed < 1) return;

_timePassed = 0;
CleanUpUpdateables();
}

#endregion

#region Private Methods

/// <summary>
/// Adds a new updateable object to the updateables list. If needed, it will also increase the size
/// of the updateables array.
/// </summary>
/// <param name="obj">The updatable object to add to the array.</param>
private void AddNewUpdateable(IUpdateable obj)
{
if (_index == _updateableObjects.Length - 1)
Array.Resize(ref _updateableObjects, _updateableObjects.Length*UpdateableScaleFactor);

_updateableObjects[_index] = obj;
_index++;
}

/// <summary>
/// Cleans up the Updateables by removing the empty references and moving the objects up the array for faster
/// traversal and setting the new index.
/// </summary>
private void CleanUpUpdateables()
{
var placementIndex = FindFirstNull();

if (placementIndex == -1) return;

ShiftDownUpdateables(ref placementIndex);

ClearRemainingUpdateables(placementIndex + 1);

_index = placementIndex;
}

/// <summary>
/// Clears all positions between the new Index and old index as all items have been squeezed downwards in
/// the array.
/// </summary>
/// <param name="newIndex">The newIndex to clear from</param>
private void ClearRemainingUpdateables(int newIndex)
{
for (var i = newIndex; i < _index; i++)
_updateableObjects[i] = null;
}

/// <summary>
/// Shifts all elements down the array to be at the start for faster traversal and memory management
/// </summary>
/// <param name="placementIndex">The position to start the moving down from.</param>
private void ShiftDownUpdateables(ref int placementIndex)
{
for (var i = placementIndex + 1; i < _index; i++)
{
if (_updateableObjects[i] == null)
continue;

_updateableObjects[placementIndex] = _updateableObjects[i];
placementIndex++;
}
}

/// <summary>
/// Finds the first null in the updateable array and returns the index.
/// </summary>
/// <returns>The first null position. If not found, returns -1.</returns>
private int FindFirstNull()
{
for (var i = 0; i < _index; i++)
{
if (_updateableObjects[i] == null)
return _index;
}
return -1;
}

#endregion

#region Inherited Members

/// <summary>
/// Called when this object is destroyed.
/// </summary>
protected override void OnDestroy()
{
base.OnDestroy();
_updateableObjects = null;
}

#endregion
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
using UnityEngine;

namespace Zephyr.MonoBehaviours
namespace Zephyr.MonoBehaviourAdditions
{
public class AdvancedMonoBehaviour : MonoBehaviour, IUpdateable, IStartable
public class AdvancedMonoBehaviour : MonoBehaviour, IUpdateable
{
/// <summary>
/// Runs on Start for all objects.
/// </summary>
private void Start()
private void Awake()
{
OnStart();
GameRunner.Instance.RegisterUpdateableObject(this);
AdvanceMonoBehvaiourRunner.Instance.RegisterUpdateableObject(this);
}

/// <summary>
/// Runs when the gameobject or this component is destroyed.
/// </summary>
protected virtual void OnDestroy()
{
if (GameRunner.IsAlive)
GameRunner.Instance.UnregisterUpdateableObject(this);
if (AdvanceMonoBehvaiourRunner.IsAlive)
AdvanceMonoBehvaiourRunner.Instance.UnregisterUpdateableObject(this);
}

/// <summary>
Expand All @@ -32,5 +31,10 @@ public virtual void OnUpdate(float delta) { }
/// Must be used instead of Start in an AdvancedMonobehaviour. Acts in the place of start.
/// </summary>
public virtual void OnStart() { }

/// <summary>
/// Must be used instead of Awake in an AdvancedMonobehaviour. Acts in the place of start.
/// </summary>
public virtual void OnAwake() { }
}
}
10 changes: 10 additions & 0 deletions Zephyr/MonoBehaviourAdditions/IUpdateable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Zephyr.MonoBehaviourAdditions {
public interface IUpdateable
{
void OnUpdate(float delta);

void OnStart();

void OnAwake();
}
}
2 changes: 1 addition & 1 deletion Zephyr/Singletons/SimpleSingleton.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using UnityEngine;
using Zephyr.MonoBehaviours;
using Zephyr.MonoBehaviourAdditions;


namespace Zephyr.Singletons
Expand Down
2 changes: 1 addition & 1 deletion Zephyr/Singletons/SingletonAsComponent.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using UnityEngine;
using Zephyr.MonoBehaviours;
using Zephyr.MonoBehaviourAdditions;

namespace Zephyr.Singletons
{
Expand Down
7 changes: 3 additions & 4 deletions Zephyr/Zephyr.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,9 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="AdvancedMonoBehaviour\AdvancedMonoBehaviour.cs" />
<Compile Include="AdvancedMonoBehaviour\GameRunner.cs" />
<Compile Include="AdvancedMonoBehaviour\IStartable.cs" />
<Compile Include="AdvancedMonoBehaviour\IUpdateable.cs" />
<Compile Include="MonoBehaviourAdditions\AdvancedMonoBehaviour.cs" />
<Compile Include="MonoBehaviourAdditions\AdvanceMonobehvaiourRunner.cs" />
<Compile Include="MonoBehaviourAdditions\IUpdateable.cs" />
<Compile Include="Class1.cs" />
<Compile Include="EventSystem\Core\EventManager.cs" />
<Compile Include="EventSystem\Core\EventManagerModel.cs" />
Expand Down

0 comments on commit bd6d83a

Please sign in to comment.