Unlock expanded venues and manage concurrent fleet voyages
This commit is contained in:
193
Assets/Soundings/Runtime/FleetPassages.cs
Normal file
193
Assets/Soundings/Runtime/FleetPassages.cs
Normal file
@@ -0,0 +1,193 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace Soundings
|
||||
{
|
||||
public sealed partial class SoundingsApp
|
||||
{
|
||||
private readonly Dictionary<Ship, FleetPassage> fleetPassages = new();
|
||||
private Label fleetDayReadout, fleetReputationReadout, fleetTreasuryReadout;
|
||||
|
||||
private sealed class FleetPassage
|
||||
{
|
||||
public readonly Voyage Voyage;
|
||||
public readonly OnboardSession Session;
|
||||
public readonly HelmNavigation Helm;
|
||||
public int Speed = 1;
|
||||
public bool ApproachAnnounced;
|
||||
|
||||
public FleetPassage(Ship ship, Voyage voyage, bool assisted)
|
||||
{
|
||||
Voyage = voyage;
|
||||
Session = new OnboardSession(ship, voyage) { Assisted = assisted };
|
||||
Helm = new HelmNavigation(PassageDuration(voyage), ship.HullIndex);
|
||||
}
|
||||
}
|
||||
|
||||
private (Voyage Voyage, OnboardSession Session, HelmNavigation Helm, int Speed) FleetStatus(Ship ship)
|
||||
{
|
||||
if (ship != null && fleetPassages.TryGetValue(ship, out FleetPassage passage))
|
||||
return (passage.Voyage, passage.Session, passage.Helm, passage.Speed);
|
||||
return default;
|
||||
}
|
||||
|
||||
private void SwitchFleetShip(int index)
|
||||
{
|
||||
if (company == null || paused || harbor.PortCinematicActive || index < 0 || index >= company.Fleet.Count)
|
||||
return;
|
||||
|
||||
fleetOpen = shipActionsOpen = false;
|
||||
if (index == company.SelectedShip)
|
||||
{
|
||||
Render();
|
||||
return;
|
||||
}
|
||||
|
||||
if (underway != null)
|
||||
{
|
||||
if (fleetPassages.TryGetValue(Ship, out FleetPassage previous))
|
||||
previous.ApproachAnnounced = passageAudio.ApproachSent;
|
||||
ReleaseHelm();
|
||||
harbor.EndPassage();
|
||||
}
|
||||
passageAudio.End();
|
||||
ClearActivePassage();
|
||||
|
||||
company.SelectedShip = index;
|
||||
page = deckIndex = 0;
|
||||
ResetItinerary();
|
||||
Notify(null);
|
||||
if (fleetPassages.TryGetValue(Ship, out FleetPassage selected))
|
||||
FocusPassage(selected, false);
|
||||
else
|
||||
harbor.ResetView(true);
|
||||
|
||||
Save();
|
||||
Render();
|
||||
}
|
||||
|
||||
private void FocusPassage(FleetPassage passage, bool departure)
|
||||
{
|
||||
underway = passage.Voyage;
|
||||
livePassage = passage.Session;
|
||||
passageHelm = passage.Helm;
|
||||
passageSpeed = passage.Speed;
|
||||
ResetPassageView();
|
||||
harbor.Show(Ship);
|
||||
harbor.BeginPassage(underway.Weather, PassageDuration(underway));
|
||||
harbor.ShowDeck(-1);
|
||||
PresentVessel(0);
|
||||
harbor.UpdatePassengers(livePassage, 0);
|
||||
passageAudio.Begin(underway.Destination, departure, passage.ApproachAnnounced);
|
||||
}
|
||||
|
||||
private void ResetPassageView()
|
||||
{
|
||||
passageDeck = passageServiceArea = -1;
|
||||
passageBridge = passageExploring = false;
|
||||
voyageMapOpen = false;
|
||||
stationFeedbackText = null;
|
||||
passageBusinessOpen = true;
|
||||
serviceReadout = null;
|
||||
SetPassageFreeLook(false);
|
||||
}
|
||||
|
||||
private void ClearActivePassage()
|
||||
{
|
||||
underway = null;
|
||||
livePassage = null;
|
||||
passageHelm = null;
|
||||
passageSpeed = 1;
|
||||
ResetPassageView();
|
||||
}
|
||||
|
||||
private void ResetFleetPassages()
|
||||
{
|
||||
fleetPassages.Clear();
|
||||
if (company != null)
|
||||
company.ReservedCash = 0;
|
||||
ClearActivePassage();
|
||||
}
|
||||
|
||||
private void UpdateVoyageReserves()
|
||||
{
|
||||
int reserved = 0;
|
||||
foreach (FleetPassage passage in fleetPassages.Values)
|
||||
{
|
||||
Voyage voyage = passage.Voyage;
|
||||
reserved += voyage.OperatingCash + voyage.LoanInterest + voyage.Principal;
|
||||
}
|
||||
company.ReservedCash = reserved;
|
||||
}
|
||||
|
||||
private string SettleFleetPassage(Ship ship, OnboardSession session)
|
||||
{
|
||||
if (!fleetPassages.TryGetValue(ship, out FleetPassage passage) || passage.Session != session)
|
||||
return "This sailing has already been settled.";
|
||||
|
||||
fleetPassages.Remove(ship);
|
||||
UpdateVoyageReserves();
|
||||
return company.Sail(ship, session.Quote.Destination, session);
|
||||
}
|
||||
|
||||
private void UpdateBackgroundPassages(float delta)
|
||||
{
|
||||
if (fleetPassages.Count == 0)
|
||||
return;
|
||||
|
||||
Ship foreground = Ship;
|
||||
bool docked = false;
|
||||
foreach (Ship ship in company.Fleet)
|
||||
{
|
||||
if (ship == foreground || !fleetPassages.TryGetValue(ship, out FleetPassage passage))
|
||||
continue;
|
||||
|
||||
passage.Helm.Advance(delta * passage.Speed, 0, passage.Session.SpeedFactor, passage.Voyage.Weather);
|
||||
passage.Session.AdvanceTo(passage.Helm.Progress);
|
||||
if (!passage.Helm.Arrived)
|
||||
continue;
|
||||
|
||||
string failure = SettleFleetPassage(ship, passage.Session);
|
||||
if (failure == null)
|
||||
{
|
||||
docked = true;
|
||||
ReviewGuideVoyage();
|
||||
Save();
|
||||
}
|
||||
RefreshFleetMetrics();
|
||||
ShowFleetArrivalNotice(failure == null ? $"{ship.Name} arrived in {Market.Ports[ship.Port].Name}." : $"{ship.Name} · {failure}",
|
||||
failure != null);
|
||||
}
|
||||
|
||||
if (docked && underway == null && !fleetOpen && !harbor.PortCinematicActive)
|
||||
Render();
|
||||
}
|
||||
|
||||
private void RefreshFleetMetrics()
|
||||
{
|
||||
if (fleetDayReadout?.panel == null)
|
||||
return;
|
||||
|
||||
SetPassageText(fleetDayReadout, company.Day.ToString("00"));
|
||||
SetPassageText(fleetReputationReadout, company.Reputation.ToString());
|
||||
SetPassageText(fleetTreasuryReadout, Money(company.Cash));
|
||||
fleetTreasuryReadout.parent.tooltip = company.ReservedCash > 0 ? $"{Money(company.AvailableCash)} available to spend" : null;
|
||||
}
|
||||
|
||||
private void ShowFleetArrivalNotice(string message, bool failed)
|
||||
{
|
||||
if (fleetOpen || harbor.PortCinematicActive)
|
||||
return;
|
||||
|
||||
Notify(message, failed);
|
||||
var shell = root.Q<VisualElement>(className: "game-shell");
|
||||
if (shell == null)
|
||||
return;
|
||||
|
||||
var toast = shell.Q<VisualElement>(className: "game-toast") ?? Add(shell, "game-toast");
|
||||
toast.pickingMode = PickingMode.Ignore;
|
||||
toast.Clear();
|
||||
AddNotice(toast);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user