internal void Spawn()
        {
            if (StaticDatabase.allCenters.ContainsKey(dbKey))
            {
                string oldName = Group;
                int    index   = 0;
                while (StaticDatabase.allCenters.ContainsKey(dbKey))
                {
                    Group = oldName + "_" + index.ToString();
                    index++;
                }
            }

            gameObject      = new GameObject();
            gameObject.name = Group + "_PQS";

            pqsCity = gameObject.AddComponent <PQSCity>();

            PQSCity.LODRange range = new PQSCity.LODRange
            {
                renderers    = new GameObject[0],
                objects      = new GameObject[0],
                visibleRange = 25000
            };
            pqsCity.lod                    = new[] { range };
            pqsCity.frameDelta             = 10000;          //update interval for its own visiblility range checking. unused by KK, so set this to a high value
            pqsCity.repositionRadial       = RadialPosition; //position
            pqsCity.repositionRadiusOffset = RadiusOffset;   //height
            pqsCity.reorientInitialUp      = Orientation;    //orientation
            pqsCity.reorientFinalAngle     = RotationAngle;  //rotation x axis
            pqsCity.reorientToSphere       = true;           //adjust rotations to match the direction of gravity
            pqsCity.sphere                 = CelestialBody.pqsController;
            pqsCity.order                  = 100;
            pqsCity.modEnabled             = true;
            pqsCity.transform.parent       = CelestialBody.pqsController.transform;

            pqsCity.repositionToSphereSurface          = true; //Snap to surface?
            pqsCity.repositionToSphereSurfaceAddHeight = true;
            pqsCity.repositionToSphere = false;

            pqsCity.OnSetup();
            pqsCity.Orientate();

            RefLatitude  = KKMath.GetLatitudeInDeg(RadialPosition);
            RefLongitude = KKMath.GetLongitudeInDeg(RadialPosition);

            StaticDatabase.allCenters.Add(dbKey, this);
        }
        public void spawnObject(Boolean editing, Boolean bPreview)
        {
            // Objects spawned at runtime should be active, ones spawned at loading not
            SetActiveRecursively(gameObject, editing);

            Transform[] gameObjectList = gameObject.GetComponentsInChildren<Transform>();
            List<GameObject> rendererList = (from t in gameObjectList where t.gameObject.renderer != null select t.gameObject).ToList();

            setLayerRecursively(gameObject, 15);

            if (bPreview) this.ToggleAllColliders(false);

            this.preview = bPreview;

            if (editing) KerbalKonstructs.instance.selectObject(this, true, true, bPreview);

            float objvisibleRange = (float)getSetting("VisibilityRange");

            if (objvisibleRange < 1) objvisibleRange = 25000f;

            PQSCity.LODRange range = new PQSCity.LODRange
            {
                renderers = rendererList.ToArray(),
                objects = new GameObject[0],
                visibleRange = objvisibleRange
            };

            pqsCity = gameObject.AddComponent<PQSCity>();
            pqsCity.lod = new[] { range };
            pqsCity.frameDelta = 1; //Unknown
            pqsCity.repositionToSphere = true; //enable repositioning
            pqsCity.repositionToSphereSurface = false; //Snap to surface?
            pqsCity.repositionRadial = (Vector3)getSetting("RadialPosition"); //position
            pqsCity.repositionRadiusOffset = (float)getSetting("RadiusOffset"); //height
            pqsCity.reorientInitialUp = (Vector3)getSetting("Orientation"); //orientation
            pqsCity.reorientFinalAngle = (float)getSetting("RotationAngle"); //rotation x axis
            pqsCity.reorientToSphere = true; //adjust rotations to match the direction of gravity
            gameObject.transform.parent = ((CelestialBody)getSetting("CelestialBody")).pqsController.transform;
            pqsCity.sphere = ((CelestialBody)getSetting("CelestialBody")).pqsController;
            pqsCity.order = 100;
            pqsCity.modEnabled = true;
            pqsCity.OnSetup();
            pqsCity.Orientate();

            foreach (StaticModule module in model.modules)
            {
                Type moduleType = AssemblyLoader.loadedAssemblies.SelectMany(asm => asm.assembly.GetTypes()).FirstOrDefault(t => t.Namespace == module.moduleNamespace && t.Name == module.moduleClassname);
                MonoBehaviour mod = gameObject.AddComponent(moduleType) as MonoBehaviour;

                if (mod != null)
                {
                    foreach (string fieldName in module.moduleFields.Keys)
                    {
                        FieldInfo field = mod.GetType().GetField(fieldName);
                        if (field != null)
                        {
                            field.SetValue(mod, Convert.ChangeType(module.moduleFields[fieldName], field.FieldType));
                        }
                        else
                        {
                            Debug.Log("KK: WARNING: Field " + fieldName + " does not exist in " + module.moduleClassname);
                        }
                    }
                }
                else
                {
                    Debug.Log("KK: WARNING: Module " + module.moduleClassname + " could not be loaded in " + gameObject.name);
                }
            }

            foreach (GameObject gorenderer in rendererList)
            {
                gorenderer.renderer.enabled = true;
            }
        }
