143 lines
4.7 KiB
C#
143 lines
4.7 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
|
|
namespace Soundings
|
|
{
|
|
public sealed partial class SoundingsApp
|
|
{
|
|
private Voyage underway;
|
|
private float passageElapsed;
|
|
private VisualElement passageFill;
|
|
private Label passageDistance;
|
|
|
|
private void Update()
|
|
{
|
|
if (root == null)
|
|
return;
|
|
|
|
if (Input.GetKeyDown(KeyCode.Escape))
|
|
{
|
|
if (title && pendingStart.HasValue)
|
|
{
|
|
pendingStart = null;
|
|
Render();
|
|
}
|
|
else if (!title)
|
|
{
|
|
paused = !paused;
|
|
Render();
|
|
}
|
|
}
|
|
|
|
if (underway != null)
|
|
{
|
|
if (!paused)
|
|
{
|
|
passageElapsed += Time.unscaledDeltaTime;
|
|
float duration = PassageDuration(underway);
|
|
float progress = Mathf.Clamp01(passageElapsed / duration);
|
|
harbor.SetPassage(progress);
|
|
UpdatePassageInstrument(progress);
|
|
if (progress >= 1)
|
|
FinishVoyage();
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (title || paused || EditingText())
|
|
return;
|
|
|
|
for (int i = 0; i < HotbarPages.Length; i++)
|
|
{
|
|
if (Input.GetKeyDown((KeyCode)((int)KeyCode.Alpha1 + i)))
|
|
{
|
|
ChangePage(HotbarPages[i]);
|
|
break;
|
|
}
|
|
}
|
|
if (page == 0 && Input.GetKeyDown(KeyCode.Home))
|
|
harbor.ResetView();
|
|
}
|
|
|
|
private static float PassageDuration(Voyage voyage) => Mathf.Clamp(4.8f + voyage.Days * .56f, 6, 10);
|
|
|
|
private bool EditingText()
|
|
{
|
|
for (var item = root.focusController?.focusedElement as VisualElement; item != null; item = item.parent)
|
|
if (item is TextField)
|
|
return true;
|
|
return false;
|
|
}
|
|
|
|
private void BeginVoyage(Voyage quote)
|
|
{
|
|
if (underway != null || Ship == null)
|
|
return;
|
|
string failure = company.DepartureBlock(Ship, quote);
|
|
if (failure != null)
|
|
{
|
|
Apply(failure, null);
|
|
return;
|
|
}
|
|
|
|
underway = quote;
|
|
passageElapsed = 0;
|
|
fleetOpen = shipActionsOpen = false;
|
|
page = 0;
|
|
Notify(null);
|
|
harbor.BeginPassage();
|
|
Render();
|
|
}
|
|
|
|
private void FinishVoyage()
|
|
{
|
|
if (underway == null || paused)
|
|
return;
|
|
Voyage voyage = underway;
|
|
underway = null;
|
|
passageFill = null;
|
|
passageDistance = null;
|
|
harbor.EndPassage();
|
|
|
|
Ship ship = Ship;
|
|
string failure = company.Sail(ship, voyage.Destination);
|
|
if (failure == null)
|
|
{
|
|
ResetItinerary();
|
|
selectedVoyage = 0;
|
|
ledgerRecordOffset = 0;
|
|
ledgerAccounts = false;
|
|
page = 4;
|
|
ReviewGuideVoyage();
|
|
}
|
|
Apply(failure, $"Arrived in {Market.Ports[voyage.Destination].Name}.");
|
|
}
|
|
|
|
private void DrawUnderway(VisualElement workspace)
|
|
{
|
|
var passage = Add(workspace, "underway-instrument");
|
|
AddText(passage, "Underway", "eyebrow passage-kicker");
|
|
string route = $"{Market.Ports[underway.Origin].Name} → {Market.Ports[underway.Destination].Name}";
|
|
AddDisplay(passage, route, "passage-route");
|
|
var trace = Add(passage, "passage-trace");
|
|
passageFill = Add(trace, "passage-progress");
|
|
var footer = Add(passage, "passage-footer");
|
|
passageDistance = AddText(footer, "", "passage-distance");
|
|
footer.Add(Action("Skip to arrival", FinishVoyage, "passage-skip", "dock-now"));
|
|
float duration = PassageDuration(underway);
|
|
UpdatePassageInstrument(Mathf.Clamp01(passageElapsed / duration));
|
|
}
|
|
|
|
private void UpdatePassageInstrument(float progress)
|
|
{
|
|
if (passageFill == null || passageDistance == null)
|
|
return;
|
|
passageFill.style.width = Length.Percent(100 * progress);
|
|
int miles = Mathf.RoundToInt(underway.Miles * (1 - progress));
|
|
string label = $"{underway.ShipName} / {miles:N0} nm to port";
|
|
if (passageDistance.text != label)
|
|
passageDistance.text = label;
|
|
}
|
|
}
|
|
}
|