Skip to content

Commit

Permalink
Almost finish destinations
Browse files Browse the repository at this point in the history
  • Loading branch information
MegaPiggy committed Sep 9, 2024
1 parent 0e68d2f commit b7edcbb
Show file tree
Hide file tree
Showing 6 changed files with 241 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,12 @@
"z": -0.25
}
}
}
},
"characters": [
{
"id": "exampleitems.destination.steve",
"titleKey": "exampleitems.destination.steve",
"speakerRootNodeOverride": "DLC1_DockMiddle_Cabin"
}
]
}
100 changes: 100 additions & 0 deletions Winch/Serialization/DredgeTypeHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,16 @@ public static List<Vector2Int> ParseItemDimensions(JArray dimensions)
return parsed;
}

public static Vector2 ParseVector2(object inVal)
{
var position = JsonConvert.DeserializeObject<Dictionary<string, float>>(inVal.ToString()) ?? throw new InvalidOperationException("Unable to parse position.");
float x = 0, y = 0;
if (position.TryGetValue("x", out var value1)) x = value1;
if (position.TryGetValue("y", out var value2)) y = value2;

return new Vector2(x, y);
}

public static HarvestableType[] ParseHarvestableTypes(JArray values)
{
List<HarvestableType> types = new();
Expand Down Expand Up @@ -613,4 +623,94 @@ private static NameKeyOverride ParseNameKeyOverride(object value)
nodesVisited = jsonDict.TryGetValue("nodesVisited", out object nodesVisited) ? ParseStringList((JArray)nodesVisited) : new List<string>(),
};
}

public static CustomCharacterDestination ParseCharacterDestination(JToken destination)
{
var config = new CustomCharacterDestination();
var meta = destination.ToObject<Dictionary<string, object>>();
DockUtil.PopulateDestinationFromMetaWithConverters(config, meta);
return config;
}

public static List<CustomCharacterDestination> ParseCharacterDestinations(JArray destinations)
{
var parsed = new List<CustomCharacterDestination>();
foreach (var destination in destinations)
{
parsed.Add(ParseCharacterDestination(destination));
}
return parsed;
}

public static CustomConstructableDestination ParseConstructableDestination(JToken destination)
{
var config = new CustomConstructableDestination();
var meta = destination.ToObject<Dictionary<string, object>>();
DockUtil.PopulateDestinationFromMetaWithConverters(config, meta);
return config;
}

public static List<CustomConstructableDestination> ParseConstructableDestinations(JArray destinations)
{
var parsed = new List<CustomConstructableDestination>();
foreach (var destination in destinations)
{
parsed.Add(ParseConstructableDestination(destination));
}
return parsed;
}

public static CustomMarketDestination ParseMarketDestination(JToken destination)
{
var config = new CustomMarketDestination();
var meta = destination.ToObject<Dictionary<string, object>>();
DockUtil.PopulateDestinationFromMetaWithConverters(config, meta);
return config;
}

public static List<CustomMarketDestination> ParseMarketDestinations(JArray destinations)
{
var parsed = new List<CustomMarketDestination>();
foreach (var destination in destinations)
{
parsed.Add(ParseMarketDestination(destination));
}
return parsed;
}

public static CustomShipyardDestination ParseShipyardDestination(JToken destination)
{
var config = new CustomShipyardDestination();
var meta = destination.ToObject<Dictionary<string, object>>();
DockUtil.PopulateDestinationFromMetaWithConverters(config, meta);
return config;
}

public static List<CustomShipyardDestination> ParseShipyardDestinations(JArray destinations)
{
var parsed = new List<CustomShipyardDestination>();
foreach (var destination in destinations)
{
parsed.Add(ParseShipyardDestination(destination));
}
return parsed;
}

public static CustomUpgradeDestination ParseUpgradeDestination(JToken destination)
{
var config = new CustomUpgradeDestination();
var meta = destination.ToObject<Dictionary<string, object>>();
DockUtil.PopulateDestinationFromMetaWithConverters(config, meta);
return config;
}

public static List<CustomUpgradeDestination> ParseUpgradeDestinations(JArray destinations)
{
var parsed = new List<CustomUpgradeDestination>();
foreach (var destination in destinations)
{
parsed.Add(ParseUpgradeDestination(destination));
}
return parsed;
}
}
6 changes: 6 additions & 0 deletions Winch/Serialization/POI/Dock/CustomDockPOIConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using UnityEngine;
using Winch.Data.POI.Dock;
using Winch.Data.POI.Dock.Destinations;

namespace Winch.Serialization.POI.Dock;

