|
- using System;
- using System.Collections;
- using TechbloxModdingAPI;
- using TechbloxModdingAPI.Engines;
- using TechbloxModdingAPI.Utility;
- using RobocraftX.Character;
- using RobocraftX.Common;
- using RobocraftX.Common.Input;
- using RobocraftX.UECS;
- using Svelto.ECS;
- using Svelto.Tasks.ExtraLean;
- using Unity.Entities;
- using Unity.Physics;
- 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 CollisionFilter _oldFilter;
- private bool _enabled;
-
- private void Enable()
- {
- if (_entityManager == default) _entityManager = FullGameFields._physicsWorld.EntityManager;
- Logging.CommandLog("Enabling noclip");
- _oldFilter = ChangeCollider(_collisionFilter);
- OnUpdate().RunOn(TechbloxModdingAPI.Tasks.Scheduler.extraLeanRunner);
- _enabled = true;
- Logging.CommandLog("Noclip enabled");
- }
-
- private void Disable()
- {
- Logging.CommandLog("Disabling noclip");
- ChangeCollider(_oldFilter);
- _enabled = false;
- Logging.CommandLog("Noclip disabled");
- }
-
- public void Toggle()
- {
- if (!_enabled) Enable();
- else Disable();
- }
-
- private IEnumerator OnUpdate()
- { //ScreenshotTakerCompositionRoot
- while (_enabled)
- {
- EnsureFlying();
- ChangeCollider(_collisionFilter);
- if (!entitiesDB.Exists<LocalInputEntityStruct>(0U, CommonExclusiveGroups.GameStateGroup))
- {
- yield break;
- }
-
- yield return Yield.It;
- }
- }
-
- private CollisionFilter ChangeCollider(CollisionFilter newFilter)
- {
- foreach (var group in CharacterExclusiveGroups.AllCharacters)
- {
- if (!entitiesDB.Exists<UECSPhysicsEntityStruct>(new EGID(Player.LocalPlayer.Id, group)))
- continue;
- ref var uecsEntity =
- ref entitiesDB.QueryEntity<UECSPhysicsEntityStruct>(new EGID(Player.LocalPlayer.Id, group));
- var collider = _entityManager.GetComponentData<PhysicsCollider>(uecsEntity.uecsEntity);
-
- unsafe
- {
- var coll = (CompoundCollider*) collider.Value.GetUnsafePtr();
- var filter = coll->Filter;
- coll->Filter = newFilter;
- return filter;
- }
- }
-
- throw new InvalidOperationException("No character physics found!");
- }
-
- private void EnsureFlying()
- {
- /*foreach (var ((coll, count), _) in entitiesDB.QueryEntities<CharacterMovementEntityStruct>(CharacterExclusiveGroups.AllCharacters))
- for (int i = 0; i < count; i++)
- coll[i].moveState = MovementState.Flying;*/
- }
-
- public void Ready()
- {
- }
-
- public EntitiesDB entitiesDB { get; set; }
- public void Dispose()
- {
- }
-
- public string Name { get; } = "BuildingToolsNoClipEngine";
- public bool isRemovable { get; } = true;
- }
- }
|