263 lines
12 KiB
C#
263 lines
12 KiB
C#
using UnityEngine;
|
||
using UnityEngine.UIElements;
|
||
|
||
namespace Soundings
|
||
{
|
||
public sealed partial class SoundingsApp
|
||
{
|
||
private bool ledgerAccounts;
|
||
private float ledgerRecordOffset;
|
||
|
||
private void DrawFinance(VisualElement pageRoot)
|
||
{
|
||
var workspace = Add(pageRoot, "log-workspace");
|
||
var reader = new ScrollView(ScrollViewMode.Vertical)
|
||
{
|
||
verticalScrollerVisibility = ScrollerVisibility.Auto
|
||
};
|
||
reader.AddToClassList("log-reader");
|
||
workspace.Add(reader);
|
||
|
||
var folio = Add(reader, "log-folio");
|
||
var head = Add(folio, "log-heading");
|
||
AddDisplay(head, "Captain’s log", "log-title");
|
||
var tabs = Add(head, "log-tabs");
|
||
AddLogTab(tabs, "Voyage records", false, "log-voyages");
|
||
AddLogTab(tabs, "Company accounts", true, "log-accounts");
|
||
|
||
var binding = Add(folio, "log-binding");
|
||
var sheets = Add(binding, "log-sheets");
|
||
var left = Add(sheets, "log-leaf log-left-leaf");
|
||
var spine = Add(sheets, "log-spine");
|
||
spine.pickingMode = PickingMode.Ignore;
|
||
var right = Add(sheets, "log-leaf log-right-leaf");
|
||
|
||
if (ledgerAccounts)
|
||
DrawAccounts(left, right);
|
||
else
|
||
DrawVoyageRecords(left, right);
|
||
}
|
||
|
||
private void AddLogTab(VisualElement parent, string label, bool accounts, string id)
|
||
{
|
||
bool selected = ledgerAccounts == accounts;
|
||
Button button = Action(label, () =>
|
||
{
|
||
if (ledgerAccounts == accounts)
|
||
return;
|
||
ledgerAccounts = accounts;
|
||
Render();
|
||
}, selected ? "log-tab log-tab-active" : "log-tab", id);
|
||
parent.Add(button);
|
||
}
|
||
|
||
private void DrawVoyageRecords(VisualElement register, VisualElement entry)
|
||
{
|
||
AddText(register, "VOYAGE REGISTER", "log-small-title");
|
||
AddDisplay(register, "Passages", "log-sheet-title");
|
||
AddText(register, $"{company.Voyages:N0} completed", "log-secondary");
|
||
AddText(register, company.Flagship
|
||
? "Flagship charter complete · Bermuda to Lisbon"
|
||
: "Flagship charter · Bermuda to Lisbon",
|
||
"log-note " + (company.Flagship ? "log-positive" : "log-charter"));
|
||
if (!company.Flagship)
|
||
AddText(register, "Ocean Class hull · 320 berths · 160 show seats", "log-charter-terms");
|
||
|
||
if (company.History.Count == 0)
|
||
{
|
||
DrawEmptyLog(register, entry);
|
||
return;
|
||
}
|
||
|
||
var tableHead = Add(register, "log-index-heading");
|
||
AddText(tableHead, "DAY", "log-index-day");
|
||
AddText(tableHead, "PASSAGE", "log-index-passage");
|
||
AddText(tableHead, "NET PROFIT", "log-index-amount");
|
||
|
||
selectedVoyage = Mathf.Clamp(selectedVoyage, 0, company.History.Count - 1);
|
||
var recordList = new ScrollView(ScrollViewMode.Vertical) { verticalScrollerVisibility = ScrollerVisibility.Auto };
|
||
recordList.AddToClassList("log-index");
|
||
register.Add(recordList);
|
||
for (int i = 0; i < company.History.Count; i++)
|
||
{
|
||
int record = i;
|
||
Voyage trip = company.History[i];
|
||
string route = $"{Market.Ports[trip.Origin].Name} to {Market.Ports[trip.Destination].Name}";
|
||
Button item = Action("", () =>
|
||
{
|
||
ledgerRecordOffset = recordList.verticalScroller.value;
|
||
selectedVoyage = record;
|
||
Render();
|
||
}, i == selectedVoyage ? "log-entry log-entry-active" : "log-entry", "voyage-" + i);
|
||
item.tooltip = route + " aboard " + trip.ShipName;
|
||
AddText(item, trip.Day.ToString(), "log-index-day");
|
||
var line = Add(item, "log-index-passage");
|
||
AddText(line, route, "log-entry-route");
|
||
AddText(line, trip.ShipName, "log-entry-ship");
|
||
AddText(item, Money(trip.NetIncome), "log-index-amount " + LogAmountColor(trip.NetIncome));
|
||
recordList.Add(item);
|
||
}
|
||
|
||
recordList.verticalScroller.valueChanged += offset => ledgerRecordOffset = offset;
|
||
if (ledgerRecordOffset > 0)
|
||
{
|
||
float restore = ledgerRecordOffset;
|
||
recordList.schedule.Execute(() => recordList.scrollOffset = new Vector2(0, restore));
|
||
}
|
||
if (company.Voyages > company.History.Count)
|
||
AddText(register, $"Showing the latest {company.History.Count} voyages", "log-note");
|
||
|
||
DrawVoyageEntry(entry, company.History[selectedVoyage]);
|
||
}
|
||
|
||
private void DrawEmptyLog(VisualElement register, VisualElement entry)
|
||
{
|
||
var ruling = Add(register, "log-unused-lines");
|
||
ruling.pickingMode = PickingMode.Ignore;
|
||
ruling.generateVisualContent += DrawLedgerRuling;
|
||
|
||
var empty = Add(entry, "log-empty");
|
||
var compass = Add(empty, "log-compass");
|
||
compass.pickingMode = PickingMode.Ignore;
|
||
compass.generateVisualContent += DrawLedgerCompass;
|
||
AddDisplay(empty, "Awaiting first arrival", "log-sheet-title log-centered");
|
||
AddText(empty, "Complete a sailing to record its passengers, costs and earnings.", "log-note log-centered");
|
||
if (Ship != null)
|
||
empty.Add(Action("Set course", OpenVoyageChart, "log-paper-action", "log-set-course"));
|
||
else
|
||
empty.Add(Action("Visit shipyard", () => ChangePage(2), "log-paper-action", "log-visit-yard"));
|
||
}
|
||
|
||
private void DrawVoyageEntry(VisualElement leaf, Voyage trip)
|
||
{
|
||
AddText(leaf, $"ARRIVED ON DAY {trip.Day}", "log-small-title");
|
||
string origin = Market.Ports[trip.Origin].Name;
|
||
string destinationName = Market.Ports[trip.Destination].Name;
|
||
AddDisplay(leaf, $"{origin} to {destinationName}", "log-sheet-title log-route-title");
|
||
AddText(leaf, $"{trip.ShipName} · {trip.Miles:N0} nm over {trip.Days} days", "log-secondary");
|
||
AddText(leaf, $"{trip.Guests:N0} passengers · {trip.FuelBurn:N0} t of fuel consumed", "log-secondary");
|
||
|
||
var statement = Add(leaf, "log-statement");
|
||
LogLine(statement, "Ticket revenue", Money(trip.TicketSales));
|
||
LogLine(statement, "Onboard revenue", Money(trip.OnboardSales));
|
||
LogLine(statement, "Crew and catering", Money(-trip.Crew - trip.Catering));
|
||
LogLine(statement, "Port dues", Money(-trip.PortFees));
|
||
LogLine(statement, "Fuel expense", Money(-trip.FuelExpense));
|
||
LogLine(statement, "Loan interest", Money(-trip.LoanInterest));
|
||
LogLine(statement, "Principal repaid", Money(-trip.Principal));
|
||
|
||
var balances = Add(leaf, "log-totals");
|
||
LogLine(balances, "Net voyage profit", Money(trip.NetIncome), LogAmountColor(trip.NetIncome));
|
||
LogLine(balances, "Cash change on arrival", Money(trip.CashChange), LogAmountColor(trip.CashChange));
|
||
}
|
||
|
||
private void DrawAccounts(VisualElement left, VisualElement right)
|
||
{
|
||
AddText(left, $"COMPANY POSITION / DAY {company.Day}", "log-small-title");
|
||
AddDisplay(left, "Balances", "log-sheet-title");
|
||
|
||
var balances = Add(left, "log-statement log-account-statement");
|
||
LogLine(balances, "Cash in reserve", Money(company.Cash), "log-positive");
|
||
LogLine(balances, "Outstanding debt", Money(company.Debt));
|
||
LogLine(balances, "Lifetime voyage profit", Money(company.LifetimeProfit), LogAmountColor(company.LifetimeProfit));
|
||
LogLine(balances, "Completed voyages", company.Voyages.ToString("N0"));
|
||
AddText(left, "Voyage profit includes operating expenses and interest. Hull purchases and port permits are tracked as company outlays.", "log-note log-account-note");
|
||
|
||
AddText(right, "CREDIT ACCOUNT", "log-small-title");
|
||
AddDisplay(right, "Financing", "log-sheet-title");
|
||
|
||
var terms = Add(right, "log-statement log-account-statement");
|
||
if (company.Debt > 0)
|
||
{
|
||
LogLine(terms, "Interest rate", "13% APR");
|
||
LogLine(terms, "Principal per sailing day", Money(850));
|
||
|
||
int payment = Mathf.Min(company.Debt, 25000);
|
||
Button pay = Action($"Repay {Money(payment)} in principal",
|
||
() => Apply(company.PayDownDebt(), "Principal payment posted."), "log-paper-action", "pay-debt");
|
||
pay.SetEnabled(company.Cash >= payment);
|
||
right.Add(pay);
|
||
if (!pay.enabledSelf)
|
||
AddText(right, $"You need {Money(payment)} in cash to make this payment.", "log-note");
|
||
}
|
||
else
|
||
AddText(terms, "No outstanding loan.", "log-note");
|
||
|
||
if (!company.Bridge)
|
||
return;
|
||
|
||
var bridge = Add(right, "log-bridge");
|
||
AddText(bridge, "BRIDGE CREDIT", "log-small-title");
|
||
Button draw = Action("Draw $125,000",
|
||
() => Apply(company.DrawBridge(), "Bridge loan posted."), "log-paper-action", "draw-bridge");
|
||
draw.SetEnabled(company.CanDrawBridge);
|
||
bridge.Add(draw);
|
||
|
||
if (!company.CanDrawBridge)
|
||
AddText(bridge, "Available when cash reserves fall below $100,000.", "log-note");
|
||
}
|
||
|
||
private static void LogLine(VisualElement parent, string label, string value, string color = "")
|
||
{
|
||
var row = Add(parent, "log-line");
|
||
AddText(row, label, "log-line-description");
|
||
AddText(row, value, "log-line-value " + color);
|
||
}
|
||
|
||
private static string LogAmountColor(long amount) => amount >= 0 ? "log-positive" : "log-negative";
|
||
|
||
private static void DrawLedgerRuling(MeshGenerationContext context)
|
||
{
|
||
Rect area = context.visualElement.contentRect;
|
||
Painter2D rule = context.painter2D;
|
||
rule.strokeColor = new Color(.44f, .37f, .28f, .24f);
|
||
rule.lineWidth = 1;
|
||
rule.BeginPath();
|
||
for (int y = 23; y < area.height; y += 24)
|
||
{
|
||
rule.MoveTo(new Vector2(0, y));
|
||
rule.LineTo(new Vector2(area.width, y));
|
||
}
|
||
rule.Stroke();
|
||
}
|
||
|
||
private static void DrawLedgerCompass(MeshGenerationContext context)
|
||
{
|
||
Vector2 center = context.visualElement.contentRect.center;
|
||
Painter2D pen = context.painter2D;
|
||
pen.strokeColor = new Color(.45f, .34f, .22f, .56f);
|
||
pen.lineWidth = 1;
|
||
pen.BeginPath();
|
||
for (int i = 0; i < 32; i++)
|
||
{
|
||
float angle = i * Mathf.PI / 16;
|
||
Vector2 point = center + new Vector2(Mathf.Cos(angle), Mathf.Sin(angle)) * 40;
|
||
if (i == 0)
|
||
pen.MoveTo(point);
|
||
else
|
||
pen.LineTo(point);
|
||
}
|
||
pen.ClosePath();
|
||
pen.Stroke();
|
||
|
||
const float arm = 48;
|
||
const float shoulder = 9;
|
||
pen.BeginPath();
|
||
pen.MoveTo(center + Vector2.up * arm);
|
||
pen.LineTo(center + new Vector2(shoulder, shoulder));
|
||
pen.LineTo(center + Vector2.right * arm);
|
||
pen.LineTo(center + new Vector2(shoulder, -shoulder));
|
||
pen.LineTo(center + Vector2.down * arm);
|
||
pen.LineTo(center + new Vector2(-shoulder, -shoulder));
|
||
pen.LineTo(center + Vector2.left * arm);
|
||
pen.LineTo(center + new Vector2(-shoulder, shoulder));
|
||
pen.ClosePath();
|
||
pen.MoveTo(center + Vector2.up * arm);
|
||
pen.LineTo(center + Vector2.down * arm);
|
||
pen.MoveTo(center + Vector2.left * arm);
|
||
pen.LineTo(center + Vector2.right * arm);
|
||
pen.Stroke();
|
||
}
|
||
}
|
||
}
|