Expand All @@ -17,6 +18,11 @@ public class CustomDockPOIConverter : CustomPOIConverter
{ "vCam", new( null, o=> DredgeTypeHelpers.ParseVector3(o)) },
{ "lookAtTarget", new( null, o=> DredgeTypeHelpers.ParseVector3(o)) },
{ "boatActions", new( null, o=> DredgeTypeHelpers.ParseVector3(o)) },
{ "characters", new( new List<CustomCharacterDestination>() , o=> DredgeTypeHelpers.ParseCharacterDestinations((JArray)o)) },
{ "markets", new( new List<CustomMarketDestination>() , o=> DredgeTypeHelpers.ParseMarketDestinations((JArray)o)) },
{ "shipyards", new( new List<CustomShipyardDestination>() , o=> DredgeTypeHelpers.ParseShipyardDestinations((JArray)o)) },
{ "upgraders", new( new List<CustomUpgradeDestination>() , o=> DredgeTypeHelpers.ParseUpgradeDestinations((JArray)o)) },
{ "constructables", new( new List<CustomConstructableDestination>() , o=> DredgeTypeHelpers.ParseConstructableDestinations((JArray)o)) },
{ "storage", new( null, o=> DredgeTypeHelpers.ParsePrebuiltStorageDestination(o)) },
{ "dockSlots", new(null, o=>DredgeTypeHelpers.ParseDockSlots((JArray)o) ) },
{ "sanityModifier", new( new DockSanityModifier(), o=> DredgeTypeHelpers.ParseDockSanityModifier(o)) },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class CustomBaseDestinationConverter : DredgeTypeConverter<CustomBaseDest
{ "highlightConditions", new(new List<HighlightCondition>(), o=>DredgeTypeHelpers.ParseHighlightConditions((JArray)o)) },
{ "useFixedScreenPosition", new( false, o=> bool.Parse(o.ToString())) },
{ "pointTo", new( Vector3.zero, o=> DredgeTypeHelpers.ParseVector3(o)) },
{ "screenPosition", new(Vector3.zero, o=> DredgeTypeHelpers.ParseVector3(o)) },
{ "screenPosition", new(Vector2.zero, o=> DredgeTypeHelpers.ParseVector2(o)) },
{ "selectOnLeft", new(new List<string>(), o => DredgeTypeHelpers.ParseStringList((JArray)o)) },
{ "selectOnRight", new(new List<string>(), o => DredgeTypeHelpers.ParseStringList((JArray)o)) },
{ "selectOnUp", new(new List<string>(), o => DredgeTypeHelpers.ParseStringList((JArray)o)) },
Expand Down
109 changes: 108 additions & 1 deletion Winch/Util/DockUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using Winch.Data.POI.Dock;
using Winch.Data.POI.Dock.Destinations;
using Winch.Serialization.Dock;
using Winch.Serialization.POI.Dock.Destinations;
using Winch.Serialization;
using Winch.Components;
using Cinemachine;
using UnityEngine.AddressableAssets;
Expand Down Expand Up @@ -200,6 +202,21 @@ internal static bool PopulateDockDataFromMetaWithConverter(DeferredDockData data
return UtilHelpers.PopulateObjectFromMeta(data, meta, DockDataConverter);
}

private static Dictionary<Type, IDredgeTypeConverter> DestinationConverters = new()
{
{ typeof(CustomCharacterDestination), new CustomCharacterDestinationConverter() },
{ typeof(CustomConstructableDestination), new CustomConstructableDestinationConverter() },
{ typeof(CustomMarketDestination), new CustomMarketDestinationConverter() },
{ typeof(CustomShipyardDestination), new CustomShipyardDestinationConverter() },
{ typeof(CustomUpgradeDestination), new CustomUpgradeDestinationConverter()}
};

internal static bool PopulateDestinationFromMetaWithConverters<T>(T item, Dictionary<string, object> meta) where T : CustomBaseDestination
{
return UtilHelpers.PopulateObjectFromMeta<T>(item, meta, DestinationConverters);
}


internal static Dictionary<string, DeferredDockData> ModdedDockDataDict = new();
internal static Dictionary<string, DockData> AllDockDataDict = new();
internal static Dictionary<string, ModdedDock> ModdedDockDict = new();
Expand Down Expand Up @@ -438,7 +455,36 @@ public static DockPOI CreateDock(CustomDockPOI customDockPoi)
dock.storageEnabled = prebuiltStorage.enabled;
dock.overflowStorageEnabled = prebuiltStorage.hasOverflow;

dock.destinations = new List<BaseDestination>();
Dictionary<CustomBaseDestination, BaseDestination> destinations = new Dictionary<CustomBaseDestination, BaseDestination>();
foreach (var destination in customDockPoi.characters)
{
destinations.Add(destination, CreateBaseDestination<CharacterDestination>(destination, dockObject.transform));
}
foreach (var destination in customDockPoi.markets)
{
destinations.Add(destination, CreateMarketDestination<MarketDestination>(destination, dockObject.transform));
}
foreach (var destination in customDockPoi.shipyards)
{
destinations.Add(destination, CreateMarketDestination<ShipyardDestination>(destination, dockObject.transform));
}
foreach (var destination in customDockPoi.upgraders)
{
destinations.Add(destination, CreateBaseDestination<UpgradeDestination>(destination, dockObject.transform));
}
foreach (var destination in customDockPoi.constructables)
{
destinations.Add(destination, CreateBaseDestination<ConstructableDestination>(destination, dockObject.transform));
}
var destinationsList = destinations.Values.ToList();
foreach (var kvp in destinations)
{
if (kvp.Key is CustomConstructableDestination custom && kvp.Value is ConstructableDestination destination)
PostConstructableDestination(custom, destination, destinationsList);
else
PostBaseDestination(kvp.Key, kvp.Value, destinationsList);
}
dock.destinations = destinationsList;

