Example #1
0
    private void SpawnEnemies(List <WaveEntity> selectedFormationEntities, int[] shipTypes, float minVerticalStartCoord,
                              float maxVerticalStartCoord, float enemyHorizontalDist, float enemyVerticalDist, float maxEnemyHorizontalDist)
    {
        bool    waveManeuvers = false;
        Vector2 maneuveringVector;
        float   maneuveringVerticalLength = 0.0f;

        float selectedVerticalStartCoord = Random.Range(minVerticalStartCoord, maxVerticalStartCoord);
        float diffFromCeiling            = maxVerticalStartCoord - selectedVerticalStartCoord;
        float diffFromFloor = selectedVerticalStartCoord - minVerticalStartCoord;

        float waveManeuveringRoom;

        if (diffFromCeiling > diffFromFloor)
        {
            waveManeuveringRoom = diffFromCeiling;
            maneuveringVector   = Vector2.up * ManeuveringDirectionMoveSpeed;
        }
        else
        {
            waveManeuveringRoom = diffFromFloor;
            maneuveringVector   = Vector2.down * ManeuveringDirectionMoveSpeed;
        }

        if (waveManeuveringRoom > ShipColliderVertSize)
        {
            //20 40 60 80 100 percent chance of a maneuvering wave depending on difficulty coefficient
            float waveManeuveringChangePercentage = (float)(_difficultyManagerScript.DifficultyCoefs[DifficultyParameter.DpWaveManeuveringChance] * 100) /
                                                    GameConstants.MaxDifficulty;

            int doManeuver = Random.Range(0, 100);
            if (doManeuver < waveManeuveringChangePercentage)
            {
                waveManeuvers             = true;
                maneuveringVerticalLength = Random.Range(ShipColliderVertSize, waveManeuveringRoom);
            }
        }

        //VII. Spawn Enemies
        GameObject waveScoreIndicator = Instantiate(WaveScoreIndicatorPrefab);

        waveScoreIndicator.transform.SetParent(CanvasScorePanel.transform, false);

        GameObject lineRendererObject = Instantiate(ShipConnectionPrefab, Vector3.zero, Quaternion.identity);

        EnemyWave curEnemyWave = new EnemyWave(_playerScript, lineRendererObject.GetComponent <LineRenderer>(),
                                               _canvasRectTransform, waveScoreIndicator, _scoreLeftAnchorXPos, _scoreRightAnchorXPos);

        for (int i = 0; i < selectedFormationEntities.Count; i++)
        {
            int        enemyKind         = shipTypes[i];
            GameObject enemyPrefab       = EnemyPrefabArray[enemyKind];
            BasicEnemy enemyPrefabScript = enemyPrefab.GetComponent <BasicEnemy>();

            Vector2 enemyPos;
            if (i > 0)
            {
                Vector2 posDiff = selectedFormationEntities[i].Position - selectedFormationEntities[i - 1].Position;

                int xPosDiff = (int)posDiff.x;
                int yPosDiff = (int)posDiff.y;

                int xIncrement = xPosDiff != 0 ? Math.Sign(xPosDiff) : 0;
                int yIncrement = yPosDiff != 0 ? Math.Sign(yPosDiff) : 0;

                Vector3 previousEnemyPos = curEnemyWave.GetLastEnemyPosition();

                enemyPos = new Vector2(previousEnemyPos.x + xIncrement * enemyHorizontalDist, previousEnemyPos.y + yIncrement * enemyVerticalDist);
            }
            else
            {
                //determining first enemy position
                enemyPos = new Vector2(enemyPrefabScript.HorizontalSpawnCoord + selectedFormationEntities[i].Position.x * maxEnemyHorizontalDist, selectedVerticalStartCoord);
            }

            GameObject enemy = Instantiate(enemyPrefab, enemyPos, Quaternion.identity);
            Assert.IsNotNull(enemy);

            BasicEnemy basicEnemyScript = enemy.GetComponent <BasicEnemy>();
            BasicMove  basicMoveScript  = enemy.GetComponent <BasicMove>();

            curEnemyWave.AddNewEnemy(enemy, basicEnemyScript);
            basicEnemyScript.Initialize(_playerScript, _difficultyManagerScript, basicMoveScript, selectedFormationEntities[i].MoveDir, curEnemyWave);
            basicEnemyScript.SetMoveDir(selectedFormationEntities[i].MoveDir);
        }
        curEnemyWave.FinalizeAfterWaveIsFilled(waveManeuvers, maneuveringVector, maneuveringVerticalLength);
        _enemyWaves.Add(curEnemyWave);
    }