Exemplo n.º 1
0
    void OnMouseUpAsButton()
    {
        if (GUIUtility.hotControl == 0)
        {
            switch (main.currentMode)
            {
            case 1:
                setSpawn(!isSpawn);
                break;

            case 2:
                isMoving = false;
                break;

            case 4:
                Destroy(this.gameObject);
                break;

            case 7:
                if (main.prevSelectedHyperlane)
                {
                    if (main.prevSelectedHyperlane != this)
                    {
                        // Debug.Log("creating hyperlane between system "+main.prevSelectedHyperlane.systemid+" and system "+systemid);
                        GameObject line = (GameObject)Instantiate(hyperlane_prefab, new Vector3(0, 0, 0), Quaternion.identity);
                        hyperlane  hl   = line.GetComponent <hyperlane>();
                        line.transform.SetParent(main.GalaxyEditor.transform, true);
                        hl.link0 = main.prevSelectedHyperlane;
                        hl.link1 = this;
                        hl.transform.position = main.prevSelectedHyperlane.transform.position;
                        main.prevSelectedHyperlane.connectedLines++;
                        this.connectedLines++;
                        main.prevSelectedHyperlane = null;
                    }
                }
                else
                {
                    main.prevSelectedHyperlane = this;
                }
                break;

            default:
                select(true);
                break;
            }
        }
    }
