Add noClip command (replacing the collision filter)

This commit is contained in:
Norbi Peti 2020-10-01 23:39:26 +02:00
parent 3a0370a94b
commit eee219f537
3 changed files with 164 additions and 0 deletions

View file

@ -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()

View file

@ -11,6 +11,7 @@
<AssemblyName>BuildingTools</AssemblyName>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
@ -35,6 +36,7 @@
<Compile Include="BuildingTools.cs" />
<Compile Include="BlockSelections.cs" />
<Compile Include="CommandUtils.cs" />
<Compile Include="NoClipCommand.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="0Harmony, Version=2.0.1.0, Culture=neutral, PublicKeyToken=null">

View file

@ -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<Collider> _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<LocalInputEntityStruct>(0U, CommonExclusiveGroups.GameStateGroup))
{
Disable();
yield break;
}
/*LocalInputEntityStruct localInput =
entitiesDB.QueryEntity<LocalInputEntityStruct>(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<CharacterCameraTransformEntityViewStruct>(
(ExclusiveGroupStruct) CameraExclusiveGroups.VisualCameraGroup);
var rot = entityViewStruct.transformComponent.rotation; //Character camera rotation
Player.LocalPlayer.Position += (float3) ((Quaternion) rot * zero);*/
yield return Yield.It;
}
}
private BlobAssetReference<Collider> ChangeCollider(CollisionFilter? newFilter, BlobAssetReference<Collider>? newCollider)
{
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);
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<CharacterMovementEntityStruct>(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<PlayerInputDisabledStruct>(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;
}
}