Exemple #1
0
    public override void Clear()
    {
      base.Clear();

      Edges.Clear();
      StartPoints.Clear();
      ModelName = "";
    }
Exemple #2
0
    public void BuildStartPointList()
    {
      StartPoints.Clear();

      if (Triangles.Count == 0)
        return;

      // How many start points do we want?
      int NumStartPoints = Math.Min(Math.Max((int) Math.Round(Math.Sqrt(Triangles.Count) / 2), 1), Consts.MaxStartPoints);

      // Use the centre points of this number of triangles evenly spaced throughout the job
      int TriNumOffset = (Triangles.Count / NumStartPoints) / 2;
      for (int StPtNum = 0; StPtNum < NumStartPoints; StPtNum++)
      {
        int TriangleNum = (StPtNum * Triangles.Count) / NumStartPoints + TriNumOffset;

        Debug.Assert(TriangleNum >= 0);

        XYZ Centroid = Triangles[TriangleNum].Centroid();
        StartPoints.Add(new TTMStartPoint(Centroid.X, Centroid.Y, Triangles[TriangleNum]));
      }
    }
Exemple #3
0
        private bool InitializePanel()
        {
            if (panel != null)
            {
                Walls.Clear();
                StartPoints.Clear();
                EndPoints.Clear();
                renderedTetrisTextures.Clear();

                PuzzleDimensions = new Point(panel.Width, panel.Height);

                // Calculation of puzzle sizes for current screen size
                int width  = screenSize.X;
                int height = screenSize.Y;

                int maxPuzzleDimension = Math.Max(PuzzleDimensions.X, PuzzleDimensions.Y);

                int screenMinSize = Math.Min(width, height);
                int puzzleMaxSize = (int)(screenMinSize * PuzzleSpaceRatio(maxPuzzleDimension));
                LineWidth          = (int)(puzzleMaxSize * LineSizeRatio(maxPuzzleDimension));
                BlockWidth         = (int)(puzzleMaxSize * BlockSizeRatio(maxPuzzleDimension));
                HalfLineWidthPoint = new Point(LineWidth / 2);
                LineWidthPoint     = new Point(LineWidth);
                BlockSizePoint     = new Point(BlockWidth);

                int endAppendixLength = (int)(BlockWidth * endPointLegth);

                int puzzleWidth  = LineWidth * (PuzzleDimensions.X + 1) + BlockWidth * PuzzleDimensions.X;
                int puzzleHeight = LineWidth * (PuzzleDimensions.Y + 1) + BlockWidth * PuzzleDimensions.Y;

                int   xMargin = (width - puzzleWidth) / 2;
                int   yMargin = (height - puzzleHeight) / 2;
                Point margins = new Point(xMargin, yMargin);

                PuzzleConfig = new Rectangle(xMargin, yMargin, puzzleWidth, puzzleHeight);

                NodePadding = LineWidth + BlockWidth;

                // Creating walls hitboxes
                CreateHorizontalWalls(false);   // Top walls
                CreateHorizontalWalls(true);    // Bottom walls
                CreateVerticalWalls(false);     // Left walls
                CreateVerticalWalls(true);      // Right walls

                for (int i = 0; i < PuzzleDimensions.X; i++)
                {
                    for (int j = 0; j < PuzzleDimensions.Y; j++)
                    {
                        Walls.Add(new Rectangle(xMargin + LineWidth * (i + 1) + BlockWidth * i, yMargin + LineWidth * (j + 1) + BlockWidth * j, BlockWidth, BlockWidth));
                    }
                }

                // Creating walls for broken edges
                var brokenEdges = panel.Edges.Where(x => x.State == EdgeState.Broken);
                foreach (Edge edge in brokenEdges)
                {
                    Point nodeA  = NodeIdToPoint(edge.Id % 100).Multiply(NodePadding) + margins;
                    Point nodeB  = NodeIdToPoint(edge.Id / 100).Multiply(NodePadding) + margins;
                    Point middle = (nodeA + nodeB).Divide(2);
                    Walls.Add(new Rectangle(middle, new Point(LineWidth)));
                }

                // Creating hitboxes for starting nodes
                foreach (Point start in GetStartNodes())
                {
                    StartPoints.Add(new Rectangle(xMargin + start.X * NodePadding - LineWidth, yMargin + start.Y * NodePadding - LineWidth, LineWidth * 3, LineWidth * 3));
                }

                // Generate textures for tetris rules
                CreateTetrisRuleTextures();

                #region === Inner methods region ===
                // Returns a coef k: Total free space in pixels * k = puzzle size in pixels
                float PuzzleSpaceRatio(float puzzleDimension) => (float)(-0.0005 * Math.Pow(puzzleDimension, 4) + 0.0082 * Math.Pow(puzzleDimension, 3) - 0.0439 * Math.Pow(puzzleDimension, 2) + 0.1011 * puzzleDimension + 0.6875);

                // Returns a coef k: Puzzle size in pixels * k = block size in pixels
                float BlockSizeRatio(float puzzleDimension) => (float)(0.8563 * Math.Pow(puzzleDimension, -1.134));

                // Returns a coef k: Puzzle size in pixels * k = line width in pixels
                float LineSizeRatio(float puzzleDimension) => - 0.0064f * puzzleDimension + 0.0859f;

                IEnumerable <Point> GetStartNodes() => panel.Nodes.Where(x => x.State == NodeState.Start).Select(x => NodeIdToPoint(x.Id));

                IEnumerable <Point> GetEndNodesTop()
                {
                    for (int i = 1; i < panel.Width; i++)
                    {
                        if (panel.Nodes[i].State == NodeState.Exit)
                        {
                            yield return(new Point(i, 0));
                        }
                    }
                }

                IEnumerable <Point> GetEndNodesBot()
                {
                    for (int i = 1; i < panel.Width; i++)
                    {
                        int index = panel.Height * (panel.Width + 1) + i;
                        if (panel.Nodes[index].State == NodeState.Exit)
                        {
                            yield return(new Point(i, panel.Height));
                        }
                    }
                }

                IEnumerable <Point> GetEndNodesLeft()
                {
                    for (int j = 0; j < panel.Height + 1; j++)
                    {
                        int index = j * (panel.Width + 1);
                        if (panel.Nodes[index].State == NodeState.Exit)
                        {
                            yield return(new Point(0, j));
                        }
                    }
                }

                IEnumerable <Point> GetEndNodesRight()
                {
                    for (int j = 0; j < panel.Height + 1; j++)
                    {
                        int index = j * (panel.Width + 1) + panel.Width;
                        if (panel.Nodes[index].State == NodeState.Exit)
                        {
                            yield return(new Point(panel.Width, j));
                        }
                    }
                }

                void CreateHorizontalWalls(bool isBottom)
                {
                    int yStartPoint = isBottom ? yMargin + puzzleHeight : 0;
                    var ends        = isBottom ? GetEndNodesBot() : GetEndNodesTop();

                    if (ends.Count() == 0)
                    {
                        Walls.Add(new Rectangle(0, yStartPoint, width, yMargin));
                    }
                    else
                    {
                        Walls.Add(new Rectangle(0, yStartPoint + (isBottom ? endAppendixLength : 0), width, yMargin - endAppendixLength));

                        int lastXPoint = 0;
                        foreach (Point endPoint in ends)
                        {
                            int x = xMargin + endPoint.X * (NodePadding);
                            Walls.Add(new Rectangle(lastXPoint, isBottom ? yStartPoint : (yMargin - endAppendixLength), x - lastXPoint, endAppendixLength));
                            EndPoints.Add(new EndPoint(new Rectangle(x, isBottom ? yStartPoint + endAppendixLength - LineWidth : (yMargin - endAppendixLength), LineWidth, LineWidth), isBottom ? Facing.Down : Facing.Up));
                            lastXPoint = x + LineWidth;
                        }
                        Walls.Add(new Rectangle(lastXPoint, isBottom ? yStartPoint : (yMargin - endAppendixLength), width - lastXPoint, endAppendixLength));
                    }
                }

                void CreateVerticalWalls(bool isRight)
                {
                    int xStartPoint = isRight ? xMargin + puzzleWidth : 0;
                    var ends        = isRight ? GetEndNodesRight() : GetEndNodesLeft();

                    if (ends.Count() == 0)
                    {
                        Walls.Add(new Rectangle(xStartPoint, 0, xMargin, height));
                    }
                    else
                    {
                        Walls.Add(new Rectangle(xStartPoint + (isRight ? endAppendixLength : 0), 0, xMargin - endAppendixLength, height));

                        int lastYPoint = 0;
                        foreach (Point endPoint in ends)
                        {
                            int y = yMargin + endPoint.Y * (NodePadding);
                            Walls.Add(new Rectangle(isRight ? xStartPoint : (xMargin - endAppendixLength), lastYPoint, endAppendixLength, y - lastYPoint));
                            EndPoints.Add(new EndPoint(new Rectangle(isRight ? xStartPoint + endAppendixLength - LineWidth : xMargin - endAppendixLength, y, LineWidth, LineWidth), isRight ? Facing.Right : Facing.Left));
                            lastYPoint = y + LineWidth;
                        }
                        Walls.Add(new Rectangle(isRight ? xStartPoint : (xMargin - endAppendixLength), lastYPoint, endAppendixLength, height - lastYPoint));
                    }
                }

                void CreateTetrisRuleTextures()
                {
                    // Render shape into texture for each tetris rule
                    var tetrisBlocks = panel.Blocks.Where(x => x.Rule is TetrisRule).ToList();

                    if (tetrisBlocks.Count > 0)
                    {
                        int maxDimension = tetrisBlocks.Select(x => x.Rule as TetrisRule).Select(x => Math.Max(x.Shape.GetLength(0), x.Shape.GetLength(1))).Max();
                        if (maxDimension < 3)
                        {
                            maxDimension++;
                        }
                        tetrisTextureSize = new Point((maxDimension + 1) * texTetris[0].Width);
                        foreach (Block block in tetrisBlocks)
                        {
                            TetrisRule rule = block.Rule as TetrisRule;
                            bool[,] shape = rule.Shape;
                            // If shape is rotatable, then rotate it randomly before render
                            if (rule is TetrisRotatableRule r)
                            {
                                shape = TetrisRotatableRule.RotateShapeCW(shape, rnd.Next(0, 4));
                            }
                            // Draw shape on a texture
                            RenderTarget2D texture = CreateTetrisTexture(shape, rule is TetrisRotatableRule, rule.IsSubtractive);
                            // Save it to the dictionary
                            renderedTetrisTextures.Add(block.Id, texture);
                        }
                    }
                }

                #endregion
                return(true);
            }
            return(false);
        }