Skip to content

Commit 032c850

Browse files
committed
Partially implemented new custom tribe previews, added debug tile coordinates in tribe preview
1 parent bc4b06d commit 032c850

1 file changed

Lines changed: 89 additions & 0 deletions

File tree

src/Managers/Visual.cs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Text.Json.Serialization;
1010
using PolytopiaBackendBase.Common;
1111
using Il2CppInterop.Runtime;
12+
using TMPro;
1213

1314
namespace PolyMod.Managers;
1415

@@ -75,6 +76,8 @@ public UnitPrefabInfo(string type, string tribe, string skin)
7576
/// A dictionary of skin types of tribes, which can flood tiles, keyed by custom flood tile effect.
7677
/// </summary>
7778
internal static Dictionary<TileData.EffectType, SkinType> customFloodingSkins = new();
79+
private static bool isTakingSnapshot = false;
80+
private static float? baseOrthographicCameraSize = null;
7881

7982
/// <summary>The type of a custom prefab.</summary>
8083
public enum PrefabType
@@ -481,6 +484,92 @@ private static void UIWorldPreview_SetPreview(UIWorldPreview __instance)
481484
}
482485
}
483486

487+
488+
[HarmonyPostfix]
489+
[HarmonyPatch(typeof(TribePreviewRegistry), nameof(TribePreviewRegistry.GetTribePreviewData))]
490+
private static void TribePreviewRegistry_GetTribePreviewData(ref SaveStateData __result, TribeType tribeType, SkinType skinType)
491+
{
492+
string tribeName = EnumCache<TribeType>.GetName(tribeType).ToLower();
493+
if (!Registry.tribePreviews.ContainsKey(tribeName))
494+
return;
495+
496+
PreviewTile[]? preview = Registry.tribePreviews[tribeName];
497+
if (preview == null)
498+
return;
499+
500+
ClientSerializationWrapper result = new ClientSerializationWrapper(null);
501+
int version;
502+
bool num = DiskSerializationHelpers.FromLZ4CompressedByteArray<ClientSerializationWrapper>(__result.byteArray, out result, out version);
503+
if (num)
504+
{
505+
GameState currentGameState = result.GetCurrentGameState();
506+
Console.Write(currentGameState.Map.tiles.Count);
507+
foreach (var previewTile in preview)
508+
{
509+
if(previewTile.x == null || previewTile.y == null)
510+
continue;
511+
512+
WorldCoordinates coordinates = new((int)previewTile.x, (int)previewTile.y);
513+
TileData tileData = currentGameState.Map.GetTile(coordinates);
514+
tileData.terrain = previewTile.terrainType;
515+
// DO THE STATES AND MAYBE THE EFFECTS
516+
//tileData.resource = previewTile.resourceType;
517+
// tileData.unitType = previewTile.unitType;
518+
// tileData.improvement = previewTile.improvementType;
519+
}
520+
// TODO: override tiles based on gld stuff
521+
}
522+
}
523+
524+
[HarmonyPrefix]
525+
[HarmonyPatch(typeof(SpriteCamera), nameof(SpriteCamera.TakeSnapshotOfMapState))]
526+
private static bool SpriteCamera_TakeSnapshotOfMapState(ref Texture2D __result, SpriteCamera.SnapshotMapData mapSnapshot,
527+
float w, float h, TribeType tribe, SkinType skin, TribeType climate, int color, bool clearMapAfterwards)
528+
{
529+
if(!Plugin.config.debug)
530+
return true;
531+
532+
var instance = SpriteCamera.instance;
533+
if(baseOrthographicCameraSize == null)
534+
baseOrthographicCameraSize = instance.spriteCamera.orthographicSize;
535+
536+
instance.spriteCamera.orthographicSize = (float)baseOrthographicCameraSize * 2;
537+
isTakingSnapshot = true;
538+
539+
return true;
540+
}
541+
542+
[HarmonyPostfix]
543+
[HarmonyPatch(typeof(SpriteCamera), nameof(SpriteCamera.TakeSnapshotOfMapState))]
544+
private static void SpriteCamera_TakeSnapshotOfMapState_Postfix(ref Texture2D __result, SpriteCamera.SnapshotMapData mapSnapshot,
545+
float w, float h, TribeType tribe, SkinType skin, TribeType climate, int color, bool clearMapAfterwards)
546+
{
547+
if(isTakingSnapshot)
548+
isTakingSnapshot = false;
549+
}
550+
551+
[HarmonyPostfix]
552+
[HarmonyPatch(typeof(Tile), nameof(Tile.RenderDebug))]
553+
public static void Tile_RenderDebug(Tile __instance)
554+
{
555+
if(!isTakingSnapshot)
556+
return;
557+
558+
GameObject textObj = new GameObject("CoordinateText");
559+
textObj.transform.SetParent(__instance.transform);
560+
textObj.transform.localPosition = new Vector3(0, 0.0f, 0);
561+
562+
TextMeshPro textMesh = textObj.AddComponent<TextMeshPro>();
563+
textMesh.text = __instance.Data.coordinates.ToString();
564+
textMesh.fontSize = 3;
565+
textMesh.alignment = TextAlignmentOptions.Center;
566+
textMesh.color = Color.white;
567+
568+
MeshRenderer renderer = textObj.GetComponent<MeshRenderer>();
569+
renderer.sortingLayerID = MeshCache.TERRAIN_LAYER_ID;
570+
renderer.sortingOrder = __instance.Depth + 10;
571+
}
572+
484573
#endregion
485574
#region UI
486575

0 commit comments

Comments
 (0)