protected override InterpolatedOrbit TransferOrbit(Planet from, Planet to) { Vector3 pos0 = from.transform.position; Vector3 v0 = new Vector3(-pos0.normalized[1], pos0.normalized[0]) * (float)Orbit.HohmannVelocityDeparture(from, to); InterpolatedOrbit newOrbit = new InterpolatedOrbit(pos0, v0, Time.timeSinceLevelLoad); return(newOrbit); }
public bool SetNextFlightState(FlightState next, InterpolatedOrbit newOrbit, double insertionDeltaV) { // inconsistent parent body if (parentBody != next.parentBody && next.flightState != StateEnum.InterplanetaryTransfer && flightState != StateEnum.InterplanetaryTransfer) { return(false); } if (next.parentBody == null) { Debug.Log("Attempt to start flight with null parent body"); return(false); } // from surface only to LO if (flightState == StateEnum.OnSurface && next.flightState != StateEnum.LO) { return(false); } if (next.flightState == StateEnum.InterplanetaryTransfer) { if (next.parentBody == null) { Debug.Log("Attempt to start interplanetary flight without specifiying the destination"); return(false); } if (newOrbit == null) { Debug.Log("Orbit required and not provided"); return(false); } parentShip.SetupOrbit(newOrbit); } if (flightState == StateEnum.InterplanetaryTransfer && next.flightState != StateEnum.InterplanetaryTransfer) { parentShip.DestroyOrbit(); } parentShip.engine.BurnFuelByDeltaV(insertionDeltaV, 3600 / 2); flightState = next.flightState; parentBody = next.parentBody; return(true); }
public void SetupOrbit(InterpolatedOrbit orb) { orbit = orb; LineRenderer template = (LineRenderer)Resources.Load("OrbitPrefab", typeof(LineRenderer)); orbitRenderer = Instantiate(template); orbitRenderer.name = $"{gameObject.name} Orbit"; orbitRenderer.transform.parent = gameObject.transform.parent; List <Vector3> positions = orbit.GetPts(); orbitRenderer.positionCount = positions.Count; orbitRenderer.SetPositions(positions.ToArray()); Color c = new Color(0 / 256.0f, 0 / 256.0f, 256 / 256.0f); orbitRenderer.startColor = c; orbitRenderer.endColor = c; }
public void DestroyOrbit() { Destroy(orbitRenderer.gameObject); orbitRenderer = null; orbit = null; }
public void Update() { if (parent.flightState.parentBody == target.parentBody && parent.flightState.flightState == target.flightState) { OnTargetReached(); } if (target != null) { if (parent.flightState.parentBody != target.parentBody) { if (parent.flightState.flightState == FlightState.StateEnum.OnSurface && parent.flightState.parentBody != null) { // launch to low orbit double deltaV = StartToLeoDeltaV(parent.flightState.parentBody); if (parent.flightState.SetNextFlightState(new FlightState(parent.flightState.parentBody, FlightState.StateEnum.LO, parent), null, deltaV)) { Debug.Log($"{parent.name} launched to LO of {parent.flightState.parentBody.name}"); } } if (parent.flightState.flightState != FlightState.StateEnum.OnSurface && parent.flightState.parentBody != null && InLaunchWindow(parent.flightState.parentBody, target.parentBody)) { InterpolatedOrbit proposedOrbit = TransferOrbit(parent.flightState.parentBody, target.parentBody); double deltaV = StartInsertionBurnDeltaV(parent.flightState.parentBody, target.parentBody); if (parent.engine.CanProvideDeltaV(deltaV, 3600 / 2)) { // launch to interplanetary transfer orbit if (parent.flightState.SetNextFlightState(new FlightState(target.parentBody, FlightState.StateEnum.InterplanetaryTransfer, parent), proposedOrbit, deltaV)) { Debug.Log($"{parent.name} launched to interplanetary transfer orbit towards {target.parentBody.name}, fuel {parent.fuelTanks[0].resource.amountKg:F0}, {parent.fuelTanks[1].resource.amountKg:F0}"); } } else { Debug.Log($"{parent.name} does not have fuel for insertion burn to {target.parentBody.name}"); } } } else { if (parent.flightState.flightState == FlightState.StateEnum.InterplanetaryTransfer) { // arrival at destination if ((parent.gameObject.transform.localPosition - target.parentBody.gameObject.transform.localPosition).sqrMagnitude < 0.00025) { double deltaV = EndInsertionBurnDeltaV(parent.flightState.parentBody, target.parentBody); if (parent.engine.CanProvideDeltaV(deltaV, 3600 / 2)) { if (parent.flightState.SetNextFlightState(new FlightState(target.parentBody, target.flightState, parent), null, deltaV)) { Debug.Log($"{parent.name} arrived from interplanetary orbit to {target.flightState} of {target.parentBody.name}, fuel {parent.fuelTanks[0].resource.amountKg:F0}, {parent.fuelTanks[1].resource.amountKg:F0}"); parent.Refuel(); } else { Debug.Log($"Setting flight state failed: {parent.name}, {target.flightState} of {target.parentBody.name}"); } } else { Debug.Log($"Ship {parent.name} run out of fuel!"); } } } } } else { Debug.Log("Autopilot target null"); } }