Exemple #1
0
    static public SimulationObjectState createObjectFromJSON(string stateStr, GameObject[] gameObjectTemplates, bool active)
    {
        if (gameObjectTemplates != null)
        {
            var state = JsonUtility.FromJson <SimulationObjectState>(stateStr);

            GameObject refObject = gameObjectTemplates[state.templateIndex];
            GameObject obj       = Object.Instantiate(refObject);

            SimulationMaterial mat = new SimulationMaterial((SimulationMaterial.TYPE)state.material);
            obj.GetComponent <Rigidbody>().SetDensity(mat.GetDensity());

            SimulationColor col    = new SimulationColor((SimulationColor.TYPE)state.color);
            Material        newMat = Object.Instantiate(refObject.GetComponent <Renderer>().material);
            newMat.SetColor("_BaseColor", col.GetColor(mat.type));
            obj.GetComponent <Renderer>().material = newMat;

            obj.transform.position = state.position;
            obj.transform.rotation = state.rotation;
            //obj.GetComponent<Rigidbody>().velocity = state.velocity;
            //obj.GetComponent<Rigidbody>().inertiaTensor = state.inertiaTensor;
            //obj.GetComponent<Rigidbody>().inertiaTensorRotation = state.inertiaTensorRotation;
            //obj.GetComponent<Rigidbody>().angularVelocity = state.angularVelocity;

            if (active && state.active == 1)
            {
                obj.SetActive(true);
            }

            state.SetGameObject(obj);
            return(state);
        }
        return(null);
    }
    protected virtual void AddRandomSimulationObject()
    {
        int templateIndex = 0;


        if (controllerState.noObjects - noObjects < controllerState.initialBigObjects)
        {
            templateIndex = Random.Range(0, numberOfDistinctObjectUsed / 2);
            templateIndex = templateIndex * 2 + 1;
        }
        else
        {
            templateIndex = Random.Range(0, numberOfDistinctObjectUsed);
        }

        /*
         * if (controllerState.noObjects - noObjects < controllerState.initialBigObjects)
         * {
         *  templateIndex = 3;
         * }
         * else
         * {
         *  templateIndex = Random.Range(2, numberOfDistinctObjectUsed);
         * }
         */

        GameObject refObject = gameObjectTemplates[templateIndex];
        GameObject obj       = Object.Instantiate(refObject);

        obj.SetActive(true);

        float x_pos = Random.Range(controllerState.throwMinX, controllerState.throwMaxX);
        float y_pos = Random.Range(controllerState.throwMinY, controllerState.throwMaxY);
        float z_pos = Random.Range(controllerState.throwMinZ, controllerState.throwMaxZ);

        obj.GetComponent <Rigidbody>().position = new Vector3(x_pos, y_pos, z_pos);

        SimulationObjectState objState = new SimulationObjectState();

        objState.material = (templateIndex < 2) ? 0 : 1;
        objState.size     = (templateIndex % 2 == 0) ? 0 : 1;

        int             colorIndex = Random.Range(0, numberOfDistinctColorsUsed);
        SimulationColor col        = new SimulationColor((SimulationColor.TYPE)colorIndex);
        Material        newMat     = Instantiate(refObject.GetComponent <Renderer>().material);

        newMat.SetColor("_BaseColor", col.GetColor((SimulationMaterial.TYPE)objState.material));
        obj.GetComponent <Renderer>().material = newMat;


        objState.SetGameObject(obj);
        objState.color         = colorIndex;
        objState.templateIndex = templateIndex;

        prevObjectState   = objState;
        prevCreatedObject = obj;
    }