dock.sanityModifier = CreateDockSanityModifier(customDockPoi.sanityModifier, dockObject.transform);
dock.safeZone = CreateDockSafeZone(customDockPoi.safeZone, dockObject.transform);
Expand All @@ -453,6 +499,67 @@ public static DockPOI CreateDock(CustomDockPOI customDockPoi)
return dockPoi;
}

public static T CreateBaseDestination<T>(CustomBaseDestination custom, Transform parent) where T : BaseDestination
{
var baseObject = new GameObject(custom.id);
baseObject.transform.SetParent(parent, false);
baseObject.transform.localPosition = custom.position;
var lookAt = CreateLookAtTarget(custom.lookAtTarget, baseObject.transform);
var vCam = CreateDockVirtualCamera(custom.vCam, lookAt, baseObject.transform);
var destinationObject = new GameObject("Destination");
destinationObject.transform.SetParent(baseObject.transform, false);
destinationObject.transform.localPosition = Vector3.zero;
var destination = destinationObject.AddComponent<T>();
destination.id = custom.id;
destination.vCam = vCam;
destination.alwaysShow = custom.alwaysShow;
destination.icon = custom.icon;
destination.titleKey = custom.titleKey;
destination.playerInventoryTabIndexesToShow = custom.playerInventoryTabIndexesToShow;
destination.highlightConditions = custom.highlightConditions;
destination.speakerData = CharacterUtil.GetSpeakerData(custom.speakerData);
destination.speakerRootNodeOverride = custom.speakerRootNodeOverride;
destination.visitSFX = custom.visitSFX;
destination.loopSFX = AudioClipUtil.GetAudioClip(custom.loopSFX);
destination.useFixedScreenPosition = custom.useFixedScreenPosition;
destination.transformToPointTo = CreateLookAtTarget(custom.pointTo, baseObject.transform, "PointTo");
destination.screenPosition = custom.screenPosition;
return destination;
}

public static T CreateMarketDestination<T>(CustomMarketDestination custom, Transform parent) where T : MarketDestination
{
var destination = CreateBaseDestination<T>(custom, parent);
destination.itemTypesBought = custom.itemTypesBought;
destination.itemSubtypesBought = custom.itemSubtypesBought;
destination.bulkItemTypesBought = custom.bulkItemTypesBought;
destination.bulkItemSubtypesBought = custom.bulkItemSubtypesBought;
destination.specificItemsBought = ItemUtil.TryGetSpatials(custom.specificItemsBought).ToArray();
destination.sellValueModifier = custom.sellValueModifier;
destination.allowSellIfGridFull = custom.allowSellIfGridFull;
destination.allowStorageAccess = custom.allowStorageAccess;
destination.allowRepairs = custom.allowRepairs;
destination.allowBulkSell = custom.allowBulkSell;
destination.bulkSellPromptString = custom.bulkSellPromptString;
destination.bulkSellNotificationString = custom.bulkSellNotificationString;
destination.marketTabs = custom.marketTabs;
return destination;
}

public static void PostBaseDestination(CustomBaseDestination custom, BaseDestination destination, List<BaseDestination> all)
{
destination.selectOnLeft = all.FindAll(x => custom.selectOnLeft.Contains(x.id));
destination.selectOnRight = all.FindAll(x => custom.selectOnRight.Contains(x.id));
destination.selectOnUp = all.FindAll(x => custom.selectOnUp.Contains(x.id));
destination.selectOnDown = all.FindAll(x => custom.selectOnDown.Contains(x.id));
}

public static void PostConstructableDestination(CustomConstructableDestination custom, ConstructableDestination destination, List<BaseDestination> all)
{
PostBaseDestination(custom, destination, all);
destination.useThisDestinationInsteadIfConstructed = all.Find(x => x.id == custom.useThisDestinationInsteadIfConstructed);
}

public static CinemachineVirtualCamera CreateSpeakerVCam(string speaker, SpeakerVCam settings, Transform parent)
{
var speakerObject = new GameObject(speaker);
Expand Down
18 changes: 18 additions & 0 deletions Winch/Util/ItemUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,24 @@ public static List<ItemData> TryGetItems(List<string> ids)
return items;
}

public static List<SpatialItemData> TryGetSpatials(List<string> ids)
{
List<SpatialItemData> spatials = new List<SpatialItemData>();

if (ids == null)
return spatials;

foreach (var spatial in ids)
{
if (!string.IsNullOrWhiteSpace(spatial) && SpatialItemDataDict.TryGetValue(spatial, out var itemData))
{
spatials.Add(itemData);
}
}

return spatials;
}

public static List<HarvestableItemData> TryGetHarvestables(List<string> ids)
{
List<HarvestableItemData> harvestables = new List<HarvestableItemData>();
Expand Down

0 comments on commit b7edcbb

Please sign in to comment.