1
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"name": "BITKit.Entities.Physics.Runtime",
|
||||
"rootNamespace": "",
|
||||
"references": [
|
||||
"GUID:14fe60d984bf9f84eac55c6ea033a8f4",
|
||||
"GUID:f51ebe6a0ceec4240a699833d6309b23",
|
||||
"GUID:709caf8d7fb6ef24bbba0ab9962a3ad0",
|
||||
"GUID:a3de65b07192e7d49bad7b4032d681de",
|
||||
"GUID:7efac18f239530141802fb139776f333"
|
||||
],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": false,
|
||||
"precompiledReferences": [],
|
||||
"autoReferenced": true,
|
||||
"defineConstraints": [],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: be39401f40b20344a8e59d5ddab532bc
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1b5481cf34bf4a145898ac81e58abf8f
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"name": "BITKit.Entities.Physics"
|
||||
}
|
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a3de65b07192e7d49bad7b4032d681de
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace BITKit.Entities.Physics
|
||||
{
|
||||
/// <summary>
|
||||
/// 实体物理接口
|
||||
/// </summary>
|
||||
public interface IEntityPhysics
|
||||
{
|
||||
Vector3 Center { get; }
|
||||
bool IsPhysics { get; }
|
||||
Vector3 Velocity { get; set; }
|
||||
Action<bool> OnSetPhysics { get; set; }
|
||||
void AddForce(Vector3 force, ForceMode mode = ForceMode.Force);
|
||||
void AddTorque(Vector3 torque, ForceMode mode = ForceMode.Force);
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3d2b4cbe319042c42a1d9c2b825a499e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -1,32 +1,74 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using BITKit.Entities.Physics;
|
||||
using UnityEngine;
|
||||
namespace BITKit.Entities
|
||||
{
|
||||
public class EntityPhysics : EntityComponent, IHealthCallback
|
||||
[CustomType(typeof(IEntityPhysics))]
|
||||
public class EntityPhysics : EntityComponent,IEntityPhysics, IHealthCallback
|
||||
{
|
||||
public Animator animator;
|
||||
public Rigidbody[] rigidbodies;
|
||||
public Collider[] ragdollColliders;
|
||||
public override void OnStart()
|
||||
[SerializeField] private Animator animator;
|
||||
[SerializeField] private Rigidbody[] rigidbodies;
|
||||
[SerializeField] private Collider[] ragdollColliders;
|
||||
[SerializeField] private new Rigidbody rigidbody;
|
||||
private CancellationToken _cancellationToken;
|
||||
public override void OnAwake()
|
||||
{
|
||||
entity.RegisterCallback<IHealthCallback>(this);
|
||||
_cancellationToken = entity.Get<CancellationToken>();
|
||||
}
|
||||
void IHealthCallback.OnSetAlive(bool alive)
|
||||
async void IHealthCallback.OnSetAlive(bool alive)
|
||||
{
|
||||
if(animator)
|
||||
animator.enabled = alive;
|
||||
rigidbodies.ForEach(x =>
|
||||
IsPhysics = !alive;
|
||||
if (animator)
|
||||
animator.enabled = alive;
|
||||
try
|
||||
{
|
||||
x.isKinematic = alive;
|
||||
});
|
||||
ragdollColliders.ForEach(x =>
|
||||
await Task.Delay(10, _cancellationToken);
|
||||
rigidbodies.ForEach(x => { x.isKinematic = alive; });
|
||||
ragdollColliders.ForEach(x => { x.enabled = !alive; });
|
||||
OnSetPhysics?.Invoke(!alive);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
x.enabled = !alive;
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
public void OnSetHP(int hp)
|
||||
{
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user