Exemple #1
0
    public void AddAsteroid()
    {
        var planets = FindObjectsOfType <CelestialObject>();

        foreach (CelestialObject co in planets)
        {
            if (!co.staticBody && !co.isShot)
            {
                Destroy(co.gameObject);
            }
        }
        GameObject asteroidGO = Instantiate(asteroid);

        asteroidGO.GetComponent <CelestialObject>().SetName("Asteroid");

        //asteroidGO.GetComponent<CelestialObject>().SetMass();
        GameObject            ARSessOrig = GameObject.Find("AR Session Origin");
        ARPlacementTrajectory placement  = ARSessOrig.GetComponent <ARPlacementTrajectory>();

        placement.setGOtoInstantiate(asteroidGO);
    }
    void Update()
    {
        if (save)
        {
            if (sceneIndex == 2)
            {
                //Save one planet to newPlanet.data
                SaveLoadStarSystem.SaveStarSystem(true, "/newPlanet.data");
                save = false;
            }
            else if (sceneIndex == 1)
            {
                //Save the entire system to system.data
                if (saveSpecific)
                {
                    SaveLoadStarSystem.SaveSpecificStarSystem(false, systemName + ".data");
                    saveSpecific = false;
                }
                else
                {
                    SaveLoadStarSystem.SaveStarSystem(false, "/system.data");
                    if (!firstGenSystemSaved)
                    {
                        firstGenSystemSaved = true;
                    }
                }
                save = false;
            }
        }
        if (load)
        {
            if (sceneIndex == 1)
            {
                //Load the entire system from system.data
                SystemSimulationData data;
                if (loadSpecific)
                {
                    data         = SaveLoadStarSystem.LoadSavedStarSystem(systemName);
                    loadSpecific = false;
                }
                else
                {
                    if (firstGenSystemSaved)
                    {
                        data = SaveLoadStarSystem.LoadStarSystem(false);
                    }
                    else
                    {
                        data = null;
                    }
                    load = false;
                }
                if (data != null)
                {
                    //CelestialObject.DestroyAll();

                    var planets = FindObjectsOfType <CelestialObject>();
                    foreach (CelestialObject co in planets)
                    {
                        if (!co.staticBody)
                        {
                            Destroy(co.gameObject);
                        }
                    }

                    int        rocky_i = 0;
                    int        gasy_i  = 0;
                    GameObject parent  = GameObject.Find("SceneObjects");
                    for (int i = 0; i < data.planetCount; i++)
                    {
                        GameObject obj = getPrefab(data, i);


                        MotherPlanet mp = obj.GetComponentInChildren <MotherPlanet>();
                        if (mp != null)
                        {
                            mp.GeneratePlanet();
                            mp.SetShape(data.planetList[rocky_i]);
                            mp.UpdateMesh();
                            rocky_i += 1;
                        }
                        //GasPlanetShaderMAterialPropertyBlock gp = co.GetComponentInChildren<GasPlanetShaderMAterialPropertyBlock>();
                        GasPlanetShaderMAterialPropertyBlock gp = obj.GetComponent <GasPlanetShaderMAterialPropertyBlock>();
                        if (gp != null)
                        {
                            gp.SetMaterial(data.gasPlanetList[gasy_i]);
                            gasy_i += 1;
                        }
                        //obj.GetComponent<SphereCollider>().enabled=false;

                        obj.GetComponent <CelestialObject>().enabled = true;

                        CelestialObject co = obj.GetComponent <CelestialObject>();
                        //print("position");
                        //print(data.physicsData[i].position);
                        //print("----------");
                        co.SetState(data.physicsData[i]);
                        obj.transform.SetParent(parent.transform);
                    }
                    load = false;
                }
                else
                {
                    load = false;
                    Debug.Log("failed to load entire system");
                }
            }
        }
        if (loadNewPlanet)
        {
            if (sceneIndex == 1)
            {
                loadNewPlanet = false;
                //Load one planet from newPlanet data and p
                SystemSimulationData data = SaveLoadStarSystem.LoadStarSystem(true);
                if (data != null)
                {
                    //CelestialObject.DestroyAll();
                    int rocky_i = 0;
                    int gasy_i  = 0;
                    for (int i = 0; i < data.planetCount; i++)
                    {
                        GameObject      obj = getPrefab(data, i);
                        CelestialObject co  = GetComponent <CelestialObject>();
                        if (co != null)
                        {
                            obj.AddComponent(typeof(CelestialObject));
                        }
                        obj.GetComponent <CelestialObject>().isShot = false;
                        //obj.GetComponent<IcoPlanet>().enabled = true;

                        MotherPlanet mp = obj.GetComponentInChildren <MotherPlanet>();
                        if (mp != null)
                        {
                            mp.GeneratePlanet();
                            mp.SetShape(data.planetList[rocky_i]);
                            mp.UpdateMesh();
                            mp.GenerateColors();
                            mp.GetComponent <IcoPlanet>().staticBody = false;
                            mp.GetComponent <IcoPlanet>().enabled    = true;

                            rocky_i += 1;
                        }
                        GasPlanetShaderMAterialPropertyBlock gp = obj.GetComponentInChildren <GasPlanetShaderMAterialPropertyBlock>();
                        if (gp != null)
                        {
                            gp.SetMaterial(data.gasPlanetList[gasy_i]);
                            gp.GetComponent <GasPlanetShaderMAterialPropertyBlock>().enabled = true;
                            gasy_i += 1;
                        }
                        string newPlanetName = PlayerPrefs.GetString("NewPlanetName", "Unknown Planet");
                        obj.GetComponent <CelestialObject>().SetName(newPlanetName);
                        PlayerPrefs.SetString("NewPlanetName", "Unknown Planet");
                        if (!obj.GetComponent <CelestialObject>().isAsteroid)
                        {
                            obj.GetComponent <CelestialObject>().SetMass();
                        }
                        GameObject            ARSessOrig = GameObject.Find("AR Session Origin");
                        ARPlacementTrajectory placement  = ARSessOrig.GetComponent <ARPlacementTrajectory>();
                        placement.setGOtoInstantiate(obj);
                    }
                }
            }
        }
        if (delete)
        {
            if (sceneIndex == 1)
            {
                SaveLoadStarSystem.DeleteStarSystem(delSystemName);
                delete = false;
            }
        }
    }