cards/CardTelephone.cs

A Card subclass for a Telephone card in a card game. On a Match event where it was the first chosen card, it collects neighbouring cards around both chosen phones, counts duplicate card types, plays sound and shakes the two phones, then shakes any card type that touched the phones at least twice.

using Sandbox;
using System.Threading.Tasks;
using static Sandbox.Gizmo;

public class CardTelephone : Card
{
	public override bool ShouldHandleEvent( EventType eventType )
	{
		return eventType == EventType.Match && Manager.Instance.ChosenCards[0] == this;
	}

	public override async Task HandleEventAsync( EventType eventType )
	{
		Manager.Instance.PushEventMessage( this, eventType );

		var phone0 = Manager.Instance.ChosenCards[0];
		var phone1 = Manager.Instance.ChosenCards[1];

		List<Card> nearbyCards = new();

		// find nearby cards
		for ( int x = -1; x <= 1; x++ )
		{
			for ( int y = -1; y <= 1; y++ )
			{
				if ( x == 0 && y == 0 )
					continue;

				var nearCard0 = Manager.Instance.GetCardAtGridPos( phone0.GridPos + new IntVector2( x, y ) );
				if ( nearCard0 != null && nearCard0 != phone1 && !nearbyCards.Contains( nearCard0 ) )
					nearbyCards.Add( nearCard0 );

				var nearCard1 = Manager.Instance.GetCardAtGridPos( phone1.GridPos + new IntVector2( x, y ) );
				if ( nearCard1 != null && nearCard1 != phone0 && !nearbyCards.Contains( nearCard1 ) )
					nearbyCards.Add( nearCard1 );
			}
		}

		// tally up card types
		Dictionary<CardType, int> cardTypes = new();
		foreach ( Card nearCard in nearbyCards )
		{
			if ( cardTypes.ContainsKey( nearCard.CardType ) )
				cardTypes[nearCard.CardType]++;
			else
				cardTypes.Add( nearCard.CardType, 1 );
		}

		// find cards that had 2+ of their type touching the telephones
		List<Card> cardsToShake = new();
		foreach ( var pair in cardTypes )
		{
			if ( pair.Value >= 2 )
			{
				foreach ( Card nearCard in nearbyCards )
				{
					if ( nearCard.CardType == pair.Key )
						cardsToShake.Add( nearCard );
				}
			}
		}

		await Task.DelayRealtime( 200 );

		Manager.Instance.PlayCardSfxBetween( "telephone", phone0, phone1, volume: 0.6f, pitch: 1f );

		await Manager.Instance.ShakeCard( phone0, time: 0.6f, easingType: EasingType.QuadIn );
		await Manager.Instance.ShakeCard( phone1, time: 0.6f, easingType: EasingType.QuadIn );

		await Task.DelayRealtime( Game.Random.Int( 700, 900 ) );

		if ( cardsToShake.Count > 0 )
		{
			// todo: maracas (?)

			foreach ( Card card in cardsToShake )
			{
				await Manager.Instance.ShakeCard( card, time: 0.75f, easingType: EasingType.ExpoIn, playSfx: false );
			}

			Manager.Instance.PlayCardSfxBetween( "telephone", phone0, phone1, volume: 0.6f, pitch: 1.1f );

			await Task.DelayRealtime( 750 );
		}

		await Task.DelayRealtime( 300 );

		Manager.Instance.PopEventMessage();
	}
}