Make Soundings voyages playable with ship operations and guided tips
This commit is contained in:
@@ -24,6 +24,18 @@ namespace Soundings
|
||||
private float passage;
|
||||
private bool sailing;
|
||||
private Bounds hullBounds;
|
||||
private DeckPassengers decks;
|
||||
private Transform exteriorRoot;
|
||||
private Ship displayedShip;
|
||||
private ServiceDeckModel serviceDeck;
|
||||
private int deckIndex = -1, serviceArea;
|
||||
private float vesselBeam = 6.65f;
|
||||
private CameraMode cameraMode;
|
||||
private float freeYaw, freePitch, freeFov = 47f;
|
||||
private Vector3 freePosition;
|
||||
private float walkHeight;
|
||||
|
||||
private enum CameraMode { Orbit, Flight, DeckWalk, Bridge, Service }
|
||||
|
||||
public RenderTexture Frame { get; private set; }
|
||||
|
||||
@@ -61,20 +73,44 @@ namespace Soundings
|
||||
|
||||
public void Orbit(Vector2 drag)
|
||||
{
|
||||
targetYaw = Mathf.Clamp(targetYaw - drag.x * .18f, 3f, 72f);
|
||||
targetPitch = Mathf.Clamp(targetPitch + drag.y * .13f, 12f, 30f);
|
||||
if (cameraMode != CameraMode.Orbit)
|
||||
return;
|
||||
targetYaw = sailing ? (targetYaw - drag.x * .18f) : Mathf.Clamp(targetYaw - drag.x * .18f, 3f, 72f);
|
||||
targetPitch = Mathf.Clamp(targetPitch + drag.y * .13f, 12f, deckIndex >= 0 ? 78f : 48f);
|
||||
}
|
||||
|
||||
public void Zoom(float delta)
|
||||
{
|
||||
targetZoom = Mathf.Clamp(targetZoom * Mathf.Exp(delta * .050f), .64f, 1.51f);
|
||||
if (cameraMode != CameraMode.Orbit)
|
||||
{
|
||||
freeFov = Mathf.Clamp(freeFov * Mathf.Exp(delta * .035f), 25f, 78f);
|
||||
return;
|
||||
}
|
||||
targetZoom = Mathf.Clamp(targetZoom * Mathf.Exp(delta * .050f), sailing ? .17f : .64f, 1.51f);
|
||||
}
|
||||
|
||||
public void ResetView(bool immediate = false)
|
||||
{
|
||||
targetYaw = DefaultYaw;
|
||||
targetPitch = DefaultPitch;
|
||||
targetZoom = 1f;
|
||||
if (cameraMode == CameraMode.Bridge)
|
||||
{
|
||||
ViewBridge();
|
||||
return;
|
||||
}
|
||||
if (cameraMode == CameraMode.Service)
|
||||
{
|
||||
ExploreServiceArea(serviceArea);
|
||||
return;
|
||||
}
|
||||
ResetOrbit(immediate);
|
||||
}
|
||||
|
||||
private void ResetOrbit(bool immediate)
|
||||
{
|
||||
cameraMode = CameraMode.Orbit;
|
||||
viewCamera.orthographic = true;
|
||||
targetYaw = deckIndex >= 0 ? 17f : DefaultYaw;
|
||||
targetPitch = deckIndex >= 0 ? 55f : DefaultPitch;
|
||||
targetZoom = deckIndex >= 0 ? .92f : 1f;
|
||||
if (!immediate)
|
||||
return;
|
||||
yaw = targetYaw;
|
||||
@@ -83,14 +119,152 @@ namespace Soundings
|
||||
ComposeCamera();
|
||||
}
|
||||
|
||||
public void Look(Vector2 drag)
|
||||
{
|
||||
if (!sailing || drag.sqrMagnitude < .0001f)
|
||||
return;
|
||||
if (cameraMode == CameraMode.Orbit)
|
||||
EnterFlight();
|
||||
freeYaw = Mathf.Repeat(freeYaw + drag.x * .22f, 360f);
|
||||
freePitch = Mathf.Clamp(freePitch + drag.y * .19f, -80f, 84f);
|
||||
}
|
||||
|
||||
public void Fly(Vector3 input, float delta, bool boost)
|
||||
{
|
||||
if (!sailing || input.sqrMagnitude < .0001f || delta <= 0)
|
||||
return;
|
||||
if (cameraMode == CameraMode.Orbit)
|
||||
EnterFlight();
|
||||
input = Vector3.ClampMagnitude(input, 1f);
|
||||
bool interior = cameraMode is CameraMode.DeckWalk or CameraMode.Bridge or CameraMode.Service;
|
||||
Quaternion heading = Quaternion.Euler(interior ? 0 : freePitch, freeYaw, 0);
|
||||
Vector3 movement = heading * new Vector3(input.x, 0, input.z);
|
||||
if (!interior)
|
||||
movement += Vector3.up * input.y;
|
||||
float speed = interior ? (boost ? 6.4f : 3.3f) : (boost ? 52f : 20f);
|
||||
freePosition += movement * (speed * Mathf.Min(.10f, delta));
|
||||
if (cameraMode == CameraMode.DeckWalk)
|
||||
{
|
||||
freePosition.x = Mathf.Clamp(freePosition.x, -vesselLength * .38f, vesselLength * .31f);
|
||||
freePosition.z = Mathf.Clamp(freePosition.z, -vesselBeam * .77f, vesselBeam * .77f);
|
||||
freePosition.y = walkHeight;
|
||||
}
|
||||
else if (cameraMode == CameraMode.Bridge)
|
||||
{
|
||||
freePosition = decks.ClampBridgePosition(freePosition);
|
||||
}
|
||||
else if (cameraMode == CameraMode.Service)
|
||||
{
|
||||
freePosition.x = Mathf.Clamp(freePosition.x, -vesselLength * .358f, vesselLength * .358f);
|
||||
freePosition.z = Mathf.Clamp(freePosition.z, -vesselBeam * .79f, vesselBeam * .79f);
|
||||
freePosition.y = walkHeight;
|
||||
}
|
||||
else
|
||||
{
|
||||
Vector3 origin = vessel != null ? vessel.position : Vector3.zero;
|
||||
freePosition.x = Mathf.Clamp(freePosition.x, origin.x - 235, origin.x + 235);
|
||||
freePosition.y = Mathf.Clamp(freePosition.y, 1.4f, 150f);
|
||||
freePosition.z = Mathf.Clamp(freePosition.z, origin.z - 180, origin.z + 180);
|
||||
}
|
||||
}
|
||||
|
||||
private void EnterFlight()
|
||||
{
|
||||
cameraMode = CameraMode.Flight;
|
||||
freeFov = 47f;
|
||||
Vector3 angles = viewCamera.transform.eulerAngles;
|
||||
freeYaw = angles.y;
|
||||
freePitch = Mathf.DeltaAngle(0f, angles.x);
|
||||
float distance = viewCamera.orthographicSize / Mathf.Tan(freeFov * .5f * Mathf.Deg2Rad);
|
||||
freePosition = OrbitFocus() - viewCamera.transform.forward * distance;
|
||||
freePosition.y = Mathf.Max(1.4f, freePosition.y);
|
||||
viewCamera.orthographic = false;
|
||||
}
|
||||
|
||||
public void ShowDeck(int index)
|
||||
{
|
||||
if (displayedShip == null || index < -1 || index >= displayedShip.Decks.Count || (index == deckIndex && cameraMode == CameraMode.Orbit))
|
||||
return;
|
||||
|
||||
deckIndex = index;
|
||||
if (serviceDeck != null)
|
||||
serviceDeck.Root.SetActive(false);
|
||||
exteriorRoot.gameObject.SetActive(index < 0);
|
||||
decks.Select(index);
|
||||
ResetOrbit(true);
|
||||
}
|
||||
|
||||
public void ExploreDeck(int index)
|
||||
{
|
||||
if (!sailing || displayedShip == null || index < 0 || index >= displayedShip.Decks.Count)
|
||||
return;
|
||||
ShowDeck(index);
|
||||
cameraMode = CameraMode.DeckWalk;
|
||||
freeFov = 62f;
|
||||
freePitch = 1.8f;
|
||||
freeYaw = 90f;
|
||||
freePosition = decks.WalkOrigin(index);
|
||||
walkHeight = freePosition.y;
|
||||
viewCamera.orthographic = false;
|
||||
ComposeCamera();
|
||||
}
|
||||
|
||||
public void ViewBridge()
|
||||
{
|
||||
if (!sailing || decks == null)
|
||||
return;
|
||||
if (serviceDeck != null)
|
||||
serviceDeck.Root.SetActive(false);
|
||||
deckIndex = -1;
|
||||
cameraMode = CameraMode.Bridge;
|
||||
exteriorRoot.gameObject.SetActive(false);
|
||||
decks.SelectBridge();
|
||||
freeFov = 68f;
|
||||
freePitch = 1.3f;
|
||||
freeYaw = 90f;
|
||||
freePosition = decks.BridgeOrigin();
|
||||
viewCamera.orthographic = false;
|
||||
ComposeCamera();
|
||||
}
|
||||
|
||||
public void ExploreServiceArea(int area)
|
||||
{
|
||||
if (!sailing || vessel == null || area < 0 || area > 5)
|
||||
return;
|
||||
if (serviceDeck == null)
|
||||
serviceDeck = new ServiceDeckModel(vessel, vesselLength, vesselBeam, surface);
|
||||
deckIndex = -1;
|
||||
serviceArea = area;
|
||||
cameraMode = CameraMode.Service;
|
||||
exteriorRoot.gameObject.SetActive(false);
|
||||
decks.HideRooms();
|
||||
serviceDeck.Root.SetActive(true);
|
||||
freeFov = 66f;
|
||||
freePitch = 2f;
|
||||
freeYaw = serviceDeck.Heading(area);
|
||||
freePosition = serviceDeck.Entry(area);
|
||||
walkHeight = freePosition.y;
|
||||
viewCamera.orthographic = false;
|
||||
ComposeCamera();
|
||||
}
|
||||
|
||||
public void UpdatePassengers(OnboardSession session, float delta)
|
||||
{
|
||||
if (sailing && decks != null && cameraMode != CameraMode.Service)
|
||||
decks.Tick(session, delta);
|
||||
}
|
||||
|
||||
public bool IsShipAt(Vector2 viewportPoint)
|
||||
{
|
||||
return vessel != null && hullBounds.IntersectRay(viewCamera.ViewportPointToRay(viewportPoint));
|
||||
}
|
||||
|
||||
public void BeginPassage()
|
||||
public void BeginPassage(WeatherCondition weather = WeatherCondition.Calm)
|
||||
{
|
||||
sailing = true;
|
||||
water.SetFloat("_WeatherSeverity", (int)weather / 2f);
|
||||
cameraMode = CameraMode.Orbit;
|
||||
viewCamera.orthographic = true;
|
||||
targetYaw = 22f;
|
||||
targetPitch = 16f;
|
||||
targetZoom = 1.02f;
|
||||
@@ -119,6 +293,9 @@ namespace Soundings
|
||||
hull.z = 0;
|
||||
water.SetVector("_HullSpan", hull);
|
||||
water.SetFloat("_Underway", 0);
|
||||
water.SetFloat("_WeatherSeverity", 0);
|
||||
ShowDeck(-1);
|
||||
decks?.HidePassengers();
|
||||
ResetView(true);
|
||||
}
|
||||
|
||||
@@ -126,6 +303,11 @@ namespace Soundings
|
||||
{
|
||||
if (!viewCamera.enabled)
|
||||
return;
|
||||
if (cameraMode != CameraMode.Orbit)
|
||||
{
|
||||
ComposeCamera();
|
||||
return;
|
||||
}
|
||||
float easing = 1 - Mathf.Exp(-Time.unscaledDeltaTime * 11f);
|
||||
if (!sailing && Mathf.Abs(yaw - targetYaw) < .005f && Mathf.Abs(pitch - targetPitch) < .005f &&
|
||||
Mathf.Abs(zoom - targetZoom) < .0001f)
|
||||
@@ -136,13 +318,33 @@ namespace Soundings
|
||||
ComposeCamera();
|
||||
}
|
||||
|
||||
private void ComposeCamera()
|
||||
private Vector3 OrbitFocus()
|
||||
{
|
||||
var center = new Vector3(2.1f, 4.25f, 0);
|
||||
float level = deckIndex >= 0 && decks != null ? decks.FloorHeight(deckIndex) + .5f : 4.25f;
|
||||
var center = new Vector3(deckIndex >= 0 ? -2f : 2.1f, level, 0);
|
||||
if (sailing)
|
||||
center.x += Mathf.Lerp(-2, 6.5f, passage);
|
||||
Quaternion orbit = Quaternion.Euler(pitch, -yaw, 0);
|
||||
viewCamera.transform.SetPositionAndRotation(center + orbit * (Vector3.back * 244f), orbit);
|
||||
return center;
|
||||
}
|
||||
|
||||
private void ComposeCamera()
|
||||
{
|
||||
if (cameraMode == CameraMode.Orbit)
|
||||
{
|
||||
viewCamera.orthographic = true;
|
||||
viewCamera.nearClipPlane = .25f;
|
||||
Quaternion orbit = Quaternion.Euler(pitch, -yaw, 0);
|
||||
viewCamera.transform.SetPositionAndRotation(OrbitFocus() + orbit * (Vector3.back * 244f), orbit);
|
||||
}
|
||||
else
|
||||
{
|
||||
viewCamera.orthographic = false;
|
||||
viewCamera.nearClipPlane = .095f;
|
||||
viewCamera.fieldOfView = freeFov;
|
||||
Vector3 placement = cameraMode is CameraMode.Bridge or CameraMode.DeckWalk or CameraMode.Service
|
||||
? vessel.TransformPoint(freePosition) : freePosition;
|
||||
viewCamera.transform.SetPositionAndRotation(placement, Quaternion.Euler(freePitch, freeYaw, 0));
|
||||
}
|
||||
FitViewport();
|
||||
}
|
||||
|
||||
@@ -190,8 +392,10 @@ namespace Soundings
|
||||
return;
|
||||
float cropX = Mathf.Min(1, aspect / backplateRatio) * .95f;
|
||||
float cropY = Mathf.Min(1, backplateRatio / aspect) * .95f;
|
||||
float shiftX = Mathf.Clamp((yaw - DefaultYaw) * .00225f, -.47f * (1 - cropX), .47f * (1 - cropX));
|
||||
float shiftY = Mathf.Clamp((pitch - DefaultPitch) * .002f, -.47f * (1 - cropY), .47f * (1 - cropY));
|
||||
float panoramaYaw = cameraMode == CameraMode.Orbit ? Mathf.DeltaAngle(DefaultYaw, yaw) : Mathf.DeltaAngle(-DefaultYaw, freeYaw);
|
||||
float shiftX = Mathf.Clamp(panoramaYaw * .00225f, -.47f * (1 - cropX), .47f * (1 - cropX));
|
||||
float panoramaPitch = cameraMode == CameraMode.Orbit ? pitch : freePitch;
|
||||
float shiftY = Mathf.Clamp((panoramaPitch - DefaultPitch) * .002f, -.47f * (1 - cropY), .47f * (1 - cropY));
|
||||
water.SetVector("_BackdropCrop", new Vector4(cropX, cropY, shiftX, shiftY));
|
||||
}
|
||||
|
||||
@@ -208,6 +412,7 @@ namespace Soundings
|
||||
return;
|
||||
}
|
||||
|
||||
displayedShip = ship;
|
||||
int hullIndex = ship.HullIndex;
|
||||
int deckCount = ship.Decks.Count;
|
||||
ulong layout = 0;
|
||||
@@ -222,12 +427,15 @@ namespace Soundings
|
||||
lastDecks = deckCount;
|
||||
lastLayout = layout;
|
||||
ReleaseShip();
|
||||
displayedShip = ship;
|
||||
var root = new GameObject("Cruise vessel");
|
||||
vessel = root.transform;
|
||||
vessel.SetParent(transform, false);
|
||||
exteriorRoot = new GameObject("Exterior").transform;
|
||||
exteriorRoot.SetParent(vessel, false);
|
||||
|
||||
vesselLength = 71 + hullIndex * 12;
|
||||
float beam = 6.65f + hullIndex * .87f;
|
||||
float beam = vesselBeam = 6.65f + hullIndex * .87f;
|
||||
water.SetVector("_HullSpan", new Vector4(vesselLength, beam, 0, 0));
|
||||
hullBounds = new Bounds(new Vector3(0, 5.5f, 0), new Vector3(vesselLength, 12.3f + deckCount * 1.7f, beam * 2.1f));
|
||||
FitViewport();
|
||||
@@ -241,13 +449,24 @@ namespace Soundings
|
||||
Batch(mesh, finish);
|
||||
Batch(mesh, reflected);
|
||||
}
|
||||
decks = new DeckPassengers(vessel, ship, vesselLength, beam, surface);
|
||||
if (deckIndex >= 0 && deckIndex < ship.Decks.Count)
|
||||
{
|
||||
exteriorRoot.gameObject.SetActive(false);
|
||||
decks.Select(deckIndex);
|
||||
}
|
||||
else
|
||||
{
|
||||
deckIndex = -1;
|
||||
decks.Select(-1);
|
||||
}
|
||||
}
|
||||
|
||||
private void Batch(Mesh mesh, Material material)
|
||||
{
|
||||
var draw = new GameObject(material.name, typeof(MeshFilter), typeof(MeshRenderer));
|
||||
draw.layer = 8;
|
||||
draw.transform.SetParent(vessel, false);
|
||||
draw.transform.SetParent(exteriorRoot, false);
|
||||
draw.GetComponent<MeshFilter>().sharedMesh = mesh;
|
||||
draw.GetComponent<MeshRenderer>().sharedMaterial = material;
|
||||
}
|
||||
@@ -297,6 +516,11 @@ namespace Soundings
|
||||
|
||||
private void ReleaseShip()
|
||||
{
|
||||
decks?.Dispose();
|
||||
decks = null;
|
||||
serviceDeck?.Release();
|
||||
serviceDeck = null;
|
||||
displayedShip = null;
|
||||
if (vessel != null)
|
||||
{
|
||||
vessel.gameObject.SetActive(false);
|
||||
|
||||
Reference in New Issue
Block a user