using System.Collections.Generic; using UnityEngine; namespace Soundings { internal sealed class VesselExterior { internal const float DeckBase = 5.38f, DeckSpacing = 2.62f; internal enum Paint { Navy, Ivory, Pearl, Gold, Teak, TeakSeam, Steel, Black, Glass, BlueGlass, LitGlass, Lounge, Lifeboat, Pool } private readonly Dictionary batches = new Dictionary(); private readonly Ship ship; private readonly float length, beam; private readonly int balconies; public VesselExterior(Ship ship, float length, float beam) { this.ship = ship; this.length = length; this.beam = beam; balconies = 21 + ship.HullIndex * 6; } public IReadOnlyDictionary Meshes() { BuildHull(); for (int deck = 0; deck < ship.Decks.Count; deck++) BuildPassengerDeck(deck); BuildBridgeAndRoof(); BuildLifeboats(); return batches; } private HarborMesh Coat(Paint paint) { if (!batches.TryGetValue(paint, out HarborMesh batch)) batches.Add(paint, batch = new HarborMesh()); return batch; } private void BuildHull() { Vector2[] outline = Outline(length, beam, 0); Vector2 center = Vector2.zero; Coat(Paint.Navy).Loft(outline, center, false, new Vector3(.88f, -2.65f, .55f), new Vector3(.967f, -.83f, .87f), new Vector3(.99f, .28f, .97f), new Vector3(1, 2.34f, 1), new Vector3(1.01f, 4.65f, 1.03f)); Coat(Paint.Gold).Loft(outline, center, false, new Vector3(1.013f, 3.54f, 1.025f), new Vector3(1.013f, 3.69f, 1.026f)); Coat(Paint.Ivory).Loft(outline, center, true, new Vector3(.998f, 4.47f, 1.015f), new Vector3(1.013f, 5.14f, 1.027f), new Vector3(1.003f, 5.32f, 1.015f)); Vector2[] promenade = Outline(length * .971f, beam * .979f, 0); Coat(Paint.Teak).Loft(promenade, center, true, new Vector3(.997f, 5.32f, 1), new Vector3(1, DeckBase, 1)); Rails(promenade, 5.39f, 5.99f, 2.7f); for (int side = -1; side <= 1; side += 2) { for (int i = 0; i < 24; i++) { float x = -length * .40f + i * length * .0285f; float z = side * beam * 1.024f * SideWidth(x / length + .50f); Coat(Paint.Black).Box(new Vector3(x, 2.7f, z), new Vector3(.46f, .56f, .18f)); Paint pane = (i + 3 * side) % 5 == 0 ? Paint.LitGlass : Paint.BlueGlass; Coat(pane).Box(new Vector3(x, 2.72f, z + side * .09f), new Vector3(.31f, .38f, .09f)); } } float fore = length * .397f; Coat(Paint.Steel).Rod(new Vector3(fore, 5.35f, 0), new Vector3(fore, 7.24f, 0), .13f); Coat(Paint.Gold).Rod(new Vector3(fore, 6.48f, -.82f), new Vector3(fore, 6.48f, .82f), .048f); for (int side = -1; side <= 1; side += 2) for (int i = 0; i < 3; i++) { float x = length * (.32f + i * .032f); float z = side * beam * (.52f - i * .135f); Coat(Paint.Black).Rod(new Vector3(x, 5.37f, z), new Vector3(x, 5.74f, z), .15f); Coat(Paint.Steel).Box(new Vector3(x, 5.73f, z), new Vector3(.50f, .085f, .32f)); } } private void BuildPassengerDeck(int deck) { float y = DeckBase + deck * DeckSpacing; float roof = y + DeckSpacing; var (deckLength, deckBeam, offset) = DeckFootprint(deck); Vector2 center = new Vector2(offset, 0); Vector2[] outline = Outline(deckLength, deckBeam, offset); Coat(Paint.Ivory).Loft(outline, center, false, new Vector3(.97f, y, .77f), new Vector3(.99f, y + .25f, .83f), new Vector3(.99f, roof - .39f, .83f)); Coat(Paint.Pearl).Loft(outline, center, true, new Vector3(.993f, roof - .39f, .96f), new Vector3(1.017f, roof - .31f, 1.01f), new Vector3(1.017f, roof - .15f, 1.015f), new Vector3(1, roof - .06f, 1)); Coat(Paint.Teak).Loft(outline, center, true, new Vector3(.992f, roof - .06f, .989f), new Vector3(.994f, roof, .992f)); Rails(outline, roof, roof + .73f, 2.4f); float width = .70f * deckLength / balconies; for (int i = 0; i < balconies; i++) { float x = offset + Mathf.Lerp(-.39f, .31f, (float)i / (balconies - 1)) * deckLength; float cross = SideWidth((x - offset) / deckLength + .5f); int column = i * ship.Hull.Columns / balconies; for (int side = -1; side <= 1; side += 2) { int row = side < 0 ? 0 : ship.Hull.Rows - 1; Facility facility = ship.Decks[deck].Cells[row * ship.Hull.Columns + column]; float z = side * deckBeam * cross; Vector3 glass = new Vector3(x, y + 1.27f, z * .846f); Coat(Paint.Black).Box(glass, new Vector3(width * 1.01f, 1.80f, .17f)); float divider = width * .21f; Paint illumination = facility switch { Facility.Dining => Paint.LitGlass, Facility.Entertainment => Paint.Lounge, Facility.Cabin when (i + deck * 5 + row * 3) % 7 < 4 => Paint.LitGlass, Facility.Tank => Paint.Black, _ => Paint.Glass }; for (int pane = -1; pane <= 1; pane += 2) Coat(pane < 0 ? illumination : Paint.BlueGlass).Box(glass + new Vector3(pane * divider, .025f, side * .09f), new Vector3(width * .38f, 1.54f, .09f)); if (facility == Facility.Tank) continue; Coat(Paint.Teak).Box(new Vector3(x, y + .28f, z * .935f), new Vector3(width * 1.10f, .12f, Mathf.Abs(z) * .19f)); Coat(Paint.BlueGlass).Box(new Vector3(x, y + .63f, z * 1.016f), new Vector3(width * .96f, .48f, .05f)); Coat(Paint.Pearl).Box(new Vector3(x, y + .92f, z * 1.02f), new Vector3(width * 1.045f, .047f, .055f)); Coat(Paint.Pearl).Box(new Vector3(x + width * .52f, y + .65f, z * 1.02f), new Vector3(.055f, .52f, .075f)); } } } private void BuildBridgeAndRoof() { int deck = ship.Decks.Count - 1; var (deckLength, deckBeam, offset) = DeckFootprint(deck); float top = DeckBase + ship.Decks.Count * DeckSpacing; float bridge = BridgeCenter(length, ship.Decks.Count); Vector2 center = new Vector2(bridge, 0); Vector2[] capsule = Outline(9.15f, beam * .67f, bridge); Coat(Paint.Ivory).Loft(capsule, center, false, new Vector3(.91f, top, .91f), new Vector3(1, top + .88f, 1)); Coat(Paint.Glass).Loft(capsule, center, false, new Vector3(1, top + .88f, 1), new Vector3(.90f, top + 2.62f, .95f)); Coat(Paint.Pearl).Loft(capsule, center, true, new Vector3(.905f, top + 2.59f, .955f), new Vector3(.975f, top + 2.72f, 1.025f), new Vector3(.82f, top + 2.91f, .90f)); foreach (Vector2 point in capsule) if (point.x > bridge - .5f) Coat(Paint.Steel).Rod(HarborMesh.Point(point, top + .89f), HarborMesh.Point(center + Vector2.Scale(point - center, new Vector2(.90f, .95f)), top + 2.61f), .031f); float mast = bridge + 1.15f; Coat(Paint.Ivory).Rod(new Vector3(mast, top + 2.88f, 0), new Vector3(mast - .48f, top + 5.72f, 0), .135f, 8); for (int i = 0; i < 3; i++) { float y = top + 3.35f + i * .66f; float span = 1.98f - i * .36f; Coat(Paint.Pearl).Rod(new Vector3(mast - .27f, y, -span), new Vector3(mast - .27f, y, span), .055f); Coat(Paint.Black).Rod(new Vector3(mast - .24f, y, span * .84f), new Vector3(mast - .24f, y + .40f, span * .84f), .022f); } Coat(Paint.LitGlass).Radome(new Vector3(mast - .48f, top + 5.77f, 0), .13f); float stack = offset - deckLength * .24f; for (int side = -1; side <= 1; side += 2) { float z = side * deckBeam * .26f; Vector2 foot = new Vector2(stack, z); Vector2[] funnel = Outline(3.85f, .94f, stack, z); Coat(Paint.Ivory).Loft(funnel, foot, false, new Vector3(1.13f, top, 1.12f), new Vector3(.99f, top + .82f, .985f)); Coat(Paint.Navy).Loft(funnel, foot, false, new Vector3(.99f, top + .82f, .985f), new Vector3(.95f, top + 2.37f, .86f)); Coat(Paint.Gold).Loft(funnel, foot, false, new Vector3(.95f, top + 2.37f, .86f), new Vector3(.90f, top + 2.91f, .80f)); Coat(Paint.Black).Loft(funnel, foot, true, new Vector3(.90f, top + 2.91f, .80f), new Vector3(.87f, top + 3.23f, .77f)); Coat(Paint.Steel).Rod(new Vector3(stack + 7.8f, top, z * 2), new Vector3(stack + 7.8f, top + .59f, z * 2), .16f); Coat(Paint.Pearl).Radome(new Vector3(stack + 7.8f, top + .93f, z * 2), .49f); } float pool = offset + deckLength * .01f; Coat(Paint.Pearl).Box(new Vector3(pool, top + .075f, 0), new Vector3(5.67f, .17f, 3.32f)); Coat(Paint.Pool).Box(new Vector3(pool, top + .17f, 0), new Vector3(5.21f, .073f, 2.89f)); for (int i = 0; i < 8; i++) { Coat(Paint.TeakSeam).Box(new Vector3(pool, top + .032f, (i - 3.5f) * .69f), new Vector3(10, .014f, .032f)); float x = pool - 3.4f + i * .94f; Coat(Paint.Pearl).Box(new Vector3(x, top + .19f, deckBeam * .67f), new Vector3(.56f, .13f, .92f)); Coat(Paint.Steel).Box(new Vector3(x, top + .34f, deckBeam * .67f + .31f), new Vector3(.52f, .17f, .26f)); } } private void BuildLifeboats() { for (int i = 0; i < 5 + ship.HullIndex * 2; i++) for (int side = -1; side <= 1; side += 2) { float x = -length * .31f + i * (ship.HullIndex == 0 ? 5.52f : 4.7f); float z = side * beam * .974f; Vector2 center = new Vector2(x, z); Vector2[] capsule = Outline(4.31f, .60f, x, z); Coat(Paint.Lifeboat).Loft(capsule, center, false, new Vector3(.86f, 6.27f, .59f), new Vector3(1, 6.49f, .99f), new Vector3(1, 6.65f, 1)); Coat(Paint.Pearl).Loft(capsule, center, true, new Vector3(1, 6.65f, 1), new Vector3(.875f, 6.94f, .82f), new Vector3(.78f, 7.04f, .65f)); Coat(Paint.Glass).Box(new Vector3(x + .96f, 6.94f, z + side * .44f), new Vector3(.50f, .15f, .13f)); for (int arm = -1; arm <= 1; arm += 2) { float mount = x + arm * 1.73f; Vector3 from = new Vector3(mount, 6.15f, z - side * .67f); Vector3 elbow = new Vector3(mount, 7.47f, z - side * .52f); Vector3 tip = new Vector3(mount, 7.53f, z + side * .20f); Coat(Paint.Ivory).Rod(from, elbow, .07f); Coat(Paint.Ivory).Rod(elbow, tip, .065f); Coat(Paint.Steel).Rod(tip, new Vector3(mount, 6.94f, tip.z), .021f); } } } private void Rails(Vector2[] contour, float low, float high, float stride) { HarborMesh metal = Coat(Paint.Pearl); for (int i = 0; i < contour.Length; i++) { Vector2 a = contour[i]; Vector2 b = contour[(i + 1) % contour.Length]; metal.Rod(HarborMesh.Point(a, high), HarborMesh.Point(b, high), .031f); metal.Rod(HarborMesh.Point(a, (high + low) * .5f), HarborMesh.Point(b, (high + low) * .5f), .021f); int studs = Mathf.Max(1, Mathf.CeilToInt(Vector2.Distance(a, b) / stride)); for (int stud = 0; stud < studs; stud++) { Vector2 foot = Vector2.Lerp(a, b, stud / (float)studs); metal.Rod(HarborMesh.Point(foot, low), HarborMesh.Point(foot, high), .035f); } } } internal static float BridgeCenter(float hullLength, int deckCount) { int deck = deckCount - 1; return DeckOffset(hullLength, deck) + DeckLength(hullLength, deck) * .29f; } internal static float DeckLength(float hullLength, int deck) => hullLength * (.81f - .027f * deck); internal static float DeckOffset(float hullLength, int deck) => -hullLength * (.034f + .0065f * deck); internal static float DeckHalfBeam(float halfBeam, int deck) => halfBeam * (.85f - .024f * deck); private (float Length, float Beam, float Offset) DeckFootprint(int deck) => (DeckLength(length, deck), DeckHalfBeam(beam, deck), DeckOffset(length, deck)); private static float SideWidth(float t) { if (t < .16f) return Mathf.Lerp(.52f, 1, Mathf.Sin(Mathf.Clamp01(t / .16f) * Mathf.PI * .5f)); if (t < .69f) return 1; return Mathf.Cos(Mathf.Clamp01((t - .69f) / .31f) * Mathf.PI * .5f); } private static Vector2[] Outline(float length, float beam, float x, float z = 0) { const int intervals = 20; var outline = new Vector2[intervals * 2 + 1]; for (int i = 0; i < intervals; i++) { float position = i / (float)intervals; float width = beam * SideWidth(position); float station = (position - .5f) * length + x; outline[i] = new Vector2(station, z - width); outline[outline.Length - 1 - i] = new Vector2(station, z + width); } outline[intervals] = new Vector2(length * .5f + x, z); return outline; } } }