Skip to content

Commit

Permalink
obstacles fading in SN1
Browse files Browse the repository at this point in the history
  • Loading branch information
repkins committed Oct 29, 2022
1 parent 3ec9db5 commit 9000715
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 20 deletions.
43 changes: 43 additions & 0 deletions TerraformingSN1/Shims/PoolShims.cs
Original file line number Diff line number Diff line change
@@ -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<T> where T: new()
{
public static T Get()
{
return new T();
}
}

public class ListPool<T>: IDisposable
{
private bool disposedValue;

public List<T> list = new List<T>();

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);
}
}
}
1 change: 1 addition & 0 deletions TerraformingSN1/TerraformingSN1.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Shims\PoolShims.cs" />
<Compile Include="Shims\TabbedControlsPanelShims.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Resources\Identifiers.Designer.cs">
Expand Down
1 change: 1 addition & 0 deletions TerraformingShared/TerraformingShared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Tools\BuilderPatches.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Tools\BuilderToolPatches.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Tools\ConstructableBasePatches.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Tools\HandReticleExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Tools\PlayerToolExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Tools\RepulsionCannonPatches.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Tools\TerraformerExtensions.cs" />
Expand Down
16 changes: 10 additions & 6 deletions TerraformingShared/Tools/BuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,24 @@ internal static class BuilderExtensions
{
public static void ClearConstructionObstacles(List<GameObject> results)
{
results.RemoveAll(IsConstructionObstacle);
results.RemoveAll(IsObstacleOf<ConstructionObstacle>);
#if !BelowZero
results.RemoveAll(IsImmuneToPropulsion);
results.RemoveAll(IsObstacleOf<ImmuneToPropulsioncannon>);
#endif
}

static bool IsConstructionObstacle(GameObject go)
static bool IsObstacleOf<T>(GameObject go)
{
return go.GetComponent<ConstructionObstacle>() != null;
return go.GetComponent<T>() != null;
}

static bool IsImmuneToPropulsion(GameObject go)
public static void GetObstacles(Vector3 position, Quaternion rotation, List<OrientedBounds> localBounds, List<GameObject> results)
{
return go.GetComponent<ImmuneToPropulsioncannon>() != null;
#if !BelowZero
Builder.GetObstacles(position, rotation, localBounds, results);
#else
Builder.GetObstacles(position, rotation, localBounds, null, results);
#endif
}
}
}
34 changes: 20 additions & 14 deletions TerraformingShared/Tools/BuilderToolPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -101,35 +105,38 @@ public static void OnHolster_Postfix()

static void HighlightDestroyableObstacles(ConstructableBase constructableBase)
{
using (var orientedBoundsListPool = Pool<ListPool<OrientedBounds>>.Get())
using (var obstacleListPool = Pool<ListPool<GameObject>>.Get())
{
var orientedBoundsList = orientedBoundsListPool.list;
var obstacleList = obstacleListPool.list;

using (var orientedBoundsListPool = Pool<ListPool<OrientedBounds>>.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<ListPool<Material>>.Get())
using (var rendererListPool = Pool<ListPool<Renderer>>.Get())
{
var materialList = materialListPool.list;
var rendererList = rendererListPool.list;

foreach (var obstacle in obstacleList)
{
if (!(obstacle.GetComponent<BaseCell>() != 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);
}
}

Expand All @@ -139,5 +146,4 @@ static void RestoreHighlightedObstacles()
obstacleRendererList.Clear();
}
}
}
#endif
}
29 changes: 29 additions & 0 deletions TerraformingShared/Tools/HandReticleExtensions.cs
Original file line number Diff line number Diff line change
@@ -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
}
}
}

0 comments on commit 9000715

Please sign in to comment.