Exemplo n.º 1
0
        public override void Apply(TilemapStructure tilemap)
        {
            _random = new System.Random(tilemap.Grid.Seed);
            var rivers = new List <DrunkenRiver>();

            var validStartPositions = TilemapHelper.GetTilesByType(tilemap, StartingTileTypes.Select(a => (int)a));
            var amountOfRivers      = _random.Next(MinRiverQuota, MaxRiverQuota + 1);

            for (int i = 0; i < amountOfRivers; i++)
            {
                // Get valid startPoint with respect to distance between existing rivers
                var startPoint = GetValidStartPosition(validStartPositions, rivers);
                if (!startPoint.HasValue)
                {
                    break;
                }

                // Find valid endPos (closest deep water tile to startPos)
                var endPos = TilemapHelper.FindClosestTileByType(tilemap, startPoint.Value, (int)GroundTileType.DeepWater);
                if (!endPos.HasValue)
                {
                    break;
                }

                // Build river from start to end position
                var river = new DrunkenRiver(_random, RiverDriftChance, startPoint.Value, endPos.Value);
                if (river.Build())
                {
                    rivers.Add(river);
                }
            }

            // Set river tiles into tilemap
            int riverTileId = (int)GroundTileType.River;

            foreach (var riverPosition in rivers.SelectMany(a => a.RiverPositions))
            {
                tilemap.SetTile(riverPosition.x, riverPosition.y, riverTileId);
            }
        }