168 lines
8.1 KiB
C#
168 lines
8.1 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using UnityEngine.UIElements;
|
||
|
||
namespace Soundings
|
||
{
|
||
public sealed partial class SoundingsApp
|
||
{
|
||
private void DrawOperations(VisualElement pageRoot)
|
||
{
|
||
if (Ship == null)
|
||
{
|
||
var empty = Card(pageRoot, "No vessel in service");
|
||
AddText(empty, "Buy a pre-owned vessel or commission a hull from the shipyard.", "body-copy");
|
||
empty.Add(Action("Open shipyard →", () => ChangePage(2), "primary"));
|
||
return;
|
||
}
|
||
|
||
Ship ship = Ship;
|
||
Voyage quote = company.Forecast(ship, destination);
|
||
PortSpec port = Market.Ports[ship.Port];
|
||
var upper = Add(pageRoot, "columns");
|
||
var hero = Add(upper, "wide hero");
|
||
AddHarbor(hero);
|
||
var portTag = Add(hero, "hero-port");
|
||
AddText(portTag, $"● BERTHED IN {port.Name.ToUpperInvariant()}", "tag on-image");
|
||
var shipLabel = Add(hero, "hero-label");
|
||
AddDisplay(shipLabel, ship.Name, "hero-title");
|
||
AddText(shipLabel, $"{ship.Hull.Name.ToUpperInvariant()} / {ship.Decks.Count} DECKS", "eyebrow on-image");
|
||
|
||
var forecast = Card(upper, "Voyage forecast", "narrow forecast");
|
||
AddText(forecast, quote != null ? $"{port.Name} → {Market.Ports[destination].Name}" : "No itinerary selected", "forecast-route");
|
||
var paired = Add(forecast, "paired");
|
||
ForecastMetric(paired, "PROJECTED PROFIT", quote == null ? "—" : Money(quote.NetIncome),
|
||
quote == null ? "" : quote.NetIncome >= 0 ? "gain" : "loss");
|
||
ForecastMetric(paired, "EXPECTED GUESTS", quote == null ? "—" : $"{quote.Guests} / {quote.Capacity}");
|
||
AddLine(forecast, "Duration", quote == null ? "—" : $"{quote.Days} days");
|
||
AddLine(forecast, "Sailing distance", quote == null ? "—" : $"{quote.Miles:N0} nm");
|
||
AddLine(forecast, "Fuel burn + reserve", quote == null ? "—" : $"{quote.FuelBurn} + {quote.Reserve} t");
|
||
|
||
var lower = Add(pageRoot, "columns second-row");
|
||
var itinerary = Card(lower, "Itinerary & fares", "wide");
|
||
AddText(itinerary, "DESTINATION", "eyebrow control-label");
|
||
var choices = new List<string>();
|
||
var destinations = new List<int>();
|
||
foreach (Lane lane in Market.Lanes)
|
||
{
|
||
int other = lane.Other(ship.Port);
|
||
if (other < 0)
|
||
continue;
|
||
PortSpec candidate = Market.Ports[other];
|
||
string status = !company.Permits[other] ? " · Permit required" : ship.HullIndex < candidate.MinimumHull ? " · Larger hull" : "";
|
||
choices.Add($"{candidate.Name} · {lane.Miles:N0} nm{status}");
|
||
destinations.Add(other);
|
||
}
|
||
if (destinations.Count > 0)
|
||
{
|
||
int selected = destinations.IndexOf(destination);
|
||
var dropdown = new DropdownField(choices, selected) { name = "destination" };
|
||
dropdown.AddToClassList("field");
|
||
dropdown.RegisterValueChangedCallback(evt =>
|
||
{
|
||
destination = destinations[choices.IndexOf(evt.newValue)];
|
||
SetRecommendedFuel();
|
||
Notify(null);
|
||
Render();
|
||
});
|
||
itinerary.Add(dropdown);
|
||
}
|
||
|
||
var fare = Add(itinerary, "control-row");
|
||
var fareDescription = Add(fare, "control-copy");
|
||
AddText(fareDescription, "PASSENGER FARE", "eyebrow");
|
||
AddText(fareDescription, "Per guest, all sailing days", "muted");
|
||
Stepper(fare, Money(ship.Fare), "fare", () => ChangeFare(ship, -50), () => ChangeFare(ship, 50),
|
||
ship.Fare > Market.MinFare, ship.Fare < Market.MaxFare);
|
||
string reason = company.DepartureBlock(ship, quote);
|
||
var dispatch = Add(itinerary, "dispatch");
|
||
string readiness = reason ?? "Ready to depart. Settlement posts on arrival.";
|
||
AddText(dispatch, readiness, reason == null ? "readiness gain" : "readiness");
|
||
Button launch = Action("Dispatch voyage →", () =>
|
||
{
|
||
string failure = company.Sail(ship, destination);
|
||
if (failure == null)
|
||
{
|
||
ResetItinerary();
|
||
selectedVoyage = 0;
|
||
page = 4;
|
||
}
|
||
Apply(failure, $"Arrived in {Market.Ports[ship.Port].Name}. Voyage posted to the ledger.");
|
||
}, "primary", "dispatch");
|
||
launch.SetEnabled(reason == null);
|
||
dispatch.Add(launch);
|
||
|
||
var bunker = Card(lower, "Bunkering", "narrow bunker");
|
||
AddText(bunker, $"{port.Name} / {port.Country}", "muted");
|
||
AddLine(bunker, "Fuel aboard", $"{ship.Fuel} / {ship.Tank} t");
|
||
var meter = Add(bunker, "meter");
|
||
var fill = Add(meter, "meter-fill");
|
||
fill.style.width = Length.Percent(100f * ship.Fuel / ship.Tank);
|
||
AddLine(bunker, "Indicative safe range", $"{ship.SafeRange:N0} nm");
|
||
AddLine(bunker, "Local bunker price", $"{Money(Market.FuelPrice(ship.Port, company.Day))} / t");
|
||
var fuelControls = Add(bunker, "control-row fuel-controls");
|
||
Stepper(fuelControls, $"{bunkerTons} t", "tons", () => ChangeQuantity(ship, -10), () => ChangeQuantity(ship, 10),
|
||
bunkerTons > 0, bunkerTons < ship.Tank - ship.Fuel);
|
||
Button purchase = Action($"Buy · {Money(company.BunkerCost(ship, bunkerTons))}", () =>
|
||
{
|
||
int quantity = bunkerTons;
|
||
string failure = company.Refuel(ship, quantity);
|
||
if (failure == null)
|
||
SetRecommendedFuel();
|
||
Apply(failure, $"Loaded {quantity} t of fuel in {port.Name}.");
|
||
}, "", "buy-fuel");
|
||
purchase.SetEnabled(bunkerTons > 0 && company.Cash >= company.BunkerCost(ship, bunkerTons));
|
||
fuelControls.Add(purchase);
|
||
}
|
||
|
||
private void ChangeFare(Ship ship, int amount)
|
||
{
|
||
ship.Fare = Market.Clamp(ship.Fare + amount, Market.MinFare, Market.MaxFare);
|
||
Apply(null, null);
|
||
}
|
||
|
||
private void ChangeQuantity(Ship ship, int amount)
|
||
{
|
||
bunkerTons = Mathf.Clamp(bunkerTons + amount, 0, ship.Tank - ship.Fuel);
|
||
Notify(null);
|
||
Render();
|
||
}
|
||
|
||
private static void Stepper(VisualElement parent, string value, string id, Action minus, Action plus, bool canMinus, bool canPlus)
|
||
{
|
||
var stepper = Add(parent, "stepper");
|
||
Button down = Action("−", minus, "step", id + "-minus");
|
||
down.tooltip = id == "fare" ? "Lower passenger fare by $50" : "Buy 10 fewer tons";
|
||
down.SetEnabled(canMinus);
|
||
stepper.Add(down);
|
||
AddText(stepper, value, "step-value");
|
||
Button up = Action("+", plus, "step", id + "-plus");
|
||
up.tooltip = id == "fare" ? "Raise passenger fare by $50" : "Buy 10 more tons";
|
||
up.SetEnabled(canPlus);
|
||
stepper.Add(up);
|
||
}
|
||
|
||
private static VisualElement Card(VisualElement parent, string titleText, string classes = "")
|
||
{
|
||
var card = Add(parent, "card " + classes);
|
||
AddText(card, titleText, "card-title");
|
||
return card;
|
||
}
|
||
|
||
private static void ForecastMetric(VisualElement parent, string caption, string value, string modifier = "")
|
||
{
|
||
var field = Add(parent, "forecast-metric");
|
||
AddText(field, caption, "eyebrow");
|
||
AddText(field, value, "forecast-value " + modifier);
|
||
}
|
||
|
||
private static void AddLine(VisualElement parent, string description, string value, string modifier = "")
|
||
{
|
||
var measure = Add(parent, "measure");
|
||
AddText(measure, description, "measure-label");
|
||
AddText(measure, value, "measure-value " + modifier);
|
||
}
|
||
}
|
||
}
|