Exemplo n.º 2
0
    IEnumerator Load(string name)
    {
        main.isGenerating           = true;
        main.generationStatusString = "Loading Project\nPlease wait...\n\nLoading: Project Settings";
        string fileName = name + ".rsgg";

        main.ResetAll();

        var    sr       = File.OpenText(fileName);
        string filetext = sr.ReadToEnd();
        var    N        = JSON.Parse(filetext);

        try
        {
            if (N["galaxy"]["version"].Value != main.version)
            {
                main.Log("Warning: Project version is " + N["galaxy"]["version"].Value);
                main.Log("Your RSGG version is " + main.version);
            }
            main.galaxyname        = N["galaxy"]["name"];
            main.autogenHyperlines = bool.Parse(N["galaxy"]["random_hyperlanes"]);
            main.galaxysize        = int.Parse(N["galaxy"]["size"].Value);
            main.genhlradius       = int.Parse(N["galaxy"]["hl_radius"].Value);
            main.genhlmaxlines     = int.Parse(N["galaxy"]["hl_maxPerSys"].Value);
            main.generationseed    = int.Parse(N["galaxy"]["seed"].Value);
            main.starstogen        = float.Parse(N["galaxy"]["stars"].Value);
            main.nscale            = float.Parse(N["galaxy"]["pnoise_scale"].Value);
            main.nfreq             = float.Parse(N["galaxy"]["pnoise_freq"].Value);
            main.npers             = float.Parse(N["galaxy"]["pnoise_pers"].Value);
            main.pngmultiplier     = float.Parse(N["galaxy"]["png_multiplier"].Value);
            main.pngrandomizer     = float.Parse(N["galaxy"]["png_randomizer"].Value);
            main.pngreducer        = float.Parse(N["galaxy"]["png_reducer"].Value);
            main.usePerlin         = bool.Parse(N["galaxy"]["pnoise"]);
            main.refreshGalaxySizeLines(int.Parse(N["galaxy"]["size"].Value));
            if (float.Parse(N["galaxy"]["version"].Value) >= 0.9f)
            {
                main.minDist = int.Parse(N["galaxy"]["minimalSystemDistance"].Value);
            }
            else
            {
                main.minDist = 12;
            }
            // int[] d = new int[N["galaxy"]["systems"].Count*2];
            // for(int x = 0; x < N["galaxy"]["systems"].Count; x++)
            // {
            // d[x] = int.Parse(N["galaxy"]["systems"][x]["position"]["x"].Value);
            // d[N["galaxy"]["systems"].Count+x] = int.Parse(N["galaxy"]["systems"][x]["position"]["y"].Value);
            // }
            // main.galaxysize = Mathf.Max(d);
        }
        catch
        {
            // yield return new WaitForSeconds(0.001f);
            main.isGenerating = false;
            main.Log("Error while loading project");
        }

        main.generationStatusString = "Loading Project\nPlease wait...\n\nLoading: Galaxy Systems";
        yield return(new WaitForSeconds(0.001f));

        List <system> TsysList = new List <system>();

        try
        {
            // Systems
            for (int x = 0; x < N["galaxy"]["systems"].Count; x++)
            {
                GameObject g = (GameObject)Instantiate(system_prefab, new Vector3(float.Parse(N["galaxy"]["systems"][x]["position"]["x"]), 0, float.Parse(N["galaxy"]["systems"][x]["position"]["y"])), Quaternion.identity);
                g.transform.SetParent(main.GalaxyEditor.transform, true);
                system sys = g.GetComponent <system>();
                if (N["galaxy"]["systems"][x]["name"].Value != "null")
                {
                    sys.systemName = N["galaxy"]["systems"][x]["name"].Value;
                }
                if (N["galaxy"]["systems"][x]["initializer"].Value != "null")
                {
                    sys.initializer = N["galaxy"]["systems"][x]["initializer"].Value;
                }
                sys.prevent  = bool.Parse(N["galaxy"]["systems"][x]["prevent_hyperlane"].Value);
                sys.systemid = int.Parse(N["galaxy"]["systems"][x]["id"].Value);
                if (bool.Parse(N["galaxy"]["systems"][x]["spawn"].Value))
                {
                    sys.setSpawn(true);
                }
                int xx = x;
                while (TsysList.Count <= sys.systemid)
                {
                    TsysList.Add(sys);
                    xx++;
                }
                // TsysList.Insert(sys.systemid, sys);
            }
        }
        catch
        {
            main.isGenerating = false;
            main.Log("Error while loading project");
        }

        main.generationStatusString = "Loading Project\nPlease wait...\n\nLoading: Galaxy Nebulas";
        yield return(new WaitForSeconds(0.001f));

        try
        {
            //Nebulas
            for (int x = 0; x < N["galaxy"]["nebulas"].Count; x++)
            {
                GameObject g = (GameObject)Instantiate(nebula_prefab, new Vector3(float.Parse(N["galaxy"]["nebulas"][x]["position"]["x"]), 0, float.Parse(N["galaxy"]["nebulas"][x]["position"]["y"])), Quaternion.identity);
                g.transform.SetParent(main.GalaxyEditor.transform, true);
                nebula neb = g.GetComponent <nebula>();
                if (N["galaxy"]["nebulas"][x]["name"].Value != "null")
                {
                    neb.nebulaName = N["galaxy"]["nebulas"][x]["name"].Value;
                }
                neb.nebulaRadius = int.Parse(N["galaxy"]["nebulas"][x]["radius"].Value);
            }
        }
        catch
        {
            // yield return new WaitForSeconds(0.001f);
            main.isGenerating = false;
            main.Log("Error while loading project");
        }

        if (loadHyperlanes)
        {
            main.generationStatusString = "Loading Project\nPlease wait...\n\nLoading: Galaxy Hyperlanes";
            yield return(new WaitForSeconds(0.001f));

            try
            {
                //Hyperlanes
                for (int x = 0; x < N["galaxy"]["hyperlanes"].Count; x++)
                {
                    GameObject g = (GameObject)Instantiate(hyperlane_prefab, new Vector3(0, 0, 0), Quaternion.identity);
                    g.transform.SetParent(main.GalaxyEditor.transform, true);
                    hyperlane hl = g.GetComponent <hyperlane>();

                    hl.link0 = TsysList[int.Parse(N["galaxy"]["hyperlanes"][x]["from"].Value)];
                    hl.link1 = TsysList[int.Parse(N["galaxy"]["hyperlanes"][x]["to"].Value)];
                    if (N["galaxy"]["hyperlanes"][x]["mode"].Value == "add")
                    {
                        hl.prevent = false;
                    }
                    else if (N["galaxy"]["hyperlanes"][x]["mode"].Value == "prevent")
                    {
                        hl.prevent = true;
                    }
                }
            }
            catch
            {
                // yield return new WaitForSeconds(0.001f);
                main.isGenerating = false;
                main.Log("Error while loading project");
            }
        }
        main.generationStatusString = "Loading Project\nPlease wait...\n\nLoading: Custom Systems";
        yield return(new WaitForSeconds(0.001f));

        try
        {
            //Custom Systems
            for (int x = 0; x < N["customSystems"].Count; x++)
            {
                GameObject   g  = (GameObject)Instantiate(systemeditor_prefab, new Vector3(0, 0, 0), Quaternion.identity);
                systemeditor se = g.GetComponent <systemeditor>();
                main.systemEditors.Add(g);
                system s = TsysList[int.Parse(N["customSystems"][x]["systemID"].Value)];

                se.SetForSys(s);
                se.systemName         = N["customSystems"][x]["name"];
                se.starType           = N["customSystems"][x]["starType"];
                se.centerStarSize     = int.Parse(N["customSystems"][x]["starSize"].Value);
                se.centerStarDisabled = bool.Parse(N["customSystems"][x]["disableCenterStar"].Value);
                se.asteroidBelt       = bool.Parse(N["customSystems"][x]["asteroidBelt"].Value);
                se.asteroidBeltRadius = int.Parse(N["customSystems"][x]["asteroidBeltRadius"].Value);
                for (int y = 0; y < N["customSystems"][x]["planets"].Count; y++)
                {
                    GameObject planetGO = (GameObject)Instantiate(planet_prefab, new Vector3(float.Parse(N["customSystems"][x]["planets"][y]["position"]["x"]), 0, float.Parse(N["customSystems"][x]["planets"][y]["position"]["y"])), Quaternion.identity);
                    planetGO.transform.SetParent(g.transform, true);
                    planet p = planetGO.GetComponent <planet>();
                    planetGO.name = "planet_" + se.thisSystem.systemid;
                    p.ownerSystem = se.thisSystem.systemid;
                    if (N["customSystems"][x]["planets"][y]["name"].Value != "null")
                    {
                        p.planetName = N["customSystems"][x]["planets"][y]["name"].Value;
                    }
                    p.planetClass   = N["customSystems"][x]["planets"][y]["class"].Value;
                    p.planetSize    = int.Parse(N["customSystems"][x]["planets"][y]["size"].Value);
                    p.orbitDistance = int.Parse(N["customSystems"][x]["planets"][y]["orbitDistance"].Value);
                    p.orbitAngle    = int.Parse(N["customSystems"][x]["planets"][y]["orbitAngle"].Value);
                    p.isHome        = bool.Parse(N["customSystems"][x]["planets"][y]["home"].Value);
                    if (p.isHome)
                    {
                        p.planetSize  = 16;
                        p.planetClass = "ideal_planet_class";
                    }

                    if (float.Parse(N["galaxy"]["version"].Value) >= 0.8f)
                    {
                        p.hasRing = bool.Parse(N["customSystems"][x]["planets"][y]["ringed"].Value);
                    }
                    else
                    {
                        p.hasRing = false;
                    }

                    if (float.Parse(N["galaxy"]["version"].Value) >= 0.9f)
                    {
                        p.noAnomaly      = bool.Parse(N["customSystems"][x]["planets"][y]["no_anomaly"].Value);
                        p.noResources    = bool.Parse(N["customSystems"][x]["planets"][y]["no_resources"].Value);
                        p.planetModifier = N["customSystems"][x]["planets"][y]["modifier"].Value;
                    }

                    g.GetComponent <systemeditor>().planets.Add(planetGO);

                    for (int z = 0; z < N["customSystems"][x]["planets"][y]["moons"].Count; z++)
                    {
                        GameObject planetmGO = (GameObject)Instantiate(planet_prefab, new Vector3(float.Parse(N["customSystems"][x]["planets"][y]["moons"][z]["position"]["x"]), 0, float.Parse(N["customSystems"][x]["planets"][y]["moons"][z]["position"]["y"])), Quaternion.identity);
                        planetmGO.transform.SetParent(g.transform, true);
                        planet pm = planetmGO.GetComponent <planet>();
                        planetmGO.name = "moon_" + se.thisSystem.systemid;
                        pm.ownerSystem = se.thisSystem.systemid;
                        pm.ownerPlanet = p;
                        if (N["customSystems"][x]["planets"][y]["moons"][z]["name"].Value != "null")
                        {
                            pm.planetName = N["customSystems"][x]["planets"][y]["moons"][z]["name"].Value;
                        }
                        pm.planetClass   = N["customSystems"][x]["planets"][y]["moons"][z]["class"].Value;
                        pm.planetSize    = int.Parse(N["customSystems"][x]["planets"][y]["moons"][z]["size"].Value);
                        pm.orbitDistance = int.Parse(N["customSystems"][x]["planets"][y]["moons"][z]["orbitDistance"].Value);
                        pm.orbitAngle    = int.Parse(N["customSystems"][x]["planets"][y]["moons"][z]["orbitAngle"].Value);
                        pm.isHome        = bool.Parse(N["customSystems"][x]["planets"][y]["moons"][z]["home"].Value);
                        if (pm.isHome)
                        {
                            pm.planetSize  = 16;
                            pm.planetClass = "ideal_planet_class";
                        }

                        if (float.Parse(N["galaxy"]["version"].Value) >= 0.8f)
                        {
                            pm.hasRing = bool.Parse(N["customSystems"][x]["planets"][y]["moons"][z]["ringed"].Value);
                        }
                        else
                        {
                            pm.hasRing = false;
                        }

                        if (float.Parse(N["galaxy"]["version"].Value) >= 0.9f)
                        {
                            pm.noAnomaly      = bool.Parse(N["customSystems"][x]["planets"][y]["moons"][z]["no_anomaly"].Value);
                            pm.noResources    = bool.Parse(N["customSystems"][x]["planets"][y]["moons"][z]["no_resources"].Value);
                            pm.planetModifier = N["customSystems"][x]["planets"][y]["moons"][z]["modifier"].Value;
                        }
                        g.GetComponent <systemeditor>().planets.Add(planetmGO);
                    }
                }
                g.SetActive(false);
            }
        }
        catch
        {
            // yield return new WaitForSeconds(0.001f);
            main.isGenerating = false;
            main.Log("Error while loading project");
        }
        main.isGenerating = false;
        main.Log("Project Loaded");
    }
