void GetStoryboardCreationData(_GAME_3BTask task,
          out int millisecondsPerUnit,
          out Func<ContentPresenter, double> getTo,
          out DependencyProperty animatedProperty,
          out IEnumerable<_GAME_3BViewModel> bubbles)
        {
            switch (task.TaskType)
            {
                case _GAME_3BTaskType.Burst:
                    millisecondsPerUnit = 250;
                    getTo = cp => (task.IsUndo ? 1.0 : 0.0);
                    animatedProperty = UIElement.OpacityProperty;
                    bubbles = task.Bubbles;
                    break;

                case _GAME_3BTaskType.MoveDown:
                    millisecondsPerUnit = 115;
                    getTo = _bubbleCanvas.CalculateTop;
                    animatedProperty = Canvas.TopProperty;

                    // Sort the bubbles to ensure that the columns move 
                    // in sync with each other in an appealing way.
                    bubbles =
                        from bubble in task.Bubbles
                        orderby bubble.PreviousColumn
                        orderby bubble.PreviousRow descending
                        select bubble;
                    break;

                case _GAME_3BTaskType.MoveRight:
                    millisecondsPerUnit = 115;
                    getTo = _bubbleCanvas.CalculateLeft;
                    animatedProperty = Canvas.LeftProperty;

                    // Sort the bubbles to ensure that the rows move 
                    // in sync with each other in an appealing way.
                    bubbles =
                        from bubble in task.Bubbles
                        orderby bubble.PreviousRow descending
                        orderby bubble.PreviousColumn descending
                        select bubble;
                    break;

                default:
                    throw new ArgumentException("Unrecognized BubblesTaskType: " + task.TaskType);
            }

            if (task.IsUndo)
            {
                bubbles = bubbles.Reverse();
            }
        }
Example #2
0
        _GAME_3BTask CreateUndoTask(_GAME_3BTask originalTask)
        {
            var bubbles = originalTask.Bubbles.ToList();
            Func <IEnumerable <_GAME_3BViewModel> > getBubbles;
            Action complete;

            switch (originalTask.TaskType)
            {
            case _GAME_3BTaskType.MoveRight:
                getBubbles = delegate
                {
                    _bubbleMatrix.IsIdle = false;
                    _bubbleMatrix.ResetBubbleGroup();
                    bubbles.ForEach(b => b.BeginUndo());
                    return(bubbles);
                };
                complete = delegate
                {
                    bubbles.ForEach(b => b.EndUndo());
                };
                break;

            case _GAME_3BTaskType.MoveDown:
                getBubbles = delegate
                {
                    bubbles.ForEach(b => b.BeginUndo());
                    return(bubbles);
                };
                complete = delegate
                {
                    bubbles.ForEach(b => b.EndUndo());
                };
                break;

            case _GAME_3BTaskType.Burst:
                getBubbles = delegate
                {
                    bubbles.ForEach(b => _bubbleMatrix.AddBubble(b));
                    return(bubbles);
                };
                complete = delegate
                {
                    _bubbleMatrix.IsIdle = true;
                };
                break;

            default:
                throw new ArgumentException("Unrecognized task type: " + originalTask.TaskType);
            }
            return(new _GAME_3BTask(originalTask.TaskType, true, getBubbles, complete));
        }
        internal Storyboard CreateStoryboard(_GAME_3BTask task)
        {
            int millisecondsPerUnit;
            Func<ContentPresenter, double> getTo;
            DependencyProperty animatedProperty;
            IEnumerable<_GAME_3BViewModel> bubbles;

            this.GetStoryboardCreationData(task,
                out millisecondsPerUnit,
                out getTo,
                out animatedProperty,
                out bubbles);

            return this.CreateStoryboard(
                task,
                millisecondsPerUnit,
                getTo,
                animatedProperty,
                bubbles.ToArray());
        }
        Storyboard CreateStoryboard(
            _GAME_3BTask task,
            int millisecondsPerUnit,
            Func<ContentPresenter, double> getTo,
            DependencyProperty animatedProperty,
            _GAME_3BViewModel[] bubbles)
        {
            if (!bubbles.Any())
                return null;

            var storyboard = new Storyboard();
            var targetProperty = new PropertyPath(animatedProperty);
            var beginTime = TimeSpan.FromMilliseconds(0);
            var beginTimeIncrement = TimeSpan.FromMilliseconds(millisecondsPerUnit / bubbles.Count());

            foreach (ContentPresenter presenter in this.GetBubblePresenters(bubbles))
            {
                var bubble = presenter.DataContext as _GAME_3BViewModel;
                var duration = CalculateDuration(task.TaskType, bubble, millisecondsPerUnit);
                var to = getTo(presenter);
                var anim = new EasingDoubleAnimation
                {
                    BeginTime = beginTime,
                    Duration = duration,
                    Equation = EasingEquation.CubicEaseIn,
                    To = to,
                };

                Storyboard.SetTarget(anim, presenter);
                Storyboard.SetTargetProperty(anim, targetProperty);

                if (IsTaskStaggered(task.TaskType))
                {
                    beginTime = beginTime.Add(beginTimeIncrement);
                }

                storyboard.Children.Add(anim);
            }

            return storyboard;
        }
        void PerformTask(_GAME_3BTask task, Storyboard storyboard)
        {
            if (storyboard != null)
            {
                // There are some bubbles that need to be animated, so we must
                // wait until the Storyboard finishs before completing the task.
                storyboard.Completed += delegate { this.CompleteTask(task); };

                // Freeze the Storyboard to improve perf.
                storyboard.Freeze();

                // Start animating the bubbles associated with the task.
                storyboard.Begin(this);
            }
            else
            {
                // There are no bubbles associated with this task,
                // so immediately move to the task completion phase.
                this.CompleteTask(task);
            }
        }
 void CompleteTask(_GAME_3BTask task)
 {
     task.Complete();
     this.ProcessNextTask();
 }