Skip to content

Commit

Permalink
docs: CardGridController.cs documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
josemgmz committed Nov 11, 2024
1 parent 6b065ce commit 0ccbd78
Showing 1 changed file with 37 additions and 12 deletions.
49 changes: 37 additions & 12 deletions Assets/Scripts/Game/Entities/CardGrid/CardGridController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,34 @@

namespace Game.Entities.CardGrid
{
/// <summary>
/// Controls the card grid, handling the creation and cleanup of the grid.
/// </summary>
public class CardGridController : GameController<GameViewUI>
{
#region Lifecycle

/// <summary>
/// Event bus for handling game events.
/// </summary>
[Inject] private IGameEventBus _eventBus;

#endregion

#region Lifecycle Methods

/// <summary>
/// Registers event listeners for setting up and cleaning the grid.
/// </summary>
private void Awake()
{
_eventBus.AddListener<OnCardGridSetupEvent>(CreateGrid);
_eventBus.AddListener<OnRoundEndEvent>(CleanGrid);
}


/// <summary>
/// Unregisters event listeners when the controller is destroyed.
/// </summary>
private void OnDestroy()
{
_eventBus.RemoveListener<OnCardGridSetupEvent>(CreateGrid);
Expand All @@ -36,21 +48,25 @@ private void OnDestroy()

#region Methods

/// <summary>
/// Creates the card grid based on the provided event data.
/// </summary>
/// <param name="eventData">The event data containing grid setup information.</param>
private void CreateGrid(OnCardGridSetupEvent eventData)
{
//Get model
// Get model
var model = GetModel<CardGridModel>();
model.ResponsiveGridLayout.SetColumns(eventData.Columns);
model.ResponsiveGridLayout.SetSpacing(model.Spacing);
//Create the grid.

// Create the grid
eventData.Cards.ForEach(it =>
{
CreateCard(model.CardPrefab, it);
CreateCard(model.CardPrefab, it);
});
//Shuffle the cards

// Shuffle the cards
if (!eventData.Shuffle) return;
var children = new List<Transform>();
for (var i = 0; i < transform.childCount; i++)
Expand All @@ -59,20 +75,29 @@ private void CreateGrid(OnCardGridSetupEvent eventData)
children.ForEach(it => it.SetSiblingIndex(0));
}

/// <summary>
/// Creates a card in the grid.
/// </summary>
/// <param name="cardPrefab">The card prefab to instantiate.</param>
/// <param name="it">The event data for setting up the card.</param>
private void CreateCard(GameObject cardPrefab, OnCardSetupEvent it)
{
//Instantiate the card
// Instantiate the card
var card = Object.Instantiate(cardPrefab, transform);
card.name = $"Card {it.CardShape.GetString()} {it.CardType.GetString()}";
//Get the view and initialize it

// Get the view and initialize it
var cardView = card.GetComponent<CardView>();
cardView.Initialize<CardModel>(it.CardShape, it.CardType, true);
}


/// <summary>
/// Cleans up the card grid by destroying all child objects.
/// </summary>
/// <param name="eventData">The event data for ending the round.</param>
private void CleanGrid(OnRoundEndEvent eventData)
{
//Destroy
// Destroy
for (var i = 0; i < transform.childCount; i++)
Object.Destroy(transform.GetChild(i).gameObject);
}
Expand Down

0 comments on commit 0ccbd78

Please sign in to comment.