Beispiel #3
0
        internal void Spawn()
        {
            if (StaticDatabase.HasGroupCenter(dbKey))
            {
                string oldName = Group;
                int    index   = 0;
                while (StaticDatabase.HasGroupCenter(dbKey))
                {
                    Group = oldName + "_" + index.ToString();
                    index++;
                }
            }

            gameObject = new GameObject();
            GameObject.DontDestroyOnLoad(gameObject);

            CelestialBody.CBUpdate();


            gameObject.name = Group;
            //gameObject.name = "SpaceCenter";

            pqsCity = gameObject.AddComponent <PQSCity>();

            PQSCity.LODRange range = new PQSCity.LODRange
            {
                renderers    = new GameObject[0],
                objects      = new GameObject[0],
                visibleRange = 25000
            };
            pqsCity.lod        = new[] { range };
            pqsCity.frameDelta = 10000; //update interval for its own visiblility range checking. unused by KK, so set this to a high value

            if (RadialPosition == Vector3.zero)
            {
                if ((RefLatitude != 361d) && (RefLongitude != 361d))
                {
                    RadialPosition = KKMath.GetRadiadFromLatLng(CelestialBody, RefLatitude, RefLongitude);
                }
                else
                {
                    Log.UserError("No Valid Position found for Group: " + Group);
                }
            }
            else
            if ((RefLatitude == 361d) || (RefLongitude == 361d))
            {
                {
                    RefLatitude  = KKMath.GetLatitudeInDeg(RadialPosition);
                    RefLongitude = KKMath.GetLongitudeInDeg(RadialPosition);
                }
            }

            pqsCity.reorientFinalAngle = 0;
            pqsCity.repositionRadial   = RadialPosition;   //position

            pqsCity.repositionRadiusOffset = RadiusOffset; //height
            pqsCity.reorientInitialUp      = Orientation;  //orientation
            pqsCity.reorientToSphere       = true;         //adjust rotations to match the direction of gravity
            pqsCity.sphere           = CelestialBody.pqsController;
            pqsCity.order            = 100;
            pqsCity.modEnabled       = true;
            pqsCity.transform.parent = CelestialBody.pqsController.transform;

            pqsCity.repositionToSphereSurfaceAddHeight = false;
            pqsCity.repositionToSphereSurface          = false;
            SetReference();

            pqsCity.OnSetup();
            pqsCity.Orientate();

            UpdateRotation2Heading();

            StaticDatabase.AddGroupCenter(this);
        }
        /// <summary>
        /// Spawns a new Instance in the Gameworld and registers itself to the Static Database
        /// </summary>
        /// <param name="editing"></param>
        /// <param name="bPreview"></param>
        internal void spawnObject(Boolean editing, Boolean bPreview)
        {
            // mangle Squads statics
            if (model.isSquad)
            {
                InstanceUtil.MangleSquadStatic(gameObject);
            }

            // Objects spawned at runtime should be active, ones spawned at loading not
            InstanceUtil.SetActiveRecursively(this, editing);

            Transform[]       gameObjectList = gameObject.GetComponentsInChildren <Transform>();
            List <GameObject> rendererList   = (from t in gameObjectList where t.gameObject.GetComponent <Renderer>() != null select t.gameObject).ToList();

            setLayerRecursively(gameObject, 15);

            if (bPreview)
            {
                this.ToggleAllColliders(false);
            }

            this.preview = bPreview;

            if (editing)
            {
                KerbalKonstructs.instance.selectObject(this, true, true, bPreview);
            }

            float objvisibleRange = VisibilityRange;

            if (objvisibleRange < 1)
            {
                objvisibleRange = 25000f;
            }

            PQSCity.LODRange range = new PQSCity.LODRange
            {
                renderers    = new GameObject[0],
                objects      = new GameObject[0],
                visibleRange = objvisibleRange
            };

            pqsCity                           = gameObject.AddComponent <PQSCity>();
            pqsCity.lod                       = new[] { range };
            pqsCity.frameDelta                = 10000;          //update interval for its own visiblility range checking. unused by KK, so set this to a high value
            pqsCity.repositionRadial          = RadialPosition; //position
            pqsCity.repositionRadiusOffset    = RadiusOffset;   //height
            pqsCity.reorientInitialUp         = Orientation;    //orientation
            pqsCity.reorientFinalAngle        = RotationAngle;  //rotation x axis
            pqsCity.reorientToSphere          = true;           //adjust rotations to match the direction of gravity
            gameObject.transform.parent       = CelestialBody.pqsController.transform;
            pqsCity.sphere                    = CelestialBody.pqsController;
            origScale                         = pqsCity.transform.localScale; // save the original scale for later use
            pqsCity.transform.localScale     *= ModelScale;
            pqsCity.order                     = 100;
            pqsCity.modEnabled                = true;
            pqsCity.repositionToSphere        = true;  //enable repositioning
            pqsCity.repositionToSphereSurface = false; //Snap to surface?



            CelestialBody.pqsController.GetSurfaceHeight(RadialPosition);

            pqsCity.OnSetup();
            pqsCity.Orientate();



            //PQSCity2.LodObject lodObject = new PQSCity2.LodObject();
            //lodObject.visibleRange = VisibilityRange;
            //lodObject.objects = new GameObject[] { };
            //pqsCity2 = gameObject.AddComponent<PQSCity2>();
            //pqsCity2.objects = new [] { lodObject } ;
            //pqsCity2.objectName = "";
            //pqsCity2.lat = RefLatitude;
            //pqsCity2.lon = RefLongitude;
            //pqsCity2.alt = RadiusOffset;
            //pqsCity2.up = Orientation;
            //pqsCity2.rotation = RotationAngle;
            //pqsCity2.sphere = CelestialBody.pqsController;


            //pqsCity2.OnSetup();
            //pqsCity2.Orientate();


            foreach (StaticModule module in model.modules)
            {
                Type          moduleType = AssemblyLoader.loadedAssemblies.SelectMany(asm => asm.assembly.GetTypes()).FirstOrDefault(t => t.Namespace == module.moduleNamespace && t.Name == module.moduleClassname);
                MonoBehaviour mod        = gameObject.AddComponent(moduleType) as MonoBehaviour;

                if (mod != null)
                {
                    foreach (string fieldName in module.moduleFields.Keys)
                    {
                        FieldInfo field = mod.GetType().GetField(fieldName);
                        if (field != null)
                        {
                            field.SetValue(mod, Convert.ChangeType(module.moduleFields[fieldName], field.FieldType));
                        }
                        else
                        {
                            Log.UserWarning("Field " + fieldName + " does not exist in " + module.moduleClassname);
                        }
                    }
                }
                else
                {
                    Log.UserError("Module " + module.moduleClassname + " could not be loaded in " + gameObject.name);
                }
            }

            foreach (GameObject gorenderer in rendererList)
            {
                gorenderer.GetComponent <Renderer>().enabled = true;
            }

            StaticDatabase.AddStatic(this);

            // Add them to the bodys objectlist, so they show up as anomalies
            // After we got a new Name from StaticDatabase.AddStatic()
            if (isScanable)
            {
                Log.Normal("Added " + gameObject.name + " to scanable Objects");
                var pqsObjectList = CelestialBody.pqsSurfaceObjects.ToList();
                pqsObjectList.Add(pqsCity as PQSSurfaceObject);
                CelestialBody.pqsSurfaceObjects = pqsObjectList.ToArray();
            }
        }
