Ejemplo n.º 1
0
    void CreateMoon(GenerationSettings.MoonSettings moonSettings, int planetID)
    {
        // find the planet that the moon will orbit
        CelestialBody planet = FindWithID(planetID);

        // create a new moon gameobject
        // make it a child of its parent planet
        GameObject newMoon = new GameObject
        {
            name = "Moon " + planet.transform.childCount.ToString()
        };

        newMoon.transform.parent = planet.transform;

        // add components
        CelestialBody cbComponent = newMoon.AddComponent <CelestialBody>();

        // assign the values to the moon from the settings
        cbComponent.SetUpBody(moonSettings, true, planet);

        // register as celestial body
        celestialBodies.Add(cbComponent);
    }
Ejemplo n.º 2
0
    public void SetUpBody(GenerationSettings.MoonSettings moonSettings, bool setPositions = true, CelestialBody newOrbitalParent = null)
    {
        // this is called when a celestial body is wanted to be set up as moon

        GenericSetup(moonSettings);

        drawPath   = moonSettings.drawPath;
        pathLength = moonSettings.pathLength;

        if (setPositions)
        {
            // place the moon away from the planet
            Vector3 direction = new Vector3(Mathf.Cos(moonSettings.offsetAngle * Mathf.Deg2Rad), 0, Mathf.Sin(moonSettings.offsetAngle * Mathf.Deg2Rad));
            transform.position = newOrbitalParent.transform.position + (direction * moonSettings.distance);
        }

        // set up the initial velocity to give a stable orbit
        if (newOrbitalParent != null)
        {
            initialVelocity   = moonSettings.eccentricity * CalculateInitialVelocity(newOrbitalParent.transform);
            orbitalParent     = newOrbitalParent;
            orbitalParentMass = orbitalParent.GetComponent <CelestialBody>().mass;
        }
    }
Ejemplo n.º 3
0
    public GenerationSettings CreateSettingsOject()
    {
        // we set up a GenerationSettings object that holds all of the information required for our solar system
        GenerationSettings newSettings = (GenerationSettings)ScriptableObject.CreateInstance("GenerationSettings");

        newSettings.settingsName = "New Settings";

        // set up the lighting settings
        newSettings.lightingSettings = randomSettings.lightingSettings;

        // set up the star settings
        // choose random numbers within the ranges specified in the random settings

        newSettings.starSettings = new GenerationSettings.StarSettings
        {
            surfaceGravity = Lerp(randomSettings.minStarSurfaceGravity, randomSettings.maxStarSurfaceGravity, UnityEngine.Random.value),
            radius         = Lerp(randomSettings.minStarRadius, randomSettings.maxStarRadius, UnityEngine.Random.value),
            axialSpin      = Lerp(randomSettings.minAxialSpin, randomSettings.maxAxialSpin, UnityEngine.Random.value) * Mathf.Sign(UnityEngine.Random.value - 0.5f),
            axisTilt       = Lerp(randomSettings.minAxialTilt, randomSettings.maxAxialTilt, UnityEngine.Random.value),
            temperature    = Lerp(randomSettings.minTemperature, randomSettings.maxTemperature, UnityEngine.Random.value)
        };

        // repeat for the planets, only there may be multiple planets
        int numPlanets = (int)Lerp(randomSettings.minNumPlanets, randomSettings.maxNumPlanets + 1, UnityEngine.Random.value);

        newSettings.planetSettings = new GenerationSettings.PlanetSettings[numPlanets];

        // as more planets are generated we want them to be increasing distances from the star
        // so we keep track of how far they are already from the star
        float distanceFromSun = Lerp(randomSettings.minInitDistanceFromSun, randomSettings.maxInitDistanceFromSun, UnityEngine.Random.value);

        for (int i = 0; i < numPlanets; i++)
        {
            // we need to generate the moons settings array before we populate the planet settings
            int   numMoons           = (int)Lerp(randomSettings.minNumMoons, randomSettings.maxNumMoons + 1, UnityEngine.Random.value);
            var   moonSettings       = new GenerationSettings.MoonSettings[numMoons];
            float distanceFromPlanet = Lerp(randomSettings.minInitDistanceFromPlanet, randomSettings.maxInitDistanceFromPlanet, UnityEngine.Random.value);

            // iterate through each moon and populate the moon settings
            for (int j = 0; j < numMoons; j++)
            {
                moonSettings[j] = new GenerationSettings.MoonSettings
                {
                    surfaceGravity    = Lerp(randomSettings.minMoonSurfaceGravity, randomSettings.maxMoonSurfaceGravity, UnityEngine.Random.value),
                    radius            = Lerp(randomSettings.minMoonRadius, randomSettings.maxMoonRadius, UnityEngine.Random.value),
                    axialSpin         = Lerp(randomSettings.minAxialSpin, randomSettings.maxAxialSpin, UnityEngine.Random.value) * Mathf.Sign(UnityEngine.Random.value - 0.5f),
                    axisTilt          = Lerp(randomSettings.minAxialTilt, randomSettings.maxAxialTilt, UnityEngine.Random.value),
                    structureSettings = CreateRandomStructureSettings(),
                    colorSettings     = CreateColourSettings(),
                    distance          = distanceFromPlanet,
                    eccentricity      = Lerp(randomSettings.minOrbitEccentricity, randomSettings.maxOrbitEccentricity, UnityEngine.Random.value),
                    offsetAngle       = Lerp(0, 360, UnityEngine.Random.value),
                    drawPath          = randomSettings.traceOrbits,
                    pathLength        = randomSettings.tracingLength
                };
                distanceFromPlanet *= 1 + Lerp(randomSettings.minMultDistanceFromPlanet, randomSettings.maxMultDistanceFromPlanet, UnityEngine.Random.value);
            }

            // now we can populate the planet settings
            newSettings.planetSettings[i] = new GenerationSettings.PlanetSettings
            {
                surfaceGravity    = Lerp(randomSettings.minPlanetSurfaceGravity, randomSettings.maxPlanetSurfaceGravity, UnityEngine.Random.value),
                radius            = Lerp(randomSettings.minPlanetRadius, randomSettings.maxPlanetRadius, UnityEngine.Random.value),
                axialSpin         = Lerp(randomSettings.minAxialSpin, randomSettings.maxAxialSpin, UnityEngine.Random.value) * Mathf.Sign(UnityEngine.Random.value - 0.5f),
                axisTilt          = Lerp(randomSettings.minAxialTilt, randomSettings.maxAxialTilt, UnityEngine.Random.value),
                structureSettings = CreateRandomStructureSettings(),
                colorSettings     = CreateColourSettings(),
                distance          = distanceFromSun,
                eccentricity      = Lerp(randomSettings.minOrbitEccentricity, randomSettings.maxOrbitEccentricity, UnityEngine.Random.value),
                offsetAngle       = Lerp(0, 360, UnityEngine.Random.value),
                drawPath          = randomSettings.traceOrbits,
                pathLength        = randomSettings.tracingLength,
                moonSettings      = moonSettings
            };
            distanceFromSun *= 1 + Lerp(randomSettings.minMultDistanceFromSun, randomSettings.maxMultDistanceFromSun, UnityEngine.Random.value);
        }
        return(newSettings);
    }