コード例 #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();
            }
        }
コード例 #2
0
        private void Spawn()
        {
            CelestialBody.CBUpdate();
            isSpawned = true;

            mesh = ModelVariant.SpawnVariant(this);
            {
                if (_mesh == null)
                {
                    Log.UserError("Cannot spawn 3dModel of Instance: " + model.name);
                    Destroy();
                    return;
                }
            }

            if (model.isSquad)
            {
                InstanceUtil.MangleSquadStatic(this);
            }
            InstanceUtil.SetLayerRecursively(this, 15);

            mesh.SetActive(true);

            //Scaling
            origScale = gameObject.transform.localScale;             // save the original scale for later use
            gameObject.transform.localScale *= ModelScale;

            foreach (StaticModule module in model.modules)
            {
                moduleKey = (module.moduleNamespace + "_" + module.moduleClassname);
                Type moduleType;
                if (staticModules.ContainsKey(moduleKey))
                {
                    moduleType = staticModules[moduleKey];
                }
                else
                {
                    moduleType = AssemblyLoader.loadedAssemblies.SelectMany(asm => asm.assembly.GetTypes()).FirstOrDefault(t => t.Namespace == module.moduleNamespace && t.Name == module.moduleClassname);
                    staticModules.Add(moduleKey, moduleType);
                }

                StaticModule mod = mesh.AddComponent(moduleType) as StaticModule;

                if (mod != null)
                {
                    mod.enabled        = false;
                    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);
                        }
                    }
                    //myStaticModules.Add(mod);
                }
                else
                {
                    Log.UserError("Module " + module.moduleClassname + " could not be loaded in " + gameObject.name);
                }
            }


            foreach (Renderer renderer in gameObject.GetComponentsInChildren <Renderer>(true))
            {
                renderer.enabled = true;
                AdvancedTextures.CheckForExistingMaterial(renderer);
            }

            ModelVariant.ApplyVariant(this);

            //Make LaunchSites more sturdy
            if (!model.isSquad)
            {
                Destructable.MakeDestructable(this);
                if (hasLauchSites)
                {
                    destructible.impactMomentumThreshold = Math.Max(destructible.impactMomentumThreshold, 3000f);
                    launchSite.AttachSelector();
                }
            }

            foreach (var facility in myFacilities)
            {
                facility.AttachSelector();
            }
        }
コード例 #3
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);
            }

            InstanceUtil.CreateGroupCenterIfMissing(this);

            groupCenter = StaticDatabase.allCenters[groupCenterName];

            if (RelativePosition.Equals(Vector3.zero))
            {
                Log.Normal("LegacySpawnInstance called for " + configPath);
                LegacySpawnInstance();
                gameObject.transform.parent = groupCenter.gameObject.transform;
                pqsCity.enabled             = false;
                pqsCity.sphere = null;
                pqsCity        = null;

                RelativePosition = gameObject.transform.localPosition;
                Orientation      = gameObject.transform.localEulerAngles;
            }
            else
            {
                gameObject.transform.position         = groupCenter.gameObject.transform.position;
                gameObject.transform.parent           = groupCenter.gameObject.transform;
                gameObject.transform.localPosition    = RelativePosition;
                gameObject.transform.localEulerAngles = Orientation;
            }

            //Scaling
            origScale = gameObject.transform.localScale;             // save the original scale for later use
            gameObject.transform.localScale *= ModelScale;

            RefLatitude    = (float)CelestialBody.GetLatitudeAndLongitude(gameObject.transform.position).x;
            RefLongitude   = (float)(CelestialBody.GetLatitudeAndLongitude(gameObject.transform.position).y);
            RadialPosition = radialPosition;

            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();
                if (!pqsObjectList.Contains((PQSSurfaceObject)groupCenter.pqsCity))
                {
                    pqsObjectList.Add(groupCenter.pqsCity as PQSSurfaceObject);
                }
                CelestialBody.pqsSurfaceObjects = pqsObjectList.ToArray();
            }
        }