Files
cruiseSimulator/Assets/Soundings/Runtime/VoyagePresentation.cs

334 lines
12 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using UnityEngine;
using UnityEngine.UIElements;
namespace Soundings
{
public sealed partial class SoundingsApp
{
private Voyage underway;
private OnboardSession livePassage;
private HelmNavigation passageHelm;
private int passageSpeed = 1, passageDeck = -1, passageServiceArea = -1;
private bool passageFreeLook, passageCursorLocked;
private bool passageBridge, passageExploring;
private void Update()
{
if (root == null)
return;
if (Input.GetKeyDown(KeyCode.Escape))
{
if (!paused && underway != null && voyageMapOpen)
ToggleVoyageMap();
else if (title && pendingStart.HasValue)
{
pendingStart = null;
Render();
}
else if (!title)
{
paused = !paused;
Render();
}
}
passageAudio.Mix(Time.unscaledDeltaTime, underway != null && !paused,
passageHelm?.Speed ?? 0, underway?.Weather ?? WeatherCondition.Calm,
passageBridge || passageExploring, passageServiceArea == 0);
if (underway != null)
{
if (!paused)
UpdateActivePassage();
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();
if (page == 0)
TurnPassageCamera(Time.unscaledDeltaTime);
}
private void UpdateActivePassage()
{
float delta = Time.unscaledDeltaTime;
if (Input.GetKeyDown(KeyCode.M))
ToggleVoyageMap();
if (Input.GetKeyDown(KeyCode.H) && (passageBridge || passageHelm.Manual))
ToggleHelm();
float rudder = 0;
if (passageHelm.Manual)
{
rudder = MoveAxis(KeyCode.D, KeyCode.A);
if (Input.GetKeyDown(KeyCode.W))
SetHelmOrder(HelmOrder + 1);
if (Input.GetKeyDown(KeyCode.S))
SetHelmOrder(HelmOrder - 1);
}
passageHelm.Advance(delta * passageSpeed, rudder, livePassage.SpeedFactor, underway.Weather);
float progress = passageHelm.Progress;
UpdatePortApproach();
bool performanceWasActive = livePassage.PerformanceActive;
livePassage.AdvanceTo(progress);
if (!performanceWasActive && livePassage.PerformanceActive)
passageAudio.Announce(PassageAnnouncement.Show);
PresentVessel(delta);
harbor.UpdatePassengers(livePassage, delta);
if (passageHelm.Arrived)
{
FinishVoyage();
return;
}
if (!Input.GetMouseButton(1) || !Application.isFocused)
SetPassageFreeLook(false);
if (passageFreeLook && passageCursorLocked)
{
Vector2 look = new Vector2(Input.GetAxisRaw("Mouse X"), -Input.GetAxisRaw("Mouse Y")) * 10;
if (look.sqrMagnitude > .001f)
harbor.Look(look);
}
TurnPassageCamera(delta);
if (!passageHelm.Manual && (passageExploring || passageBridge))
{
Vector2 movement = new Vector2(MoveAxis(KeyCode.D, KeyCode.A), MoveAxis(KeyCode.W, KeyCode.S));
if (movement.sqrMagnitude > 0)
{
Vector3 previous = harbor.FirstPersonPosition;
harbor.Walk(movement, delta, Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift));
passageAudio.Step(Vector3.Distance(previous, harbor.FirstPersonPosition), passageServiceArea >= 0);
}
}
if (Input.GetKeyDown(KeyCode.F))
UseFacingStation();
if (Input.GetKeyDown(KeyCode.Home))
{
if (passageServiceArea >= 0)
harbor.ExploreServiceArea(passageServiceArea);
else if (passageBridge)
harbor.ViewBridge();
else if (passageExploring)
harbor.ExploreDeck(passageDeck);
else
harbor.ResetView();
}
if (Input.GetKeyDown(KeyCode.B))
ViewPassageBridge();
if (Input.GetKeyDown(KeyCode.V))
ExplorePassageDeck();
for (int i = 0; i <= Ship.Decks.Count; i++)
{
if (Input.GetKeyDown((KeyCode)((int)KeyCode.Alpha0 + i)))
{
SelectPassageDeck(i - 1);
break;
}
}
UpdatePassageUi(progress);
UpdateServiceNavigation();
UpdateHelmControls();
UpdateStationInteractions();
UpdateAnnouncement();
UpdateVoyageMap();
}
private void UpdatePortApproach()
{
if (passageAudio.ApproachSent || passageHelm.Arrived)
return;
float usualApproach = Mathf.Clamp(PassageDuration(underway) * .17f, 18f, 44f);
float speakingTime = (passageAudio.ApproachSeconds + 1.2f) * passageSpeed * livePassage.SpeedFactor;
if (passageHelm.RemainingSeconds <= Mathf.Max(usualApproach, speakingTime))
passageAudio.AnnounceApproach();
}
private void TurnPassageCamera(float delta) =>
harbor.RotateView(MoveAxis(KeyCode.E, KeyCode.Q) * 82f * delta);
private void PresentVessel(float delta)
{
Vector2 position = passageHelm.Position / PassageDuration(underway);
harbor.SetPassage(position, passageHelm.Heading, passageHelm.Speed, delta);
harbor.SetRudder(passageHelm.Rudder);
}
private void SetPassageFreeLook(bool active)
{
if (active == passageFreeLook)
return;
passageFreeLook = active;
if (Application.platform == RuntimePlatform.WebGLPlayer)
return;
passageCursorLocked = active;
UnityEngine.Cursor.lockState = active ? CursorLockMode.Locked : CursorLockMode.None;
UnityEngine.Cursor.visible = !active;
}
private static float MoveAxis(KeyCode forward, KeyCode reverse) =>
(Input.GetKey(forward) ? 1 : 0) - (Input.GetKey(reverse) ? 1 : 0);
// Real seconds at 1×. Put enough real time between onboard service windows to act.
private static float PassageDuration(Voyage voyage)
{
float weather = voyage.Weather switch { WeatherCondition.Swell => 1.14f, WeatherCondition.Storm => 1.32f, _ => 1 };
return Mathf.Clamp((90f + voyage.Miles * .125f + voyage.Days * 6f) * weather, 115f, 360f);
}
private bool EditingText()
{
for (var item = root.focusController?.focusedElement as VisualElement; item != null; item = item.parent)
if (item is TextField or IntegerField)
return true;
return false;
}
private void BeginVoyage(Voyage quote)
{
Ship ship = Ship;
if (underway != null || ship == null)
return;
string failure = company.DepartureBlock(ship, quote);
if (failure != null)
{
Apply(failure, null);
return;
}
underway = quote;
livePassage = new OnboardSession(ship, quote) { Assisted = company.Voyages < 3 && ship.HullIndex == 0 };
passageHelm = new HelmNavigation(PassageDuration(quote), ship.HullIndex);
passageSpeed = 1;
passageDeck = passageServiceArea = -1;
passageBridge = passageExploring = false;
voyageMapOpen = false;
stationFeedbackText = null;
passageBusinessOpen = true;
SetPassageFreeLook(false);
fleetOpen = shipActionsOpen = false;
page = 0;
Notify(null);
harbor.BeginPassage(quote.Weather, PassageDuration(quote));
harbor.ShowDeck(passageDeck);
harbor.UpdatePassengers(livePassage, 0);
passageAudio.Begin(quote.Destination);
Render();
}
private void SelectPassageDeck(int index)
{
if (underway == null || index < -1 || index >= Ship.Decks.Count ||
(passageDeck == index && !passageBridge && !passageExploring && passageServiceArea < 0))
return;
passageDeck = index;
if (index >= 0)
ReleaseHelm();
passageServiceArea = -1;
passageBridge = passageExploring = false;
harbor.ShowDeck(index);
harbor.ResetView();
Render();
}
private void ViewPassageBridge()
{
if (underway == null)
return;
if (!passageBridge)
passageBusinessOpen = false;
passageBridge = true;
passageExploring = false;
passageDeck = passageServiceArea = -1;
harbor.ViewBridge();
Render();
}
private void ExplorePassageDeck()
{
if (underway == null)
return;
if (passageDeck < 0)
passageDeck = Ship.Decks.Count - 1;
ReleaseHelm();
passageBridge = false;
passageExploring = true;
passageServiceArea = -1;
harbor.ExploreDeck(passageDeck);
Render();
}
private void ExploreServiceArea(int index)
{
if (underway == null || index < 0 || index >= ServiceDeckModel.Areas.Length)
return;
if (passageServiceArea < 0)
passageBusinessOpen = false;
ReleaseHelm();
passageBridge = false;
passageExploring = true;
passageDeck = -1;
passageServiceArea = index;
harbor.ExploreServiceArea(index);
Render();
}
private void SetPassageSpeed(int speed)
{
if (underway == null || (speed != 1 && speed != 2 && speed != 4) || speed == passageSpeed)
return;
passageSpeed = speed;
Render();
}
private void FinishVoyage()
{
if (underway == null || paused)
return;
Voyage voyage = underway;
livePassage.AdvanceTo(1);
string failure = company.Sail(Ship, voyage.Destination, livePassage);
underway = null;
livePassage = null;
passageHelm = null;
passageServiceArea = -1;
voyageMapOpen = false;
passageAudio.End();
serviceReadout = null;
SetPassageFreeLook(false);
harbor.EndPassage();
if (failure == null)
{
ResetItinerary();
selectedVoyage = 0;
ledgerRecordOffset = 0;
ledgerAccounts = false;
page = 4;
ReviewGuideVoyage();
}
string arrival = $"Arrived in {Market.Ports[voyage.Destination].Name}.";
if (failure == null && voyage.ItineraryLeg > 0)
arrival += Ship.Itinerary is { HasNext: true } itinerary
? $" Next call is {Market.Ports[itinerary.NextPort].Name}."
: " Itinerary complete.";
Apply(failure, arrival);
}
}
}