From eee219f53790442c329e14593a5c2488872aa3b3 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Thu, 1 Oct 2020 23:39:26 +0200 Subject: [PATCH] Add noClip command (replacing the collision filter) --- BuildingTools/BuildingTools.cs | 4 + BuildingTools/BuildingTools.csproj | 2 + BuildingTools/NoClipCommand.cs | 158 +++++++++++++++++++++++++++++ 3 files changed, 164 insertions(+) create mode 100644 BuildingTools/NoClipCommand.cs diff --git a/BuildingTools/BuildingTools.cs b/BuildingTools/BuildingTools.cs index d05c00b..2fde772 100644 --- a/BuildingTools/BuildingTools.cs +++ b/BuildingTools/BuildingTools.cs @@ -127,6 +127,10 @@ namespace BuildingTools { new Player(PlayerType.Local).AngularVelocity += new float3(x, y, z); }).Build(); + var noClip = new NoClipCommand(); + GameEngineManager.AddGameEngine(noClip); + CommandBuilder.Builder("noClip", "Allows you to go through blocks. Run again to disable.") + .Action(noClip.Toggle).Build(); } private string GetBlockInfo() diff --git a/BuildingTools/BuildingTools.csproj b/BuildingTools/BuildingTools.csproj index db51b51..a70a8e9 100644 --- a/BuildingTools/BuildingTools.csproj +++ b/BuildingTools/BuildingTools.csproj @@ -11,6 +11,7 @@ BuildingTools v4.8 512 + true AnyCPU @@ -35,6 +36,7 @@ + diff --git a/BuildingTools/NoClipCommand.cs b/BuildingTools/NoClipCommand.cs new file mode 100644 index 0000000..b2660dd --- /dev/null +++ b/BuildingTools/NoClipCommand.cs @@ -0,0 +1,158 @@ +using System; +using System.Collections; +using GamecraftModdingAPI; +using GamecraftModdingAPI.Engines; +using GamecraftModdingAPI.Utility; +using RobocraftX.Character; +using RobocraftX.Character.Camera; +using RobocraftX.Character.Factories; +using RobocraftX.Character.Movement; +using RobocraftX.Common; +using RobocraftX.Common.Input; +using RobocraftX.Common.UnityECSWrappers; +using RobocraftX.UECS; +using Svelto.ECS; +using Svelto.Tasks.ExtraLean; +using Unity.Entities; +using Unity.Mathematics; +using Unity.Physics; +using UnityEngine; +using Collider = Unity.Physics.Collider; +using Yield = Svelto.Tasks.Yield; + +namespace BuildingTools +{ + public class NoClipCommand : IApiEngine + { + private readonly CollisionFilter _collisionFilter = new CollisionFilter + { //AddCollidersToRigidBodyEngineUECS._simCubeNoCollisionFilter + BelongsTo = 0, + CollidesWith = 239532 + }; + + private EntityManager _entityManager; + private BlobAssetReference _oldCollider; + private bool _enabled; + + private void Enable() + { + if (_entityManager == default) _entityManager = FullGameFields._physicsWorld.EntityManager; + Logging.CommandLog("Enabling no clip"); + _oldCollider = ChangeCollider(_collisionFilter, null); + OnUpdate().RunOn(GamecraftModdingAPI.Tasks.Scheduler.extraLeanRunner); + _enabled = true; + Logging.CommandLog("No clip enabled"); + } + + private void Disable() + { + Logging.CommandLog("Disabling no clip"); + ChangeCollider(null, _oldCollider).Dispose(); //Dispose old (cloned) collider + _enabled = false; + Logging.CommandLog("No clip disabled"); + } + + public void Toggle() + { + // ReSharper disable once AssignmentInConditionalExpression + if (_enabled = !_enabled) Enable(); + else Disable(); + } + + private IEnumerator OnUpdate() + { //ScreenshotTakerCompositionRoot + while (_enabled) + { + EnsureFlying(); + //float unscaledDeltaTime = Time.unscaledDeltaTime; + if (!entitiesDB.Exists(0U, CommonExclusiveGroups.GameStateGroup)) + { + Disable(); + yield break; + } + + /*LocalInputEntityStruct localInput = + entitiesDB.QueryEntity(0U, + (ExclusiveGroupStruct) CommonExclusiveGroups.GameStateGroup); + Vector3 zero = Vector3.zero; + if (localInput.CheckInputAction(ActionInput.Forward)) + zero.z = 3f * unscaledDeltaTime; + if (localInput.CheckInputAction(ActionInput.Backward)) + zero.z = -3f * unscaledDeltaTime; + if (localInput.CheckInputAction(ActionInput.Right)) + zero.x = 3f * unscaledDeltaTime; + if (localInput.CheckInputAction(ActionInput.Left)) + zero.x = -3f * unscaledDeltaTime; + if (localInput.CheckInputAction(ActionInput.Up)) + zero.y = 3f * unscaledDeltaTime; + if (localInput.CheckInputAction(ActionInput.Down)) + zero.y = -3f * unscaledDeltaTime; + if (localInput.CheckInputAction(ActionInput.Sprint)) + zero *= 3f; + var entityViewStruct = + entitiesDB.QueryUniqueEntity( + (ExclusiveGroupStruct) CameraExclusiveGroups.VisualCameraGroup); + var rot = entityViewStruct.transformComponent.rotation; //Character camera rotation + Player.LocalPlayer.Position += (float3) ((Quaternion) rot * zero);*/ + yield return Yield.It; + } + } + + private BlobAssetReference ChangeCollider(CollisionFilter? newFilter, BlobAssetReference? newCollider) + { + foreach (var group in CharacterExclusiveGroups.AllCharacters) + { + if (!entitiesDB.Exists(new EGID(Player.LocalPlayer.Id, group))) + continue; + ref var uecsEntity = + ref entitiesDB.QueryEntity(new EGID(Player.LocalPlayer.Id, group)); + var collider = _entityManager.GetComponentData(uecsEntity.uecsEntity); + var oldCollider = collider.Value; + if (newFilter.HasValue) + { + unsafe + { + var colRef = ColliderUtilityUECS.ClonePhysicsCollider(collider.ColliderPtr, newFilter.Value); + collider.Value = colRef; + } + } + else if (newCollider.HasValue) + collider.Value = newCollider.Value; + _entityManager.SetComponentData(uecsEntity.uecsEntity, collider); + return oldCollider; + } + + throw new InvalidOperationException("No character physics found!"); + } + + private void EnsureFlying() + { + foreach (ref var ent in entitiesDB.QueryEntities(CharacterExclusiveGroups.AllCharacters).entities) + { + ent.moveState = MovementState.Flying; + //ent.inputMovement = default; //Don't allow regular movement - doesn't do anything + } + } + + /*private void DisableInput(bool disabled) + { - Disables way more than movements + ref var inputDisabled = ref entitiesDB.QueryEntity(CommonExclusiveGroups.GameStateEGID); + if (disabled) + inputDisabled.PushMouseAndKeyboard(false); + else + inputDisabled.PopMouseAndKeyboard(false); + }*/ + + public void Ready() + { + } + + public EntitiesDB entitiesDB { get; set; } + public void Dispose() + { + } + + public string Name { get; } = "BuildingToolsNoClipEngine"; + public bool isRemovable { get; } = true; + } +} \ No newline at end of file