Beispiel #1
0
        // TODO: Use _polygonShape.Vertices instead of _bottomLeftVertices, etc...
        public void Iterate(int iterationNumber = 1)
        {
            if (iterationNumber == 0)
            {
                return;
            }

            var minY = 0f;
            var maxY = 0f;

            var bottomVertex = _bottomLeftVertices.Last();

            // We revert it earlier
            _topLeftVertices.Reverse();

            var topVertex = Vector2.Zero;

            if (_topLeftVertices.Count > 0)
            {
                topVertex = _topLeftVertices.Last();
            }

            for (var i = 0; i < iterationNumber; i++)
            {
                var possibleDirections = new List <Direction>()
                {
                    Direction.Up, Direction.Down, Direction.Right, Direction.UpRight, Direction.DownRight
                };

                if (bottomVertex.Y <= _step)
                {
                    possibleDirections.Remove(Direction.Up);
                    possibleDirections.Remove(Direction.UpRight);
                    possibleDirections.Remove(Direction.Right);
                }

                if (i == iterationNumber - 1 || _bottomLeftVertices.Count == 1)
                {
                    possibleDirections.Remove(Direction.Down);
                    possibleDirections.Remove(Direction.Up);
                }

                bottomVertex = GenerateRandomPosition(bottomVertex, possibleDirections, ref _bottomRightLastDirection);

                if (bottomVertex.Y > maxY)
                {
                    maxY = bottomVertex.Y;
                }

                _bottomLeftVertices.Add(bottomVertex);

                _size.X = bottomVertex.X * 2;

                // Update top part
                while (topVertex.X < bottomVertex.X)
                {
                    possibleDirections = new List <Direction>()
                    {
                        Direction.Up, Direction.Down, Direction.Right, Direction.UpRight, Direction.DownRight
                    };

                    if (topVertex.Y >= -_step)
                    {
                        possibleDirections.Remove(Direction.Down);
                        possibleDirections.Remove(Direction.DownRight);
                        possibleDirections.Remove(Direction.Right);
                    }

                    topVertex = GenerateRandomPosition(topVertex, possibleDirections, ref _topRightLastDirection);

                    if (topVertex.Y < minY)
                    {
                        minY = topVertex.Y;
                    }

                    _topLeftVertices.Add(topVertex);
                }
            }

            // Generate vertical symmetry
            _bottomRightVertices = GenerateSymmetry(_bottomLeftVertices, Symmetry.Vertical, _size.X / 2f);
            _topRightVertices    = GenerateSymmetry(_topLeftVertices, Symmetry.Vertical, _size.X / 2f);

            var centerVerticesDistance = _bottomRightVertices.First().Y + _topRightVertices.First().Y;

            _size.Y = Math.Abs(minY - maxY);
            Origin  = new Vector2(_size.X / 2f, centerVerticesDistance / 2f);

            // We want CCW order
            _topRightVertices.Reverse();
            _topLeftVertices.Reverse();

            var vertices = new List <Vector2>();

            vertices.AddRange(_bottomLeftVertices);
            vertices.AddRange(_bottomRightVertices);
            vertices.AddRange(_topRightVertices);
            vertices.AddRange(_topLeftVertices);

            _polygonShape.UpdateVertices(vertices.ToArray());
        }