Ejemplo n.º 1
0
    private void GenerateCBodiesAsteroids(int nCBodiesAsteroidClusters, GameObject centreCBodyStar)
    {
        //Properties
        float minimumDistanceBetweenClusters = 100f;
        float distanceOut = 1300f - minimumDistanceBetweenClusters;
        float randSpacing;
        float spawnRadius;
        float spawnAngle;
        int   clusterSize;
        byte  clusterType;

        //Spawn all
        for (int i = 0; i < nCBodiesAsteroidClusters; i++)
        {
            //Instance cBody
            randSpacing = Random.Range(0f, 600f) + Mathf.Pow(Random.Range(0f, 15f), 2f);
            spawnRadius = distanceOut + minimumDistanceBetweenClusters + randSpacing;
            distanceOut = spawnRadius;                          //increment distanceOut for the next cBody
            spawnAngle  = Random.Range(0f, 360f);
            clusterSize = Control.LowBiasedRandomIntSquared(4); //range of 1 to 16 (4^2 = 16)

            //We don't have to add 1 here to format for Random.Range max being exclusive for ints because the length is already 1 greater than the index (since index starts at 0)
            clusterType = (byte)Random.Range(0, Ore.typeLength);

            for (int clusterI = 0; clusterI < clusterSize; clusterI++)
            {
                GameObject instanceCBodyAsteroid = Instantiate(
                    cBodyAsteroid,
                    new Vector3(
                        Mathf.Cos(spawnAngle) * spawnRadius,
                        0f,
                        Mathf.Sin(spawnAngle) * spawnRadius
                        ),
                    Quaternion.Euler(
                        Random.Range(0f, 360f),
                        Random.Range(0f, 360f),
                        Random.Range(0f, 360f)
                        )
                    );

                //Put in CBodies tree
                instanceCBodyAsteroid.transform.parent = cBodiesAsteroids.transform;

                //Spread out within cluster
                instanceCBodyAsteroid.transform.position += 2f * new Vector3(Random.value, Random.value, Random.value);

                Gravity instanceCBodyGravityScript = instanceCBodyAsteroid.GetComponent <Gravity>();
                //Orbit central star
                instanceCBodyGravityScript.SetVelocityToOrbit(centreCBodyStar, spawnAngle);

                CBodyAsteroid instanceCBodyAsteroidScript = instanceCBodyAsteroid.GetComponent <CBodyAsteroid>();
                //Randomize size and type
                instanceCBodyAsteroidScript.SetSize(CBodyAsteroid.GetRandomSize()); //MUST SET SIZE FIRST SO THAT MODEL IS SELECTED
                instanceCBodyAsteroidScript.SetType(clusterType);
                //Give control reference
                instanceCBodyAsteroidScript.control = control;
                instanceCBodyGravityScript.control  = control;
            }
        }
    }
Ejemplo n.º 2
0
    public GameObject SpawnAsteroidManually(Vector3 position, Vector3 velocity, string size, byte type, byte health) //bool randomType)
    {
        GameObject instanceCBodyAsteroid = Instantiate(
            cBodyAsteroid,
            position,
            Quaternion.Euler(
                Random.Range(0f, 360f),
                Random.Range(0f, 360f),
                Random.Range(0f, 360f)
                )
            );

        CBodyAsteroid instanceCBodyAsteroidScript = instanceCBodyAsteroid.GetComponent <CBodyAsteroid>();

        //Put in CBodies tree
        instanceCBodyAsteroid.transform.parent = cBodiesAsteroids.transform;

        //Give control reference
        instanceCBodyAsteroidScript.control = control;
        instanceCBodyAsteroid.GetComponent <Gravity>().control = control;

        //Set velocity
        instanceCBodyAsteroid.GetComponent <Rigidbody>().velocity = velocity;

        //Randomize size and type
        instanceCBodyAsteroidScript.SetSize(size);
        //instanceCBodyAsteroidScript.SetSize(instanceCBodyAsteroidScript.RandomSize()); //MUST SET SIZE FIRST SO THAT MODEL IS SELECTED

        //Type
        instanceCBodyAsteroidScript.SetType(type);

        /*
         * if (randomType)
         * {
         *  instanceCBodyAsteroidScript.SetType((byte)Random.Range(0, Ore.typeLength));
         * }
         * else
         * {
         *  instanceCBodyAsteroidScript.SetType(0);
         * }
         */

        //Health
        instanceCBodyAsteroidScript.health = health;

        return(instanceCBodyAsteroid);
    }
Ejemplo n.º 3
0
    private void SpawnAsteroid(string size)
    {
        //Instantiate at parent position, plus some randomness
        GameObject instanceCBodyAsteroid = Instantiate(
            control.generation.cBodyAsteroid,
            transform.position + (1.2f * new Vector3(Random.value, Random.value, Random.value)),
            Quaternion.Euler(
                Random.Range(0f, 360f),
                Random.Range(0f, 360f),
                Random.Range(0f, 360f)
                )
            );

        //Put in CBodies tree
        instanceCBodyAsteroid.transform.parent = control.generation.cBodiesAsteroids.transform;

        //Pass control reference
        instanceCBodyAsteroid.GetComponent <Gravity>().control = control;

        //Rigidbody
        Rigidbody instanceCBodyAsteroidRb = instanceCBodyAsteroid.GetComponent <Rigidbody>();

        //Ignore all collisions unless explicitly enabled (once asteroid is separated from siblings)
        instanceCBodyAsteroidRb.detectCollisions = false;
        //Copy velocity and add some random impulse force
        instanceCBodyAsteroidRb.velocity              = rb.velocity;
        instanceCBodyAsteroidRb.angularVelocity       = rb.angularVelocity;
        instanceCBodyAsteroidRb.inertiaTensor         = rb.inertiaTensor;
        instanceCBodyAsteroidRb.inertiaTensorRotation = rb.inertiaTensorRotation;
        instanceCBodyAsteroidRb.AddForce(25f * new Vector3(
                                             0.5f + (0.5f * Random.value),
                                             0.5f + (0.5f * Random.value),
                                             0.5f + (0.5f * Random.value)
                                             ));
        instanceCBodyAsteroidRb.AddTorque(100f * new Vector3(
                                              Random.value,
                                              Random.value,
                                              Random.value
                                              ));

        //Script
        CBodyAsteroid instanceCBodyAsteroidScript = instanceCBodyAsteroid.GetComponent <CBodyAsteroid>();

        instanceCBodyAsteroidScript.control = control;
        instanceCBodyAsteroidScript.SetSize(size);
        instanceCBodyAsteroidScript.SetType(type);
    }