BITFALL/Assets/Artists/Scripts/Entities/Physics/EntityPhysics.cs

223 lines
8.2 KiB
C#
Raw Normal View History

2023-08-27 02:58:19 +08:00
using System;
2023-06-08 14:09:50 +08:00
using System.Collections;
using System.Collections.Generic;
2023-08-27 02:58:19 +08:00
using System.Threading;
using System.Threading.Tasks;
2024-03-29 00:58:24 +08:00
using BITFALL.Cosmetic;
using BITKit;
using BITKit.Entities;
2023-08-27 02:58:19 +08:00
using BITKit.Entities.Physics;
2024-03-29 00:58:24 +08:00
using Cysharp.Threading.Tasks;
2023-06-08 14:09:50 +08:00
using UnityEngine;
2024-03-29 00:58:24 +08:00
namespace BITFALL.Entities
2023-06-08 14:09:50 +08:00
{
2023-08-27 02:58:19 +08:00
[CustomType(typeof(IEntityPhysics))]
2023-10-30 01:25:53 +08:00
public class EntityPhysics : EntityBehavior,IEntityPhysics
2023-06-08 14:09:50 +08:00
{
2023-08-27 02:58:19 +08:00
[SerializeField] private Animator animator;
2024-03-29 00:58:24 +08:00
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;
2024-03-29 00:58:24 +08:00
[Inject(true)] private ICosmeticService _cosmeticService;
2023-10-25 17:26:42 +08:00
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();
2024-03-29 00:58:24 +08:00
private readonly IntervalUpdate _intervalUpdate=new(0.01f);
private bool isDirty = false;
private readonly ValidHandle allowUpdate = new();
2023-08-27 02:58:19 +08:00
public override void OnAwake()
2023-06-08 14:09:50 +08:00
{
2024-03-29 00:58:24 +08:00
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();
}
2024-04-19 00:40:34 +08:00
2024-03-29 00:58:24 +08:00
[BIT]
private void OnCosmeticsChanged()
{
allowUpdate.SetDisableElements(this);
colliders = Array.Empty<Collider>();
rigidbodies = Array.Empty<Rigidbody>();
joints = Array.Empty<Joint>();
2024-04-19 00:40:34 +08:00
2024-03-29 00:58:24 +08:00
var rigidbodyList = new List<Rigidbody>();
var collidersList = new List<Collider>();
var jointList = new List<Joint>();
foreach (var physicsComponent in GetComponentsInChildren<EntityPhysicsComponent>(true))
{
2024-04-19 00:40:34 +08:00
if (physicsComponent.Rigidbody)
2024-03-29 00:58:24 +08:00
rigidbodyList.Add(physicsComponent.Rigidbody);
2024-04-19 00:40:34 +08:00
if (physicsComponent.Collider)
2024-03-29 00:58:24 +08:00
collidersList.Add(physicsComponent.Collider);
if (physicsComponent.Joint)
jointList.Add(physicsComponent.Joint);
}
2024-04-19 00:40:34 +08:00
2024-03-29 00:58:24 +08:00
colliders = collidersList.ToArray();
rigidbodies = rigidbodyList.ToArray();
joints = jointList.ToArray();
2024-04-19 00:40:34 +08:00
2024-03-29 00:58:24 +08:00
_jointXMotions.Clear();
_jointYMotions.Clear();
_jointZMotions.Clear();
_jointAngularXMotions.Clear();
_jointAngularYMotions.Clear();
_jointAngularZMotions.Clear();
2023-10-25 17:26:42 +08:00
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);
2023-10-25 17:26:42 +08:00
break;
}
}
2024-04-19 00:40:34 +08:00
if (animator)
rigidbody = animator.GetBoneTransform(HumanBodyBones.Hips)?.GetComponent<Rigidbody>();
EnsureConf();
2024-03-29 00:58:24 +08:00
isDirty = false;
allowUpdate.RemoveDisableElements(this);
2023-06-08 14:09:50 +08:00
}
2024-04-19 00:40:34 +08:00
2024-03-29 00:58:24 +08:00
private void SetDirty(bool any) => SetDirty();
private void SetDirty()
2023-06-08 14:09:50 +08:00
{
2024-03-29 00:58:24 +08:00
_intervalUpdate.Reset();
isDirty = true;
}
2024-03-29 00:58:24 +08:00
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,
2024-03-29 00:58:24 +08:00
_=>true,
};
2023-08-27 02:58:19 +08:00
if (animator)
{
animator.enabled = allow;
}
2023-08-27 02:58:19 +08:00
try
2023-06-08 14:09:50 +08:00
{
2023-12-16 23:30:08 +08:00
foreach (var x in rigidbodies)
{
2024-03-29 00:58:24 +08:00
try
{
x.isKinematic= allow;
x.gameObject.SetActive(!allow);
}
catch (Exception e)
{
BIT4Log.LogException(e);
}
2023-12-16 23:30:08 +08:00
}
2024-03-29 00:58:24 +08:00
foreach (var x in colliders)
2023-12-16 23:30:08 +08:00
{
x.enabled = !allow;
2023-12-16 23:30:08 +08:00
}
2024-03-29 00:58:24 +08:00
OnSetPhysics?.Invoke(!allow);
2023-08-27 02:58:19 +08:00
}
catch (OperationCanceledException)
2023-06-08 14:09:50 +08:00
{
2024-03-29 00:58:24 +08:00
return;
}
catch (Exception e)
{
BIT4Log.LogException(e);
return;
2023-08-27 02:58:19 +08:00
}
2023-10-24 23:37:59 +08:00
2023-10-25 17:26:42 +08:00
foreach (var joint in joints)
2023-10-24 23:37:59 +08:00
{
2023-10-25 17:26:42 +08:00
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;
2023-10-25 17:26:42 +08:00
configurableJoint.angularXMotion =
allow ? _jointAngularXMotions[joint] : ConfigurableJointMotion.Free;
2023-10-25 17:26:42 +08:00
configurableJoint.angularYMotion =
allow ? _jointAngularYMotions[joint] : ConfigurableJointMotion.Free;
2023-10-25 17:26:42 +08:00
configurableJoint.angularZMotion =
allow ? _jointAngularZMotions[joint] : ConfigurableJointMotion.Free;
2023-10-25 17:26:42 +08:00
break;
}
2023-10-24 23:37:59 +08:00
}
2023-10-25 17:26:42 +08:00
2023-06-08 14:09:50 +08:00
}
2023-08-27 02:58:19 +08:00
public Vector3 Center => rigidbody.worldCenterOfMass;
public bool IsPhysics { get; private set; }
public Vector3 Velocity
{
2024-03-29 00:58:24 +08:00
get => rigidbody ? rigidbody.velocity : default;
2023-08-27 02:58:19 +08:00
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);
}
}
2023-06-08 14:09:50 +08:00
}
}