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

275 lines
13 KiB
C#

using System.Collections.Generic;
using UnityEngine;
using SunDeckVenue = BigShipTycoon.VesselExterior.SunDeckVenue;
namespace BigShipTycoon
{
internal sealed partial class VesselExterior
{
private void BuildSunAttraction(SunDeckVenue venue, float top)
{
switch (venue.Kind)
{
case SunAttraction.MiniGolf: BuildPuttingGreens(venue, top); break;
case SunAttraction.GoKarts: BuildKartCircuit(venue, top); break;
case SunAttraction.RollerCoaster: BuildCoaster(venue, top); break;
}
}
private void BuildPuttingGreens(SunDeckVenue venue, float top)
{
Vector2 half = venue.PuttingHalfSize;
Coat(Paint.TeakSeam).Box(new Vector3(0, top + .027f, 0), new Vector3(half.x * 1.96f, .06f, half.y * 1.88f));
for (int lane = -1; lane <= 1; lane++)
{
Vector2 center = new(lane * half.x * .09f, lane * half.y * .60f);
var contour = new Vector2[36];
for (int point = 0; point < contour.Length; point++)
{
float a = point * Mathf.PI * 2 / contour.Length;
float x = Mathf.Cos(a);
contour[point] = center + new Vector2(x * half.x * .83f,
(Mathf.Sin(a) * (.237f + .035f * Mathf.Cos(3 * a + lane)) + .042f * x * lane) * half.y);
}
Coat(Paint.Fringe).Loft(contour, center, true, new Vector3(1, top + .052f, 1), new Vector3(1, top + .092f, 1));
Coat(Paint.Turf).Loft(contour, center, true, new Vector3(.95f, top + .094f, .86f));
float end = lane == 0 ? -1 : 1;
Vector3 cup = new(center.x + end * half.x * .61f, top + .102f, center.y + .13f * lane);
Coat(Paint.Black).Disc(cup, Vector3.right * .106f, Vector3.back * .106f, 14);
Coat(Paint.Pearl).Rod(cup, cup + Vector3.up * .58f, .015f, 6);
Coat(lane == 0 ? Paint.Lifeboat : Paint.PortLight).Box(cup + new Vector3(.14f, .49f, 0),
new Vector3(.28f, .17f, .021f));
Coat(Paint.Teak).Box(new Vector3(center.x - end * half.x * .56f, top + .105f, center.y), new Vector3(.56f, .019f, .37f));
if (lane != 0)
{
Vector3 bunker = new(center.x + end * half.x * .10f, top + .105f, center.y - lane * half.y * .075f);
Coat(Paint.Ivory).Disc(bunker, Vector3.right * .54f, Vector3.back * .20f, 18);
}
}
for (int end = -1; end <= 1; end += 2)
for (int side = -1; side <= 1; side += 2)
{
Vector3 pot = new(end * half.x * .90f, top + .21f, side * half.y * .90f);
Coat(Paint.Teak).Box(pot, new Vector3(.58f, .42f, .43f));
Coat(Paint.Fringe).Radome(pot + Vector3.up * .27f, .30f);
Coat(Paint.Turf).Radome(pot + new Vector3(.14f, .37f, .04f), .22f);
}
}
private void BuildKartCircuit(SunDeckVenue venue, float top)
{
float trackWidth = SunRideRoute.KartHalfWidth(venue);
for (int i = 0; i < SunRideRoute.Segments; i++)
{
float first = i / (float)SunRideRoute.Segments, next = (i + 1f) / SunRideRoute.Segments;
Vector3 a = SunRideRoute.Point(venue, top, first), b = SunRideRoute.Point(venue, top, next);
Vector3 acrossA = SunRideRoute.Across(venue, top, first), acrossB = SunRideRoute.Across(venue, top, next);
Ribbon(Paint.Black, a, b, acrossA, acrossB, trackWidth);
bool stripe = i % 6 < 2;
if (stripe)
Ribbon(Paint.Ivory, a + Vector3.up * .008f, b + Vector3.up * .008f, acrossA, acrossB, .026f);
for (int side = -1; side <= 1; side += 2)
{
Vector3 edgeA = a + acrossA * (trackWidth * side), edgeB = b + acrossB * (trackWidth * side);
Coat(i / 2 % 2 == 0 ? Paint.Ivory : Paint.PortLight).Rod(edgeA, edgeB, .072f, 5);
if (side > 0)
continue;
edgeA -= acrossA * .13f;
edgeB -= acrossB * .13f;
Barrier(edgeA, edgeB, i % 3 == 0);
}
}
Vector3 start = SunRideRoute.Point(venue, top, .25f);
Coat(Paint.Pearl).Box(start + Vector3.up * .014f, new Vector3(.19f, .012f, trackWidth * 2));
for (int edge = -1; edge <= 1; edge += 2)
{
Vector3 pylon = start + Vector3.forward * edge * (trackWidth + .16f);
Coat(Paint.Steel).Rod(pylon, pylon + Vector3.up * 1.24f, .043f, 6, true);
}
Coat(Paint.DeckCanvas).Box(start + Vector3.up * 1.27f, new Vector3(.26f, .22f, trackWidth * 2.32f));
}
private void Barrier(Vector3 a, Vector3 b, bool post)
{
Coat(Paint.Steel).Rod(a + Vector3.up * .31f, b + Vector3.up * .31f, .027f, 6);
if (post)
Coat(Paint.Steel).Rod(a, a + Vector3.up * .31f, .026f, 6, true);
}
private void Ribbon(Paint paint, Vector3 a, Vector3 b, Vector3 acrossA, Vector3 acrossB, float halfWidth)
{
Vector3 sideA = acrossA * halfWidth, sideB = acrossB * halfWidth;
Coat(paint).Quad(a - sideA, a + sideA, b + sideB, b - sideB);
}
private void BuildCoaster(SunDeckVenue venue, float top)
{
const float gauge = .31f;
for (int i = 0; i < SunRideRoute.Segments; i++)
{
float first = i / (float)SunRideRoute.Segments, next = (i + 1f) / SunRideRoute.Segments;
Vector3 a = SunRideRoute.Point(venue, top, first), b = SunRideRoute.Point(venue, top, next);
Vector3 acrossA = SunRideRoute.Across(venue, top, first), acrossB = SunRideRoute.Across(venue, top, next);
for (int side = -1; side <= 1; side += 2)
Coat(Paint.Lifeboat).Rod(a + acrossA * (gauge * side), b + acrossB * (gauge * side), .057f, 7, true);
Coat(Paint.Gold).Rod(a - acrossA * .38f, a + acrossA * .38f, .045f, 6);
Coat(Paint.Navy).Rod(a - Vector3.up * .18f, b - Vector3.up * .18f, .047f, 6);
if (i % 4 != 0)
continue;
Vector3 lower = a - Vector3.up * .10f;
for (int side = -1; side <= 1; side += 2)
{
Vector3 foot = new(a.x, top + .045f, a.z);
foot += acrossA * (side * .46f);
Coat(Paint.Pearl).Box(foot, new Vector3(.24f, .098f, .24f));
Coat(Paint.Steel).Rod(foot, lower, .063f, 8, true);
if (a.y - top > 1.45f)
Coat(Paint.Steel).Rod(foot, lower + Vector3.forward * .40f, .039f, 6);
}
}
Vector3 station = SunRideRoute.Point(venue, top, .70f) + Vector3.forward * .63f;
Coat(Paint.Teak).Box(new Vector3(station.x, top + .28f, station.z), new Vector3(2.92f, .54f, .83f));
}
}
internal static class SunRideRoute
{
internal const int Segments = 56;
internal static float KartHalfWidth(SunDeckVenue venue) => Mathf.Clamp(venue.HalfSize.y * .18f, .48f, .73f);
internal static Vector3 Point(SunDeckVenue venue, float top, float cycle)
{
float angle = cycle * Mathf.PI * 2f;
Vector2 half = venue.HalfSize;
bool coaster = venue.Kind == SunAttraction.RollerCoaster;
float x = Mathf.Cos(angle) * half.x * .82f;
float z = Mathf.Sin(angle) * half.y * (coaster ? .68f : .70f);
if (!coaster)
return new Vector3(x, top + .081f, z);
float rise = Mathf.Clamp(half.x * .28f, 2.3f, 3.55f);
float lift = .5f + .5f * Mathf.Sin(angle + .09f);
return new Vector3(x, top + .62f + rise * lift * lift + .13f * Mathf.Sin(angle * 2), z);
}
internal static Vector3 Tangent(SunDeckVenue venue, float top, float cycle) =>
(Point(venue, top, cycle + .0015f) - Point(venue, top, cycle - .0015f)).normalized;
internal static Vector3 Across(SunDeckVenue venue, float top, float cycle) =>
Vector3.Cross(Tangent(venue, top, cycle), Vector3.up).normalized;
}
internal sealed partial class DeckPassengers
{
private readonly List<SunDeckRide> sunDeckRides = new();
private sealed class SunDeckRide
{
internal readonly SunDeckVenue Venue;
internal readonly Transform[] Vehicles = new Transform[3];
internal float Phase;
internal SunDeckRide(SunDeckVenue venue) => Venue = venue;
}
internal float SunRidePhase(SunAttraction kind)
{
foreach (SunDeckRide ride in sunDeckRides)
if (ride.Venue.Kind == kind)
return ride.Phase;
return -1;
}
private void AddSunAttractionGuests(Layout layout, SunDeckVenue venue, float height)
{
if (venue.Kind == SunAttraction.MiniGolf)
{
Vector2 half = venue.PuttingHalfSize;
for (int lane = -1; lane <= 1; lane++)
{
float end = lane == 0 ? -1 : 1;
float x = lane * half.x * .09f - end * half.x * .51f;
var player = new Vector3(x, height + .085f, (lane * .60f + .115f) * half.y);
AddSunGuest(layout, venue, GuestActivity.Talk, player, 347 + lane * 13, end * 90,
(int)OnboardService.Amenities, .16f + (lane + 1) * .18f);
}
return;
}
if (venue.Kind is not (SunAttraction.GoKarts or SunAttraction.RollerCoaster))
return;
bool coaster = venue.Kind == SunAttraction.RollerCoaster;
var ride = new SunDeckRide(venue);
sunDeckRides.Add(ride);
Transform track = Piece(coaster ? "Roller coaster train" : "Kart circuit traffic", layout.Room.transform).transform;
track.SetLocalPositionAndRotation(venue.Position(Vector3.zero), venue.Orientation);
for (int i = 0; i < ride.Vehicles.Length; i++)
{
var car = Piece(coaster ? "Coaster car" : "Go-kart", track);
ride.Vehicles[i] = car.transform;
FitRideVehicle(car.transform, coaster, i);
}
PositionRideVehicles(ride, height - .015f);
Vector3 queue = new(-venue.HalfSize.x * .67f, height, venue.HalfSize.y * .91f);
for (int i = 0; i < 3; i++)
AddSunGuest(layout, venue, GuestActivity.Talk, queue + Vector3.right * i * .52f, 198 + i * 17, 179,
(int)OnboardService.Amenities, .16f + i * .21f);
}
private void FitRideVehicle(Transform parent, bool coaster, int variant)
{
var shape = new Geometry(this, parent);
int body = variant switch { 0 => ScreenRed, 1 => ScreenAmber, _ => ScreenChart };
if (coaster)
{
shape.Box(body, new Vector3(0, .17f, 0), new Vector3(.77f, .28f, .78f));
shape.Box(Gasket, new Vector3(0, .36f, -.21f), new Vector3(.61f, .31f, .16f));
shape.Box(Alloy, new Vector3(0, .38f, .18f), new Vector3(.61f, .048f, .081f));
shape.Box(DeckTrim, new Vector3(0, .08f, .01f), new Vector3(.83f, .088f, .42f));
}
else
{
shape.Box(Gasket, new Vector3(0, .135f, 0), new Vector3(.62f, .115f, 1.12f));
shape.Box(body, new Vector3(0, .218f, .29f), new Vector3(.46f, .16f, .49f));
shape.Box(Gasket, new Vector3(0, .29f, -.17f), new Vector3(.38f, .22f, .14f));
shape.Box(Alloy, new Vector3(0, .29f, -.36f), new Vector3(.33f, .12f, .25f));
for (int side = -1; side <= 1; side += 2)
for (int end = -1; end <= 1; end += 2)
{
Vector3 axle = new(side * .28f, .119f, end * .34f);
shape.Mesh(Gasket).Rod(axle, axle + Vector3.right * side * .14f, .118f, 10, true);
}
}
shape.Finish();
}
private void TickSunAttractions(OnboardSession session, float delta)
{
if (session.Quote.Weather == WeatherCondition.Storm || session.Get(OnboardService.Amenities).Served == 0)
return;
foreach (SunDeckRide ride in sunDeckRides)
{
float pace = ride.Venue.Kind == SunAttraction.RollerCoaster ? .145f : .112f;
ride.Phase = Mathf.Repeat(ride.Phase + Mathf.Max(0, delta) * pace, 1);
PositionRideVehicles(ride, BridgeHeight);
}
}
private static void PositionRideVehicles(SunDeckRide ride, float top)
{
float spacing = ride.Venue.Kind == SunAttraction.RollerCoaster ? .028f : .31f;
for (int i = 0; i < ride.Vehicles.Length; i++)
{
float t = ride.Phase + .17f - i * spacing;
ride.Vehicles[i].SetLocalPositionAndRotation(SunRideRoute.Point(ride.Venue, top, t) + Vector3.up * .025f,
Quaternion.LookRotation(SunRideRoute.Tangent(ride.Venue, top, t), Vector3.up));
}
}
}
}