Constantly set the collision filter

Something is resetting it
This commit is contained in:
Norbi Peti 2020-12-31 00:52:43 +01:00
parent 2b0cec100e
commit 5a24074fef

View file

@ -1,9 +1,11 @@
using System;
using System.Collections;
using System.Reflection;
using GamecraftModdingAPI;
using GamecraftModdingAPI.Engines;
using GamecraftModdingAPI.Players;
using GamecraftModdingAPI.Utility;
using HarmonyLib;
using RobocraftX.Character;
using RobocraftX.Character.Camera;
using RobocraftX.Character.Factories;
@ -32,14 +34,14 @@ namespace BuildingTools
};
private EntityManager _entityManager;
private CollisionFilter _oldCollider;
private CollisionFilter _oldFilter;
private bool _enabled;
private void Enable()
{
if (_entityManager == default) _entityManager = FullGameFields._physicsWorld.EntityManager;
Logging.CommandLog("Enabling noclip");
_oldCollider = ChangeCollider(_collisionFilter, null);
_oldFilter = ChangeCollider(_collisionFilter);
OnUpdate().RunOn(GamecraftModdingAPI.Tasks.Scheduler.extraLeanRunner);
_enabled = true;
Logging.CommandLog("Noclip enabled");
@ -48,7 +50,7 @@ namespace BuildingTools
private void Disable()
{
Logging.CommandLog("Disabling noclip");
ChangeCollider(null, _oldCollider); //Dispose old (cloned) collider
ChangeCollider(_oldFilter);
_enabled = false;
Logging.CommandLog("Noclip disabled");
}
@ -65,6 +67,7 @@ namespace BuildingTools
while (_enabled)
{
EnsureFlying();
ChangeCollider(_collisionFilter);
if (!entitiesDB.Exists<LocalInputEntityStruct>(0U, CommonExclusiveGroups.GameStateGroup))
{
//Disable();
@ -75,7 +78,8 @@ namespace BuildingTools
}
}
private CollisionFilter ChangeCollider(CollisionFilter? newFilter, CollisionFilter? newCollider)
//private BlobAssetReference<Collider> ChangeCollider(CollisionFilter? newFilter, BlobAssetReference<Collider>? newCollider)
private CollisionFilter ChangeCollider(CollisionFilter newFilter)
{
foreach (var group in CharacterExclusiveGroups.AllCharacters)
{
@ -89,31 +93,53 @@ namespace BuildingTools
var collider = _entityManager.GetComponentData<PhysicsCollider>(uecsEntity.uecsEntity);
//var collider = _entityManager.GetComponentData<CharacterEnvironmentCollision>(uecsEntity.uecsEntity);
//Console.WriteLine("Collider: " + collider.sphereCollider.Value.Filter.BelongsTo);
unsafe
/*unsafe
{
Console.WriteLine("Collider ptr: " + (long) collider.ColliderPtr);
}
var oldCollider = collider.Value.Value.Filter;
Console.WriteLine("Old filter: " + collider.Value.Value.Filter.BelongsTo);
Console.WriteLine("Collider type: " + collider.Value.Value.Type);
var oldCollider = collider.Value;
if (newFilter.HasValue)
{
unsafe
{
//var colRef = ColliderUtilityUECS.ClonePhysicsCollider(collider.ColliderPtr, newFilter.Value);
//collider.Value = colRef;
collider.Value.Value.Filter = newFilter.Value;
Console.WriteLine("New collider: " + (long) collider.ColliderPtr);
var colRef = ColliderUtilityUECS.ClonePhysicsCollider(collider.ColliderPtr, newFilter.Value);
collider.Value = colRef;
//collider.Value.Value.Filter = newFilter.Value;
Console.WriteLine("New filter: " + collider.Value.Value.Filter.BelongsTo);
Console.WriteLine("Should be: " + newFilter.Value.BelongsTo);
Console.WriteLine("New collider ptr: " + (long) collider.ColliderPtr);
}
}
else if (newCollider.HasValue)
collider.Value.Value.Filter = newCollider.Value;
collider.Value = newCollider.Value;
//Console.WriteLine("New collider: " + collider.sphereCollider.Value.Filter.BelongsTo);
_entityManager.SetComponentData(uecsEntity.uecsEntity, collider);
Console.WriteLine("Resulting filter: " + _entityManager
.GetComponentData<PhysicsCollider>(uecsEntity.uecsEntity).Value.Value.Filter
.BelongsTo);
return oldCollider;
unsafe
{
Console.WriteLine("Resulting collider ptr: " + (long) _entityManager
.GetComponentData<PhysicsCollider>(uecsEntity.uecsEntity).ColliderPtr);
}
return oldCollider;*/
unsafe
{
var coll = (CompoundCollider*) collider.Value.GetUnsafePtr();
var filter = coll->Filter;
coll->Filter = newFilter;
Console.WriteLine("Changed filter from " + filter.BelongsTo + " to " + newFilter.BelongsTo);
//_entityManager.SetComponentData(uecsEntity.uecsEntity, collider);
return filter;
}
}
throw new InvalidOperationException("No character physics found!");
@ -128,6 +154,7 @@ namespace BuildingTools
public void Ready()
{
new Harmony("BuildingTools").PatchAll(Assembly.GetExecutingAssembly());
}
public EntitiesDB entitiesDB { get; set; }
@ -137,5 +164,19 @@ namespace BuildingTools
public string Name { get; } = "BuildingToolsNoClipEngine";
public bool isRemovable { get; } = true;
[HarmonyPatch]
public static class Patch
{
public static void Prefix(CompoundCollider __instance, CollisionFilter value)
{
Console.WriteLine("Compound collider filter changed for " + __instance.Type + " to " + value.BelongsTo);
}
public static MethodBase TargetMethod()
{
return typeof(CompoundCollider).GetProperty("Filter").SetMethod;
}
}
}
}