using UnityEngine; using UnityEngine.UIElements; namespace Soundings { public sealed partial class SoundingsApp { private static readonly string[] HelmOrders = { "Stop", "Economy", "Cruise", "Power" }; private readonly Button[] helmOrderButtons = new Button[HelmOrders.Length]; private Label helmReadout; private float courseBearing; private int HelmOrder => passageHelm.PropulsionStopped ? -1 : livePassage.EngineMode; private void DrawHelmControls(VisualElement overlay) { helmReadout = null; if (!passageBridge && !passageHelm.Manual) return; var station = Add(overlay, "passage-helm"); AddText(station, passageHelm.Manual ? "MANUAL HELM" : "AUTOPILOT", "voyage-caption"); courseBearing = CourseBearing(Market.Ports[underway.Origin], Market.Ports[underway.Destination]); helmReadout = AddText(station, "", "helm-readout"); if (passageHelm.Manual) { var throttle = Add(station, "helm-throttle"); for (int i = 0; i < HelmOrders.Length; i++) { int order = i - 1; helmOrderButtons[i] = Action(HelmOrders[i], () => SetHelmOrder(order), "helm-order", $"helm-order-{i}"); throttle.Add(helmOrderButtons[i]); } AddText(station, "A / D rudder · W / S throttle", "helm-help"); } station.Add(Action(passageHelm.Manual ? "Return to autopilot H" : "Take the helm H", ToggleHelm, "helm-toggle", "helm-toggle")); UpdateHelmControls(); } private void ToggleHelm() { if (passageHelm.Manual) ReleaseHelm(); else if (passageBridge) passageHelm.Manual = true; Render(); } private void ReleaseHelm() { passageHelm.Manual = false; passageHelm.PropulsionStopped = false; } private void SetHelmOrder(int order) { if (order < -1 || order > 2 || (order >= 0 && !livePassage.CanSetEngineMode(order))) return; passageHelm.PropulsionStopped = order == -1; if (order >= 0) livePassage.SetEngineMode(order); UpdateHelmControls(); } private void UpdateHelmControls() { if (helmReadout == null || passageHelm == null) return; int heading = Mathf.RoundToInt(Mathf.Repeat(courseBearing + passageHelm.Heading, 360)) % 360; int rudder = Mathf.RoundToInt(passageHelm.Rudder); string rudderText = rudder == 0 ? "Centered" : $"{Mathf.Abs(rudder)}° {(rudder < 0 ? "Port" : "Stbd")}"; SetPassageText(helmReadout, $"{heading:000}° true · {passageHelm.Speed:0.0} kn\nRudder {rudderText}"); if (!passageHelm.Manual) return; for (int i = 0; i < helmOrderButtons.Length; i++) { helmOrderButtons[i].EnableInClassList("voyage-control-active", HelmOrder == i - 1); helmOrderButtons[i].SetEnabled(i == 0 || livePassage.CanSetEngineMode(i - 1)); } } private static float CourseBearing(PortSpec origin, PortSpec destination) { float originLatitude = origin.Latitude * Mathf.Deg2Rad; float destinationLatitude = destination.Latitude * Mathf.Deg2Rad; float longitudeDifference = (destination.Longitude - origin.Longitude) * Mathf.Deg2Rad; float east = Mathf.Sin(longitudeDifference) * Mathf.Cos(destinationLatitude); float north = Mathf.Cos(originLatitude) * Mathf.Sin(destinationLatitude) - Mathf.Sin(originLatitude) * Mathf.Cos(destinationLatitude) * Mathf.Cos(longitudeDifference); return Mathf.Atan2(east, north) * Mathf.Rad2Deg; } } }