public static void AddKSC2() { CelestialBody body = ConfigUtil.GetCelestialBody("HomeWorld"); var mods = body.pqsController.transform.GetComponentsInChildren <PQSCity>(true); PQSCity ksc2PQS = null; foreach (var m in mods) { if (m.name == "KSC2") { ksc2PQS = m; break; } } if (ksc2PQS == null) { return; } StaticInstance ksc2Instance = new StaticInstance(); ksc2Instance.gameObject = ksc2PQS.gameObject; //ksc2Instance.gameObject = ksc2PQS.gameObject.GetComponentsInChildren<Transform>(true).Where(x => x.name.Equals("launchpad", StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault().gameObject; ksc2Instance.hasLauchSites = true; //ksc2Instance.pqsCity = ksc2PQS; ksc2Instance.RadialPosition = ksc2PQS.repositionRadial; ksc2Instance.RefLatitude = KKMath.GetLatitudeInDeg(ksc2PQS.repositionRadial); ksc2Instance.RefLongitude = KKMath.GetLongitudeInDeg(ksc2PQS.repositionRadial); ksc2Instance.CelestialBody = body; ksc2Instance.groupCenter = StaticDatabase.GetGroupCenter(body.name + "_KSC2_Builtin"); ksc2.staticInstance = ksc2Instance; ksc2.LaunchSiteName = "KSC2"; ksc2.LaunchPadTransform = "launchpad/PlatformPlane"; ksc2.LaunchSiteAuthor = "KerbalKonstructs"; ksc2.logo = UIMain.MakeSprite("KerbalKonstructs/Assets/DefaultSiteLogo"); ksc2.LaunchSiteType = SiteType.VAB; ksc2.sitecategory = LaunchSiteCategory.RocketPad; ksc2.LaunchSiteDescription = "The hidden KSC2"; ksc2.body = ConfigUtil.GetCelestialBody("HomeWorld"); ksc2.refLat = (float)ksc2Instance.RefLatitude; ksc2.refLon = (float)ksc2Instance.RefLongitude; ksc2.refAlt = (float)(body.pqsController.GetSurfaceHeight(ksc2PQS.repositionRadial) - body.Radius); ksc2.LaunchSiteLength = 15f; ksc2.LaunchSiteWidth = 15f; ksc2.InitialCameraRotation = 135f; ksc2.staticInstance.mesh = ksc2PQS.gameObject; ksc2.OpenCost = 1f; ksc2.SetClosed(); ksc2.LaunchSiteIsHidden = true; ksc2.isSquad = true; ksc2Instance.launchSite = ksc2; ksc2Instance.groupCenter.launchsites.Add(ksc2); RegisterLaunchSite(ksc2); }
/// <summary> /// Creates a GroupCenter if needed /// </summary> /// <param name="instance"></param> internal static void CreateGroupCenterIfMissing(StaticInstance instance) { if (!StaticDatabase.HasGroupCenter(instance.groupCenterName)) { if (instance.RadialPosition.Equals(Vector3.zero)) { Log.UserError("No Group Found and no position found to create a Group: " + instance.configPath); return; } Log.Normal("Creating a new Group Center: " + instance.groupCenterName); GroupCenter center = new GroupCenter(); center.Group = instance.Group; center.RadialPosition = instance.RadialPosition; center.CelestialBody = instance.CelestialBody; center.isInSavegame = instance.isInSavegame; center.Spawn(); instance.Group = center.Group; } else { // we have a GroupCenter and a legacy Object we might regroup now if ((instance.RelativePosition.Equals(Vector3.zero)) && (!instance.RadialPosition.Equals(Vector3.zero))) { Vector3 groupPostion = StaticDatabase.GetGroupCenter(instance.groupCenterName).gameObject.transform.position; Vector3 instancePosition = instance.CelestialBody.GetWorldSurfacePosition(KKMath.GetLatitudeInDeg(instance.RadialPosition), KKMath.GetLongitudeInDeg(instance.RadialPosition), (instance.CelestialBody.pqsController.GetSurfaceHeight(instance.RadialPosition) - instance.CelestialBody.Radius)); if (Vector3.Distance(groupPostion, instancePosition) > KerbalKonstructs.localGroupRange) { // Check if we have a similar named GC somewhere GroupCenter closestCenter = CheckForClosesCenter(instance); if (closestCenter != null) { instance.Group = closestCenter.Group; } else { Log.Normal("Creating a new local GroupCenter on: " + instance.CelestialBody.name + " for " + instance.Group); GroupCenter center = new GroupCenter(); // we use index keys for new Group Names center.Group = instance.Group; center.RadialPosition = instance.RadialPosition; center.CelestialBody = instance.CelestialBody; center.isInSavegame = instance.isInSavegame; center.Spawn(); instance.Group = center.Group; Log.Normal("New GroupCenter Created: " + instance.groupCenterName); } } } } }
internal void RenameGroup(string newName) { StaticDatabase.RemoveGroupCenter(this); Group = newName; StaticDatabase.AddGroupCenter(this); StaticInstance[] staticInstances = childInstances.ToArray(); foreach (StaticInstance instance in staticInstances) { StaticDatabase.ChangeGroup(instance, 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 Orientate() { // mangle Squads statics CelestialBody.CBUpdate(); InstanceUtil.CreateGroupCenterIfMissing(this); if (!StaticDatabase.HasGroupCenter(groupCenterName)) { Log.UserWarning("cannot load " + configPath); return; } groupCenter = StaticDatabase.GetGroupCenter(groupCenterName); if (RelativePosition.Equals(Vector3.zero)) { Log.Normal("LegacySpawnInstance called for " + groupCenterName + "_" + model.name); LegacySpawnInstance(); gameObject.transform.parent = groupCenter.gameObject.transform; RelativePosition = gameObject.transform.localPosition; Orientation = gameObject.transform.localEulerAngles; } else { gameObject.transform.position = groupCenter.gameObject.transform.position + RelativePosition;; gameObject.transform.parent = groupCenter.gameObject.transform; gameObject.transform.localEulerAngles = Orientation; gameObject.transform.localPosition = RelativePosition; } RefLatitude = (float)CelestialBody.GetLatitudeAndLongitude(gameObject.transform.position).x; RefLongitude = (float)(CelestialBody.GetLatitudeAndLongitude(gameObject.transform.position).y); RadialPosition = radialPosition; 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) { var pqsObjectList = CelestialBody.pqsSurfaceObjects.ToList(); if (!pqsObjectList.Contains((PQSSurfaceObject)groupCenter.pqsCity)) { Log.Normal("Added " + groupCenter.Group + " to scanable Objects"); pqsObjectList.Add(groupCenter.pqsCity as PQSSurfaceObject); } CelestialBody.pqsSurfaceObjects = pqsObjectList.ToArray(); } }
internal void DeleteGroupCenter() { foreach (StaticInstance child in childInstances.ToArray()) { KerbalKonstructs.instance.DeleteInstance(child); } StaticDatabase.RemoveGroupCenter(this); // check later when saving if this file is empty GameObject.Destroy(gameObject); KerbalKonstructs.deletedGroups.Add(this); Log.Normal("deleted GroupCenter: " + dbKey); }
internal void Destroy() { if (groupCenter != null) { groupCenter.RemoveInstance(this); } if (StaticDatabase.HasInstance(this)) { StaticDatabase.DeleteStaticFromDB(this); } if (_mesh != null) { GameObject.DestroyImmediate(_mesh); } if (wreck != null) { wreck.transform.parent = null; GameObject.DestroyImmediate(wreck); } GameObject.DestroyImmediate(gameObject); }
internal static GroupCenter CheckForClosesCenter(StaticInstance instance) { GroupCenter closestCenter = null; Vector3 groupPostion = StaticDatabase.GetGroupCenter(instance.groupCenterName).gameObject.transform.position; Vector3 instancePosition = instance.CelestialBody.GetWorldSurfacePosition(KKMath.GetLatitudeInDeg(instance.RadialPosition), KKMath.GetLongitudeInDeg(instance.RadialPosition), (instance.CelestialBody.pqsController.GetSurfaceHeight(instance.RadialPosition) - instance.CelestialBody.Radius)); float distance = Vector3.Distance(groupPostion, instancePosition); float oldDistance = KerbalKonstructs.localGroupRange * 2; if (distance > KerbalKonstructs.localGroupRange) { // Check if we have a similar named GC somewhere int index = 0; while (StaticDatabase.HasGroupCenter(instance.CelestialBody.name + "_" + instance.Group + "_" + index.ToString())) { groupPostion = StaticDatabase.GetGroupCenter(instance.CelestialBody.name + "_" + instance.Group + "_" + index.ToString()).gameObject.transform.position; distance = Vector3.Distance(groupPostion, instancePosition); if (distance < KerbalKonstructs.localGroupRange) { if (closestCenter == null || distance < oldDistance) { oldDistance = distance; closestCenter = StaticDatabase.GetGroupCenter(instance.CelestialBody.name + "_" + instance.Group + "_" + index.ToString()); } } index++; } } else { closestCenter = StaticDatabase.GetGroupCenter(instance.groupCenterName); } return(closestCenter); }
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(); } }
/// <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(); } }