407 lines
14 KiB
C#
407 lines
14 KiB
C#
using UnityEngine;
|
||
using UnityEngine.UIElements;
|
||
|
||
namespace BigShipTycoon
|
||
{
|
||
public sealed partial class TycoonApp
|
||
{
|
||
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;
|
||
|
||
float delta = Time.unscaledDeltaTime;
|
||
deckAmbience.Mix(delta, !title && !paused && !fleetOpen && !voyageMapOpen && !crewManifestOpen
|
||
&& !ShorePlanningOpen && careerMenu == CareerMenu.Closed && !harbor.PortCinematicActive && HarborOnScreen,
|
||
livePassage, passageAudio.CurrentAnnouncement.HasValue);
|
||
if (careerMenu != CareerMenu.Closed)
|
||
{
|
||
if (Input.GetKeyDown(KeyCode.Escape))
|
||
CloseCareerMenu();
|
||
return;
|
||
}
|
||
|
||
AutosaveCareer();
|
||
if (harbor.PortCinematicActive)
|
||
{
|
||
if (!paused)
|
||
{
|
||
AdvanceShoreCalls(delta);
|
||
UpdateBackgroundPassages(delta);
|
||
}
|
||
if (holdCinematicClock)
|
||
return;
|
||
if (Input.GetKeyDown(KeyCode.Escape))
|
||
EndPortCinematic();
|
||
else
|
||
UpdatePortCinematic(delta);
|
||
return;
|
||
}
|
||
|
||
if (Input.GetKeyDown(KeyCode.Escape))
|
||
{
|
||
if (ShorePlanningOpen)
|
||
CloseShorePlanning();
|
||
else if (crewManifestOpen)
|
||
ToggleCrewManifest();
|
||
else if (!paused && fleetOpen)
|
||
ToggleFleetOverview();
|
||
else if (!paused && underway != null && voyageMapOpen)
|
||
ToggleVoyageMap();
|
||
else if (title && pendingStart.HasValue)
|
||
{
|
||
pendingStart = null;
|
||
careerError = null;
|
||
Render();
|
||
}
|
||
else if (!title)
|
||
{
|
||
paused = !paused;
|
||
Render();
|
||
}
|
||
}
|
||
|
||
if (!paused && !title)
|
||
{
|
||
if (!ShorePlanningOpen && !crewManifestOpen && !EditingText() && Input.GetKeyDown(KeyCode.F2))
|
||
ToggleFleetOverview();
|
||
AdvanceShoreCalls(delta);
|
||
UpdateBackgroundPassages(delta);
|
||
}
|
||
|
||
passageAudio.Mix(delta, underway != null && !paused,
|
||
passageHelm?.Speed ?? 0, underway?.Weather ?? WeatherCondition.Calm,
|
||
passageBridge || passageExploring, passageServiceArea == 0);
|
||
|
||
if (underway != null)
|
||
{
|
||
if (!paused)
|
||
UpdateActivePassage(delta);
|
||
return;
|
||
}
|
||
|
||
if (title || paused || fleetOpen || ShorePlanningOpen || EditingText())
|
||
return;
|
||
|
||
if (page == 1 && Input.GetKeyDown(KeyCode.R))
|
||
RotateDeckTool();
|
||
|
||
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(delta);
|
||
}
|
||
|
||
private void UpdateActivePassage(float delta)
|
||
{
|
||
float rudder = 0;
|
||
if (fleetOpen)
|
||
{
|
||
SetPassageFreeLook(false);
|
||
if (passageHelm.Manual)
|
||
ReleaseHelm();
|
||
}
|
||
else if (!ShorePlanningOpen && !crewManifestOpen)
|
||
rudder = ReadNavigationControls();
|
||
|
||
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 (!fleetOpen && !ShorePlanningOpen && !crewManifestOpen)
|
||
UpdatePassageView(delta);
|
||
|
||
UpdatePassageUi(progress);
|
||
UpdateServiceNavigation();
|
||
RefreshCrewManifest();
|
||
UpdateHelmControls();
|
||
UpdateStationInteractions();
|
||
UpdateAnnouncement();
|
||
UpdateVoyageMap();
|
||
}
|
||
|
||
private float ReadNavigationControls()
|
||
{
|
||
if (Input.GetKeyDown(KeyCode.M))
|
||
ToggleVoyageMap();
|
||
if (Input.GetKeyDown(KeyCode.H) && (passageBridge || passageHelm.Manual))
|
||
ToggleHelm();
|
||
|
||
if (!passageHelm.Manual)
|
||
return 0;
|
||
|
||
if (Input.GetKeyDown(KeyCode.W))
|
||
SetHelmOrder(HelmOrder + 1);
|
||
if (Input.GetKeyDown(KeyCode.S))
|
||
SetHelmOrder(HelmOrder - 1);
|
||
return MoveAxis(KeyCode.D, KeyCode.A);
|
||
}
|
||
|
||
private void UpdatePassageView(float delta)
|
||
{
|
||
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;
|
||
}
|
||
}
|
||
}
|
||
|
||
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((135f + voyage.Miles * .18f + voyage.Days * 8f) * weather, 175f, 540f);
|
||
}
|
||
|
||
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 || fleetPassages.ContainsKey(ship))
|
||
return;
|
||
string failure = company.DepartureBlock(ship, quote);
|
||
if (failure != null)
|
||
{
|
||
Apply(failure, null);
|
||
return;
|
||
}
|
||
|
||
var passage = new FleetPassage(ship, quote, company.Voyages < 3 && ship.HullIndex == 0);
|
||
ResetShorePlanning();
|
||
fleetPassages.Add(ship, passage);
|
||
UpdateVoyageReserves();
|
||
fleetOpen = shipActionsOpen = false;
|
||
page = 0;
|
||
Notify(null);
|
||
FocusPassage(passage, true);
|
||
Save();
|
||
if (!StartPortCinematic(quote.Origin, false))
|
||
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;
|
||
if (fleetPassages.TryGetValue(Ship, out FleetPassage passage))
|
||
passage.Speed = speed;
|
||
Render();
|
||
}
|
||
|
||
private void FinishVoyage()
|
||
{
|
||
if (underway == null || paused || harbor.PortCinematicActive)
|
||
return;
|
||
ResetShorePlanning();
|
||
Voyage voyage = underway;
|
||
string failure = SettleFleetPassage(Ship, livePassage);
|
||
if (failure != null)
|
||
{
|
||
Notify(failure, true);
|
||
CompleteDocking();
|
||
return;
|
||
}
|
||
|
||
ResetItinerary();
|
||
selectedVoyage = 0;
|
||
ledgerRecordOffset = 0;
|
||
ledgerAccounts = false;
|
||
page = Ship.ShoreCall != null ? 0 : 4;
|
||
ReviewGuideVoyage();
|
||
string arrival = $"Arrived in {Market.Ports[voyage.Destination].Name}.";
|
||
if (Ship.ShoreCall is ShoreCall call)
|
||
arrival += $" {call.Guests:N0} guests heading ashore.";
|
||
else if (voyage.ItineraryLeg > 0)
|
||
arrival += Ship.Itinerary is { HasNext: true } itinerary
|
||
? $" Next call is {Market.Ports[itinerary.NextPort].Name}."
|
||
: " Itinerary complete.";
|
||
Notify(arrival);
|
||
Save();
|
||
if (fleetOpen)
|
||
{
|
||
CompleteDocking();
|
||
return;
|
||
}
|
||
passageAudio.AnnounceApproach();
|
||
if (!StartPortCinematic(voyage.Destination, true))
|
||
CompleteDocking();
|
||
}
|
||
|
||
private void CompleteDocking()
|
||
{
|
||
ClearActivePassage();
|
||
passageAudio.End();
|
||
harbor.EndPassage();
|
||
Render();
|
||
}
|
||
}
|
||
}
|