From 2ec15427d62ab579ffedfc3ef0082527c80c6750 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Thu, 25 Nov 2021 02:01:30 +0100 Subject: [PATCH] Remove noGarage and improve mirror mode - Mirroring relative to where the player enabled the mode - Removing blocks that are where the mirrored counterpart of the removed block would be --- BuildingTools/BuildingTools.cs | 6 ---- BuildingTools/MirrorModeEngine.cs | 39 +++++++++++++++++---- BuildingTools/NoGarageCommand.cs | 58 ------------------------------- 3 files changed, 33 insertions(+), 70 deletions(-) delete mode 100644 BuildingTools/NoGarageCommand.cs diff --git a/BuildingTools/BuildingTools.cs b/BuildingTools/BuildingTools.cs index a2288fd..f2e6eed 100644 --- a/BuildingTools/BuildingTools.cs +++ b/BuildingTools/BuildingTools.cs @@ -244,12 +244,6 @@ namespace BuildingTools ScalingPermission.NonUniform; Logging.CommandLog("Free scaling enabled for " + blockID + " until the game is restarted."); }).Build(); - var noGarage = new NoGarageCommand(); - CommandBuilder.Builder("noGarage", "Disables the environment switching and allows building on the track.") - .Action(() => - { - noGarage.Toggle(); - }).Build(); _mirrorModeEngine.Init(); diff --git a/BuildingTools/MirrorModeEngine.cs b/BuildingTools/MirrorModeEngine.cs index 513f44d..b0e1360 100644 --- a/BuildingTools/MirrorModeEngine.cs +++ b/BuildingTools/MirrorModeEngine.cs @@ -31,6 +31,7 @@ namespace BuildingTools private bool _enabled; private Block _lastPlaced; private Block _ghostBlock; + private float3 _offset; private void BlockOnPlaced(object sender, BlockPlacedRemovedEventArgs e) { @@ -38,25 +39,51 @@ namespace BuildingTools if (Player.LocalPlayer.BuildingMode != PlayerBuildingMode.BlockMode) return; if (e.ID == _lastPlaced?.Id) return; _lastPlaced = e.Block.Copy(); - _lastPlaced.Position *= new float3(-1, 1, 1); - //_lastPlaced.Flipped = !_lastPlaced.Flipped; + _lastPlaced.Position = MirrorPos(_lastPlaced.Position); + _lastPlaced.Flipped = !_lastPlaced.Flipped; if (math.abs(_lastPlaced.Rotation.y - 90) < float.Epsilon || math.abs(_lastPlaced.Rotation.y - 270) < float.Epsilon) _lastPlaced.Rotation += new float3(0, 180, 0); } + private void BlockOnRemoved(object sender, BlockPlacedRemovedEventArgs e) + { + if (!_enabled) return; + if (Player.LocalPlayer.BuildingMode != PlayerBuildingMode.BlockMode) return; + var newpos = MirrorPos(e.Block.Position); + foreach (var block in Game.CurrentGame().GetBlocksInGame()) + { + if (math.all(math.abs(block.Position - newpos) < 0.1f)) + block.Remove(); + } + } + + private float3 MirrorPos(float3 pos) + { + pos -= _offset; + pos *= new float3(-1, 1, 1); + pos += _offset; + return pos; + } + public void Init() { - /*Block.Placed += BlockOnPlaced; - TODO. + Block.Placed += BlockOnPlaced; + Block.Removed+=BlockOnRemoved; Game.Enter += OnGameOnEnter; - Game.Exit += (sender, args) => _button = null;*/ + Game.Exit += (sender, args) => _button = null; } private void OnGameOnEnter(object sender, GameEventArgs args) { _button = new Button("Mirror mode"); - _button.OnClick += (_, __) => _enabled = !_enabled; - TryCreateGhostBlock().RunOn(Scheduler.leanRunner); + _button.OnClick += (_, __) => + { + _enabled = !_enabled; + if(_enabled) + _offset = new float3((float)(Math.Round(Player.LocalPlayer.Position.x / 2, 1) * 2), 0, 0); + }; + //TryCreateGhostBlock().RunOn(Scheduler.leanRunner); } private IEnumerator TryCreateGhostBlock() diff --git a/BuildingTools/NoGarageCommand.cs b/BuildingTools/NoGarageCommand.cs deleted file mode 100644 index 3c9bbff..0000000 --- a/BuildingTools/NoGarageCommand.cs +++ /dev/null @@ -1,58 +0,0 @@ -using System; -using System.Reflection; -using HarmonyLib; -using TechbloxModdingAPI.Utility; -using UnityEngine; - -namespace BuildingTools -{ - public class NoGarageCommand - { - private static bool _enabled; - - public void Toggle() - { - Logging.CommandLog("Toggling no garage"); - // ReSharper disable once AssignmentInConditionalExpression - if(_enabled = !_enabled) - Enable(); - Logging.CommandLog($"{(_enabled ? "Enabled" : "Disabled")} no garage"); - } - - private void Enable() - { - var type = AccessTools.TypeByName("Techblox.Environment.Temporary.EnvironmentSwitchEngine"); - var simObj = (GameObject)AccessTools.Field(type, - "THIS_IS_TEMPORARY_CODE_IT_IS_GOING_TO_BE_DELETED_ONCE_WE_HAVE_THE_FINAL_WORLD_SWITCHING_sim_go") - .GetValue(null); - var componentType = AccessTools.TypeByName("Techblox.Garage.GarageMachineBoundaryImplementor"); - simObj.AddComponent(componentType); - var component = simObj.GetComponent(componentType); - AccessTools.Field(componentType, "_bounds") - .SetValue(component, new Bounds(default, new Vector3(50, 2, 50))); - AccessTools.Field(componentType, "_padding") - .SetValue(component, 1f); - AccessTools.Field(componentType, "_cameraLookPointDistance") - .SetValue(component, 1f); - } - - [HarmonyPatch] - private static class EnvironmentPatch - { - public static bool Prefix(object __instance) - { - if (!_enabled) return true; - Logging.MetaDebugLog("Got a time stopped init event"); - AccessTools.Method("Techblox.Environment.Temporary.EnvironmentSwitchEngine:OnTimeRunningInitializationComplete") - .Invoke(__instance, Array.Empty()); - Logging.MetaDebugLog("Successfully called time running init event"); - return false; - } - - public static MethodBase TargetMethod() - { - return AccessTools.Method("Techblox.Environment.Temporary.EnvironmentSwitchEngine:OnTimeStoppedInitializationComplete"); - } - } - } -} \ No newline at end of file