public void UpdateBody()
            {
                print("UpdateLocal:" + name);
                localUpdate(this);

                print("UpdateScaled:" + name);
                var smallPlanet = PFUtil.FindScaled(name);

                if (templateName == "Sun")
                {
                    var sunBody = PFUtil.FindCB("Sun");
                    var cb      = PFUtil.FindCB(name);

                    var orbitDriver = smallPlanet.AddComponent <OrbitDriver>();
                    orbitDriver.updateMode = OrbitDriver.UpdateMode.UPDATE;
                    orbitDriver.name       = cb.name;
                    orbitDriver.orbit      = new Orbit(orbit.inclination, orbit.eccentricity, orbit.semiMajorAxis, orbit.LAN,
                                                       orbit.argumentOfPeriapsis, orbit.meanAnomalyAtEpoch, orbit.epoch, sunBody);

                    cb.orbitDriver = orbitDriver;

                    orbitDriver.referenceBody = sunBody;
                    orbitDriver.celestialBody = cb;

                    cb.sphereOfInfluence        = cb.orbit.semiMajorAxis * Math.Pow(cb.Mass / cb.orbit.referenceBody.Mass, 0.4);
                    cb.hillSphere               = cb.orbit.semiMajorAxis * (1 - cb.orbit.eccentricity) * Math.Pow(cb.Mass / cb.orbit.referenceBody.Mass, 0.333333333333333);
                    cb.orbitDriver.QueuedUpdate = true;
                    cb.CBUpdate();

                    orbitDriver.UpdateOrbit();
                }

                LoadScaledPlanet(smallPlanet, name);
            }
            public static void LoadScaledPlanet(GameObject smallPlanet, string name, bool bLoadTemp = false)
            {
                var root = ConfigNode.Load(DataPath + name + ".cfg");

                if (root != null)
                {
                    var sConfig = root.nodes.GetNode("ScaledTransform");
                    //print(cbConfig);
                    if (sConfig != null)
                    {
                        var scaledBody = PFUtil.FindScaled(name);

                        var ratio    = float.Parse(sConfig.GetValue("ratio"));
                        var newScale = (float)PFUtil.FindCB(name).Radius *ratio;
                        scaledBody.transform.localScale = new Vector3(newScale, newScale, newScale);
                    }
                }

                var binName = name + ".bin";

                if (!bLoadTemp)
                {
                    var colorTexture = PFUtil.LoadTexture(DataPath + name + "_map.png");
                    var bumpTexture  = PFUtil.LoadTexture(DataPath + name + "_normal.png");

                    LoadScaledPlanetTextures(name, colorTexture, bumpTexture);
                }
                else
                {
                    var colorTexture = PFUtil.LoadTexture(DataPath + name + "_map_.png");
                    var bumpTexture  = PFUtil.LoadTexture(DataPath + name + "_normal_.png");
                    binName = name + "_.bin";
                    LoadScaledPlanetTextures(name, colorTexture, bumpTexture);
                }

                if (KSP.IO.File.Exists <PlanetFactory>(binName))
                {
                    //print("Loading mesh");
                    var smallPlanetMeshFilter = (MeshFilter)smallPlanet.GetComponentInChildren((typeof(MeshFilter)));
                    var newVerts = new Vector3[smallPlanetMeshFilter.mesh.vertices.Count()];
                    var reader   = KSP.IO.BinaryReader.CreateForType <PlanetFactory>(binName);
                    for (var i = 0; i < newVerts.Count(); i++)
                    {
                        newVerts[i].x = reader.ReadSingle();
                        newVerts[i].y = reader.ReadSingle();
                        newVerts[i].z = reader.ReadSingle();
                    }
                    smallPlanetMeshFilter.mesh.vertices = newVerts;
                    smallPlanetMeshFilter.mesh.RecalculateNormals();

                    //smallPlanetMeshFilter.mesh.tangents = null;
                    PFUtil.RecalculateTangents(smallPlanetMeshFilter.mesh);
                }
            }
        public static Orbit ParseOrbit(ConfigNode node)
        {
            var inc     = double.Parse(node.GetValue("inclination"));
            var ecc     = double.Parse(node.GetValue("eccentricity"));
            var sma     = double.Parse(node.GetValue("semiMajorAxis"));
            var lan     = double.Parse(node.GetValue("LAN"));
            var aop     = double.Parse(node.GetValue("argumentOfPeriapsis"));
            var mae     = double.Parse(node.GetValue("meanAnomalyAtEpoch"));
            var epo     = double.Parse(node.GetValue("epoch"));
            var refBody = node.GetValue("referenceBody");

            return(new Orbit(inc, ecc, sma, lan, aop, mae, epo, PFUtil.FindCB(refBody)));
        }
        public static void LoadOrbit(CelestialBody body)
        {
            var orbit = new PFOrbit();

            if (!orbit.Load(body.name))
            {
                return;
            }

            var parentBody = PFUtil.FindCB(body.name);

            if (body.orbitDriver != null)
            {
                body.orbitDriver.orbit = new Orbit(orbit.inclination, orbit.eccentricity,
                                                   orbit.semiMajorAxis, orbit.LAN,
                                                   orbit.argumentOfPeriapsis, orbit.meanAnomalyAtEpoch, orbit.epoch, body.orbit.referenceBody);// parentBody);

                body.orbitDriver.UpdateOrbit();
            }
        }
Esempio n. 5
0
        private void OnGUI()
        {
            if (!isTooLateToLoad)
            {
                windowRect = GUI.Window(0, windowRect, LogoWindow, "Krags Planet Factory");

                autoLoadSave = GUI.Toggle(new Rect(20, 280, 160, 20), autoLoadSave, "Auto Load Savegame->");

                int selectedItemIndex = comboBoxControl.SelectedItemIndex;
                selectedItemIndex = comboBoxControl.List(
                    new Rect(170, 280, 160, 20), comboBoxList[selectedItemIndex].text, comboBoxList, listStyle);

                autoLoadSaveName = comboBoxList[selectedItemIndex].text;

                if (GUI.changed)
                {
                    SaveConfig();
                }
            }
#if DEBUG
            if (guiHidden)
            {
                return;
            }

            if (FlightGlobals.currentMainBody == null)
            {
                return;
            }

            var curBodyName = FlightGlobals.currentMainBody.bodyName;
            if (GUI.Button(new Rect(20, 60, 100, 20), "Reload " + curBodyName))
            {
                LoadPQS(curBodyName);

                var cb = PFUtil.FindCB(curBodyName);
                if (cb)
                {
                    LoadCB(cb);
                    LoadOrbit(cb);
                }
            }
            if (GUI.Button(new Rect(20, 80, 100, 20), "Reload scaled " + curBodyName))
            {
                PFBody.LoadScaledPlanet(PFUtil.FindScaled(curBodyName), curBodyName, true);
            }

            if (GUI.Button(new Rect(20, 120, 100, 20), "Export " + curBodyName))
            {
                var width = 2048;
                if (FlightGlobals.currentMainBody.Radius < 50000)
                {
                    width = 1024;
                }
                PFExport.CreateScaledSpacePlanet(curBodyName, "Laythe", null, width, 20000);
            }
            //if (GUI.Button(new Rect(20, 140, 100, 20), "Effect " + curBodyName))
            //{
            //    PFEffects.TestEffects(PFUtil.FindScaled(curBodyName));
            //}
#endif
        }