Skip to content

Commit

Permalink
docs: card controllers documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
josemgmz committed Nov 11, 2024
1 parent 0ccbd78 commit 709ff81
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 19 deletions.
49 changes: 39 additions & 10 deletions Assets/Scripts/Game/Entities/Card/CardAnimationController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,41 @@

namespace Game.Entities.Card
{
/// <summary>
/// Controls the card animations, handling the flip and match animations.
/// </summary>
public class CardAnimationController : GameController<CardView>
{
#region Services

/// <summary>
/// Service for managing audio playback.
/// </summary>
[Inject] private IAudioService _audioService;

#endregion

#region Lifecycle

/// <summary>
/// Initializes the card animation controller.
/// </summary>
private void Start()
{
//Setup the face of the card
// Setup the face of the card
var model = GetModel<CardModel>();
if (model.IsFlipped) return;
transform.rotation *= Quaternion.Euler(0, -180, 0);
}

#endregion

#region Events

/// <summary>
/// Handles the pointer up event to flip the card.
/// </summary>
/// <param name="other">The pointer event data.</param>
private void OnPointerUp(PointerEventData other)
{
var model = GetModel<CardModel>();
Expand All @@ -42,13 +55,20 @@ private void OnPointerUp(PointerEventData other)

#region Public Methods

/// <summary>
/// Flips the card with an optional sound effect.
/// </summary>
/// <param name="playSound">Whether to play a sound effect.</param>
public void FlipCard(bool playSound = true)
{
var model = GetModel<CardModel>();
if(model.FlipCardCoroutine != null) return;
if (model.FlipCardCoroutine != null) return;
model.FlipCardCoroutine = StartCoroutine(_FlipCard(playSound));
}


/// <summary>
/// Matches the card with an animation.
/// </summary>
public void MatchCard()
{
StartCoroutine(_MatchCard());
Expand All @@ -58,9 +78,14 @@ public void MatchCard()

#region Private Methods

/// <summary>
/// Coroutine to flip the card with an animation.
/// </summary>
/// <param name="playSound">Whether to play a sound effect.</param>
/// <returns>An enumerator for the animation.</returns>
private IEnumerator _FlipCard(bool playSound)
{
if(playSound) _audioService.PlaySfx(AudioData.Sfx.CardFlip);
if (playSound) _audioService.PlaySfx(AudioData.Sfx.CardFlip);
var model = GetModel<CardModel>();
var duration = model.RotationTime;
var startRotation = transform.rotation;
Expand All @@ -79,17 +104,21 @@ private IEnumerator _FlipCard(bool playSound)
transform.rotation = endRotation;
model.IsFlipped = !model.IsFlipped;
model.FlipCardCoroutine = null;
//Send event to check if the card is a match
if(model.IsFlipped)GetController<CardEventController>().SendOnCardFlippedEvent();

// Send event to check if the card is a match
if (model.IsFlipped) GetController<CardEventController>().SendOnCardFlippedEvent();
}

/// <summary>
/// Coroutine to match the card with an animation.
/// </summary>
/// <returns>An enumerator for the animation.</returns>
private IEnumerator _MatchCard()
{
var model = GetModel<CardModel>();
var duration = model.ScaleTime;
var startScale = transform.localScale;
var midScale = startScale * 1.1f; // 10% más grande
var midScale = startScale * 1.1f; // 10% larger
var endScale = Vector3.zero;

float elapsedTime = 0;
Expand Down
32 changes: 28 additions & 4 deletions Assets/Scripts/Game/Entities/Card/CardEventController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,45 @@

namespace Game.Entities.Card
{
/// <summary>
/// Controls the card events, handling card flip and match events.
/// </summary>
public class CardEventController : GameController<CardView>
{
#region Services


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

#endregion

#region Lifecycle

/// <summary>
/// Registers event listeners when the controller is awakened.
/// </summary>
private void Awake()
{
_gameEventBus.AddListener<OnRoundStartEvent>(OnCardFlipEvent);
}

/// <summary>
/// Unregisters event listeners when the controller is destroyed.
/// </summary>
public void OnDestroy()
{
_gameEventBus.RemoveListener<OnRoundStartEvent>(OnCardFlipEvent);
}

#endregion

#region Public Methods

/// <summary>
/// Sends the card flipped event.
/// </summary>
public void SendOnCardFlippedEvent()
{
var model = GetModel<CardModel>();
Expand All @@ -46,19 +61,28 @@ public void SendOnCardFlippedEvent()
#endregion

#region Private Methods


/// <summary>
/// Handles the failure of a card flip event.
/// </summary>
private async void OnFail()
{
await Task.Delay(250);
GetController<CardAnimationController>().FlipCard();
}


/// <summary>
/// Handles the success of a card flip event.
/// </summary>
private async void OnSuccess()
{
await Task.Delay(250);
GetController<CardAnimationController>().MatchCard();
}

/// <summary>
/// Handles the card flip event.
/// </summary>
private void OnCardFlipEvent()
{
GetController<CardAnimationController>().FlipCard(false);
Expand Down
32 changes: 27 additions & 5 deletions Assets/Scripts/Game/Entities/Card/CardImageController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,28 @@

namespace Game.Entities.Card
{
/// <summary>
/// Controls the card image, handling the front and back sprites and flipping the card.
/// </summary>
public class CardImageController : GameController<CardView>
{
#region Services

/// <summary>
/// Service for managing addressable assets.
/// </summary>
[Inject] private IGameAddressablesService _gameAddressablesService;

#endregion

#region Lifecycle


/// <summary>
/// Initializes the card image controller.
/// </summary>
private void Start()
{
//Get model
// Get model
var model = GetModel<CardModel>();
model.FrontSprite = GetFrontSprite(model.CardShape, model.CardType);
model.BackSprite = GetBackSprite();
Expand All @@ -35,21 +44,34 @@ private void Start()

#region Public Methods

/// <summary>
/// Flips the card image between the front and back sprites.
/// </summary>
public void FlipCard()
{
var model = GetModel<CardModel>();
model.Image.sprite = model.Image.sprite == model.BackSprite ? model.FrontSprite : model.BackSprite;
}

#endregion

#region Private Methods

/// <summary>
/// Gets the front sprite for the card based on its shape and type.
/// </summary>
/// <param name="cardShape">The shape of the card.</param>
/// <param name="cardType">The type of the card.</param>
/// <returns>The front sprite of the card.</returns>
private Sprite GetFrontSprite(CardShape cardShape, CardType cardType)
{
return _gameAddressablesService.GetCardSprite(cardShape, cardType);
}


/// <summary>
/// Gets the back sprite for the card.
/// </summary>
/// <returns>The back sprite of the card.</returns>
private Sprite GetBackSprite()
{
return _gameAddressablesService.GetCardSprite(CardShape.Back);
Expand Down

0 comments on commit 709ff81

Please sign in to comment.