Esempio n. 1
0
        public HexagonGrid(int width, int height, Canvas canvas, TappedEventHandler tapHandler )
        {
            // Size of the grid ( size x size)
            Width = width;
            Height = height;

            // Determine hexagon size
            _canvas = canvas;
            _hexagonRadius = (int)(Window.Current.Bounds.Height / Height / 2);
            _hexagonHeight = _hexagonRadius * Math.Sin(Math.PI / 3);

            // Determine pixel width/height
            double pixelWidth = Width * 1.5 * _hexagonRadius + (Width % 2) * _hexagonRadius;
            double pixelHeight = _hexagonHeight * 2 * Height;

            // Offset for centering on screen
            _offsetX = (Window.Current.Bounds.Width - pixelWidth) / 2 + _hexagonRadius;
            _offsetY = (Window.Current.Bounds.Height - pixelHeight) / 2 + _hexagonHeight / 2;

            // Create grid
            Grid = new Hexagon[Width, Height];

            // Populate grid to null
            for (int i = 0; i < Width; i++)
            {
                for (int j = 0; j < Height; j++)
                {
                    Hexagon hex = new Hexagon(_hexagonRadius, i, j);
                    Point coord = coordinatesToHexagonPosition(i, j);

                    // Position Grid
                    hex.Top = _offsetY + coord.Y;
                    hex.Left = _offsetX + coord.X;

                    // Hide (for now)
                    hex.IsVisible = false;

                    // Save grid
                    Grid[i, j] = hex;

                    // Add to visual tree
                    hex.UIElement.Tapped += tapHandler;
                    _canvas.Children.Add(hex.UIElement);
                }
            }
        }
Esempio n. 2
0
        void HexTapped(object sender, TappedRoutedEventArgs e)
        {
            if (Game.State == GameState.GameOver) return;

            Hexagon hex = ((Polygon)sender).DataContext as Hexagon;
            if (hex != null && Game.Grid[hex.X, hex.Y].HexColor != Color.Null)
            {
                swapSoundEffect.Play();

                if (selectedHex != null)
                {
                    // Swap (sucess == adjacent)
                    Coordinate coordA = new Coordinate(selectedHex.X, selectedHex.Y);
                    Coordinate coordB = new Coordinate(hex.X, hex.Y);
                    if (Game.Swap(coordA, coordB))
                    {
                        // Process Explosions
                        List<Coordinate> explodedElements = Game.Purge(1);
                        List<Coordinate> updatedElement = new List<Coordinate>() { coordA, coordB };

                        // Update board
                        Grid.Update(Game, updatedElement, explodedElements, true);

                        // Update score
                        ScoreTxtBlock.Text = Game.TotalPoints.ToString();

                        // Level up
                        if (Game.TotalPoints > nextUpgrade)
                        {
                            nextUpgrade += upgradeCriteria;
                            audio.PlaybackRate += upgradeIncrease;
                            Timer.Interval = TimeSpan.FromMilliseconds(timerInterval * (1 - level * upgradeIncrease));
                            level++;
                        }

                        // Remove selection
                        hex.Selected = false;
                        selectedHex.Selected = false;
                        selectedHex = null;
                    }
                    else
                    {
                        // Update selection
                        selectedHex.Selected = false;
                        hex.Selected = true;
                        selectedHex = hex;
                    }
                }
                else
                {
                    // Remember selection
                    hex.Selected = true;
                    selectedHex = hex;
                }
            }
        }
Esempio n. 3
0
        public void StartGame(object sender, RoutedEventArgs e)
        {
            // Remove selection
            if (selectedHex != null)
            {
                selectedHex.Selected = false;
                selectedHex = null;
            }

            // Hide Menu
            HideMenu();

            // Reset upgrades
            nextUpgrade = upgradeCriteria;
            audio.PlaybackRate = 1;
            level = 1;

            // Start Game
            Game.Start(1);

            // Show Score
            ScoreTxtBlock.Text = Game.TotalPoints.ToString();
            ScoreTxtBlock.Visibility = Windows.UI.Xaml.Visibility.Visible;

            // Start Timer
            Timer.Tick += GameTick;
            Timer.Interval = TimeSpan.FromMilliseconds(timerInterval);
            Timer.Start();

            // Update grid
            Grid.Update(Game);
        }
