using System;
using System.Collections.Generic;
using Chernobyl.Destruction;
namespace Chernobyl.Unity.Work
{
///
/// Interface for objects which perform some work on a regular basis.
///
public interface IUpdateable
{
// TODO: Replace the Chernobyl.Update.IUpdateable with this or vice versa.
///
/// Instructs the object to perform the work.
///
void Update();
}
///
/// A module that has components that require updates.
///
public interface IUpdateableModule
{
///
/// The components that require updates. Note that this list may be updated by the module.
///
IEnumerable Updateables { get; }
}
///
/// A module that has components that require fixed updates.
///
public interface IFixedUpdateableModule
{
///
/// The components that require updates. Note that this list may be updated by the module.
///
IEnumerable FixedUpdateables { get; }
}
///
/// Invokes a method when an .
///
public class FuncUpdateable : IUpdateable
{
/// The method to invoke on each update.
public FuncUpdateable(Action action)
{
_action = action;
}
///
public void Update() => _action();
readonly Action _action;
}
///
/// Utility and extension methods for .
///
public static class Updateable
{
///
/// Converts a to a .
///
public static IUpdateable From(Action action) => new FuncUpdateable(action);
}
///
/// Wraps disposal and updating in one type.
///
public class DisposableUpdateable : IDisposable, IUpdateable
{
/// The method to invoke when disposal occurs.
/// The method to invoke when update occurs.
public DisposableUpdateable(Action dispose, Action update)
: this(Disposable.From(dispose), Updateable.From(update))
{ }
/// The instance to invoke when disposal occurs.
/// The instance to invoke when update occurs.
public DisposableUpdateable(IDisposable disposable, IUpdateable updateable)
{
_disposable = disposable;
_updateable = updateable;
}
///
public void Dispose() => _disposable.Dispose();
///
public void Update() => _updateable.Update();
readonly IDisposable _disposable;
readonly IUpdateable _updateable;
}
}