Exemplo n.º 1
0
        private void GenerateWaterfall(Vector3Int coord)
        {
            var nextPoint = coord;

            var waterTracker = new WaterTracker(water.config, this);

            waterTracker.Start(coord);

            var count = 0;

            while (true)
            {
                var point = ProcessNextWater(nextPoint, waterTracker);

                if (point == null)
                {
                    break;
                }

                if (nextPoint == point.Value)
                {
                    break;
                }

                nextPoint = point.Value;

                count++;
                if (count > 128)
                {
                    break;
                }
            }

            if (waterTracker.ReachedWater)
            {
                waterTracker.Apply(groundChunks);
            }
        }
Exemplo n.º 2
0
        private Vector3Int?ProcessNextWater(Vector3Int coord, WaterTracker waterTracker)
        {
            if (coord.y <= water.config.waterLevel)
            {
                waterTracker.ReachedWater = true;
                return(null);
            }

            var down = new Vector3Int(coord.x, coord.y - 1, coord.z);

            if (groundChunks.Get(down) < 0)
            {
                waterTracker.FreeFall(down);
                return(down);
            }

            Vector3Int[] coords =
            {
                new Vector3Int(coord.x,     coord.y, coord.z - 1),
                new Vector3Int(coord.x - 1, coord.y, coord.z),
                new Vector3Int(coord.x + 1, coord.y, coord.z),
                new Vector3Int(coord.x,     coord.y, coord.z + 1),

                new Vector3Int(coord.x - 1, coord.y, coord.z - 1),
                new Vector3Int(coord.x + 1, coord.y, coord.z - 1),
                new Vector3Int(coord.x - 1, coord.y, coord.z + 1),
                new Vector3Int(coord.x + 1, coord.y, coord.z + 1),
            };

            var coordsBelow = new List <Vector3Int>();

            for (var i = 0; i < coords.Length; i++)
            {
                var c = coords[i];
                var v = groundChunks.Get(c);
                if (v > 0)
                {
                    continue;
                }

                var coordBelow = new Vector3Int(c.x, c.y - 1, c.z);
                var v2         = groundChunks.Get(coordBelow);
                if (v2 > 0)
                {
                    coordsBelow.Add(coordBelow);
                }
                else
                {
                    // From the sides
                    if (i < 4)
                    {
                        waterTracker.Flow(coord, down);
                        return(down);
                    }
                }
            }

            var listToUse = coordsBelow.Count > 0 ? coordsBelow.ToArray() : coords;

            var        minValue = Mathf.Infinity;
            Vector3Int?minCoord = null;

            foreach (var t in listToUse)
            {
                var value = groundChunks.Get(t);
                if (value < 0)
                {
                    continue;
                }

                if (GetWaterfall(t) > 0)
                {
                    continue;
                }

                if (value >= minValue)
                {
                    continue;
                }

                minValue = value;
                minCoord = t;
            }

            if (minCoord.HasValue)
            {
                waterTracker.Flow(coord, minCoord.Value);
            }

            return(minCoord);
        }