Skip to content

Instantly share code, notes, and snippets.

@belackriv
Created June 2, 2015 00:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save belackriv/17f6620e6d0084029a2e to your computer and use it in GitHub Desktop.
Save belackriv/17f6620e6d0084029a2e to your computer and use it in GitHub Desktop.
using UnityEngine;
using System;
using System.Collections.Generic;
namespace BelacCorp
{
[KSPAddon(KSPAddon.Startup.SpaceCentre, true)]
public class BeamActionTracker : MonoBehaviour
{
public class BeamAction
{
private Part toBeamer = null;
public virtual Part ToBeamer { get { return this.toBeamer; } }
private ProtoCrewMember kerbal = null;
public virtual ProtoCrewMember Kerbal { get { return this.kerbal; } }
public BeamAction(Part toBeamer, ProtoCrewMember kerbal)
{
this.toBeamer = toBeamer;
this.kerbal = kerbal;
}
}
public static BeamAction currentBeamAction = null;
public static bool doBeam = false;
public static double crewBeamedTime;
public const double crewBeamDelay = 0.25;
public void Start()
{
Debug.Log("[BelacCorp]Setting Up event");
DontDestroyOnLoad(this);
GameEvents.onFlightReady.Add(CheckForBeamAction);
}
public void CheckForBeamAction()
{
Debug.Log("[BelacCorp]CheckForBeamAction called");
if (BeamActionTracker.doBeam == true)
{
Debug.Log("[BelacCorp]currentBeamAction != null");
if (FlightGlobals.ActiveVessel.id == BeamActionTracker.currentBeamAction.ToBeamer.vessel.id)
{
Debug.Log("[BelacCorp]vessel.ids match");
GameEvents.onPartUnpack.Add(PutCrewInBeamer);
}
}
}
public void PutCrewInBeamer(Part part)
{
Debug.Log("[BelacCorp]PutCrewInBeamer called");
if (BeamActionTracker.currentBeamAction.ToBeamer.flightID == part.flightID)
{
Debug.Log("[BelacCorp]part.flightIDs match");
GameEvents.onPartUnpack.Remove(PutCrewInBeamer);
part.Unpack();
Debug.Log("[BelacCorp]part.Unpack() called, adding" + BeamActionTracker.currentBeamAction.Kerbal.name);
BeamActionTracker.currentBeamAction.ToBeamer.AddCrewmember(BeamActionTracker.currentBeamAction.Kerbal);
crewBeamedTime = Planetarium.GetUniversalTime();
BeamActionTracker.doBeam = false;
}
}
}
public class Beamer : PartModule
{
private Rect windowPos;
private bool windowIsActive = false;
private class ValidBeamerGUIInfo
{
public Part beamer;
public string vesselName;
public ValidBeamerGUIInfo(Part part, string name)
{
this.beamer = part;
this.vesselName = name;
}
}
private List<ValidBeamerGUIInfo> ValidBeamerList = new List<ValidBeamerGUIInfo>();
private void WindowGUI(int windowID)
{
GUIStyle mySty = new GUIStyle(GUI.skin.button);
mySty.normal.textColor = mySty.focused.textColor = Color.white;
mySty.hover.textColor = mySty.active.textColor = Color.yellow;
mySty.onNormal.textColor = mySty.onFocused.textColor = mySty.onHover.textColor = mySty.onActive.textColor = Color.green;
mySty.padding = new RectOffset(8, 8, 8, 8);
GUILayout.BeginVertical();
GUILayout.Label("Note: Vessel Type must be a \"Ship\" for it to appear in this list.", mySty, GUILayout.MinHeight(20));
if(this.part.protoModuleCrew.Count == 1)
{
if(this.ValidBeamerList.Count > 0)
{
this.ValidBeamerList.ForEach(delegate(ValidBeamerGUIInfo beamerInfo)
{
if (beamerInfo.beamer.protoModuleCrew.Count == 0)
{
if (GUILayout.Button("Beam to " + beamerInfo.vesselName, mySty, GUILayout.ExpandWidth(true)))
{
this.BeamKerbal(this.part, beamerInfo.beamer);
}
}
else
{
GUILayout.Label(beamerInfo.vesselName + " beamer is not emtpy!", mySty, GUILayout.MinHeight(20));
}
});
}
else
{
GUILayout.Label("No Oher Vessesls With a Beamer in Range", mySty, GUILayout.MinHeight(20));
}
}
else
{
GUILayout.Label("No Kerbals In Cabin To Beam", mySty, GUILayout.MinHeight(20));
}
if (GUILayout.Button("Close Window", mySty, GUILayout.ExpandWidth(true)))
{
RenderingManager.RemoveFromPostDrawQueue(3, new Callback(drawGUI));
windowIsActive = false;
}
GUILayout.EndVertical();
GUI.DragWindow(new Rect(0, 0, 10000, 20));
}
private void BeamKerbal(Part FromBeamer, Part ToBeamer)
{
//only holds one kerbal
var kerbal = FromBeamer.protoModuleCrew[0];
BeamActionTracker.currentBeamAction = new BeamActionTracker.BeamAction(ToBeamer, kerbal);
FromBeamer.RemoveCrewmember(kerbal);
RenderingManager.RemoveFromPostDrawQueue(3, new Callback(drawGUI));
windowIsActive = false;
if (ToBeamer.vessel.loaded)
{
BeamActionTracker.currentBeamAction.ToBeamer.AddCrewmember(BeamActionTracker.currentBeamAction.Kerbal);
BeamActionTracker.crewBeamedTime = Planetarium.GetUniversalTime();
BeamActionTracker.doBeam = false;
}
FlightGlobals.SetActiveVessel(ToBeamer.vessel);
}
private void drawGUI()
{
if ((this.windowPos.x == 0) && (this.windowPos.y == 0))
{
this.windowPos = new Rect(Screen.width / 2, Screen.height / 2, 10, 10);
}
GUI.skin = HighLogic.Skin;
this.windowPos = GUILayout.Window(1, this.windowPos, WindowGUI, "Beam Kerbal", GUILayout.MinWidth(400));
}
[KSPEvent(guiActive=true,guiName="Beam Kerbal",active=true,guiActiveEditor=false)]
private void ShowBeamWindow()
{
if(!this.windowIsActive){
this.ValidBeamerList.Clear();
if (this.part.protoModuleCrew.Count == 1)
{
FlightGlobals.Vessels.ForEach(delegate(Vessel vessel)
{
if (vessel != this.part.vessel && vessel.vesselType == VesselType.Ship)
{
print("[BelacCorp]checking "+vessel.vesselName);
if (!vessel.loaded)
{
print("[BelacCorp]loading "+vessel.vesselName);
vessel.Load();
}
print("[BelacCorp]checking "+vessel.parts.Count+" "+vessel.vesselName+" parts");
vessel.parts.ForEach(delegate(Part vesselPart)
{
//print("[BelacCorp]checking part " + vesselPart.partInfo.name+" == "+this.part.partInfo.name);
if (vesselPart.partInfo.name == this.part.partInfo.name &&
this.part.vessel.mainBody == vessel.mainBody)
{
this.ValidBeamerList.Add(new ValidBeamerGUIInfo(vesselPart, vessel.vesselName));
}
});
}
});
}
windowIsActive = true;
RenderingManager.AddToPostDrawQueue(3,new Callback(drawGUI));
}
}
public override void OnUpdate()
{
if (BeamActionTracker.crewBeamedTime != 0)
{
if (Planetarium.GetUniversalTime() - BeamActionTracker.crewBeamedTime >= BeamActionTracker.crewBeamDelay)
{
FlightGlobals.ActiveVessel.SpawnCrew();
BeamActionTracker.crewBeamedTime = 0;
}
}
base.OnUpdate();
}
}
}
@belackriv
Copy link
Author

If you click the "Beam Kerbal" button, then close the window, and click it again, you get:

[BelacCorp]checking Beamer2
[LOG 19:01:15.622] [BelacCorp]loading Beamer2
[WRN 19:01:15.631] HighlightingSystem : Multiple Highlighter components on a single GameObject is not allowed! Highlighter has been disabled on a GameObject with name 'model'.
[WRN 19:01:15.635] ...FlagDecal module found at index 0.
[WRN 19:01:15.636] PartModule is null.

@belackriv
Copy link
Author

Also, doing a beam to a vessel out of range only removes the kerbal, but never puts them into the switched to vessel.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment