Build Soundings cruise management simulator
This commit is contained in:
442
Assets/Soundings/Runtime/SoundingsApp.cs
Normal file
442
Assets/Soundings/Runtime/SoundingsApp.cs
Normal file
@@ -0,0 +1,442 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace Soundings
|
||||
{
|
||||
public sealed partial class SoundingsApp : MonoBehaviour
|
||||
{
|
||||
private const string SaveKey = "soundings.company.v1";
|
||||
private static readonly string[] Pages = { "Operations", "Deck plans", "Shipyard", "Ports", "Finance" };
|
||||
private Company company;
|
||||
private Harbor harbor;
|
||||
private PanelSettings panel;
|
||||
private VisualElement root;
|
||||
private Font displayFont;
|
||||
private ScrollView pageScroll;
|
||||
private bool title = true;
|
||||
private bool? pendingStart;
|
||||
private int page, renderedPage = -1, deckIndex, destination = -1, bunkerTons, yardSelection;
|
||||
private Facility tool = Facility.Cabin;
|
||||
private string shipName = "Peregrine";
|
||||
private string notice;
|
||||
private bool warning;
|
||||
private bool autoSmoke;
|
||||
private Ship Ship => company?.ActiveShip;
|
||||
|
||||
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
|
||||
private static void Boot()
|
||||
{
|
||||
var game = new GameObject("Soundings");
|
||||
game.AddComponent<SoundingsApp>().Initialize();
|
||||
}
|
||||
|
||||
private void Initialize()
|
||||
{
|
||||
Culture();
|
||||
autoSmoke = Array.IndexOf(Environment.GetCommandLineArgs(), "-soundings-smoke") >= 0;
|
||||
Application.targetFrameRate = 60;
|
||||
harbor = gameObject.AddComponent<Harbor>();
|
||||
harbor.Initialize();
|
||||
displayFont = Resources.Load<Font>("Display");
|
||||
panel = ScriptableObject.CreateInstance<PanelSettings>();
|
||||
panel.scaleMode = PanelScaleMode.ScaleWithScreenSize;
|
||||
panel.referenceResolution = new Vector2Int(1440, 810);
|
||||
panel.screenMatchMode = PanelScreenMatchMode.MatchWidthOrHeight;
|
||||
panel.match = 0;
|
||||
panel.themeStyleSheet = Resources.Load<ThemeStyleSheet>("SoundingsTheme");
|
||||
var document = gameObject.AddComponent<UIDocument>();
|
||||
document.panelSettings = panel;
|
||||
root = document.rootVisualElement;
|
||||
root.style.flexGrow = 1;
|
||||
root.styleSheets.Add(Resources.Load<StyleSheet>("Soundings"));
|
||||
company = autoSmoke ? null : Load();
|
||||
Render();
|
||||
if (autoSmoke)
|
||||
StartCoroutine(VisualSmoke());
|
||||
}
|
||||
|
||||
private static void Culture()
|
||||
{
|
||||
System.Globalization.CultureInfo.CurrentCulture = System.Globalization.CultureInfo.GetCultureInfo("en-US");
|
||||
}
|
||||
|
||||
private Company Load()
|
||||
{
|
||||
if (!PlayerPrefs.HasKey(SaveKey))
|
||||
return null;
|
||||
try
|
||||
{
|
||||
Company loaded = JsonUtility.FromJson<Company>(PlayerPrefs.GetString(SaveKey));
|
||||
if (loaded != null && loaded.CanLoad())
|
||||
return loaded;
|
||||
}
|
||||
catch (ArgumentException exception)
|
||||
{
|
||||
Debug.LogWarning("Could not read the company save. " + exception.Message);
|
||||
}
|
||||
notice = "The company save could not be read.";
|
||||
warning = true;
|
||||
return null;
|
||||
}
|
||||
|
||||
private void Save()
|
||||
{
|
||||
if (autoSmoke)
|
||||
return;
|
||||
PlayerPrefs.SetString(SaveKey, JsonUtility.ToJson(company));
|
||||
PlayerPrefs.Save();
|
||||
}
|
||||
|
||||
private void StartCompany(bool financed)
|
||||
{
|
||||
company = Company.Start(financed);
|
||||
title = false;
|
||||
page = financed ? 2 : 0;
|
||||
yardSelection = 1;
|
||||
deckIndex = 0;
|
||||
inspectedPort = 1;
|
||||
selectedVoyage = 0;
|
||||
worldChart = false;
|
||||
tool = Facility.Cabin;
|
||||
shipName = "Peregrine";
|
||||
pendingStart = null;
|
||||
ResetItinerary();
|
||||
Notify(financed ? "Choose a hull in the shipyard to begin." : "Meridian is fueled for its first sailing to Nassau.");
|
||||
Save();
|
||||
Render();
|
||||
}
|
||||
|
||||
private void ResetItinerary()
|
||||
{
|
||||
destination = -1;
|
||||
bunkerTons = 0;
|
||||
if (Ship == null)
|
||||
return;
|
||||
foreach (Lane lane in Market.Lanes)
|
||||
{
|
||||
int other = lane.Other(Ship.Port);
|
||||
if (other >= 0 && company.Permits[other] && Ship.HullIndex >= Market.Ports[other].MinimumHull)
|
||||
{
|
||||
destination = other;
|
||||
break;
|
||||
}
|
||||
}
|
||||
SetRecommendedFuel();
|
||||
}
|
||||
|
||||
private void SetRecommendedFuel()
|
||||
{
|
||||
if (Ship == null)
|
||||
return;
|
||||
Voyage quote = company.Forecast(Ship, destination);
|
||||
int preferred = Mathf.Max(10, (quote?.RequiredFuel ?? 10) - Ship.Fuel);
|
||||
bunkerTons = Mathf.Clamp(preferred, 0, Ship.Tank - Ship.Fuel);
|
||||
}
|
||||
|
||||
private void ChangePage(int next)
|
||||
{
|
||||
page = next;
|
||||
if (destination < 0)
|
||||
ResetItinerary();
|
||||
Notify(null);
|
||||
Render();
|
||||
}
|
||||
|
||||
private void Notify(string message, bool failed = false)
|
||||
{
|
||||
notice = message;
|
||||
warning = failed;
|
||||
}
|
||||
|
||||
private void Apply(string failure, string success)
|
||||
{
|
||||
Notify(failure ?? success, failure != null);
|
||||
if (failure == null)
|
||||
Save();
|
||||
Render();
|
||||
}
|
||||
|
||||
private void Render()
|
||||
{
|
||||
string focus = (root.focusController?.focusedElement as VisualElement)?.name;
|
||||
float scrollOffset = page == renderedPage ? pageScroll?.verticalScroller.value ?? 0 : 0;
|
||||
root.Clear();
|
||||
bool showingVessel = !title && Ship != null && (page == 0 || page == 1);
|
||||
harbor.SetVisible(showingVessel);
|
||||
if (showingVessel)
|
||||
harbor.Show(Ship);
|
||||
if (title || company == null)
|
||||
{
|
||||
pageScroll = null;
|
||||
renderedPage = -1;
|
||||
DrawTitle();
|
||||
}
|
||||
else
|
||||
{
|
||||
renderedPage = page;
|
||||
DrawShell();
|
||||
}
|
||||
|
||||
if (pageScroll != null && scrollOffset > 0)
|
||||
{
|
||||
ScrollView scroll = pageScroll;
|
||||
scroll.schedule.Execute(() => scroll.verticalScroller.value = scrollOffset);
|
||||
}
|
||||
if (!string.IsNullOrEmpty(focus))
|
||||
root.schedule.Execute(() => root.Q<VisualElement>(focus)?.Focus());
|
||||
}
|
||||
|
||||
private void DrawTitle()
|
||||
{
|
||||
var screen = Add(root, "title-screen");
|
||||
var intro = Add(screen, "title-intro");
|
||||
var crest = Add(intro, "brand crest");
|
||||
AddMark(crest);
|
||||
AddText(intro, "CRUISE LINE SIMULATOR", "eyebrow");
|
||||
AddDisplay(intro, "Soundings", "title-display");
|
||||
AddText(intro, "From a single ship to a world-spanning cruise line.", "title-copy");
|
||||
|
||||
if (pendingStart.HasValue)
|
||||
{
|
||||
AddText(intro, "Replace your saved company?", "card-title");
|
||||
AddText(intro, "Starting a new company overwrites your current fleet and voyage history.", "body-copy");
|
||||
intro.Add(Action("Replace and start", () => StartCompany(pendingStart.Value), "primary title-action", "replace"));
|
||||
intro.Add(Action("Keep current company", () => { pendingStart = null; Render(); }, "title-action", "keep"));
|
||||
}
|
||||
else
|
||||
{
|
||||
if (company != null)
|
||||
intro.Add(Action("Resume company →", () =>
|
||||
{
|
||||
title = false;
|
||||
ResetItinerary();
|
||||
Notify(null);
|
||||
Render();
|
||||
}, "primary title-action", "resume"));
|
||||
StartOption(intro, "Take over a regional ship", "Used coastal ship · $135,000 cash · No debt", false);
|
||||
StartOption(intro, "Finance a new fleet", "$580,000 cash · $500,000 loan · Empty fleet", true);
|
||||
}
|
||||
|
||||
AddNotice(intro);
|
||||
var picture = Add(screen, "title-visual");
|
||||
var artwork = new Image
|
||||
{
|
||||
image = Resources.Load<Texture2D>("Art/FlagshipTitle"),
|
||||
scaleMode = ScaleMode.ScaleAndCrop,
|
||||
pickingMode = PickingMode.Ignore
|
||||
};
|
||||
artwork.AddToClassList("harbor-image");
|
||||
picture.Add(artwork);
|
||||
var location = Add(picture, "splash-location");
|
||||
AddText(location, "OCEAN CLASS", "eyebrow on-image");
|
||||
AddText(location, "FLAGSHIP CONCEPT", "tag on-image");
|
||||
}
|
||||
|
||||
private void StartOption(VisualElement parent, string titleText, string description, bool financed)
|
||||
{
|
||||
Button option = Action("", () =>
|
||||
{
|
||||
if (company == null)
|
||||
StartCompany(financed);
|
||||
else
|
||||
{
|
||||
pendingStart = financed;
|
||||
Render();
|
||||
}
|
||||
}, "start-option", financed ? "start-financed" : "start-used");
|
||||
AddText(option, titleText, "option-title");
|
||||
AddText(option, description, "muted");
|
||||
parent.Add(option);
|
||||
}
|
||||
|
||||
private void DrawShell()
|
||||
{
|
||||
var shell = Add(root, "shell");
|
||||
var mast = Add(shell, "mast");
|
||||
var brand = Add(mast, "brand mast-brand");
|
||||
AddMark(brand);
|
||||
AddText(brand, "Soundings", "brand-type");
|
||||
AddText(brand, "CRUISE LINE SIMULATOR", "mast-sub");
|
||||
var metrics = Add(mast, "mast-metrics");
|
||||
HeadMetric(metrics, "CASH", Money(company.Cash), "gold");
|
||||
HeadMetric(metrics, "REPUTATION", $"{company.Reputation} / 100");
|
||||
HeadMetric(metrics, "DAY", company.Day.ToString("00"));
|
||||
mast.Add(Action("Company menu", () => { title = true; Notify(null); Render(); }, "menu-button", "company-menu"));
|
||||
|
||||
var body = Add(shell, "body");
|
||||
var rail = Add(body, "rail");
|
||||
AddText(rail, "MANAGEMENT", "rail-heading eyebrow");
|
||||
for (int i = 0; i < Pages.Length; i++)
|
||||
{
|
||||
int target = i;
|
||||
Button nav = Action("", () => ChangePage(target), page == i ? "nav current" : "nav", "nav-" + i);
|
||||
AddText(nav, Pages[i], "nav-name");
|
||||
rail.Add(nav);
|
||||
}
|
||||
|
||||
AddText(rail, $"FLEET / {company.Fleet.Count:00}", "rail-heading fleet-heading eyebrow");
|
||||
var fleet = new ScrollView(ScrollViewMode.Vertical) { verticalScrollerVisibility = ScrollerVisibility.Auto };
|
||||
fleet.AddToClassList("fleet-scroll");
|
||||
rail.Add(fleet);
|
||||
for (int i = 0; i < company.Fleet.Count; i++)
|
||||
{
|
||||
int selected = i;
|
||||
Ship ship = company.Fleet[i];
|
||||
Button entry = Action("", () =>
|
||||
{
|
||||
company.SelectedShip = selected;
|
||||
deckIndex = 0;
|
||||
ResetItinerary();
|
||||
Apply(null, null);
|
||||
}, i == company.SelectedShip ? "ship-item selected" : "ship-item", "fleet-" + i);
|
||||
AddText(entry, ship.Name, "ship-name");
|
||||
AddText(entry, Market.Ports[ship.Port].Name + " · " + ship.Hull.Name, "ship-location");
|
||||
fleet.Add(entry);
|
||||
}
|
||||
if (company.Fleet.Count == 0)
|
||||
AddText(fleet, "No ships yet", "fleet-empty");
|
||||
|
||||
var goal = Add(rail, "rail-goal");
|
||||
AddText(goal, company.Flagship ? "MILESTONE REACHED" : "FLAGSHIP MILESTONE", "eyebrow");
|
||||
AddText(goal, company.Flagship ? "Atlantic crossing complete" : "Bermuda → Lisbon", "goal-name");
|
||||
AddText(goal, company.Flagship ? "Continue running your fleet." : "Ocean Class · 320 berths · 160 show seats", "goal-detail");
|
||||
|
||||
pageScroll = new ScrollView(ScrollViewMode.Vertical);
|
||||
pageScroll.AddToClassList("page-scroll");
|
||||
body.Add(pageScroll);
|
||||
var canvas = Add(pageScroll, "page");
|
||||
var head = Add(canvas, "page-head");
|
||||
var heading = Add(head, "heading");
|
||||
AddDisplay(heading, Pages[page], "page-title");
|
||||
AddText(head, page == 0 && Ship != null ? Market.Ports[Ship.Port].Region.ToUpperInvariant()
|
||||
: $"{company.Voyages:00} VOYAGES / GLOBAL NETWORK", "page-meta eyebrow");
|
||||
AddNotice(canvas);
|
||||
switch (page)
|
||||
{
|
||||
case 0: DrawOperations(canvas); break;
|
||||
case 1: DrawDecks(canvas); break;
|
||||
case 2: DrawShipyard(canvas); break;
|
||||
case 3: DrawPorts(canvas); break;
|
||||
case 4: DrawFinance(canvas); break;
|
||||
}
|
||||
}
|
||||
|
||||
private void AddHarbor(VisualElement holder)
|
||||
{
|
||||
var view = new Image { scaleMode = ScaleMode.StretchToFill, pickingMode = PickingMode.Ignore };
|
||||
view.AddToClassList("harbor-image");
|
||||
holder.Add(view);
|
||||
holder.RegisterCallback<GeometryChangedEvent>(evt =>
|
||||
{
|
||||
if (evt.newRect.width < 32 || evt.newRect.height < 32)
|
||||
return;
|
||||
float panelWidth = root.resolvedStyle.width;
|
||||
float scale = panelWidth > 0 ? Screen.width / panelWidth : 1;
|
||||
if (Application.platform != RuntimePlatform.WebGLPlayer)
|
||||
scale *= Mathf.Clamp(1250 / (evt.newRect.width * scale), 1, 2);
|
||||
int width = Mathf.Clamp(Mathf.RoundToInt(evt.newRect.width * scale), 32, SystemInfo.maxTextureSize);
|
||||
int height = Mathf.Clamp(Mathf.RoundToInt(evt.newRect.height * scale), 32, SystemInfo.maxTextureSize);
|
||||
harbor.Resize(width, height);
|
||||
view.image = harbor.Frame;
|
||||
});
|
||||
if (harbor.Frame != null)
|
||||
view.image = harbor.Frame;
|
||||
}
|
||||
|
||||
private void AddNotice(VisualElement parent)
|
||||
{
|
||||
if (string.IsNullOrEmpty(notice))
|
||||
return;
|
||||
Label banner = AddText(parent, notice, warning ? "notice warning" : "notice");
|
||||
banner.SetEnabled(true);
|
||||
}
|
||||
|
||||
private static void AddMark(VisualElement parent)
|
||||
{
|
||||
var mark = Add(parent, "brand-mark");
|
||||
mark.generateVisualContent += context =>
|
||||
{
|
||||
Painter2D painter = context.painter2D;
|
||||
painter.lineWidth = 1.35f;
|
||||
painter.strokeColor = new Color(.91f, .74f, .48f);
|
||||
painter.BeginPath();
|
||||
painter.MoveTo(new Vector2(16, 2));
|
||||
painter.LineTo(new Vector2(29, 10));
|
||||
painter.LineTo(new Vector2(29, 24));
|
||||
painter.LineTo(new Vector2(16, 32));
|
||||
painter.LineTo(new Vector2(3, 24));
|
||||
painter.LineTo(new Vector2(3, 10));
|
||||
painter.ClosePath();
|
||||
painter.Stroke();
|
||||
painter.BeginPath();
|
||||
painter.MoveTo(new Vector2(8, 20));
|
||||
painter.LineTo(new Vector2(13, 15));
|
||||
painter.LineTo(new Vector2(16, 19));
|
||||
painter.LineTo(new Vector2(21, 13));
|
||||
painter.LineTo(new Vector2(24, 17));
|
||||
painter.MoveTo(new Vector2(8, 24));
|
||||
painter.LineTo(new Vector2(24, 24));
|
||||
painter.Stroke();
|
||||
};
|
||||
}
|
||||
|
||||
private static void HeadMetric(VisualElement parent, string label, string value, string modifier = "")
|
||||
{
|
||||
var metric = Add(parent, "head-metric");
|
||||
AddText(metric, label, "eyebrow");
|
||||
AddText(metric, value, "head-value " + modifier);
|
||||
}
|
||||
|
||||
private static void Classes(VisualElement element, string classes)
|
||||
{
|
||||
foreach (string item in classes.Split(' '))
|
||||
if (item.Length != 0)
|
||||
element.AddToClassList(item);
|
||||
}
|
||||
|
||||
private static VisualElement Add(VisualElement parent, string classes)
|
||||
{
|
||||
var element = new VisualElement();
|
||||
Classes(element, classes);
|
||||
parent.Add(element);
|
||||
return element;
|
||||
}
|
||||
|
||||
private static Label Text(string value, string classes)
|
||||
{
|
||||
var label = new Label(value) { pickingMode = PickingMode.Ignore };
|
||||
Classes(label, classes);
|
||||
return label;
|
||||
}
|
||||
|
||||
private static Label AddText(VisualElement parent, string value, string classes)
|
||||
{
|
||||
var label = Text(value, classes);
|
||||
parent.Add(label);
|
||||
return label;
|
||||
}
|
||||
|
||||
private Label AddDisplay(VisualElement parent, string value, string classes)
|
||||
{
|
||||
Label label = AddText(parent, value, classes);
|
||||
label.style.unityFontDefinition = FontDefinition.FromFont(displayFont);
|
||||
return label;
|
||||
}
|
||||
|
||||
private static Button Action(string text, Action clicked, string classes = "", string id = null)
|
||||
{
|
||||
var button = new Button(clicked) { text = text, name = id ?? text };
|
||||
Classes(button, "action " + classes);
|
||||
return button;
|
||||
}
|
||||
|
||||
private static string Money(long amount) => amount < 0 ? "−" + (-amount).ToString("C0") : amount.ToString("C0");
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
if (panel != null)
|
||||
Destroy(panel);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user