예제 #1
0
    // Game initialization routine.
    private IEnumerator InitializeGame()
    {
        // Wait for all the Start() in all components to be called and completed
        yield return(new WaitForEndOfFrame());

        GridManager.Instance.CreateGrid();

        // Fire the event so game object know when the game has been initialized and can be played
        if (onGameReady != null)
        {
            onGameReady();
        }

        _isGameReady = true;

        _safeZones = GameObject.FindGameObjectsWithTag("SafeZone");
        List <Road> allRoads = GridManager.Instance.GetAllGridObjectsOfType <Road>(GridObjectTypes.Building.ROAD);

        while (_totalSpawns > 0)
        {
            Road spawnRoad  = null;
            int  randAmount = Mathf.Clamp(UnityEngine.Random.Range(_minSpawns, _maxSpawns + 1), 0, _totalSpawns);
            while (spawnRoad == null && allRoads.Count > 0)
            {
                int randRoad = UnityEngine.Random.Range(0, allRoads.Count);
                spawnRoad = allRoads[randRoad];
                if (spawnRoad.Attachment != null && spawnRoad.Attachment.CompareTag("SafeZone"))
                {
                    allRoads.Remove(spawnRoad);
                    spawnRoad = null;
                }
            }
            if (spawnRoad == null)
            {
                break;
            }

            spawnRoad.AddCivilians(randAmount);
            CiviliansRemaining += randAmount;
            _totalSpawns       -= randAmount;
            allRoads.Remove(spawnRoad);
        }

        Builder b = TemplateManager.Instance.Spawn <Builder>(GetRandomBuilderTemplate());

        AddBuilder(b, false);
        b.SetHome(HQ);

        Marshal m = TemplateManager.Instance.Spawn <Marshal>(GetRandomMarshalTemplate());

        AddMarshal(m, false);
        m.SetHome(HQ);

        WaveManager.Instance.StartNextWaveCountdown();

        AudioManager.Instance.PlayBuildMusic();
    }