/// <summary> /// Checks if a vessel is valid. If it is, creates a new PTGUI_Vessel Monobehaviour and adds its interface to the IVesselElements list. /// Otherwise, it removes it from the vessel list. /// </summary> /// <param name="v"> Vessel that is being examined. </param> private void AddVesselToIVesselList(Vessel v) { // Check if vessel is valid, in a valid situation, and if it has any persistent engine bool remove; if (IvesselElements.ContainsKey(v.id)) { remove = !v.IsVesselValid(); } else { remove = !v.IsVesselValid() || !v.HasPersistentEngineModules(); } // If not in the dictionary and valid, add it if (!IvesselElements.ContainsKey(v.id) && !remove) { var veInterface = PTGUI_Vessel.Create(v); // Add new vessel to the list and instantiate IvesselElements[v.id] = veInterface; veInterface.HasPersistentThrustActive = veInterface.PersistentThrustEnabled(v).Contains(true); } // Fails filter, so remove the vessel if (remove && IvesselElements.ContainsKey(v.id)) { window.RemoveVessel(IvesselElements[v.id]); IvesselElements.Remove(v.id); } }
/// <summary> /// Adds a PTGUI_Vessel Monobehaviour component to a GameObject (or creates a new one). /// Initializes the script with values from the vessel. /// <para /> FIXME: Definitely not optimal. Creates 2 GameObjects for each vessel (one VesselElement, one PTGUI_Vessel). /// </summary> /// <param name="v"> The vessel whose parameters are used to initialize the script. </param> /// <param name="gameObject"> The GameObject to which the component will be added. </param> /// <returns> Initialized PTGUI_Vessel component </returns> public static PTGUI_Vessel Create(Vessel v, GameObject gameObject = null) { if (gameObject == null) { gameObject = new GameObject(); } PTGUI_Vessel x = gameObject.AddComponent <PTGUI_Vessel>(); x.GameObj = gameObject; x.vessel = v; x.VesselId = v.id; x.IsActiveVessel = v.isActiveVessel; x.IsVesselCommandable = v.IsVesselSituationValid(); x.VesselName = v.vesselName; x.VesselIcon = GetVesselIcon(v.vesselType); x.HasInfoWindowActive = false; x.currentVesselType = v.vesselType; x.VesselAutopilotMode = v.GetAutopilotMode().ToPTIEnum(); return(x); }