Skip to content

Commit

Permalink
docs: mvc framework documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
josemgmz committed Nov 11, 2024
1 parent 709ff81 commit 9a8d14b
Show file tree
Hide file tree
Showing 8 changed files with 291 additions and 31 deletions.
51 changes: 47 additions & 4 deletions Assets/Scripts/Framework/GameAddressableContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,80 @@

namespace Framework
{
/// <summary>
/// Manages the loading and releasing of addressable assets in the game.
/// </summary>
public class GameAddressableContext
{
#region Variables

/// <summary>
/// List of async operation handles for tracking asset requests.
/// </summary>
private List<AsyncOperationHandle> _requestHandles = new ();

#endregion

#region Public Methods

/// <summary>
/// Asynchronously loads a list of assets by their keys.
/// </summary>
/// <typeparam name="TObject">The type of the assets to load.</typeparam>
/// <param name="keys">The keys of the assets to load.</param>
/// <param name="instantiate">Whether to instantiate the loaded assets.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the list of loaded assets.</returns>
public async Task<List<TObject>> LoadAssetsAsync<TObject>(List<string> keys, bool instantiate = true) where TObject : Object
{
var request = _LoadAssetsAsync<TObject>(keys);
_requestHandles.Add(request);
var result = await request.Task;
return instantiate ? result.Select(Object.Instantiate).ToList() : result.ToList();
}


/// <summary>
/// Asynchronously loads a single asset by its key.
/// </summary>
/// <typeparam name="TObject">The type of the asset to load.</typeparam>
/// <param name="key">The key of the asset to load.</param>
/// <param name="instantiate">Whether to instantiate the loaded asset.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the loaded asset.</returns>
public async Task<TObject> LoadAssetAsync<TObject>(string key, bool instantiate = true) where TObject : Object
{
var result = await LoadAssetsAsync<TObject>(new List<string> { key }, instantiate);
return result.FirstOrDefault();
}


/// <summary>
/// Synchronously loads a list of assets by their keys.
/// </summary>
/// <typeparam name="TObject">The type of the assets to load.</typeparam>
/// <param name="keys">The keys of the assets to load.</param>
/// <param name="instantiate">Whether to instantiate the loaded assets.</param>
/// <returns>The list of loaded assets.</returns>
public List<TObject> LoadAssets<TObject>(List<string> keys, bool instantiate = true) where TObject : Object
{
var request = _LoadAssetsAsync<TObject>(keys);
_requestHandles.Add(request);
var result = request.WaitForCompletion();
return instantiate ? result.Select(Object.Instantiate).ToList() : result.ToList();
}


/// <summary>
/// Synchronously loads a single asset by its key.
/// </summary>
/// <typeparam name="TObject">The type of the asset to load.</typeparam>
/// <param name="key">The key of the asset to load.</param>
/// <param name="instantiate">Whether to instantiate the loaded asset.</param>
/// <returns>The loaded asset.</returns>
public TObject LoadAsset<TObject>(string key, bool instantiate = true) where TObject : Object
{
return LoadAssets<TObject>(new List<string> { key }, instantiate).First();
}


/// <summary>
/// Releases all tracked asset requests.
/// </summary>
public void Release()
{
_requestHandles.Clear();
Expand All @@ -53,6 +90,12 @@ public void Release()

#region Private Methods

/// <summary>
/// Creates an async operation handle for loading a list of assets by their keys.
/// </summary>
/// <typeparam name="TObject">The type of the assets to load.</typeparam>
/// <param name="keys">The keys of the assets to load.</param>
/// <returns>The async operation handle for the asset loading request.</returns>
private AsyncOperationHandle<IList<TObject>> _LoadAssetsAsync<TObject>(List<string> keys) where TObject : Object
{
return Addressables.LoadAssetsAsync<TObject>(keys, null, Addressables.MergeMode.Union, false);
Expand Down
159 changes: 135 additions & 24 deletions Assets/Scripts/Framework/GameController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,44 @@

namespace Framework
{

/// <summary>
/// Abstract base class for game controllers, providing common functionality for managing views and models.
/// </summary>
/// <typeparam name="TView">The type of the view associated with the controller.</typeparam>
public abstract class GameController<TView> where TView : GameView
{
#region Properties


/// <summary>
/// The view associated with the controller.
/// </summary>
private TView _view = null;

/// <summary>
/// Dictionary to store models associated with the controller.
/// </summary>
private Dictionary<Type, object> _models;

/// <summary>
/// Gets or sets the transform of the view.
/// </summary>
protected Transform transform
{
get => _view.transform;
set => _view.transform.SetPositionAndRotation(value.position, value.rotation);
}

/// <summary>
/// Gets the game object of the view.
/// </summary>
protected GameObject gameObject
{
get => _view.gameObject;
}


/// <summary>
/// Gets the RectTransform component of the view.
/// </summary>
protected RectTransform rectTransform
{
get => _view.GetComponent<RectTransform>();
Expand All @@ -33,46 +52,90 @@ protected RectTransform rectTransform
#endregion

#region Getters and Setters



/// <summary>
/// Starts a coroutine on the view.
/// </summary>
/// <param name="coroutine">The coroutine to start.</param>
/// <returns>The started coroutine.</returns>
protected Coroutine StartCoroutine(IEnumerator coroutine)
{
return _view.StartCoroutine(coroutine);
}


/// <summary>
/// Instantiates a game object.
/// </summary>
/// <param name="value">The game object to instantiate.</param>
/// <returns>The instantiated game object.</returns>
protected GameObject Instantiate(GameObject value)
{
return GameObject.Instantiate(value);
}


/// <summary>
/// Instantiates a game object at a specified position and rotation.
/// </summary>
/// <param name="value">The game object to instantiate.</param>
/// <param name="position">The position to instantiate the game object at.</param>
/// <param name="rotation">The rotation to instantiate the game object with.</param>
/// <returns>The instantiated game object.</returns>
protected GameObject Instantiate(GameObject value, Vector3 position, Quaternion rotation)
{
return GameObject.Instantiate(value, position, rotation);
}


/// <summary>
/// Instantiates a game object at a specified position, rotation, and parent transform.
/// </summary>
/// <param name="value">The game object to instantiate.</param>
/// <param name="position">The position to instantiate the game object at.</param>
/// <param name="rotation">The rotation to instantiate the game object with.</param>
/// <param name="parent">The parent transform to instantiate the game object under.</param>
/// <returns>The instantiated game object.</returns>
protected GameObject Instantiate(GameObject value, Vector3 position, Quaternion rotation, Transform parent)
{
return GameObject.Instantiate(value, position, rotation, parent);
}


/// <summary>
/// Instantiates a game object under a specified parent transform.
/// </summary>
/// <param name="value">The game object to instantiate.</param>
/// <param name="parent">The parent transform to instantiate the game object under.</param>
/// <returns>The instantiated game object.</returns>
protected GameObject Instantiate(GameObject value, Transform parent)
{
return GameObject.Instantiate(value, parent);
}



/// <summary>
/// Gets a component of a specified type from the view.
/// </summary>
/// <typeparam name="T">The type of the component to get.</typeparam>
/// <returns>The component of the specified type.</returns>
protected T GetComponent<T>()
{
return _view.GetComponent<T>();
}


/// <summary>
/// Sets the view for the controller.
/// </summary>
/// <param name="view">The view to set.</param>
/// <exception cref="Exception">Thrown if the view is already set.</exception>
public void SetView(TView view)
{
if (_view != null) throw new Exception("View already exist, can't set new view");
_view = view;
}

/// <summary>
/// Sets a model for the controller.
/// </summary>
/// <typeparam name="T">The type of the model to set.</typeparam>
/// <param name="model">The model to set.</param>
/// <exception cref="Exception">Thrown if a model of the specified type already exists.</exception>
public void SetModel<T>(object model)
{
var type = typeof(T);
Expand All @@ -85,32 +148,67 @@ public void SetModel<T>(object model)
throw new Exception("Model already exist, can't set new model with this type");
}

/// <summary>
/// Gets a model of a specified type from the controller.
/// </summary>
/// <typeparam name="T">The type of the model to get.</typeparam>
/// <returns>The model of the specified type.</returns>
/// <exception cref="Exception">Thrown if a model of the specified type is not found.</exception>
protected T GetModel<T>()
{
var type = typeof(T);
return _models.ContainsKey(type) ? (T) _models[type] : throw new Exception("Model not found");
}


/// <summary>
/// Gets the view associated with the controller.
/// </summary>
/// <returns>The view associated with the controller.</returns>
public TView GetView() => _view;

/// <summary>
/// Gets a model of a specified type from another game object.
/// </summary>
/// <typeparam name="TControllerType">The type of the model to get.</typeparam>
/// <param name="value">The game object to get the model from.</param>
/// <returns>The model of the specified type.</returns>
/// <exception cref="Exception">Thrown if the game object does not have a GameView component.</exception>
protected TControllerType GetModelFromOtherObject<TControllerType>(GameObject value) where TControllerType : GameModel
{
var componentExist = value.TryGetComponent(out GameView viewComponent);
if(!componentExist) throw new Exception("GameView component not found");
return viewComponent.GetModel<TControllerType>();
}

/// <summary>
/// Gets a controller of a specified type.
/// </summary>
/// <typeparam name="ControllerType">The type of the controller to get.</typeparam>
/// <returns>The controller of the specified type.</returns>
protected ControllerType GetController<ControllerType>()
{
return GetControllerFromOtherObject<ControllerType>(gameObject);
}

/// <summary>
/// Gets a controller of a specified type from another game object.
/// </summary>
/// <typeparam name="TControllerType">The type of the controller to get.</typeparam>
/// <param name="value">The game object to get the controller from.</param>
/// <returns>The controller of the specified type.</returns>
/// <exception cref="Exception">Thrown if the game object does not have a GameView component.</exception>
protected TControllerType GetControllerFromOtherObject<TControllerType>(GameObject value)
{
var componentExist = value.TryGetComponent(out GameView viewComponent);
if(!componentExist) throw new Exception("GameView component not found");
return viewComponent.GetController<TControllerType>();
}

/// <summary>
/// Gets a list of child controllers of a specified type.
/// </summary>
/// <typeparam name="TControllerType">The type of the child controllers to get.</typeparam>
/// <returns>A list of child controllers of the specified type.</returns>
protected List<TControllerType> GetChildControllers<TControllerType>()
{
var controllers = new List<TControllerType>();
Expand All @@ -132,6 +230,11 @@ protected List<TControllerType> GetChildControllers<TControllerType>()
return controllers;
}

/// <summary>
/// Gets a list of child models of a specified type.
/// </summary>
/// <typeparam name="TModelType">The type of the child models to get.</typeparam>
/// <returns>A list of child models of the specified type.</returns>
protected List<TModelType> GetChildModels<TModelType>() where TModelType : GameModel
{
var models = new List<TModelType>();
Expand All @@ -144,21 +247,37 @@ protected List<TModelType> GetChildModels<TModelType>() where TModelType : GameM
}
return models;
}


/// <summary>
/// Gets a controller of a specified type from the parent game object.
/// </summary>
/// <typeparam name="TControllerType">The type of the controller to get.</typeparam>
/// <returns>The controller of the specified type.</returns>
/// <exception cref="Exception">Thrown if the parent game object does not have a GameView component.</exception>
protected TControllerType GetControllerFromParent<TControllerType>()
{
var componentExist = GetView().transform.parent.TryGetComponent(out GameView viewComponent);
if(!componentExist) throw new Exception("GameView component not found");
return viewComponent.GetController<TControllerType>();
}


/// <summary>
/// Gets a model of a specified type from the parent game object.
/// </summary>
/// <typeparam name="TModelType">The type of the model to get.</typeparam>
/// <returns>The model of the specified type.</returns>
/// <exception cref="Exception">Thrown if the parent game object does not have a GameView component.</exception>
protected TModelType GetModelFromParent<TModelType>() where TModelType : GameModel
{
var componentExist = GetView().transform.parent.TryGetComponent(out GameView viewComponent);
if(!componentExist) throw new Exception("GameView component not found");
return viewComponent.GetModel<TModelType>();
}


/// <summary>
/// Destroys the specified object or the view if no object is specified.
/// </summary>
/// <param name="value">The object to destroy. If null, the view is destroyed.</param>
protected void Destroy(Object value = null)
{
if (value == null)
Expand All @@ -169,14 +288,6 @@ protected void Destroy(Object value = null)
Object.Destroy(value);
}

/*
protected void SendEventToParent<ControllerType>(string functionName, params object[] data)
{
var componentExist = GetView().transform.parent.TryGetComponent(out GameView gameView);
if(!componentExist) throw new Exception("GameView component not found");
gameView.ReceiveEventFromChild<ControllerType>(functionName, data);
}*/

#endregion
}
}
Loading

0 comments on commit 9a8d14b

Please sign in to comment.