Make Soundings voyages playable with ship operations and guided tips

This commit is contained in:
josh
2026-11-27 22:24:17 -05:00
parent 4b870fb0ce
commit 33cdd6dfa7
37 changed files with 2734 additions and 126 deletions

View File

@@ -0,0 +1,522 @@
using System;
using System.Collections.Generic;
using UnityEngine;
namespace Soundings
{
internal sealed class DeckPassengers : IDisposable
{
private const float BaseFloor = 5.38f;
private const float FloorSpacing = 1.88f;
private static readonly string[] Paint =
{
"142C3F", "E1DBCA", "B88754", "8A613C", "283846", "678B93", "B45944", "497975",
"D3A064", "688566", "354B64", "54435D", "AAD4D4", "D7906C", "ECE4D7", "337F9A",
"573B2E", "A46C50", "DFAB86", "40577D", "BF7756", "D3B973", "76ADA8", "A684AB", "E3DED0"
};
private readonly Transform shipRoot;
private readonly Ship ship;
private readonly float length, beam;
private readonly Shader shader;
private readonly Dictionary<int, Material> paint = new();
private readonly List<Mesh> meshes = new();
private readonly Layout[] decks;
private readonly Layout promenade;
private Layout bridge;
private Mesh clothes, head;
private int activeIndex = -1;
private bool bridgeView;
private float animationTime;
private sealed class Layout
{
public readonly GameObject Room, Crowd;
public readonly List<Passenger> People = new();
public Layout(string name, Transform parent)
{
Room = Piece(name, parent);
Crowd = Piece("Passenger activity", Room.transform);
}
}
private sealed class Passenger
{
public Transform Body;
public Vector3 Position;
public int Slot, Service;
public float Yaw, Phase, Travel;
public bool Seated;
}
public DeckPassengers(Transform parent, Ship ship, float length, float beam, Shader surface)
{
shipRoot = parent;
this.ship = ship;
this.length = length;
this.beam = beam;
shader = surface;
decks = new Layout[ship.Decks.Count];
BuildPeopleMeshes();
promenade = BuildPromenade();
promenade.Crowd.SetActive(false);
}
public float FloorHeight(int index) => BaseFloor + index * FloorSpacing + .08f;
public Vector3 WalkOrigin(int index)
{
float aisle = ship.Hull.Rows == 3 ? -beam * .34f : 0;
return new Vector3(-length * .34f, FloorHeight(index) + 1.57f, aisle);
}
private float BridgeHeight => BaseFloor + ship.Decks.Count * FloorSpacing;
public Vector3 BridgeOrigin() => new(BridgeCenter - 1.2f, BridgeHeight + 1.65f, 0);
public Vector3 ClampBridgePosition(Vector3 position)
{
position.x = Mathf.Clamp(position.x, BridgeCenter - 3.2f, BridgeCenter + 1.3f);
position.z = Mathf.Clamp(position.z, -beam * .44f, beam * .44f);
position.y = BridgeHeight + 1.65f;
return position;
}
public void Select(int index)
{
if (activeIndex >= 0 && decks[activeIndex] != null)
decks[activeIndex].Room.SetActive(false);
if (bridgeView)
bridge.Room.SetActive(false);
bridgeView = false;
activeIndex = index;
promenade.Room.SetActive(index == -1);
if (index < 0)
return;
if (decks[index] == null)
decks[index] = BuildDeck(index);
decks[index].Room.SetActive(true);
}
public void SelectBridge()
{
if (activeIndex >= 0 && decks[activeIndex] != null)
decks[activeIndex].Room.SetActive(false);
promenade.Room.SetActive(false);
activeIndex = -1;
if (bridge == null)
bridge = BuildBridge();
bridgeView = true;
bridge.Room.SetActive(true);
}
public void HideRooms()
{
if (activeIndex >= 0 && decks[activeIndex] != null)
decks[activeIndex].Room.SetActive(false);
if (bridgeView)
bridge.Room.SetActive(false);
bridgeView = false;
activeIndex = -1;
promenade.Room.SetActive(false);
}
public void HidePassengers()
{
promenade.Crowd.SetActive(false);
foreach (Layout deck in decks)
deck?.Crowd.SetActive(false);
}
public void Tick(OnboardSession session, float delta)
{
if (bridgeView || session == null)
return;
animationTime += Mathf.Max(0, delta);
Layout layout = activeIndex < 0 ? promenade : decks[activeIndex];
if (!layout.Crowd.activeSelf)
layout.Crowd.SetActive(true);
int guests = Math.Max(1, session.Quote.Guests);
foreach (Passenger person in layout.People)
{
float proportion = person.Service < 0 ? Mathf.Clamp01(guests / 100f)
: Mathf.Clamp01(session.Get((OnboardService)person.Service).Served / (float)guests);
bool appears = guests >= 10 && (person.Slot == 0 ? proportion > 0 : (person.Slot == 1 ? proportion > .2f : proportion > .48f));
if (person.Body.gameObject.activeSelf != appears)
person.Body.gameObject.SetActive(appears);
if (!appears)
continue;
float time = animationTime + person.Phase;
Vector3 point = person.Position;
if (person.Travel > 0)
{
float drift = Mathf.Sin(time * .65f);
point.x += drift * person.Travel;
point.y += Mathf.Abs(Mathf.Sin(time * 8)) * .105f;
person.Body.localRotation = Quaternion.Euler(0, Mathf.Cos(time * .65f) >= 0 ? 90 : -90, 0);
}
else
{
point.y += Mathf.Sin(time * 2.9f) * .035f;
person.Body.localRotation = Quaternion.Euler(person.Seated ? 7f : 0, person.Yaw + Mathf.Sin(time * 2.1f) * 6f, 0);
}
person.Body.localPosition = point;
}
}
private Layout BuildPromenade()
{
var layout = new Layout("Open promenade", shipRoot);
float y = BridgeHeight + .04f;
float width = beam * (.82f - (ship.Decks.Count - 1) * .044f);
for (int i = 0; i < 16; i++)
{
float z = (i % 2 == 0 ? -1 : 1) * width * .76f;
float x = length * Mathf.Lerp(-.32f, .23f, i / 15f);
AddPerson(layout, new Vector3(x, y, z), -1, i % 3, i, i % 5 == 0 ? 0 : 1.2f, false);
}
AddPerson(layout, new Vector3(-1.5f, y, -2.0f), (int)OnboardService.Amenities, 0, 16, 0, false, 0);
AddPerson(layout, new Vector3(3.0f, y, 1.95f), (int)OnboardService.Drinks, 1, 17, 0, false, 180);
return layout;
}
private Layout BuildDeck(int index)
{
var layout = new Layout($"Deck {index + 1} cutaway", shipRoot);
float y = FloorHeight(index);
var structure = new Geometry(this, layout.Room.transform);
Vector2[] outline = Outline(.97f, beam * .98f);
structure.Mesh(0).Loft(outline, Vector2.zero, false,
new Vector3(.93f, -1.2f, .69f), new Vector3(.99f, 0, .95f),
new Vector3(1f, 2.0f, 1f), new Vector3(1f, y - .20f, 1f));
structure.Mesh(1).Loft(outline, Vector2.zero, false,
new Vector3(1.002f, y - .21f, 1.003f), new Vector3(1.003f, y, 1.003f));
structure.Mesh(2).Loft(outline, Vector2.zero, true,
new Vector3(.998f, y - .01f, .998f), new Vector3(.998f, y + .01f, .998f));
structure.Mesh(8).Loft(outline, Vector2.zero, false,
new Vector3(1.003f, 3.3f, 1.004f), new Vector3(1.003f, 3.37f, 1.004f));
for (int i = 0; i < outline.Length; i++)
{
Vector2 a = outline[i], b = outline[(i + 1) % outline.Length];
structure.Mesh(1).Rod(HarborMesh.Point(a, y + .52f), HarborMesh.Point(b, y + .52f), .031f);
structure.Mesh(1).Rod(HarborMesh.Point(a, y), HarborMesh.Point(a, y + .52f), .035f);
}
float total = length * .68f;
int columns = ship.Hull.Columns, rows = ship.Hull.Rows;
float cellWidth = total / columns;
float rowDepth = beam * 1.48f / rows;
for (int i = 0; i < 3; i++)
structure.Box(3, new Vector3(-length * .025f, y + .013f, (i - 1) * .28f), new Vector3(total + 5, .014f, .025f));
for (int col = 0; col < columns; col++)
{
float x = -.025f * length - total * .5f + (col + .5f) * cellWidth;
for (int row = 0; row < rows; row++)
{
int slot = row * columns + col;
float z = (row - (rows - 1) * .5f) * (rowDepth + .47f);
var center = new Vector3(x, y + .026f, z);
Facility facility = ship.Decks[index].Cells[slot];
Room(structure, layout, facility, center, cellWidth - .60f, rowDepth - .21f, index * 70 + slot);
}
}
AddPerson(layout, new Vector3(-length * .30f, y + .021f, rows == 3 ? beam * .36f : .03f), -1, 0, index * 6, 1.1f, false);
AddPerson(layout, new Vector3(length * .29f, y + .021f, rows == 3 ? -beam * .36f : -.03f), -1, 1, index * 6 + 1, 1.2f, false);
structure.Finish();
return layout;
}
private void Room(Geometry shape, Layout layout, Facility facility, Vector3 pos, float width, float depth, int seed)
{
int floor = facility switch
{
Facility.Cabin => 5, Facility.Dining => 6, Facility.Entertainment => 11, Facility.Bar => 7,
Facility.Amenity => 9, Facility.Tank => 4, Facility.Galley => 6, Facility.Stores => 5,
Facility.Water => 15, Facility.Waste => 11, Facility.Workshop => 7, _ => 3
};
void Box(int color, float x, float y, float z, float w, float height, float d)
=> shape.Box(color, pos + new Vector3(x, y, z), new Vector3(w, height, d));
Box(floor, 0, .006f, 0, width, .045f, depth);
if (facility == Facility.Empty)
{
Box(8, 0, .04f, depth * .48f, width, .025f, .045f);
return;
}
Box(1, 0, .20f, depth * .49f, width, .36f, .12f);
Box(1, -width * .48f, .21f, 0, .12f, .40f, depth);
Box(1, width * .48f, .21f, 0, .12f, .40f, depth);
int service = -1;
switch (facility)
{
case Facility.Cabin:
for (int side = -1; side <= 1; side += 2)
{
Box(4, width * .25f * side, .23f, depth * .018f, 1.07f, .40f, 1.63f);
Box(14, width * .25f * side, .49f, depth * .018f, 1.04f, .18f, 1.58f);
Box(12, width * .25f * side, .61f, -depth * .11f, 1.02f, .06f, 1.02f);
Box(14, width * .25f * side, .62f, depth * .23f, .83f, .07f, .29f);
}
Box(8, 0, .35f, depth * .23f, .72f, .64f, .57f);
service = (int)OnboardService.Rooms;
break;
case Facility.Dining:
for (int side = -1; side <= 1; side += 2)
{
float x = side * width * .24f;
Box(3, x, .34f, 0, .22f, .54f, .19f);
Box(14, x, .67f, 0, 1.16f, .15f, .94f);
Box(13, x, .77f, -.13f, .36f, .06f, .27f);
Chair(shape, pos + new Vector3(x, 0, -depth * .31f), 4);
Chair(shape, pos + new Vector3(x, 0, depth * .27f), 4);
}
service = (int)OnboardService.Meals;
break;
case Facility.Bar:
Box(3, 0, .47f, depth * .28f, width * .80f, .88f, .73f);
Box(8, 0, .96f, depth * .28f, width * .83f, .13f, .78f);
for (int i = -1; i <= 1; i++)
{
float x = i * width * .21f;
Box(11, x, 1.09f, depth * .28f, .12f, .20f, .16f);
Chair(shape, pos + new Vector3(x, .17f, -depth * .10f), 6);
}
service = (int)OnboardService.Drinks;
break;
case Facility.Entertainment:
Box(4, 0, .30f, depth * .28f, width * .82f, .52f, .66f);
Box(13, 0, .88f, depth * .42f, width * .80f, .68f, .13f);
for (int i = -1; i <= 1; i++)
Chair(shape, pos + new Vector3(i * width * .23f, 0, -depth * .23f), 10);
AddPerson(layout, pos + new Vector3(0, .55f, depth * .23f), (int)OnboardService.Shows, 0, seed + 41, 0, false, 180);
service = (int)OnboardService.Shows;
break;
case Facility.Amenity:
Box(14, 0, .18f, depth * .16f, width * .66f, .30f, depth * .44f);
Box(15, 0, .35f, depth * .16f, width * .55f, .04f, depth * .34f);
Box(8, -width * .29f, .25f, -depth * .26f, 1.38f, .42f, .43f);
Box(8, width * .29f, .25f, -depth * .26f, 1.38f, .42f, .43f);
service = (int)OnboardService.Amenities;
break;
case Facility.Tank:
var start = pos + new Vector3(-width * .32f, .61f, 0);
shape.Mesh(5).Rod(start, pos + new Vector3(width * .32f, .61f, 0), .49f, 12);
Box(8, 0, .94f, -.12f, .29f, .22f, .24f);
return;
case Facility.Galley:
Box(3, 0, .45f, depth * .23f, width * .82f, .88f, .76f);
Box(14, 0, .94f, depth * .23f, width * .84f, .11f, .87f);
for (int i = -1; i <= 1; i++)
Box(4, i * width * .24f, 1.02f, depth * .23f, .55f, .06f, .50f);
Box(14, 0, 1.53f, depth * .32f, width * .78f, .34f, .55f);
break;
case Facility.Stores:
for (int shelf = 1; shelf <= 2; shelf++)
{
float y = shelf * .53f;
Box(8, 0, y, depth * .28f, width * .82f, .09f, .60f);
for (int i = -1; i <= 1; i++)
Box(i == 0 ? 6 : 5, i * width * .24f, y + .21f, depth * .28f, width * .19f, .34f, .47f);
}
break;
case Facility.Water:
case Facility.Waste:
for (int side = -1; side <= 1; side += 2)
{
var foot = pos + new Vector3(side * width * .24f, .18f, depth * .17f);
shape.Mesh(facility == Facility.Water ? 15 : 11).Rod(foot, foot + Vector3.up * 1.15f, .39f, 12);
Box(3, side * width * .24f, .12f, depth * .17f, .94f, .22f, .92f);
}
Box(14, 0, .99f, depth * .34f, width * .53f, .15f, .13f);
break;
case Facility.Workshop:
Box(3, 0, .47f, depth * .28f, width * .83f, .88f, .72f);
Box(8, 0, .97f, depth * .28f, width * .86f, .12f, .79f);
Box(4, 0, 1.42f, depth * .46f, width * .78f, .77f, .12f);
Box(7, width * .24f, 1.10f, depth * .27f, .47f, .20f, .34f);
break;
}
if (service < 0)
return;
bool seats = facility is Facility.Dining or Facility.Bar or Facility.Entertainment or Facility.Amenity;
float sittingZ = facility == Facility.Bar ? -depth * .10f : -depth * .26f;
for (int i = 0; i < (seats ? 3 : 2); i++)
{
float x = (i - (seats ? 1f : .5f)) * width * .23f;
AddPerson(layout, pos + new Vector3(x, seats ? .23f : .03f, sittingZ), service, i, seed + i, 0, seats);
}
}
private Layout BuildBridge()
{
var layout = new Layout("Bridge interior", shipRoot);
var geometry = new Geometry(this, layout.Room.transform);
float y = BridgeHeight;
float x = BridgeCenter;
float extent = beam * .65f;
geometry.Box(2, new Vector3(x, y, 0), new Vector3(8.8f, .18f, extent * 2));
geometry.Box(4, new Vector3(x + 3.5f, y + .46f, 0), new Vector3(.30f, .88f, extent * 2));
geometry.Box(1, new Vector3(x + 3.5f, y + 2.62f, 0), new Vector3(.28f, .33f, extent * 2));
geometry.Box(1, new Vector3(x, y + 2.85f, 0), new Vector3(8.9f, .26f, extent * 2));
geometry.Box(4, new Vector3(x - 4.15f, y + 1.45f, 0), new Vector3(.25f, 2.9f, extent * 2));
for (int i = -2; i <= 2; i++)
{
float z = i * extent * .48f;
geometry.Mesh(1).Rod(new Vector3(x + 3.56f, y + .78f, z), new Vector3(x + 3.18f, y + 2.52f, z), .088f, 6);
}
for (int side = -1; side <= 1; side += 2)
{
float z = side * extent;
geometry.Box(4, new Vector3(x, y + .48f, z), new Vector3(8.8f, .95f, .28f));
for (int i = -1; i <= 1; i++)
geometry.Box(1, new Vector3(x + i * 3.4f, y + 1.8f, z), new Vector3(.15f, 1.75f, .18f));
}
for (int column = -1; column <= 1; column++)
{
float z = column * extent * .57f;
geometry.Box(4, new Vector3(x + 1.8f, y + .54f, z), new Vector3(1.02f, 1.03f, 1.96f));
geometry.Box(10, new Vector3(x + 1.76f, y + 1.16f, z), new Vector3(.85f, .26f, 2.1f));
geometry.Box(15, new Vector3(x + 1.45f, y + 1.36f, z + .42f), new Vector3(.13f, .41f, .63f));
geometry.Box(12, new Vector3(x + 1.37f, y + 1.37f, z + .42f), new Vector3(.02f, .26f, .46f));
for (int button = 0; button < 4; button++)
geometry.Box(button == 2 ? 13 : 12, new Vector3(x + 1.54f, y + 1.31f, z - .55f + button * .14f), new Vector3(.11f, .035f, .10f));
Chair(geometry, new Vector3(x + .6f, y, z), 10);
}
Vector3 fore = new(length * .355f, y - .28f, 0);
geometry.Mesh(2).Loft(new[]
{
new Vector2(fore.x - 8, -beam * .70f), new Vector2(fore.x + length * .11f, 0),
new Vector2(fore.x - 8, beam * .70f)
}, new Vector2(fore.x - 2f, 0), true, new Vector3(1f, y - .4f, 1f), new Vector3(1, y - .28f, 1f));
geometry.Finish();
return layout;
}
private float BridgeCenter => length * (.190f - ship.Decks.Count * .014f);
private void BuildPeopleMeshes()
{
var body = new HarborMesh();
body.Rod(new Vector3(0, .54f, 0), new Vector3(0, 1.15f, 0), .20f, 7);
body.Rod(new Vector3(-.094f, .05f, .025f), new Vector3(-.090f, .57f, 0), .074f, 6);
body.Rod(new Vector3(.094f, .045f, -.035f), new Vector3(.090f, .57f, 0), .074f, 6);
body.Rod(new Vector3(-.28f, .71f, .07f), new Vector3(-.16f, 1.08f, 0), .066f, 6);
body.Rod(new Vector3(.28f, .76f, .13f), new Vector3(.16f, 1.08f, 0), .066f, 6);
var face = new HarborMesh();
face.Radome(new Vector3(0, 1.29f, .025f), .151f);
clothes = Bake(body);
head = Bake(face);
}
private void AddPerson(Layout layout, Vector3 position, int service, int slot, int seed, float travel, bool seated, float yaw = 0)
{
GameObject person = Piece(service < 0 ? "Walking passenger" : OnboardSession.ServiceLabel((OnboardService)service), layout.Crowd.transform);
person.transform.localPosition = position;
person.transform.localScale = new Vector3(1, seated ? .74f : 1, 1) * (1f + seed % 3 * .045f);
RenderMesh("Outfit", person.transform, clothes, MaterialFor(19 + seed % 6));
RenderMesh("Head", person.transform, head, MaterialFor(16 + seed % 3));
layout.People.Add(new Passenger { Body = person.transform, Position = position, Phase = seed * 1.37f, Slot = slot,
Service = service, Travel = travel, Yaw = yaw, Seated = seated });
person.SetActive(false);
}
private static void Chair(Geometry shape, Vector3 at, int color)
{
shape.Box(color, at + new Vector3(0, .39f, 0), new Vector3(.42f, .12f, .39f));
shape.Box(4, at + new Vector3(0, .19f, 0), new Vector3(.12f, .35f, .12f));
}
private Vector2[] Outline(float size, float width)
{
int steps = 22;
var points = new Vector2[steps * 2 + 2];
for (int i = 0; i <= steps; i++)
{
float t = i / (float)steps;
float x = (t - .5f) * length * size;
float shape = t < .20f ? Mathf.Lerp(.66f, 1, 1f - Mathf.Pow(1f - t / .20f, 2))
: t > .69f ? Mathf.Sqrt(Mathf.Clamp01(1f - Mathf.Pow((t - .69f) / .31f, 1.72f))) : 1f;
float z = Mathf.Max(.012f, width * shape);
points[i] = new Vector2(x, -z);
points[points.Length - 1 - i] = new Vector2(x, z);
}
return points;
}
private Material MaterialFor(int id)
{
if (paint.TryGetValue(id, out Material material))
return material;
ColorUtility.TryParseHtmlString("#" + Paint[id], out Color color);
material = new Material(shader) { color = color, name = "Deck fittings " + Paint[id] };
material.SetFloat("_Roughness", .77f);
material.SetFloat("_Metal", id == 4 ? .4f : .03f);
material.SetFloat("_Emissive", id is 12 or 15 ? .24f : .025f);
paint.Add(id, material);
return material;
}
private Mesh Bake(HarborMesh shape)
{
Mesh mesh = shape.Bake();
meshes.Add(mesh);
return mesh;
}
private static GameObject Piece(string name, Transform parent)
{
var result = new GameObject(name) { layer = 8 };
result.transform.SetParent(parent, false);
return result;
}
private static void RenderMesh(string name, Transform parent, Mesh mesh, Material material)
{
GameObject result = Piece(name, parent);
result.AddComponent<MeshFilter>().sharedMesh = mesh;
result.AddComponent<MeshRenderer>().sharedMaterial = material;
}
public void Dispose()
{
foreach (Mesh mesh in meshes)
UnityEngine.Object.Destroy(mesh);
foreach (Material material in paint.Values)
UnityEngine.Object.Destroy(material);
}
private sealed class Geometry
{
private readonly Dictionary<int, HarborMesh> shapes = new();
private readonly DeckPassengers owner;
private readonly Transform parent;
public Geometry(DeckPassengers owner, Transform parent)
{
this.owner = owner;
this.parent = parent;
}
public HarborMesh Mesh(int color)
{
if (!shapes.TryGetValue(color, out HarborMesh shape))
shapes.Add(color, shape = new HarborMesh());
return shape;
}
public void Box(int color, Vector3 center, Vector3 size) => Mesh(color).Box(center, size);
public void Finish()
{
foreach (var (color, shape) in shapes)
RenderMesh("Deck structure", parent, owner.Bake(shape), owner.MaterialFor(color));
}
}
}
}