Throwing Knife Added
This commit is contained in:
@@ -20,6 +20,11 @@ namespace BITKit.Entities.Movement
|
||||
public Vector3 GroundVelocity { get; }
|
||||
public Vector3 AngularVelocity { get; }
|
||||
public bool IsGrounded { get; }
|
||||
|
||||
[Inject(true)]
|
||||
private IHealth health;
|
||||
|
||||
|
||||
public void SyncMovement(Vector3 velocity, Vector3 position, Quaternion rotation, bool isGrounded)
|
||||
{
|
||||
}
|
||||
@@ -37,6 +42,20 @@ namespace BITKit.Entities.Movement
|
||||
}
|
||||
|
||||
public event Action<object> OnCommand;
|
||||
public override void OnAwake()
|
||||
{
|
||||
base.OnAwake();
|
||||
if (health is not null)
|
||||
{
|
||||
health.OnSetAlive += OnSetAlive;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnSetAlive(bool obj)
|
||||
{
|
||||
rigidbody.isKinematic = !obj;
|
||||
}
|
||||
|
||||
public override void OnFixedUpdate(float deltaTime)
|
||||
{
|
||||
rigidbody.MovePosition(rigidbody.position + Velocity * deltaTime
|
||||
|
@@ -1,5 +1,8 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using BITKit.Entities;
|
||||
using UnityEngine;
|
||||
|
||||
@@ -10,44 +13,37 @@ namespace BITKit
|
||||
[SerializeField] private IntervalUpdate respawnInterval;
|
||||
private bool requestRespawn;
|
||||
[Inject] private IHealth _health;
|
||||
private int _initialHp;
|
||||
|
||||
private CancellationTokenSource _cancellationTokenSource;
|
||||
public override void OnAwake()
|
||||
{
|
||||
_initialHp=_health.HealthPoint;
|
||||
_health.OnSetAlive += OnSetAlive;
|
||||
_health.OnSetHealthPoint += OnSetHP;
|
||||
}
|
||||
|
||||
public override void OnUpdate(float deltaTime)
|
||||
{
|
||||
if (requestRespawn && respawnInterval.AllowUpdate)
|
||||
{
|
||||
requestRespawn = false;
|
||||
Execute();
|
||||
}
|
||||
}
|
||||
|
||||
public void OnSetAlive(bool alive)
|
||||
private async void OnSetAlive(bool alive)
|
||||
{
|
||||
if (alive)
|
||||
{
|
||||
requestRespawn = false;
|
||||
_cancellationTokenSource?.Cancel();
|
||||
}
|
||||
else
|
||||
{
|
||||
respawnInterval.Reset();
|
||||
requestRespawn = true;
|
||||
_cancellationTokenSource?.Cancel();
|
||||
_cancellationTokenSource = new CancellationTokenSource();
|
||||
try
|
||||
{
|
||||
await Task.Delay(TimeSpan.FromSeconds(respawnInterval.Interval), _cancellationTokenSource.Token);
|
||||
destroyCancellationToken.ThrowIfCancellationRequested();
|
||||
Execute();
|
||||
}
|
||||
catch (OperationCanceledException){}
|
||||
}
|
||||
}
|
||||
public void OnSetHP(int hp)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void Execute()
|
||||
{
|
||||
if (TryGetComponent<IHealth>(out var health))
|
||||
{
|
||||
health.HealthPoint = 100;
|
||||
}
|
||||
if (_health.IsAlive is false)
|
||||
_health.HealthPoint = _initialHp;
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -13,18 +13,40 @@ namespace BITKit.Entities
|
||||
[SerializeField] private Animator animator;
|
||||
[SerializeField] private Rigidbody[] rigidbodies;
|
||||
[SerializeField] private Collider[] ragdollColliders;
|
||||
[SerializeField] private Joint joint;
|
||||
[SerializeField] private Joint[] joints;
|
||||
[SerializeField] private new Rigidbody rigidbody;
|
||||
private CancellationToken _cancellationToken;
|
||||
[Inject]
|
||||
private IHealth _health;
|
||||
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();
|
||||
|
||||
public override void OnAwake()
|
||||
{
|
||||
_health.OnSetAlive += OnSetAlive;
|
||||
_health.OnSetHealthPoint += OnSetHP;
|
||||
_cancellationToken = entity.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;
|
||||
}
|
||||
}
|
||||
}
|
||||
private async void OnSetAlive(bool alive)
|
||||
|
||||
private async void OnSetAlive(bool alive)
|
||||
{
|
||||
IsPhysics = !alive;
|
||||
if (animator)
|
||||
@@ -38,15 +60,29 @@ namespace BITKit.Entities
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
if (alive is false && joint is not null)
|
||||
foreach (var joint in joints)
|
||||
{
|
||||
Destroy(joint);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void OnSetHP(int hp)
|
||||
{
|
||||
}
|
||||
|
@@ -61,7 +61,7 @@ namespace BITKit.Entities
|
||||
}
|
||||
else if(attribute?.CanBeNull is false)
|
||||
{
|
||||
BIT4Log.Warning<Entity>($"{name}未找到{type.FullName}");
|
||||
BIT4Log.Warning<Entity>($"{name}未找到{obj.GetType().Name}需要的{type.FullName}");
|
||||
BIT4Log.Warning<Entity>(genericEvent.GetDiagnostics());
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,56 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Animations;
|
||||
using UnityEngine.Assertions;
|
||||
|
||||
namespace BITKit
|
||||
{
|
||||
public static class ConstraintExtensions
|
||||
{
|
||||
public static ParentConstraint SetParentConstraint(this Transform target, Transform parentSource,
|
||||
ParentConstraint constraint = null)
|
||||
{
|
||||
Assert.IsTrue(!constraint || target == constraint.transform);
|
||||
|
||||
if (!constraint)
|
||||
{
|
||||
if (!parentSource) return null;
|
||||
constraint = target.gameObject.AddComponent<ParentConstraint>();
|
||||
}
|
||||
|
||||
// 清空已有约束
|
||||
constraint.constraintActive = false;
|
||||
for (int i = constraint.sourceCount - 1; i >= 0; i--)
|
||||
{
|
||||
constraint.RemoveSource(i);
|
||||
}
|
||||
|
||||
// 无约束
|
||||
if (!parentSource) return constraint;
|
||||
|
||||
// 设置新约束
|
||||
constraint.AddSource(new ConstraintSource
|
||||
{
|
||||
sourceTransform = parentSource,
|
||||
weight = 1,
|
||||
});
|
||||
|
||||
// 设置 Position offset
|
||||
var positionOffset = parentSource.InverseTransformPoint(target.position);
|
||||
constraint.SetTranslationOffset(0, positionOffset);
|
||||
|
||||
// 设置 Rotation offset
|
||||
var localForward = parentSource.InverseTransformDirection(target.forward);
|
||||
var localUpward = parentSource.InverseTransformDirection(target.up);
|
||||
var rotationOffset = Quaternion.LookRotation(localForward, localUpward).eulerAngles;
|
||||
constraint.SetRotationOffset(0, rotationOffset);
|
||||
|
||||
// 激活约束
|
||||
constraint.constraintActive = true;
|
||||
|
||||
return constraint;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -24,7 +24,6 @@ namespace BITKit.Physics
|
||||
[SerializeField] private Optional<ConfigurableJointMotion> overrideMotion;
|
||||
[SerializeField] private Optional<ConfigurableJointMotion> overrideAngularMotion;
|
||||
|
||||
|
||||
private void Start()
|
||||
{
|
||||
foreach (var x in jointConfigures)
|
||||
@@ -45,12 +44,10 @@ namespace BITKit.Physics
|
||||
{
|
||||
jointConfigure.joint.angularXDrive = drive;
|
||||
jointConfigure.joint.angularYZDrive = drive;
|
||||
// jointConfigure.joint.targetRotation =
|
||||
// Quaternion.Lerp(
|
||||
// Quaternion.identity,
|
||||
// Quaternion.Inverse(jointConfigure.animate.localRotation) * jointConfigure.InitialRotation,
|
||||
// Blend
|
||||
// );
|
||||
jointConfigure.joint.xDrive = drive;
|
||||
jointConfigure.joint.yDrive = drive;
|
||||
jointConfigure.joint.zDrive = drive;
|
||||
|
||||
jointConfigure.joint.targetRotation = Quaternion.Inverse(jointConfigure.animate.localRotation) *
|
||||
jointConfigure.InitialRotation;
|
||||
jointConfigure.joint.targetPosition = jointConfigure.animate.localPosition;
|
||||
|
Reference in New Issue
Block a user