Skip to content

Commit

Permalink
Added project for example
Browse files Browse the repository at this point in the history
  • Loading branch information
RimuruDev committed Nov 15, 2023
1 parent 01d8de2 commit afd4a6e
Show file tree
Hide file tree
Showing 628 changed files with 39,390 additions and 13 deletions.
624 changes: 611 additions & 13 deletions .gitignore

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions UnityActionUpdaterService/Assets/Internal.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions UnityActionUpdaterService/Assets/Internal/Codebase.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Resharper disable all
// **************************************************************** //
//
// Copyright (c) RimuruDev. All rights reserved.
// Contact me:
// - Gmail: rimuru.dev@gmail.com
// - GitHub: https://github.com/RimuruDev
// - LinkedIn: https://www.linkedin.com/in/rimuru/
// - GitHub Organizations: https://github.com/Rimuru-Dev
//
// **************************************************************** //

using Zenject;
using UnityEngine;
using RimuruDev.Internal.Codebase.ActionUpdater.Service;

namespace RimuruDev.Internal.Codebase.ActionUpdater.Dispatcher
{
/// <summary>
/// The ActionUpdateDispatcher is responsible for dispatching update calls
/// to the IActionUpdaterService. It ensures that these updates continue
/// across different scenes, as it persists across scene loads.
/// </summary>
[DisallowMultipleComponent]
public sealed class ActionUpdateDispatcher : MonoBehaviour
{
private IActionUpdaterService actionUpdater;

/// <summary>
/// Injects the IActionUpdaterService dependency.
/// This method is called by Zenject to provide the service instance.
/// </summary>
/// <param name="actionUpdater">The action updater service to be used for dispatching update events.</param>
[Inject]
private void Constructor(IActionUpdaterService actionUpdater) =>
this.actionUpdater = actionUpdater;

/// <summary>
/// Called when the script instance is being loaded.
/// Ensures this object is not destroyed when loading a new scene.
/// </summary>
private void Awake()
{
transform.SetParent(null);
DontDestroyOnLoad(gameObject);
}

/// <summary>
/// Called every fixed framerate frame. Delegates the call to the action updater service.
/// </summary>
private void FixedUpdate() =>
actionUpdater?.FixedUpdate();

/// <summary>
/// Called every frame. Delegates the call to the action updater service.
/// </summary>
private void Update() =>
actionUpdater?.Update();

/// <summary>
/// Called every frame after Update. Delegates the call to the action updater service.
/// </summary>
private void LateUpdate() =>
actionUpdater?.LateUpdate();

/// <summary>
/// Called when the application is quitting. Disposes the action updater service.
/// </summary>
private void OnApplicationQuit() =>
actionUpdater?.Dispose();
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// **************************************************************** //
//
// Copyright (c) RimuruDev. All rights reserved.
// Contact me:
// - Gmail: rimuru.dev@gmail.com
// - GitHub: https://github.com/RimuruDev
// - LinkedIn: https://www.linkedin.com/in/rimuru/
// - GitHub Organizations: https://github.com/Rimuru-Dev
//
// **************************************************************** //

using System;

namespace RimuruDev.Internal.Codebase.ActionUpdater.Enum
{
/// <summary>
/// Specifies the type of update method to which an action can be subscribed.
/// </summary>
[Serializable]
public enum UpdateType : byte
{
/// <summary>
/// Subscribe to the FixedUpdate method, which is called every fixed framerate frame.
/// Suitable for updates in physics calculations.
/// </summary>
FixedUpdate = 0,

/// <summary>
/// Subscribe to the Update method, which is called once per frame.
/// Suitable for most game logic.
/// </summary>
Update = 1,

/// <summary>
/// Subscribe to the LateUpdate method, which is called once per frame after Update.
/// Suitable for actions that require objects to be updated first.
/// </summary>
LateUpdate = 2,
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
// Resharper disable all
// **************************************************************** //
//
// Copyright (c) RimuruDev. All rights reserved.
// Contact me:
// - Gmail: rimuru.dev@gmail.com
// - GitHub: https://github.com/RimuruDev
// - LinkedIn: https://www.linkedin.com/in/rimuru/
// - GitHub Organizations: https://github.com/Rimuru-Dev
//
// **************************************************************** //

using System;
using UnityEngine;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using RimuruDev.Internal.Codebase.ActionUpdater.Enum;

namespace RimuruDev.Internal.Codebase.ActionUpdater.Service
{
/// <summary>
/// Manages the subscription of actions to Unity's update methods (FixedUpdate, Update, LateUpdate).
/// Allows for centralized control of update logic, with the ability to pause all actions.
/// </summary>
public sealed class ActionUpdaterService : IActionUpdaterService
{
private readonly List<Action> fixedUpdateActionCache = new();
private readonly List<Action> updateActionCache = new();
private readonly List<Action> lateUpdateActionCache = new();

private event Action OnFixedUpdate;
private event Action OnUpdate;
private event Action OnLateUpdate;

private bool isPause;

/// <summary>
/// Subscribes an action to a specified update method.
/// </summary>
/// <param name="updateable">The action to subscribe.</param>
/// <param name="updateType">The type of update method to subscribe to (FixedUpdate, Update, or LateUpdate).</param>
public void Subscribe([NotNull] Action updateable, UpdateType updateType)
{
switch (updateType)
{
case UpdateType.FixedUpdate:
OnFixedUpdate += updateable;
fixedUpdateActionCache.Add(updateable);
break;
case UpdateType.Update:
OnUpdate += updateable;
updateActionCache.Add(updateable);
break;
case UpdateType.LateUpdate:
OnLateUpdate += updateable;
lateUpdateActionCache.Add(updateable);
break;
default:
Debug.LogError($"ArgumentOutOfRangeException: {nameof(updateType)}, {updateType}");
break;
}
}

/// <summary>
/// Unsubscribes an action from a specified update method.
/// </summary>
/// <param name="updateable">The action to unsubscribe.</param>
/// <param name="updateType">The type of update method to unsubscribe from.</param>
public void Unsubscribe([NotNull] Action updateable, UpdateType updateType)
{
switch (updateType)
{
case UpdateType.FixedUpdate:
OnFixedUpdate -= updateable;
fixedUpdateActionCache.Remove(updateable);
break;
case UpdateType.Update:
OnUpdate -= updateable;
updateActionCache.Remove(updateable);
break;
case UpdateType.LateUpdate:
OnLateUpdate -= updateable;
lateUpdateActionCache.Remove(updateable);
break;
default:
Debug.LogError($"ArgumentOutOfRangeException: {nameof(updateType)}, {updateType}");
break;
}
}

/// <summary>
/// Invokes all actions subscribed to FixedUpdate.
/// </summary>
public void FixedUpdate()
{
if (isPause)
return;

OnFixedUpdate?.Invoke();
}

/// <summary>
/// Invokes all actions subscribed to Update.
/// </summary>
public void Update()
{
if (isPause)
return;

OnUpdate?.Invoke();
}

/// <summary>
/// Invokes all actions subscribed to LateUpdate.
/// </summary>
public void LateUpdate()
{
if (isPause)
return;

OnLateUpdate?.Invoke();
}

/// <summary>
/// Sets or unsets the pause state of the update actions.
/// </summary>
/// <param name="pause">True to pause, false to resume.</param>
public void SetPause(bool pause) =>
isPause = pause;

/// <summary>
/// Unsubscribes all actions from their respective update methods.
/// </summary>
public void Dispose()
{
UnsubscribeAll(fixedUpdateActionCache, ref OnFixedUpdate);
UnsubscribeAll(updateActionCache, ref OnUpdate);
UnsubscribeAll(lateUpdateActionCache, ref OnLateUpdate);
}

private static void UnsubscribeAll(ICollection<Action> actionList, ref Action eventDelegate)
{
eventDelegate = actionList.Aggregate(eventDelegate, (current, action) => current - action);

actionList.Clear();
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Resharper disable all
// **************************************************************** //
//
// Copyright (c) RimuruDev. All rights reserved.
// Contact me:
// - Gmail: rimuru.dev@gmail.com
// - GitHub: https://github.com/RimuruDev
// - LinkedIn: https://www.linkedin.com/in/rimuru/
// - GitHub Organizations: https://github.com/Rimuru-Dev
//
// **************************************************************** //

using System;
using System.Diagnostics.CodeAnalysis;
using RimuruDev.Internal.Codebase.ActionUpdater.Enum;

namespace RimuruDev.Internal.Codebase.ActionUpdater.Service
{
/// <summary>
/// Provides a service for subscribing actions to Unity's lifecycle methods.
/// </summary>
public interface IActionUpdaterService : IDisposable
{
/// <summary>
/// Subscribes an action to a specified update type.
/// </summary>
/// <param name="updateable">The action to subscribe.</param>
/// <param name="updateType">The type of update to subscribe to (FixedUpdate, Update, or LateUpdate).</param>
public void Subscribe([NotNull] Action updateable, UpdateType updateType);

/// <summary>
/// Unsubscribes an action from a specified update type.
/// </summary>
/// <param name="updateable">The action to unsubscribe.</param>
/// <param name="updateType">The type of update to unsubscribe from.</param>
public void Unsubscribe([NotNull] Action updateable, UpdateType updateType);

/// <summary>
/// Invokes actions subscribed to FixedUpdate.
/// </summary>
public void FixedUpdate();

/// <summary>
/// Invokes actions subscribed to Update.
/// </summary>
public void Update();

/// <summary>
/// Invokes actions subscribed to LateUpdate.
/// </summary>
public void LateUpdate();

/// <summary>
/// Pauses or resumes the execution of subscribed actions.
/// </summary>
/// <param name="pause">True to pause actions, false to resume.</param>
public void SetPause(bool pause);
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit afd4a6e

Please sign in to comment.