From 90007153f006aa20a4078295a28f20407ec19c85 Mon Sep 17 00:00:00 2001 From: Antons Repins Date: Sun, 23 Oct 2022 20:58:16 +0300 Subject: [PATCH] obstacles fading in SN1 --- TerraformingSN1/Shims/PoolShims.cs | 43 +++++++++++++++++++ TerraformingSN1/TerraformingSN1.csproj | 1 + .../TerraformingShared.projitems | 1 + TerraformingShared/Tools/BuilderExtensions.cs | 16 ++++--- .../Tools/BuilderToolPatches.cs | 34 +++++++++------ .../Tools/HandReticleExtensions.cs | 29 +++++++++++++ 6 files changed, 104 insertions(+), 20 deletions(-) create mode 100644 TerraformingSN1/Shims/PoolShims.cs create mode 100644 TerraformingShared/Tools/HandReticleExtensions.cs diff --git a/TerraformingSN1/Shims/PoolShims.cs b/TerraformingSN1/Shims/PoolShims.cs new file mode 100644 index 0000000..f432dc8 --- /dev/null +++ b/TerraformingSN1/Shims/PoolShims.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Terraforming.Shims +{ + public static class Pool where T: new() + { + public static T Get() + { + return new T(); + } + } + + public class ListPool: IDisposable + { + private bool disposedValue; + + public List list = new List(); + + protected virtual void Dispose(bool disposing) + { + if (!disposedValue) + { + if (disposing) + { + list.Clear(); + } + + disposedValue = true; + } + } + + public void Dispose() + { + // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method + Dispose(disposing: true); + GC.SuppressFinalize(this); + } + } +} diff --git a/TerraformingSN1/TerraformingSN1.csproj b/TerraformingSN1/TerraformingSN1.csproj index c71ef05..bab0377 100644 --- a/TerraformingSN1/TerraformingSN1.csproj +++ b/TerraformingSN1/TerraformingSN1.csproj @@ -77,6 +77,7 @@ + diff --git a/TerraformingShared/TerraformingShared.projitems b/TerraformingShared/TerraformingShared.projitems index 1d79b50..14453e8 100644 --- a/TerraformingShared/TerraformingShared.projitems +++ b/TerraformingShared/TerraformingShared.projitems @@ -21,6 +21,7 @@ + diff --git a/TerraformingShared/Tools/BuilderExtensions.cs b/TerraformingShared/Tools/BuilderExtensions.cs index b9be18d..95fc43c 100644 --- a/TerraformingShared/Tools/BuilderExtensions.cs +++ b/TerraformingShared/Tools/BuilderExtensions.cs @@ -9,20 +9,24 @@ internal static class BuilderExtensions { public static void ClearConstructionObstacles(List results) { - results.RemoveAll(IsConstructionObstacle); + results.RemoveAll(IsObstacleOf); #if !BelowZero - results.RemoveAll(IsImmuneToPropulsion); + results.RemoveAll(IsObstacleOf); #endif } - static bool IsConstructionObstacle(GameObject go) + static bool IsObstacleOf(GameObject go) { - return go.GetComponent() != null; + return go.GetComponent() != null; } - static bool IsImmuneToPropulsion(GameObject go) + public static void GetObstacles(Vector3 position, Quaternion rotation, List localBounds, List results) { - return go.GetComponent() != null; +#if !BelowZero + Builder.GetObstacles(position, rotation, localBounds, results); +#else + Builder.GetObstacles(position, rotation, localBounds, null, results); +#endif } } } diff --git a/TerraformingShared/Tools/BuilderToolPatches.cs b/TerraformingShared/Tools/BuilderToolPatches.cs index c7eb845..1f4c7eb 100644 --- a/TerraformingShared/Tools/BuilderToolPatches.cs +++ b/TerraformingShared/Tools/BuilderToolPatches.cs @@ -7,8 +7,12 @@ using UnityEngine; using System.Collections; using System.Linq; +using Terraforming.Tools; + +#if !BelowZero +using Terraforming.Shims; +#endif -#if BelowZero namespace TerraformingShared.Tools { [HarmonyPatch(typeof(BuilderTool))] @@ -84,10 +88,10 @@ public static void ShowObstaclesTooltip(Constructable constructable) var obstaclesUseText = string.Format("{0} (Hold {1})", storedDestroyingEnabled ? disableText : enableText, uGUI.FormatButton(GameInput.Button.AltTool)); - var handSubscriptText = HandReticle.main.textHandSubscript; + var handSubscriptText = HandReticle.main.GetHandSubscript(); handSubscriptText = handSubscriptText.Insert(handSubscriptText.IndexOf(Environment.NewLine), string.Format(", {0}", obstaclesUseText)); - HandReticle.main.SetTextRaw(HandReticle.TextType.HandSubscript, handSubscriptText); + HandReticle.main.SetHandSubscriptText(handSubscriptText); } } @@ -101,35 +105,38 @@ public static void OnHolster_Postfix() static void HighlightDestroyableObstacles(ConstructableBase constructableBase) { + using (var orientedBoundsListPool = Pool>.Get()) using (var obstacleListPool = Pool>.Get()) { + var orientedBoundsList = orientedBoundsListPool.list; var obstacleList = obstacleListPool.list; - using (var orientedBoundsListPool = Pool>.Get()) - { - var orientedBoundsList = orientedBoundsListPool.list; - Builder.CacheBounds(constructableBase.gameObject.transform, constructableBase.gameObject, orientedBoundsList, false); - Builder.GetObstacles(constructableBase.transform.position, constructableBase.transform.rotation, orientedBoundsList, null, obstacleList); - } + Builder.CacheBounds(constructableBase.gameObject.transform, constructableBase.gameObject, orientedBoundsList, false); + BuilderExtensions.GetObstacles(constructableBase.transform.position, constructableBase.transform.rotation, orientedBoundsList, obstacleList); + + Terraforming.Logger.Info($"obstacleList.Count = {obstacleList.Count}"); if (obstacleList.Count > 0) { using (var materialListPool = Pool>.Get()) + using (var rendererListPool = Pool>.Get()) { var materialList = materialListPool.list; + var rendererList = rendererListPool.list; foreach (var obstacle in obstacleList) { if (!(obstacle.GetComponent() != null) && Builder.CanDestroyObject(obstacle)) { - obstacle.GetComponentsInChildren(Builder.sRenderers); + obstacle.GetComponentsInChildren(rendererList); - obstacleRendererList.AddRange(Builder.sRenderers); - obstacleRendererList.ForEach(r => r.fadeAmount = Config.Instance.destroyableObstacleTransparency); + obstacleRendererList.AddRange(rendererList); } } } } + + obstacleRendererList.ForEach(r => r.fadeAmount = Config.Instance.destroyableObstacleTransparency); } } @@ -139,5 +146,4 @@ static void RestoreHighlightedObstacles() obstacleRendererList.Clear(); } } -} -#endif \ No newline at end of file +} \ No newline at end of file diff --git a/TerraformingShared/Tools/HandReticleExtensions.cs b/TerraformingShared/Tools/HandReticleExtensions.cs new file mode 100644 index 0000000..8a6d0ff --- /dev/null +++ b/TerraformingShared/Tools/HandReticleExtensions.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Terraforming.Tools +{ + internal static class HandReticleExtensions + { + public static string GetHandSubscript(this HandReticle handReticle) + { +#if BelowZero + return handReticle.textHandSubscript; +#else + return handReticle.interactText2; +#endif + } + + public static void SetHandSubscriptText(this HandReticle handReticle, string subscriptText) + { +#if BelowZero + handReticle.SetTextRaw(HandReticle.TextType.HandSubscript, subscriptText); +#else + handReticle.SetInteractText(handReticle.interactText1, subscriptText); +#endif + } + } +}