2023-09-01 14:35:05 +08:00
|
|
|
using System;
|
2023-06-05 19:57:17 +08:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
2023-09-01 14:35:05 +08:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using BITKit.Entities.Physics;
|
2023-06-05 19:57:17 +08:00
|
|
|
using UnityEngine;
|
|
|
|
namespace BITKit.Entities
|
|
|
|
{
|
2023-09-01 14:35:05 +08:00
|
|
|
[CustomType(typeof(IEntityPhysics))]
|
2023-11-06 01:17:23 +08:00
|
|
|
public class EntityPhysics : EntityBehavior,IEntityPhysics
|
2023-06-05 19:57:17 +08:00
|
|
|
{
|
2023-09-01 14:35:05 +08:00
|
|
|
[SerializeField] private Animator animator;
|
|
|
|
[SerializeField] private Rigidbody[] rigidbodies;
|
|
|
|
[SerializeField] private Collider[] ragdollColliders;
|
2023-11-06 01:17:23 +08:00
|
|
|
[SerializeField] private Joint[] joints;
|
2023-09-01 14:35:05 +08:00
|
|
|
[SerializeField] private new Rigidbody rigidbody;
|
|
|
|
private CancellationToken _cancellationToken;
|
2023-10-24 23:38:22 +08:00
|
|
|
[Inject]
|
|
|
|
private IHealth _health;
|
2023-11-06 01:17:23 +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();
|
|
|
|
|
2023-09-01 14:35:05 +08:00
|
|
|
public override void OnAwake()
|
2023-06-05 19:57:17 +08:00
|
|
|
{
|
2023-10-24 23:38:22 +08:00
|
|
|
_health.OnSetAlive += OnSetAlive;
|
|
|
|
_health.OnSetHealthPoint += OnSetHP;
|
2023-11-06 01:17:23 +08:00
|
|
|
_cancellationToken = UnityEntity.Get<CancellationToken>();
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2023-06-05 19:57:17 +08:00
|
|
|
}
|
2023-11-06 01:17:23 +08:00
|
|
|
|
|
|
|
private async void OnSetAlive(bool alive)
|
2023-06-05 19:57:17 +08:00
|
|
|
{
|
2023-09-01 14:35:05 +08:00
|
|
|
IsPhysics = !alive;
|
|
|
|
if (animator)
|
|
|
|
animator.enabled = alive;
|
|
|
|
try
|
2023-06-05 19:57:17 +08:00
|
|
|
{
|
2023-09-01 14:35:05 +08:00
|
|
|
await Task.Delay(10, _cancellationToken);
|
|
|
|
rigidbodies.ForEach(x => { x.isKinematic = alive; });
|
|
|
|
ragdollColliders.ForEach(x => { x.enabled = !alive; });
|
|
|
|
OnSetPhysics?.Invoke(!alive);
|
|
|
|
}
|
|
|
|
catch (OperationCanceledException)
|
2023-06-05 19:57:17 +08:00
|
|
|
{
|
2023-11-06 01:17:23 +08:00
|
|
|
|
2023-09-01 14:35:05 +08:00
|
|
|
}
|
2023-10-24 23:38:22 +08:00
|
|
|
|
2023-11-06 01:17:23 +08:00
|
|
|
foreach (var joint in joints)
|
2023-10-24 23:38:22 +08:00
|
|
|
{
|
2023-11-06 01:17:23 +08:00
|
|
|
switch (joint)
|
|
|
|
{
|
|
|
|
case ConfigurableJoint configurableJoint:
|
|
|
|
configurableJoint.xMotion = alive ? _jointXMotions[joint] : ConfigurableJointMotion.Free;
|
|
|
|
configurableJoint.yMotion = alive ? _jointYMotions[joint] : ConfigurableJointMotion.Free;
|
|
|
|
configurableJoint.zMotion = alive ? _jointZMotions[joint] : ConfigurableJointMotion.Free;
|
|
|
|
configurableJoint.angularXMotion =
|
|
|
|
alive ? _jointAngularXMotions[joint] : ConfigurableJointMotion.Free;
|
|
|
|
configurableJoint.angularYMotion =
|
|
|
|
alive ? _jointAngularYMotions[joint] : ConfigurableJointMotion.Free;
|
|
|
|
configurableJoint.angularZMotion =
|
|
|
|
alive ? _jointAngularZMotions[joint] : ConfigurableJointMotion.Free;
|
|
|
|
break;
|
|
|
|
}
|
2023-10-24 23:38:22 +08:00
|
|
|
}
|
2023-11-06 01:17:23 +08:00
|
|
|
|
2023-06-05 19:57:17 +08:00
|
|
|
}
|
2023-11-06 01:17:23 +08:00
|
|
|
|
2023-06-05 19:57:17 +08:00
|
|
|
public void OnSetHP(int hp)
|
|
|
|
{
|
|
|
|
}
|
2023-09-01 14:35:05 +08:00
|
|
|
|
|
|
|
public Vector3 Center => rigidbody.worldCenterOfMass;
|
|
|
|
public bool IsPhysics { get; private set; }
|
|
|
|
public Vector3 Velocity
|
|
|
|
{
|
|
|
|
get=>rigidbody.velocity;
|
|
|
|
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-05 19:57:17 +08:00
|
|
|
}
|
|
|
|
}
|