Skip to content

Commit

Permalink
Speaker VCams
Browse files Browse the repository at this point in the history
  • Loading branch information
MegaPiggy committed Sep 9, 2024
1 parent 5a666c4 commit f3d27ff
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Winch/Data/POI/Dock/CustomDockPOI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,9 @@ public class CustomDockPOI : CustomPOI
/// The dock's safe zone settings
/// </summary>
public DockSafeZone safeZone = new DockSafeZone();

/// <summary>
/// Cameras for any speakers in the dock data
/// </summary>
public Dictionary<string, SpeakerVCam> speakerVCams = new Dictionary<string, SpeakerVCam>();
}
20 changes: 20 additions & 0 deletions Winch/Data/POI/Dock/SpeakerVCam.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using UnityEngine;

namespace Winch.Data.POI.Dock;

[Serializable]
public class SpeakerVCam
{
/// <summary>
/// Relative position of the camera from this speaker.
/// </summary>
[SerializeField]
public Vector3 vCam = new Vector3(13, 2.5f, 9);

/// <summary>
/// The relative position for the camera look at target
/// </summary>
[SerializeField]
public Vector3 lookAtTarget = new Vector3(-0.7f, -2f, -0.25f);
}
26 changes: 26 additions & 0 deletions Winch/Serialization/DredgeTypeHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -477,4 +477,30 @@ public static List<AssetReferenceOverride> ParseAudioReferenceOverrides(JArray o
}
return parsed;
}

public static Dictionary<string, SpeakerVCam> GetSpeakerVCamsFromJsonObject(object value)
{
var jsonDict = JsonConvert.DeserializeObject<Dictionary<string, object>>(value.ToString()) ?? throw new InvalidOperationException("Unable to parse speaker cameras.");
return GetSpeakerVCamsDictionary(jsonDict);
}

private static Dictionary<string, SpeakerVCam> GetSpeakerVCamsDictionary(Dictionary<string, object> vcams)
{
var parsed = new Dictionary<string, SpeakerVCam>();
foreach (var kvp in vcams)
{
parsed.Add(kvp.Key, ParseSpeakerVCam(kvp.Value));
}
return parsed;
}

private static SpeakerVCam ParseSpeakerVCam(object value)
{
var jsonDict = JsonConvert.DeserializeObject<Dictionary<string, object>>(value.ToString()) ?? throw new InvalidOperationException("Unable to parse speaker camera.");
return new SpeakerVCam
{
vCam = jsonDict.TryGetValue("vCam", out object vCam) ? ParseVector3(vCam) : new Vector3(13, 2.5f, 9),
lookAtTarget = jsonDict.TryGetValue("lookAtTarget", out object lookAtTarget) ? ParseVector3(lookAtTarget) : new Vector3(-0.7f, -2f, -0.25f)
};
}
}
1 change: 1 addition & 0 deletions Winch/Serialization/POI/Dock/CustomDockPOIConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class CustomDockPOIConverter : CustomPOIConverter
{ "dockSlots", new(null, o=>DredgeTypeHelpers.ParseDockSlots((JArray)o) ) },
{ "sanityModifier", new( new DockSanityModifier(), o=> DredgeTypeHelpers.ParseDockSanityModifier(o)) },
{ "safeZone", new( new DockSafeZone(), o=> DredgeTypeHelpers.ParseDockSafeZone(o)) },
{ "speakerVCams", new( new Dictionary<string, SpeakerVCam>(), o=> DredgeTypeHelpers.GetSpeakerVCamsFromJsonObject(o)) },
};

private readonly Dictionary<string, string> _reroutes = new()
Expand Down
16 changes: 16 additions & 0 deletions Winch/Util/DockUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -443,9 +443,25 @@ public static DockPOI CreateDock(CustomDockPOI customDockPoi)
dock.sanityModifier = CreateDockSanityModifier(customDockPoi.sanityModifier, dockObject.transform);
dock.safeZone = CreateDockSafeZone(customDockPoi.safeZone, dockObject.transform);

Dictionary<string, CinemachineVirtualCamera> speakerVCams = new Dictionary<string, CinemachineVirtualCamera>();
foreach (var speakerVCam in customDockPoi.speakerVCams)
{
speakerVCams.Add(speakerVCam.Key, CreateSpeakerVCam(speakerVCam.Key, speakerVCam.Value, dockObject.transform));
}
dock.speakerVCams = speakerVCams;

return dockPoi;
}

public static CinemachineVirtualCamera CreateSpeakerVCam(string speaker, SpeakerVCam settings, Transform parent)
{
var speakerObject = new GameObject(speaker);
speakerObject.transform.SetParent(parent, false);
speakerObject.transform.localPosition = Vector3.zero;
var lookAt = CreateLookAtTarget(settings.lookAtTarget, speakerObject.transform);
return CreateDockVirtualCamera(settings.vCam, lookAt, speakerObject.transform);
}

public static Transform CreateLookAtTarget(Vector3 position, Transform parent, string name = "LookAt")
{
var lookAt = new GameObject(name);
Expand Down

0 comments on commit f3d27ff

Please sign in to comment.