コード例 #1
0
    void InitializeSystem()
    {
        //Create planets here

        float currentDistance = MinPlanetDistance;

        List <int> FreePlanets = new List <int>();

        for (int i = 0; i < PlanetCount; i++)
        {
            //Generate random position
            Vector3 newPos = new Vector3(0f, 0f, 0f);

            //try to avoid overlays of planets

            currentDistance += Random.Range(1f, 7f);
            float dist = currentDistance;

            //check that planet on proper distance from each other
            float resultSpeed = Random.Range(PlanetInitSpeedMin, PlanetInitSpeedMax);

            //ability to set any start angle on orbit
            newPos.x = Mathf.Sin(0f) * dist;
            newPos.y = Mathf.Cos(0f) * dist;

            //better if depends from size
            //  float newPlanetMass = Random.Range(PlanetInitMassMin, PlanetInitMassMax);

            GameObject     newPlanet = Instantiate(PlanetPrefab, newPos, PlanetPrefab.transform.rotation);
            PlanetBehavior planet    = newPlanet.GetComponent <PlanetBehavior>();
            planet.ID = i;

            //randomize planet type
            int        randomType = Random.Range(0, 3);
            PlanetType t          = (PlanetType)randomType;

            int weaponType = Random.Range(1, 4);
            //choose weapon here

            planet.Initialize(resultSpeed, t, weaponType);

            PlanetList.Add(planet);

            GameObject barObject = Instantiate(HpBarPrefab, newPos + new Vector3(0f, 10f, -10f), Quaternion.identity);
            HealthBar  bar       = barObject.GetComponent <HealthBar>();
            bar.AttachPlanet(planet);
            PlanetBarsList.Add(bar);
            Storage.Instance.AddToPool(barObject);

            //add offset from planet
            currentDistance += planet.GetSize() + 3f;
            FreePlanets.Add(planet.ID);
        }

        int playerPlanetIndex = Random.Range(0, FreePlanets.Count);
        int playerPlanetID    = FreePlanets[playerPlanetIndex];

        FreePlanets.Remove(playerPlanetID);

        //also select planet for enemy
        int enemyIndex    = Random.Range(0, FreePlanets.Count);
        int enemyPlanetID = FreePlanets[enemyIndex];

        FreePlanets.Remove(enemyPlanetID);


        foreach (PlanetBehavior planet in PlanetList)
        {
            if (planet.ID == playerPlanetID)
            {
                planet.SetPlanetOwner(PlanetBehavior.PlanetOwnerType.Player);
                Messenger.Broadcast(MessageKeys.ON_PLANET_SETTED, planet, planet.GetWeaponID());
                Debug.Log("Player planet ID:" + playerPlanetID);
            }
            else if (planet.ID == enemyPlanetID)
            {
                planet.SetPlanetOwner(PlanetBehavior.PlanetOwnerType.AI);
                Debug.Log("AI planet ID:" + playerPlanetID);
            }
            else
            {
                planet.SetPlanetOwner(PlanetBehavior.PlanetOwnerType.None);
            }
        }
    }