Beispiel #1
0
        private void FindPathToWater(WorldTile tile, Direction direction, ref River river)
        {
            if (tile.TerrainData.Rivers.Contains(river))
            {
                return;
            }

            // check if there is already a river on this tile
            if (tile.TerrainData.Rivers.Count > 0)
            {
                river.Intersections++;
            }

            river.AddTile(tile);

            // get neighbors
            WorldTile left   = GetLeft(tile);
            WorldTile right  = GetRight(tile);
            WorldTile top    = GetTop(tile);
            WorldTile bottom = GetBottom(tile);

            float leftValue   = int.MaxValue;
            float rightValue  = int.MaxValue;
            float topValue    = int.MaxValue;
            float bottomValue = int.MaxValue;

            // query height values of neighbors
            if (left != null && left.GetRiverNeighborCount(river) < 2 && !river.Tiles.Contains(left))
            {
                leftValue = left.TerrainData.HeightValue;
            }
            if (right != null && right.GetRiverNeighborCount(river) < 2 && !river.Tiles.Contains(right))
            {
                rightValue = right.TerrainData.HeightValue;
            }
            if (top != null && top.GetRiverNeighborCount(river) < 2 && !river.Tiles.Contains(top))
            {
                topValue = top.TerrainData.HeightValue;
            }
            if (bottom != null && bottom.GetRiverNeighborCount(river) < 2 && !river.Tiles.Contains(bottom))
            {
                bottomValue = bottom.TerrainData.HeightValue;
            }

            // if neighbor is existing river that is not this one, flow into it
            if (bottom != null && bottom.TerrainData.Rivers.Count == 0 && !bottom.TerrainData.Collidable)
            {
                bottomValue = 0;
            }
            if (top != null && top.TerrainData.Rivers.Count == 0 && !top.TerrainData.Collidable)
            {
                topValue = 0;
            }
            if (left != null && left.TerrainData.Rivers.Count == 0 && !left.TerrainData.Collidable)
            {
                leftValue = 0;
            }
            if (right != null && right.TerrainData.Rivers.Count == 0 && !right.TerrainData.Collidable)
            {
                rightValue = 0;
            }

            // override flow direction if a tile is significantly lower
            if (direction == Direction.Left)
            {
                if (Math.Abs(rightValue - leftValue) < 0.1f)
                {
                    rightValue = int.MaxValue;
                }
            }
            if (direction == Direction.Right)
            {
                if (Math.Abs(rightValue - leftValue) < 0.1f)
                {
                    leftValue = int.MaxValue;
                }
            }
            if (direction == Direction.Top)
            {
                if (Math.Abs(topValue - bottomValue) < 0.1f)
                {
                    bottomValue = int.MaxValue;
                }
            }
            if (direction == Direction.Bottom)
            {
                if (Math.Abs(topValue - bottomValue) < 0.1f)
                {
                    topValue = int.MaxValue;
                }
            }

            // find mininum
            float min = Math.Min(Math.Min(Math.Min(leftValue, rightValue), topValue), bottomValue);

            // if no minimum found - exit
            if (min == int.MaxValue)
            {
                return;
            }

            //Move to next neighbor
            if (min == leftValue)
            {
                if (left != null && left.TerrainData.Collidable)
                {
                    if (river.CurrentDirection != Direction.Left)
                    {
                        river.TurnCount++;
                        river.CurrentDirection = Direction.Left;
                    }
                    FindPathToWater(left, direction, ref river);
                }
            }
            else if (min == rightValue)
            {
                if (right != null && right.TerrainData.Collidable)
                {
                    if (river.CurrentDirection != Direction.Right)
                    {
                        river.TurnCount++;
                        river.CurrentDirection = Direction.Right;
                    }
                    FindPathToWater(right, direction, ref river);
                }
            }
            else if (min == bottomValue)
            {
                if (bottom != null && bottom.TerrainData.Collidable)
                {
                    if (river.CurrentDirection != Direction.Bottom)
                    {
                        river.TurnCount++;
                        river.CurrentDirection = Direction.Bottom;
                    }
                    FindPathToWater(bottom, direction, ref river);
                }
            }
            else if (min == topValue)
            {
                if (top != null && top.TerrainData.Collidable)
                {
                    if (river.CurrentDirection != Direction.Top)
                    {
                        river.TurnCount++;
                        river.CurrentDirection = Direction.Top;
                    }
                    FindPathToWater(top, direction, ref river);
                }
            }
        }