Files
cruiseSimulator/Assets/BigShipTycoon/Runtime/PassengerCrowd.cs

212 lines
8.7 KiB
C#

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
namespace BigShipTycoon
{
internal enum GuestActivity
{
Walk, Rail, Talk, Photograph, Read, Dine, Drink, Watch, Perform, Rest, Guitar, Revue, Swim, Wade, TendBar, Lounge, Shopper, ShopClerk,
PlayCards, Dealer
}
internal sealed class DeckGuest
{
internal readonly GuestActivity Activity;
internal readonly Vector3 Station;
internal readonly int Service, Seed;
internal readonly float Heading, RequiredOccupancy, SeatHeight, RouteEnd, LaneRadius, RouteStart, SwimHalfLength;
internal readonly EntertainmentProgram? Program;
internal float Presence;
internal bool Initialized;
internal DeckGuest(GuestActivity activity, Vector3 station, int seed, float heading, int service, float occupancy,
float seatHeight, EntertainmentProgram? program = null, float swimHalfLength = 0)
{
Activity = activity;
Station = station;
Seed = seed;
Heading = heading;
Service = service;
RequiredOccupancy = occupancy;
SeatHeight = seatHeight;
Program = program;
SwimHalfLength = swimHalfLength;
}
internal DeckGuest(Vector3 start, float end, float laneRadius, float phase, int seed, float occupancy)
: this(GuestActivity.Walk, start, seed, 0, -1, occupancy, 0)
{
RouteEnd = end;
LaneRadius = laneRadius;
RouteStart = phase;
}
}
internal sealed partial class PassengerCrowd : System.IDisposable
{
private static readonly Color32[] Shirts =
{
new(222, 213, 191, 255), new(61, 115, 125, 255), new(172, 94, 75, 255),
new(55, 73, 96, 255), new(195, 161, 101, 255), new(129, 147, 122, 255),
new(103, 86, 110, 255), new(125, 167, 179, 255), new(200, 124, 129, 255)
};
private static readonly Color32[] Trousers =
{
new(52, 66, 76, 255), new(66, 78, 92, 255), new(169, 154, 128, 255), new(104, 105, 100, 255)
};
private static readonly Color32[] Skin =
{
new(222, 175, 145, 255), new(178, 123, 91, 255), new(199, 150, 116, 255),
new(111, 73, 55, 255), new(236, 190, 160, 255)
};
private static readonly Color32[] Hair =
{
new(53, 43, 39, 255), new(97, 69, 48, 255), new(170, 130, 78, 255), new(140, 139, 130, 255)
};
private static readonly Color32 Shoes = new(54, 47, 42, 255);
private static readonly Color32 Paper = new(219, 202, 161, 255);
private static readonly Color32 Dark = new(46, 57, 61, 255);
private static readonly Color32 Glass = new(188, 160, 110, 255);
private readonly List<Vector3> vertices = new(22000);
private readonly List<Vector3> normals = new(22000);
private readonly List<Color32> colors = new(22000);
private readonly List<Vector4> surface = new(22000);
private readonly List<int> indices = new(76000);
private readonly Dictionary<Mesh, Transform> meshes = new();
private readonly Material material;
private Matrix4x4 guestMatrix;
private Quaternion guestRotation;
private byte coverage;
private int activeActivities;
internal bool HasActiveGuest(GuestActivity activity) => (activeActivities & (1 << (int)activity)) != 0;
internal PassengerCrowd()
{
material = new Material(Resources.Load<Shader>("PassengerSurface")) { name = "Passenger clothing and skin" };
}
internal Mesh CreateMesh(GameObject root, float length, float beam)
{
var mesh = new Mesh { name = "Articulated deck passengers", indexFormat = IndexFormat.UInt32 };
mesh.MarkDynamic();
root.AddComponent<MeshFilter>().sharedMesh = mesh;
root.AddComponent<MeshRenderer>().sharedMaterial = material;
mesh.bounds = new Bounds(Vector3.up * 16, new Vector3(length, 40, beam * 2));
meshes.Add(mesh, root.transform);
return mesh;
}
internal void Draw(Mesh mesh, List<DeckGuest> people, OnboardSession session, bool outside, float clock, float delta, Camera view)
{
vertices.Clear();
normals.Clear();
colors.Clear();
surface.Clear();
indices.Clear();
activeActivities = 0;
Transform crowd = meshes[mesh];
float voyageOccupancy = Mathf.Clamp01(session.Quote.Guests / (float)Mathf.Max(1, session.Ship.Beds));
EntertainmentProgram? playing = session.PerformanceActive ? session.Quote.Plan?.Program : null;
float promenadeAvailability = session.Quote.Weather switch
{
WeatherCondition.Storm => 0,
WeatherCondition.Swell => .66f,
_ => 1
};
foreach (DeckGuest guest in people)
{
float occupancy = voyageOccupancy;
if (guest.Service >= 0)
{
OnboardSample service = session.Get((OnboardService)guest.Service);
occupancy = guest.Activity is (GuestActivity.TendBar or GuestActivity.ShopClerk or GuestActivity.Dealer) && service.Capacity > 0
? 1 : service.Served / (float)Mathf.Max(1, service.Capacity);
}
if (guest.Program.HasValue)
occupancy = guest.Program == playing ? 1 : 0;
else if (guest.Activity == GuestActivity.Perform && playing.HasValue)
occupancy = 0;
if (outside)
occupancy *= guest.Activity is GuestActivity.Swim or GuestActivity.Wade
&& session.Quote.Weather != WeatherCondition.Calm ? 0 : promenadeAvailability;
float goal = Mathf.SmoothStep(0, 1, Mathf.InverseLerp(guest.RequiredOccupancy - .13f,
guest.RequiredOccupancy + .13f, occupancy));
if (session.Quote.Guests == 0 || occupancy <= 0)
goal = 0;
if (!guest.Initialized)
{
guest.Presence = goal;
guest.Initialized = true;
}
else
guest.Presence = Mathf.MoveTowards(guest.Presence, goal, delta * .47f);
if (guest.Presence > .012f)
{
if (guest.Presence > .25f)
activeActivities |= 1 << (int)guest.Activity;
RenderGuest(guest, clock, crowd, view);
}
}
Bounds bounds = mesh.bounds;
mesh.Clear();
mesh.SetVertices(vertices);
mesh.SetNormals(normals);
mesh.SetColors(colors);
mesh.SetUVs(0, surface);
mesh.SetTriangles(indices, 0, false);
mesh.bounds = bounds;
}
private static Vector3 WalkRoute(DeckGuest guest, float distance, out float heading)
{
float length = guest.RouteEnd - guest.Station.x;
float radius = guest.LaneRadius;
float turn = Mathf.PI * radius;
float lap = (length + turn) * 2;
float point = Mathf.Repeat(distance + guest.RouteStart * lap, lap);
Vector3 anchor = guest.Station;
if (point < length)
{
heading = 90;
return anchor + new Vector3(point, 0, -radius);
}
point -= length;
if (point < turn)
{
float arc = point / radius;
heading = 90 - arc * Mathf.Rad2Deg;
return anchor + new Vector3(length + radius * Mathf.Sin(arc), 0, -radius * Mathf.Cos(arc));
}
point -= turn;
if (point < length)
{
heading = -90;
return anchor + new Vector3(length - point, 0, radius);
}
float lastArc = (point - length) / radius;
heading = -90 - lastArc * Mathf.Rad2Deg;
return anchor + new Vector3(-radius * Mathf.Sin(lastArc), 0, radius * Mathf.Cos(lastArc));
}
private static float Hash(int seed)
{
uint value = unchecked((uint)seed + 105789u);
value = (value ^ (value >> 16)) * 0x7feb352d;
value = (value ^ (value >> 15)) * 0x846ca68b;
return ((value ^ (value >> 16)) & 0xffff) / 65535f;
}
public void Dispose()
{
foreach (Mesh mesh in meshes.Keys)
Object.Destroy(mesh);
Object.Destroy(material);
}
}
}