1
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"name": "BITFALL.Entities.Physics.Runtime",
|
||||
"rootNamespace": "",
|
||||
"references": [
|
||||
"GUID:14fe60d984bf9f84eac55c6ea033a8f4",
|
||||
"GUID:f51ebe6a0ceec4240a699833d6309b23",
|
||||
"GUID:709caf8d7fb6ef24bbba0ab9962a3ad0",
|
||||
"GUID:a3de65b07192e7d49bad7b4032d681de",
|
||||
"GUID:7efac18f239530141802fb139776f333",
|
||||
"GUID:2b6752324f5c76d4cad13e2095c77b9e"
|
||||
],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": false,
|
||||
"precompiledReferences": [],
|
||||
"autoReferenced": true,
|
||||
"defineConstraints": [],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
218
Assets/Artists/Scripts/Entities/Physics/EntityPhysics.cs
Normal file
218
Assets/Artists/Scripts/Entities/Physics/EntityPhysics.cs
Normal file
@@ -0,0 +1,218 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using BITFALL.Cosmetic;
|
||||
using BITKit;
|
||||
using BITKit.Entities;
|
||||
using BITKit.Entities.Physics;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
namespace BITFALL.Entities
|
||||
{
|
||||
[CustomType(typeof(IEntityPhysics))]
|
||||
public class EntityPhysics : EntityBehavior,IEntityPhysics
|
||||
{
|
||||
[SerializeField] private Animator animator;
|
||||
|
||||
private Rigidbody[] rigidbodies=Array.Empty<Rigidbody>();
|
||||
private Collider[] colliders=Array.Empty<Collider>();
|
||||
private Joint[] joints=Array.Empty<Joint>();
|
||||
private new Rigidbody rigidbody;
|
||||
|
||||
[Inject(true)] private IHealth _health;
|
||||
[Inject(true)] private IEntityOverride _override;
|
||||
[Inject(true)] private ICosmeticService _cosmeticService;
|
||||
|
||||
private readonly Dictionary<Joint,ConfigurableJointMotion> _jointXMotions=new();
|
||||
private readonly Dictionary<Joint,ConfigurableJointMotion> _jointYMotions=new();
|
||||
private readonly Dictionary<Joint,ConfigurableJointMotion> _jointZMotions=new();
|
||||
private readonly Dictionary<Joint,ConfigurableJointMotion> _jointAngularXMotions=new();
|
||||
private readonly Dictionary<Joint,ConfigurableJointMotion> _jointAngularYMotions=new();
|
||||
private readonly Dictionary<Joint,ConfigurableJointMotion> _jointAngularZMotions=new();
|
||||
|
||||
private readonly IntervalUpdate _intervalUpdate=new(0.01f);
|
||||
private bool isDirty = false;
|
||||
private readonly ValidHandle allowUpdate = new();
|
||||
public override void OnAwake()
|
||||
{
|
||||
OnCosmeticsChanged();
|
||||
|
||||
if (_health is not null)
|
||||
_health.OnSetAlive += SetDirty;
|
||||
|
||||
if (_override is not null)
|
||||
_override.OnOverride += SetDirty;
|
||||
|
||||
if (_cosmeticService is not null)
|
||||
_cosmeticService.OnCosmeticsChanged += OnCosmeticsChanged;
|
||||
|
||||
destroyCancellationToken.Register(() =>
|
||||
{
|
||||
if (_cosmeticService is not null)
|
||||
_cosmeticService.OnCosmeticsChanged -= OnCosmeticsChanged;
|
||||
});
|
||||
|
||||
allowUpdate.AddElement(this);
|
||||
}
|
||||
public override void OnLateUpdate(float deltaTime)
|
||||
{
|
||||
base.OnLateUpdate(deltaTime);
|
||||
if (
|
||||
destroyCancellationToken.IsCancellationRequested
|
||||
|| !isDirty
|
||||
|| !_intervalUpdate.AllowUpdateWithoutReset
|
||||
|| allowUpdate.Allow is false
|
||||
) return;
|
||||
isDirty = false;
|
||||
EnsureConf();
|
||||
}
|
||||
[BIT]
|
||||
private void OnCosmeticsChanged()
|
||||
{
|
||||
allowUpdate.SetDisableElements(this);
|
||||
colliders = Array.Empty<Collider>();
|
||||
rigidbodies = Array.Empty<Rigidbody>();
|
||||
joints = Array.Empty<Joint>();
|
||||
|
||||
var rigidbodyList = new List<Rigidbody>();
|
||||
var collidersList = new List<Collider>();
|
||||
var jointList = new List<Joint>();
|
||||
foreach (var physicsComponent in GetComponentsInChildren<EntityPhysicsComponent>(true))
|
||||
{
|
||||
if(physicsComponent.Rigidbody)
|
||||
rigidbodyList.Add(physicsComponent.Rigidbody);
|
||||
if(physicsComponent.Collider)
|
||||
collidersList.Add(physicsComponent.Collider);
|
||||
if (physicsComponent.Joint)
|
||||
jointList.Add(physicsComponent.Joint);
|
||||
}
|
||||
colliders = collidersList.ToArray();
|
||||
rigidbodies = rigidbodyList.ToArray();
|
||||
joints = jointList.ToArray();
|
||||
|
||||
_jointXMotions.Clear();
|
||||
_jointYMotions.Clear();
|
||||
_jointZMotions.Clear();
|
||||
_jointAngularXMotions.Clear();
|
||||
_jointAngularYMotions.Clear();
|
||||
_jointAngularZMotions.Clear();
|
||||
foreach (var x in joints)
|
||||
{
|
||||
switch (x)
|
||||
{
|
||||
case ConfigurableJoint configurableJoint:
|
||||
_jointXMotions.Add(configurableJoint, configurableJoint.xMotion);
|
||||
_jointYMotions.Add(configurableJoint, configurableJoint.yMotion);
|
||||
_jointZMotions.Add(configurableJoint, configurableJoint.zMotion);
|
||||
_jointAngularXMotions.Add(configurableJoint, configurableJoint.angularXMotion);
|
||||
_jointAngularYMotions.Add(configurableJoint, configurableJoint.angularYMotion);
|
||||
_jointAngularZMotions.Add(configurableJoint, configurableJoint.angularZMotion);
|
||||
break;
|
||||
}
|
||||
}
|
||||
rigidbody = animator.GetBoneTransform(HumanBodyBones.Hips)?.GetComponent<Rigidbody>();
|
||||
EnsureConf();
|
||||
isDirty = false;
|
||||
allowUpdate.RemoveDisableElements(this);
|
||||
}
|
||||
private void SetDirty(bool any) => SetDirty();
|
||||
private void SetDirty()
|
||||
{
|
||||
_intervalUpdate.Reset();
|
||||
isDirty = true;
|
||||
}
|
||||
private void EnsureConf()
|
||||
{
|
||||
var allow = (_health, _override) switch
|
||||
{
|
||||
(not null,not null)=>_health.IsAlive || _override.IsOvering,
|
||||
(not null,null)=>_health.IsAlive,
|
||||
(null,not null)=>_override.IsOvering,
|
||||
_=>true,
|
||||
};
|
||||
if (animator)
|
||||
{
|
||||
animator.enabled = allow;
|
||||
}
|
||||
try
|
||||
{
|
||||
foreach (var x in rigidbodies)
|
||||
{
|
||||
try
|
||||
{
|
||||
x.isKinematic= allow;
|
||||
x.gameObject.SetActive(!allow);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
BIT4Log.LogException(e);
|
||||
}
|
||||
}
|
||||
foreach (var x in colliders)
|
||||
{
|
||||
x.enabled = !allow;
|
||||
}
|
||||
|
||||
OnSetPhysics?.Invoke(!allow);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
return;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
BIT4Log.LogException(e);
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var joint in joints)
|
||||
{
|
||||
switch (joint)
|
||||
{
|
||||
case ConfigurableJoint configurableJoint:
|
||||
configurableJoint.xMotion = allow ? _jointXMotions[joint] : ConfigurableJointMotion.Free;
|
||||
configurableJoint.yMotion = allow ? _jointYMotions[joint] : ConfigurableJointMotion.Free;
|
||||
configurableJoint.zMotion = allow ? _jointZMotions[joint] : ConfigurableJointMotion.Free;
|
||||
configurableJoint.angularXMotion =
|
||||
allow ? _jointAngularXMotions[joint] : ConfigurableJointMotion.Free;
|
||||
configurableJoint.angularYMotion =
|
||||
allow ? _jointAngularYMotions[joint] : ConfigurableJointMotion.Free;
|
||||
configurableJoint.angularZMotion =
|
||||
allow ? _jointAngularZMotions[joint] : ConfigurableJointMotion.Free;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
public Vector3 Center => rigidbody.worldCenterOfMass;
|
||||
public bool IsPhysics { get; private set; }
|
||||
public Vector3 Velocity
|
||||
{
|
||||
get => rigidbody ? rigidbody.velocity : default;
|
||||
set
|
||||
{
|
||||
foreach (var x in rigidbodies)
|
||||
{
|
||||
x.velocity = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
public Action<bool> OnSetPhysics { get; set; }
|
||||
public void AddForce(Vector3 force, ForceMode mode = ForceMode.Force)
|
||||
{
|
||||
foreach (var x in rigidbodies)
|
||||
{
|
||||
x.AddForce(force, mode);
|
||||
}
|
||||
}
|
||||
public void AddTorque(Vector3 torque, ForceMode mode = ForceMode.Force)
|
||||
{
|
||||
foreach (var x in rigidbodies)
|
||||
{
|
||||
x.AddTorque(torque, mode);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,31 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using BITKit;
|
||||
using UnityEngine;
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor;
|
||||
#endif
|
||||
namespace BITFALL.Entities
|
||||
{
|
||||
public class EntityPhysicsComponent : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private new Collider collider;
|
||||
[SerializeField] private new Rigidbody rigidbody;
|
||||
[SerializeField] private Joint joint;
|
||||
public Collider Collider => collider;
|
||||
public Rigidbody Rigidbody => rigidbody;
|
||||
public Joint Joint => joint;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
[BIT]
|
||||
private void SetUp()
|
||||
{
|
||||
collider = GetComponent<Collider>();
|
||||
rigidbody = GetComponent<Rigidbody>();
|
||||
joint = GetComponent<Joint>();
|
||||
EditorUtility.SetDirty(this);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user