Exemplo n.º 3
0
    IEnumerator generateHyperlanes()
    {
        isGenerating = true;
        float        progressPercent = 0f;
        float        timeLeft        = 0f;
        List <float> times           = new List <float>();
        var          temp            = Time.realtimeSinceStartup;

        GameObject[] gameObjects = GameObject.FindGameObjectsWithTag("star_system");
        int          cnt         = 0;
        int          lcnt        = 0;

        foreach (GameObject sy in gameObjects)
        {
            var tempL = Time.realtimeSinceStartup;
            if (sy)
            {
                float[] distances = new float[gameObjects.Length];
                int[]   indexes   = new int[gameObjects.Length];
                int     arrayind  = 0;
                int     j         = 0;
                foreach (GameObject go in gameObjects)
                {
                    float dist = Vector3.Distance(sy.transform.position, go.transform.position);
                    if (dist <= genhlradius && dist > 0)
                    {
                        distances[arrayind] = dist;
                        indexes[arrayind]   = j;
                        arrayind++;
                    }
                    j++;
                }
                foreach (float distance in distances)
                {
                    if (distance <= 0)
                    {
                        distances[Array.IndexOf(distances, distance)] = genhlradius + 1;
                    }
                }
                Array.Sort(distances, indexes);
                Array.Resize(ref distances, genhlmaxlines + 1);
                Array.Resize(ref indexes, genhlmaxlines + 1);
                for (int i = 0; i < distances.Length; i++)
                {
                    float  mindDist = Mathf.Min(distances);
                    int    ind      = indexes[Array.IndexOf(distances, mindDist)];
                    system sys0     = sy.GetComponent <system>();
                    system sys1     = gameObjects[ind].GetComponent <system>();
                    distances[Array.IndexOf(distances, mindDist)] = genhlradius + 1;
                    bool         ignorelane = false;
                    GameObject[] hlt        = GameObject.FindGameObjectsWithTag("hyperlane");
                    foreach (GameObject ht in hlt)
                    {
                        if (ht.name == "hl_" + sys0.systemid + "_to_" + sys1.systemid || ht.name == "hl_" + sys1.systemid + "_to_" + sys0.systemid)
                        {
                            ignorelane = true;
                            // Debug.Log("Ignoring:" + ht.name);
                            break;
                        }
                    }

                    if (!ignorelane)
                    {
                        float checkdist = Vector3.Distance(sy.transform.position, gameObjects[ind].transform.position);
                        if (checkdist <= genhlradius && sy != gameObjects[ind] && sys0.connectedLines < genhlmaxlines && sys1.connectedLines < genhlmaxlines && checkdist > 0 && !sys0.prevent && !sys1.prevent)
                        {
                            GameObject line = (GameObject)Instantiate(hyperlane_prefab, new Vector3(0, 0, 0), Quaternion.identity);
                            line.transform.SetParent(GalaxyEditor.transform, true);
                            hyperlane hl = line.GetComponent <hyperlane>();
                            hl.link0 = sys0;
                            hl.link1 = sys1;
                            hl.transform.position = sys0.transform.position;
                            hl.SetName();
                            sys0.connectedLines++;
                            sys1.connectedLines++;
                            cnt++;
                        }
                    }
                }
            }
            lcnt++;
            //Stats
            if (lcnt % 10 == 0)
            {
                float thislTime = float.Parse((Time.realtimeSinceStartup - tempL).ToString("f3"));
                times.Add(thislTime);
                progressPercent = (float)lcnt / (float)gameObjects.Length * 100;
                timeLeft        = ((float)gameObjects.Length - (float)lcnt) * times.Average();
                TimeSpan t              = TimeSpan.FromSeconds(timeLeft);
                TimeSpan tt             = TimeSpan.FromSeconds(thislTime);
                string   timeLeftString = string.Format("{0:D2}h:{1:D2}m:{2:D2}s",
                                                        t.Hours,
                                                        t.Minutes,
                                                        t.Seconds);
                string thislTimeString = string.Format("{0:D2}m:{1:D2}s:{2:D3}ms",
                                                       tt.Minutes,
                                                       tt.Seconds,
                                                       tt.Milliseconds);
                generationStatusString = "Generating: " + lcnt + "/" + gameObjects.Length + "\nHyperlanes: " + cnt + "\nLast gen duration: " + thislTimeString + "\n\nProgress: " + progressPercent.ToString("f2") + "%" + "\nEstimated Time Left: " + timeLeftString;
                yield return(new WaitForSeconds(0.001f));
            }
        }
        string tText = (Time.realtimeSinceStartup - temp).ToString("f2");

        Log("Generated " + cnt + " hyperlanes in\n" + tText + "s");
        UnityAnalytics.CustomEvent("genHyperlane", new Dictionary <string, object>
        {
            { "radius", genhlradius },
            { "maxLines", genhlmaxlines },
            { "finisgHyperlane", cnt },
            { "finishDuration", tText }
        });
        isGenerating = false;
    }