public void AnimateSlideAndMerge() { for (int i = 0; i < gl.SIZE; i++) { for (int j = 0; j < gl.SIZE; j++) { Coordinates t = new Coordinates(gl.GetRightCoordx(i, j), gl.GetRightCoordy(i, j)); if (gl.GetValueAt(t.X, t.Y) != 0) { if (gl.GetFromAt(t.X, t.Y).Count == 1) { Animation(gl.GetFromAt(t.X, t.Y).ElementAt(0), t, true); } else if (gl.GetFromAt(t.X, t.Y).Count == 2) { Animation(gl.GetFromAt(t.X, t.Y).ElementAt(0), t, false); Animation(gl.GetFromAt(t.X, t.Y).ElementAt(1), t, true); } } } } }
public void Animation(Coordinates from, Coordinates to, bool last) { Storyboard sb = new Storyboard(); DoubleAnimation slide = new DoubleAnimation(); slide.Duration = new Duration(TimeSpan.FromMilliseconds(150)); if (gl.MovingDir == Direction.UP || gl.MovingDir == Direction.DOWN) { slide.To = to.Y * 125; slide.From = from.Y * 125; Storyboard.SetTargetProperty(slide, new PropertyPath("RenderTransform.(TranslateTransform.Y)")); } else { slide.To = to.X * 125; slide.From = from.X * 125; Storyboard.SetTargetProperty(slide, new PropertyPath("RenderTransform.(TranslateTransform.X)")); } Storyboard.SetTarget(slide, GameGridCells[from.X, from.Y]); sb.Completed += (sender, e) => AnimationEnds(from, to, last); sb.Children.Add(slide); sb.Begin(); }
public void AnimationEnds(Coordinates from, Coordinates to, bool last) { RemoveOldGameCell(from.X, from.Y); if (last) { AddNewGameCell(to.X, to.Y); } }
private Coordinates GetRandomCoordinates() { Random r = new Random(); Coordinates c = new Coordinates(r.Next() % SIZE, r.Next() % SIZE); return c; }