Exemplo n.º 1
0
    static public GameObject GetPlanetObject(PlanetTypes.e_type planetType)
    {
        GameObject planet   = Resources.Load <GameObject>("GameObjects/PlanetTravelScreen");
        string     basePath = "Graphics/planets";

        switch (planetType)
        {
        case PlanetTypes.e_type.ANTI_MATER: planet.GetComponent <SpriteRenderer>().sprite = GetSprite(basePath + "/antimater"); break;

        case PlanetTypes.e_type.ICE: planet.GetComponent <SpriteRenderer>().sprite = GetSprite(basePath + "/ice"); break;

        case PlanetTypes.e_type.MAGMA: planet.GetComponent <SpriteRenderer>().sprite = GetSprite(basePath + "/magma"); break;

        case PlanetTypes.e_type.VEGETATION: planet.GetComponent <SpriteRenderer>().sprite = GetSprite(basePath + "/vegetation"); break;

        case PlanetTypes.e_type.WATER: planet.GetComponent <SpriteRenderer>().sprite = GetSprite(basePath + "/water"); break;
        }
        return(planet);
    }
Exemplo n.º 2
0
    void RadialPlanetSpawn(Vector2 centerPos, float maxRadiusSpawn, float density, PlanetTypes.e_type planetType, string planetGroupName)
    {
        float       startTime             = Time.realtimeSinceStartup;
        const float minDistanceFromCenter = 8f;                                                                                           //used since density close to center is huge(planets would be to close to each other)
        GameObject  planetsHolder         = new GameObject(planetGroupName);                                                              //Parent GameObject of all instantiated objects

        for (float distanceFromCenter = minDistanceFromCenter; distanceFromCenter < maxRadiusSpawn; distanceFromCenter += LAYER_DISTANCE) //loop through every layer(layer=one of manny circles around center with given radius. Every next circle will have bigger radius/will be further from center)
        {
            List <GameObject> currentLayerPlanetList = new List <GameObject>();
            for (int i = 0; i < density; ++i)                                          //Try to spawn this many planets on current layer
            {
                float distancePercentFactor = 1 - distanceFromCenter / maxRadiusSpawn; //it gets lower, the chance that planet will be spawn are also lower
                bool  shouldPlanetBeSpawn   = (distancePercentFactor > Random.Range(0f, 1f)) ? true : false;
                if (shouldPlanetBeSpawn)
                {
                    float   randomAngle             = Random.Range(0f, 360f) * Mathf.Deg2Rad;                                                                                            //sin and cos function work with radians
                    Vector2 newPlanetRandPosOnLayer = new Vector2(distanceFromCenter * Mathf.Cos(randomAngle) + centerPos.x, distanceFromCenter * Mathf.Sin(randomAngle) + centerPos.y); //calculate position of coordinate with given angle

                    bool planetDistanceToShort = false;
                    foreach (GameObject p in currentLayerPlanetList)    //loop through every planet on current layer and check if there is AT LEAST ONE that is to close to our planet
                    {
                        if (Vector2.Distance(p.transform.position, newPlanetRandPosOnLayer) < MIN_PLANET_ON_SAME_LAYER_DISTANCE)
                        {
                            planetDistanceToShort = true;
                        }
                    }
                    if (!planetDistanceToShort)
                    {
                        GameObject newPlanet = Instantiate(ResourceManager.GetPlanetObject(planetType), newPlanetRandPosOnLayer, Quaternion.identity, planetsHolder.transform);
                        currentLayerPlanetList.Add(newPlanet);

                        /*
                         * visiblePlanets are here for now
                         */
                        visiblePlanets.Add(newPlanet);
                    }
                }
            }
        }
        Debug.Log("Planets spawn => [" + visiblePlanets.Count + "," + planetType + "] Time=" + (Time.realtimeSinceStartup - startTime));
    }