Beispiel #5
0
        private void InstantiateStatic(PQS celestialPQS, StaticObject stObject, bool freshObject = false)
        {
            #region Staitc Object Core Parameters

            float visibilityRange = stObject.VisRange;
            float localRotationAngle = stObject.RotAngle;
            float radiusOffset = stObject.RadOffset;
            string modelUrl = stObject.ModelUrl;
            Vector3 orientDirection = stObject.Orientation;
            Vector3 radialPosition = stObject.RadPosition;

            if (radialPosition == Vector3.zero)
            {
                radialPosition =
                    _currentCelestialObj.CelestialBodyComponent.transform.InverseTransformPoint(
                        FlightGlobals.ActiveVessel.transform.position);

                stObject.RadPosition = radialPosition;
            }

            if (orientDirection == Vector3.zero)
            {
                orientDirection = Vector3.up;
                stObject.Orientation = orientDirection;
            }

            stObject.Latitude = GetLatitude(radialPosition);
            stObject.Longitude = GetLongitude(radialPosition);

            #endregion

            // Instantiate
            GameObject ktGameObject = GameDatabase.Instance.GetModel(modelUrl);

            // Add the reference component.
            var soModule = ktGameObject.AddComponent<StaticObjectModule>();

            // Active the game object.
            ktGameObject.SetActive(true);

            // Set objects to layer 15 so that they collide correctly with Kerbals.
            SetLayerRecursively(ktGameObject, 15);

            // Set the parent object to the celestial component's GameObject.
            ktGameObject.transform.parent = celestialPQS.transform;

            // Obtain all active transforms in the static game object.
            Transform[] gameObjectList = ktGameObject.GetComponentsInChildren<Transform>();

            // Create a list of renderers to be manipulated by the default PQSCity class.
            List<GameObject> rendererList =
                (from t in gameObjectList where t.gameObject.renderer != null select t.gameObject).ToList();

            // Create the LOD range.
            _myLodRange = new PQSCity.LODRange
                          {
                              renderers = rendererList.ToArray(),
                              objects = new GameObject[0],
                              //new[] {staticGameObject},  // Todo: change to GameObject children.
                              visibleRange = visibilityRange
                          };

            // Add the PQSCity class (extended by KerbTown).
            var myCity = ktGameObject.AddComponent<PQSCityEx>();

            // Assign PQSCity variables.
            myCity.lod = new[] {_myLodRange};
            myCity.frameDelta = 1;
            myCity.repositionToSphere = true;
            myCity.repositionToSphereSurface = false;
            myCity.repositionRadial = radialPosition;
            myCity.repositionRadiusOffset = radiusOffset;
            myCity.reorientFinalAngle = localRotationAngle;
            myCity.reorientToSphere = true;
            myCity.reorientInitialUp = orientDirection;
            myCity.sphere = celestialPQS;
            myCity.order = 100;
            myCity.modEnabled = true;

            // Assign custom variables.
            myCity.StaticObjectRef = stObject;

            // Setup and orientate the PQSCity instanced object.
            myCity.OnSetup();
            myCity.Orientate();

            // If the object was instantiated by "Create", override all renderers to active.
            if (freshObject)
            {
                foreach (GameObject renObj in rendererList)
                    renObj.renderer.enabled = true;
            }

            // Add component references to the static object.
            stObject.PQSCityComponent = myCity;
            stObject.StaticGameObject = ktGameObject;
            //stObject.ModuleReference = soModule;

            // Add the static object as a reference to the StaticObjectModule
            soModule.StaticObjectRef = stObject;

            // Add remaining modules.
            switch (stObject.ObjectID)
            {
                case "MushroomCave":
                    AddNativeComponent(ktGameObject, typeof (MushroomCave));
                    break;

                case "PurplePathway":
                    AddNativeComponent(ktGameObject, typeof (PurplePathway));
                    break;

                default:
                    AddModuleComponents(stObject);
                    break;
            }

            // Alter the Launch Site spawn object name if necessary.
            // Todo: optimize
            if (stObject.LaunchSiteName != "")
            {
                Transform launchSiteObject =
                    ktGameObject.transform.Cast<Transform>().FirstOrDefault(t => t.name.EndsWith("_spawn"));
                if (launchSiteObject != null)
                {
                    launchSiteObject.name = stObject.LaunchSiteName + "_spawn";
                }
                else
                {
                    Extensions.LogWarning("Launch Site '" + ktGameObject.name + "'does not have a spawn transform.");
                }
            }
        }
        public void spawnObject(Boolean editing, Boolean bPreview)
        {
            // Objects spawned at runtime should be active, ones spawned at loading not
            SetActiveRecursively(gameObject, editing);

            Transform[]       gameObjectList = gameObject.GetComponentsInChildren <Transform>();
            List <GameObject> rendererList   = (from t in gameObjectList where t.gameObject.GetComponent <Renderer>() != null select t.gameObject).ToList();

            setLayerRecursively(gameObject, 15);

            if (bPreview)
            {
                this.ToggleAllColliders(false);
            }

            this.preview = bPreview;

            if (editing)
            {
                KerbalKonstructs.instance.selectObject(this, true, true, bPreview);
            }

            float objvisibleRange = (float)getSetting("VisibilityRange");

            if (objvisibleRange < 1)
            {
                objvisibleRange = 25000f;
            }

            PQSCity.LODRange range = new PQSCity.LODRange
            {
                renderers    = rendererList.ToArray(),
                objects      = new GameObject[0],
                visibleRange = objvisibleRange
            };

            pqsCity                           = gameObject.AddComponent <PQSCity>();
            pqsCity.lod                       = new[] { range };
            pqsCity.frameDelta                = 1;                                     //Unknown
            pqsCity.repositionToSphere        = true;                                  //enable repositioning
            pqsCity.repositionToSphereSurface = false;                                 //Snap to surface?
            pqsCity.repositionRadial          = (Vector3)getSetting("RadialPosition"); //position
            pqsCity.repositionRadiusOffset    = (float)getSetting("RadiusOffset");     //height
            pqsCity.reorientInitialUp         = (Vector3)getSetting("Orientation");    //orientation
            pqsCity.reorientFinalAngle        = (float)getSetting("RotationAngle");    //rotation x axis
            pqsCity.reorientToSphere          = true;                                  //adjust rotations to match the direction of gravity
            gameObject.transform.parent       = ((CelestialBody)getSetting("CelestialBody")).pqsController.transform;
            pqsCity.sphere                    = ((CelestialBody)getSetting("CelestialBody")).pqsController;
            pqsCity.order                     = 100;
            pqsCity.modEnabled                = true;
            pqsCity.OnSetup();
            pqsCity.Orientate();

            foreach (StaticModule module in model.modules)
            {
                Type          moduleType = AssemblyLoader.loadedAssemblies.SelectMany(asm => asm.assembly.GetTypes()).FirstOrDefault(t => t.Namespace == module.moduleNamespace && t.Name == module.moduleClassname);
                MonoBehaviour mod        = gameObject.AddComponent(moduleType) as MonoBehaviour;

                if (mod != null)
                {
                    foreach (string fieldName in module.moduleFields.Keys)
                    {
                        FieldInfo field = mod.GetType().GetField(fieldName);
                        if (field != null)
                        {
                            field.SetValue(mod, Convert.ChangeType(module.moduleFields[fieldName], field.FieldType));
                        }
                        else
                        {
                            Debug.Log("KK: WARNING: Field " + fieldName + " does not exist in " + module.moduleClassname);
                        }
                    }
                }
                else
                {
                    Debug.Log("KK: WARNING: Module " + module.moduleClassname + " could not be loaded in " + gameObject.name);
                }
            }

            foreach (GameObject gorenderer in rendererList)
            {
                gorenderer.GetComponent <Renderer>().enabled = true;
            }
        }
        private void LegacySpawnInstance()
        {
            float objvisibleRange = VisibilityRange;

            if (objvisibleRange < 1)
            {
                objvisibleRange = KerbalKonstructs.localGroupRange;
            }

            pqsCity = gameObject.AddComponent <PQSCity>();

            PQSCity.LODRange range = new PQSCity.LODRange
            {
                renderers    = new GameObject[0],
                objects      = new GameObject[0],
                visibleRange = objvisibleRange
            };
            pqsCity.lod                    = new[] { range };
            pqsCity.frameDelta             = 10000;          //update interval for its own visiblility range checking. unused by KK, so set this to a high value
            pqsCity.repositionRadial       = RadialPosition; //position
            pqsCity.repositionRadiusOffset = RadiusOffset;   //height
            pqsCity.reorientInitialUp      = Orientation;    //orientation
            pqsCity.reorientFinalAngle     = RotationAngle;  //rotation x axis
            pqsCity.reorientToSphere       = true;           //adjust rotations to match the direction of gravity
            gameObject.transform.parent    = CelestialBody.pqsController.transform;
            pqsCity.sphere                 = CelestialBody.pqsController;
            pqsCity.order                  = 100;
            pqsCity.modEnabled             = true;
            pqsCity.repositionToSphere     = true; //enable repositioning


            switch (heighReference)
            {
            case HeightReference.Sphere:
                pqsCity.repositionToSphereSurface = false;     //Snap to surface?

                break;

            case HeightReference.Terrain:

                pqsCity.repositionToSphereSurface          = true; //Snap to surface?
                pqsCity.repositionToSphereSurfaceAddHeight = true;
                pqsCity.repositionToSphere = false;
                break;

            default:
                // we try to descide which one is the best to take
                string biome = ScienceUtil.GetExperimentBiome(CelestialBody, RefLatitude, RefLongitude);
                float  heightAboveTerrain = SDRescale.GetSurfaceRefereceHeight(this);

                if ((biome == "Water" || biome == "Shores") && ((Math.Abs(RadiusOffset) < 5) && heightAboveTerrain > 5))     // most likely at ocean surface
                {
                    Log.Normal("Found a swimming object: " + this.gameObject.name);
                    pqsCity.repositionToSphereSurface = false;     //Snap to surface?
                    heighReference = HeightReference.Sphere;
                }
                else
                {
                    {
                        //    Log.Normal("found new Radiusffset: " + heightAboveTerrain);
                        RadiusOffset = heightAboveTerrain;
                        pqsCity.repositionToSphereSurface          = true; //Snap to surface?#
                        pqsCity.repositionToSphereSurfaceAddHeight = true;
                        pqsCity.repositionRadiusOffset             = heightAboveTerrain;
                        pqsCity.repositionToSphere = false;

                        heighReference = HeightReference.Terrain;
                    }
                }
                break;
            }
            pqsCity.OnSetup();
            pqsCity.Orientate();

            KerbalKonstructs.convertLegacyConfigs = true;
        }