in Games/TwoZeroFourEight/MainPage.xaml.cs [546:654]
private int AddAdjacent(int start, int end, int delta)
{
int doubledVal = 0;
int totalBonus = -1; //If it returns -1 then no additions happened regardless of whether they were bonus worthy
int cur = start;
for (int i = 0; i < _boardSize - 1; i++)
{
if ((Cells[cur + delta].IntVal != 0) && (Cells[cur].IntVal > 0) && (Cells[cur].IntVal == Cells[cur + delta].IntVal))
{
///Animate cell movement
var slideToCellVisual = ElementCompositionPreview.GetElementVisual(Buttons[cur]);
var compositor = slideToCellVisual.Compositor;
var slideFromCellVisual = ElementCompositionPreview.GetElementVisual(Buttons[cur + delta]);
//very brittle way to get visuals for the text fields inside the cell template
var lvp = VisualTreeHelper.GetChild(Buttons[cur], 0);
var border = VisualTreeHelper.GetChild(lvp, 0);
var grid = VisualTreeHelper.GetChild(border, 0);
var cellText = VisualTreeHelper.GetChild(grid, 0);
var answerText = VisualTreeHelper.GetChild(grid, 1);
var answerTextVisual = ElementCompositionPreview.GetElementVisual(answerText as FrameworkElement);
var cellTextVisual = ElementCompositionPreview.GetElementVisual(cellText as FrameworkElement);
CubicBezierEasingFunction easing = compositor.CreateCubicBezierEasingFunction(new Vector2(.86f, 0.0f), new Vector2(.07f, 1.00f));
///Scale the ToCell to breifly be twice the size and then back down to regular size
///
slideToCellVisual.CenterPoint = new System.Numerics.Vector3(slideToCellVisual.Size.X / 2, slideToCellVisual.Size.Y / 2, 0f);
var scaleUpAnimation = compositor.CreateVector3KeyFrameAnimation();
scaleUpAnimation.InsertKeyFrame(0f, new System.Numerics.Vector3(1.0f, 1.0f, 0f), easing);
scaleUpAnimation.InsertKeyFrame(0.5f, new System.Numerics.Vector3(1.2f, 1.2f, 0f), easing);
scaleUpAnimation.InsertKeyFrame(1f, new System.Numerics.Vector3(1.0f, 1.0f, 0f), easing);
scaleUpAnimation.Duration = TimeSpan.FromMilliseconds(_AddSpeed);
///Slide the ToCell back from the FromCell position
///Be sure to return it to its original position afterwards
var initialFromOffset = new Vector3(slideFromCellVisual.Offset.X, slideFromCellVisual.Offset.Y, slideFromCellVisual.Offset.Z);
var initialToOffset = new Vector3(slideToCellVisual.Offset.X, slideToCellVisual.Offset.Y, slideToCellVisual.Offset.Z);
var slideAnimation = compositor.CreateVector3KeyFrameAnimation();
slideAnimation.InsertKeyFrame(0, initialFromOffset);
slideAnimation.InsertKeyFrame(1, initialToOffset, easing);
slideAnimation.Duration = TimeSpan.FromMilliseconds(_SlideSpeed / 2);
//show cell text
var showCellTextAnimation = compositor.CreateScalarKeyFrameAnimation();
showCellTextAnimation.InsertKeyFrame(0, 0f);
showCellTextAnimation.InsertKeyFrame(1, 1f);
showCellTextAnimation.Duration = TimeSpan.FromMilliseconds(_AddSpeed * 2);
//Show answer
var showAnswerAnimation = compositor.CreateScalarKeyFrameAnimation();
showAnswerAnimation.InsertKeyFrame(0, 1f);
showAnswerAnimation.InsertKeyFrame(1, 0.0f);
showAnswerAnimation.Duration = TimeSpan.FromMilliseconds(_AddSpeed * 3);
var scaleAnswerAnimation = compositor.CreateVector3KeyFrameAnimation();
scaleAnswerAnimation.InsertKeyFrame(0f, new System.Numerics.Vector3(1.0f, 1.0f, 0f), easing);
scaleAnswerAnimation.InsertKeyFrame(0.5f, new System.Numerics.Vector3(1.5f, 1.5f, 0f), easing);
scaleAnswerAnimation.InsertKeyFrame(1f, new System.Numerics.Vector3(1.0f, 1.0f, 0f), easing);
scaleAnswerAnimation.Duration = TimeSpan.FromMilliseconds(_AddSpeed);
slideToCellVisual.StartAnimation(nameof(slideToCellVisual.Offset), slideAnimation);
_slideBatchAnimation.Suspend();
_addAdjacentBatchAnimation.Suspend();
slideToCellVisual.StartAnimation(nameof(slideToCellVisual.Scale), scaleUpAnimation);
if (_boardSpeed == BoardSpeed.Slow)
{
answerTextVisual.StartAnimation(nameof(answerTextVisual.Opacity), showAnswerAnimation);
answerTextVisual.StartAnimation(nameof(answerTextVisual.Scale), scaleAnswerAnimation);
cellTextVisual.StartAnimation(nameof(cellTextVisual.Opacity), showCellTextAnimation);
}
_addAdjacentBatchAnimation.Resume();
_slideBatchAnimation.Resume();
Canvas.SetZIndex(Buttons[cur], GetHighestButtonIndex() + 1);
if (_boardSpeed == BoardSpeed.Slow)
{
Cells[cur].AnswerString = Cells[cur].IntVal.ToString() + " + " + Cells[cur + delta].IntVal.ToString();
}
Cells[cur].IntVal += Cells[cur + delta].IntVal;
doubledVal = Cells[cur].IntVal;
if (totalBonus > -1)
{
totalBonus += MilestoneBonus(doubledVal);
}
else
{
totalBonus = 0;
totalBonus += MilestoneBonus(doubledVal);
}
if (doubledVal > HighTile)
{
HighTile = doubledVal;
}
Cells[cur + delta].IntVal = 0;
Score += Cells[cur].IntVal;
}
cur += delta;
}
return totalBonus;
}