Esempio n. 4
0
        public void UpdateHex(Hexagon hUI, Hex h)
        {
            if (h.HexColor == Color.Null)
            {
                hUI.Color = new SolidColorBrush(Colors.Gray);
                hUI.IsVisible = false;
            }
            else
            {
                GradientStopCollection gsc = new GradientStopCollection();
                GradientStop white = new GradientStop();
                GradientStop primary = new GradientStop();
                primary.Offset = -0.25;
                white.Offset = 1.5;
                white.Color = Colors.White;

                switch (h.HexColor)
                {
                    case Color.Blue:
                        primary.Color = Colors.Blue;
                        break;

                    case Color.Green:
                        primary.Color = Colors.Green;
                        break;

                    case Color.Orange:
                        primary.Color = Colors.Orange;
                        break;

                    case Color.White:
                        primary.Color = Colors.White;
                        break;

                    case Color.Purple:
                        primary.Color = Colors.Purple;
                        break;

                    case Color.Red:
                        primary.Color = Colors.Red;
                        break;

                    case Color.Yellow:
                        primary.Color = Colors.Yellow;
                        break;
                }

                gsc.Add(primary);
                gsc.Add(white);

                // Update UIElement
                hUI.IsVisible = true;
                hUI.Color = new LinearGradientBrush(gsc, 0.0f); ;
            }
        }
Esempio n. 5
0
        // Show score
        public void DisplayScore(Hexagon hex, int value)
        {
            // Create new textElement
            TextBlock score = new TextBlock();
            score.FontSize = 35;
            score.Text = string.Format("+{0}", value);

            // Center Textblock
            score.SetValue(Canvas.LeftProperty, hex.Left - 25);
            score.SetValue(Canvas.TopProperty, hex.Top - 10);

            // Add to Canvas
            _canvas.Children.Add(score);

            // Animate score
            AnimateScore(hex, score);
        }
Esempio n. 6
0
        public void AnimateScore(Hexagon hex, TextBlock score)
        {
            var key = string.Format("explode-score-animation-{0}-{1}-{2}", Utilities.RandomGenerator.Next(), hex.X, hex.Y);

            if (_canvas.Resources.ContainsKey(key))
            {
                (_canvas.Resources[key] as Storyboard).Begin();
            }
            else
            {
                // Create animation
                Storyboard storyboard = new Storyboard();

                // Opacity
                DoubleAnimation animation = new DoubleAnimation();
                animation.From = 1;
                animation.To = 0;
                animation.Duration = new Duration(TimeSpan.FromMilliseconds(1000));
                Storyboard.SetTargetProperty(animation, "(UIElement.Opacity)");
                Storyboard.SetTarget(animation, score);
                storyboard.Children.Add(animation);

                // Hide after done
                storyboard.Completed += (object sender, object e) =>
                {
                    _canvas.Children.Remove(score);
                    _canvas.Resources.Remove(key);
                };

                // Add to Canvas
                _canvas.Resources.Add(new KeyValuePair<object, object>(key, storyboard));
                storyboard.Begin();
            }
        }
Esempio n. 7
0
        // Display
        public void AnimateNewHex(Hexagon hex)
        {
            var key = "display-animation-" + hex.X + "-" + hex.Y;

            if(_canvas.Resources.ContainsKey(key))
            {
                (_canvas.Resources[key] as Storyboard).Begin();
            }
            else
            {
                // Create animation
                Storyboard storyboard = new Storyboard();

                DoubleAnimation animation = new DoubleAnimation();
                animation.From = 0.5;
                animation.To = 1;
                animation.Duration = new Duration(TimeSpan.FromMilliseconds(1000));

                Storyboard.SetTarget(animation, hex.UIElement);
                Storyboard.SetTargetProperty(animation, "(UIElement.Opacity)");
                storyboard.Children.Add(animation);

                // Add to Canvas
                _canvas.Resources.Add(new KeyValuePair<object, object>(key, storyboard));
                storyboard.Begin();
            }
        }
Esempio n. 8
0
        // Hide
        public void AnimateDestroyHex(Hexagon hex)
        {
            var key = "explode-animation-" + hex.X + "-" + hex.Y;

            if(_canvas.Resources.ContainsKey(key))
            {
                (_canvas.Resources[key] as Storyboard).Begin();
            }
            else
            {
                // Create animation
                Storyboard storyboard = new Storyboard();

                DoubleAnimation animation = new DoubleAnimation();
                animation.From = 1;
                animation.To = 0;
                animation.Duration = new Duration(TimeSpan.FromMilliseconds(1000));

                Storyboard.SetTarget(animation, hex.UIElement);
                Storyboard.SetTargetProperty(animation, "(UIElement.Opacity)");
                storyboard.Children.Add(animation);

                // Hide after don
                storyboard.Completed += (object sender, object e) =>
                {
                    hex.IsVisible = false;
                };

                // Add to Canvas
                _canvas.Resources.Add(new KeyValuePair<object, object>(key, storyboard));
                storyboard.Begin();
            }
        }