Ejemplo n.º 1
0
    public void GenerateGalaxy()
    {
        if (_galaxy.SolarSystems != null)
        {
            foreach (SolarSystem g in _galaxy.SolarSystems)
            {
                Destroy(g.gameObject);
            }

            _galaxy.SolarSystems.Clear();

            if (_galaxy.BlackHole != null)
            {
                Destroy(_galaxy.BlackHole.gameObject);
            }
        }


        GameObject blackHoleGameObject = Instantiate(GalaxyPrefabs.BlackHolePrefab, transform.position, Quaternion.identity, this.transform);
        BlackHole  blackHole           = blackHoleGameObject.GetComponent <BlackHole>();

        _galaxy.BlackHole = blackHole;

        //Set galaxy name
        System.Random rand = new System.Random();
        _galaxy.Name = StarName.Generate(rand);
        this.name    = _galaxy.Name;

        _galaxy.Size = new Vector2(Settings.Radius, Settings.Radius);

        int solarSystems = Random.Range(Settings.MinGalaxies, Settings.MaxGalaxies);

        List <Vector3> positions = Spiral.GenerateSpiral();

        //Generate solar systems of galaxy
        for (int i = 0; i < solarSystems; i++)
        {
            int     random = Random.Range(0, (Spiral.iterations * 2) - 1);
            Vector2 pos    = new Vector2(positions[random].x, positions[random].z) + Random.insideUnitCircle * Settings.Radius;

            while (CheckForGalaxies(pos))
            {
                pos = new Vector2(positions[random].x, positions[random].z) + Random.insideUnitCircle * Settings.Radius;
            }

            GameObject solarSystemGameObject = Instantiate(GalaxyPrefabs.SolarSystemPrefab, new Vector3(pos.x, pos.y, 0), Quaternion.identity, this.transform);

            SolarSystemGenerator solarSystemGenerator = solarSystemGameObject.GetComponent <SolarSystemGenerator>();
            SolarSystem          solarSystem          = solarSystemGameObject.GetComponent <SolarSystem>();

            solarSystem.Name           = StarName.Generate(rand);
            solarSystemGameObject.name = solarSystem.Name;

            solarSystem.Position = solarSystemGameObject.transform.position;

            _galaxy.SolarSystems.Add(solarSystem);

            solarSystemGenerator.GenerateSolarSystem();

            foreach (Planet p in solarSystem.Planets)
            {
                p.gameObject.SetActive(false);
            }
        }

        GetComponent <GalaxyNavigator>().Connect(_galaxy.SolarSystems);
    }