Ejemplo n.º 1
0
        /// <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 = false, Boolean bPreview = false)
        {
            // mangle Squads statics
            if (model.isSquad)
            {
                InstanceUtil.MangleSquadStatic(this);
            }

            // 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();

            InstanceUtil.SetLayerRecursively(this, 15);

            if (bPreview && editing)
            {
                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
            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
            gameObject.transform.parent    = CelestialBody.pqsController.transform;
            pqsCity.transform.parent       = CelestialBody.pqsController.transform;

            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.lat = RefLatitude ;
            //pqsCity.lon = RefLongitude;
            pqsCity.alt = RadiusOffset;
            pqsCity.ResetCelestialBody();
            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);
                StaticModule mod        = gameObject.AddComponent(moduleType) as StaticModule;

                if (mod != null)
                {
                    mod.staticInstance = this;
                    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();
            }
        }
        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;
        }