示例#1
0
    void AttemptSpawn(IntRect rect, EnemySpawnSettings.EnemySpawn spawn)
    {
        if (_coordsList == null)
        {
            _coordsList = new List <SpawnCoords>();
        }
        _coordsList.Clear();
        IntRect windowRect = CloudSpawner.GetCurrentRect(LayerDepth, GridSize, new Vector2(0.6f, 0.6f));

        int minY = rect.MinY;

        if (spawn.HasMinY)
        {
            minY = Mathf.Max(minY, spawn.MinY);
        }
        int maxY = rect.MaxY;

        if (spawn.HasMaxY)
        {
            maxY = Mathf.Min(maxY, spawn.MaxY + 1);
        }
        for (int x = rect.MinX; x < rect.MaxX; x++)
        {
            for (int y = minY; y < maxY; y++)
            {
                if (x > windowRect.MinX &&
                    x < windowRect.MaxX - 1 &&
                    y > windowRect.MinY &&
                    y < windowRect.MaxY - 1)
                {
                    continue;
                }
                if (IsSpawnValid(spawn, x, y) == false)
                {
                    continue;
                }
                _coordsList.Add(new SpawnCoords()
                {
                    X = x,
                    Y = y,
                });
            }
        }

        if (_coordsList.Count > 0)
        {
            var coord = _coordsList[Random.Range(0, _coordsList.Count)];
            SpawnEnemy(spawn, coord);
        }
    }
示例#2
0
    void Update()
    {
        if (_firstEnemyKilled)
        {
            _timer += Time.deltaTime;
        }

        IntRect currentRect = CloudSpawner.GetCurrentRect(LayerDepth, GridSize, MaxSpawnRectModifier);

        float periodT;
        var   spawnSettings = GetCurrentSpawnSettings(_timer, out periodT);

        if (_firstEnemyKilled)
        {
            _SpawnAccumulator += spawnSettings.SpawnSpeed * spawnSettings.IntensityCurve.Evaluate(periodT) * Time.deltaTime;
        }

        if (_SpawnAccumulator >= 0)
        {
            float totalSpawnChance = 0;
            for (int i = 0; i < spawnSettings.Spawns.Length; i++)
            {
                var spawn = spawnSettings.Spawns[i];
                if (IsValidSpawn(currentRect, spawn, _timer))
                {
                    totalSpawnChance += spawn.SpawnChance;
                }
            }

            float randomVal = Random.Range(0, totalSpawnChance);

            for (int i = 0; i < spawnSettings.Spawns.Length; i++)
            {
                var spawn = spawnSettings.Spawns[i];
                if (IsValidSpawn(currentRect, spawn, _timer))
                {
                    if (spawn.SpawnChance > randomVal)
                    {
                        AttemptSpawn(currentRect, spawn);
                        break;
                    }
                    randomVal -= spawn.SpawnChance;
                }
            }
        }
    }