This commit is contained in:
CortexCore
2023-10-24 23:37:59 +08:00
parent 325f63d6bc
commit 3e39e627bc
388 changed files with 29043 additions and 889 deletions

View File

@@ -5,10 +5,12 @@ namespace BITKit
{
public class AnimatorHelper : MonoBehaviour
{
public Transform root;
void OnAnimatorMove()
[SerializeField] private Transform root;
private void OnAnimatorMove()
{
root?.SendMessage(nameof(OnAnimatorMove));
if (root is not null)
root.SendMessage(nameof(OnAnimatorMove));
}
}
}

View File

@@ -10,17 +10,12 @@ namespace BITKit.Animations
{
public class UnityAnimator : MonoBehaviour, IAnimator
{
[System.Serializable]
public record AnimatorPlayEvent
{
public string stateName;
public int index;
}
[System.Serializable]
[Serializable]
public class AnimatorLayerInfo
{
public string stateName;
public string fullStateName;
internal string entryName;
public AnimatorStateInfo currentState = new();
public event Action<string> onStateEnter;
public event Action<string> onStateExit;
@@ -33,6 +28,10 @@ namespace BITKit.Animations
onStateExit?.Invoke(name);
}
}
[Header(Constant.Header.Settings)]
[SerializeField] private bool debug;
[Header(Constant.Header.Components)]
public Animator animator;
public AnimatorLayerInfo this[int index]
@@ -54,10 +53,22 @@ namespace BITKit.Animations
return layerInfos[index];
}
}
public List<AnimatorLayerInfo> layerInfos = new();
[SerializeField] private List<AnimatorLayerInfo> layerInfos = new();
[Header(Constant.Header.InternalVariables)]
private readonly Dictionary<string,UnityAnimatorStateInfo> _registryStates=new ();
private void OnEnable()
{
animator.enabled = true;
}
private void OnDisable()
{
animator.enabled = false;
foreach (var x in layerInfos)
{
x.entryName = null;
}
}
public void CrossFade(string name, float duration, int index = 0, float normalizedTimeOffset = 0)
{
name = name.Replace(".", "_");
@@ -84,17 +95,29 @@ namespace BITKit.Animations
public void OnStateEnter(int index, string name)
{
if (debug)
{
BIT4Log.Log<UnityAnimator>($"OnEntry:{name}");
}
this[index].fullStateName = name;
foreach (var item in name.Split("."))
{
name = item;
break;
}
this[index].stateName = name;
this[index].stateName = this[index].entryName = name;
this[index].OnStateEnter(name);
}
public void OnStateExit(int index, string name)
{
if (string.IsNullOrEmpty(this[index].entryName))
{
return;
}
if (debug)
{
BIT4Log.Log<UnityAnimator>($"OnExit:{name}");
}
this[index].OnStateExit(name);
}
public float3 GetRootVelocity()

View File

@@ -62,6 +62,9 @@ namespace BITKit.Entities
/// </summary>
public interface IEntityMovement:IStateMachine<IEntityMovementState>
{
Vector3 Position { get; set; }
Quaternion Rotation { get; set; }
Vector3 Forward { get; }
/// <summary>
/// 视角中心,通常是摄像机的位置
/// </summary>

View File

@@ -13,16 +13,40 @@ namespace BITKit
[SerializeField] private new Rigidbody rigidbody;
[Inject] private IHealth _health;
[Inject(true)] private IEntityOverride _override;
#endregion
public override void OnStart()
{
if (_override is not null)
{
_override.OnOverride += (x) =>
{
agent.isStopped = x;
};
}
_health.OnSetAlive += OnSetAlive;
_health.OnSetHealthPoint += OnSetHP;
}
#region
public Vector3 Position
{
get=>transform.position;
set=>transform.position=value;
}
public Quaternion Rotation
{
get=>transform.rotation;
set=>transform.rotation=value;
}
public Vector3 Forward => transform.forward;
public Vector3 ViewCenter { get; set; }
public Quaternion ViewRotation { get; set; }
public Vector3 LocomotionBasedVelocity { get; private set; }

View File

@@ -0,0 +1,52 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
namespace BITKit.Entities.Movement
{
public class RigidbodyBasedMovement : StateBasedComponent<IEntityMovementState>,IEntityMovement
{
[SerializeField] private new Rigidbody rigidbody;
[SerializeField] private Animator animator;
public Vector3 Position { get; set; }
public Quaternion Rotation { get; set; }
public Vector3 Forward { get; }
public Vector3 ViewCenter { get; }
public Quaternion ViewRotation { get; }
public Vector3 LocomotionBasedVelocity { get; }
public Vector3 Velocity { get;private set; }
public Vector3 GroundVelocity { get; }
public Vector3 AngularVelocity { get; }
public bool IsGrounded { get; }
public void SyncMovement(Vector3 velocity, Vector3 position, Quaternion rotation, bool isGrounded)
{
}
public void Movement(Vector3 relativeVector)
{
}
public void Movement(InputAction.CallbackContext context)
{
}
public void ExecuteCommand<T>(T command = default)
{
}
public event Action<object> OnCommand;
public override void OnFixedUpdate(float deltaTime)
{
rigidbody.MovePosition(rigidbody.position + Velocity * deltaTime
);
}
private void OnAnimatorMove()
{
Velocity = animator.velocity;
}
}
}

View File

@@ -61,12 +61,13 @@ namespace BITKit.Entities
}
public record DamageMessage
{
public IEntity initiator;
public IEntity target;
public int damage;
public IDamagable hit;
public Location location;
public IDamageType damageType;
public IEntity Initiator;
public IEntity Target;
public bool RawDamage;
public int Damage;
public IDamagable Hit;
public Location Location;
public IDamageType DamageType;
}
public interface IDamageCallback
{
@@ -92,13 +93,13 @@ namespace BITKit.Entities
{
while (Messages.TryDequeue(out var damageMessage))
{
var unityEntity = (Entity)damageMessage.target;
var unityEntity = (Entity)damageMessage.Target;
if (unityEntity is null || !unityEntity.TryGetComponent<IHealth>(out var heal) || !heal.IsAlive) continue;
damageMessage.initiator?.Invoke(damageMessage);
damageMessage.target?.Invoke(damageMessage);
damageMessage.Initiator?.Invoke(damageMessage);
damageMessage.Target?.Invoke(damageMessage);
foreach (var x in damageMessage.target?.GetCallbacks<IDamageCallback>()!)
foreach (var x in damageMessage.Target?.GetCallbacks<IDamageCallback>()!)
{
x.OnGetDamage(damageMessage);
}

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using UnityEngine;
using UnityEngine.Events;
@@ -19,7 +20,7 @@ namespace BITKit.Entities
/// <summary>
/// 当受到伤害时的回调
/// </summary>
public event Func<DamageMessage,int,int> OnDamage;
public event Func<DamageMessage,int,int> OnDamageFactory;
int HealthPoint { get; set; }
int MaxHealthPoint { get; set; }
bool IsAlive { get; }
@@ -33,7 +34,7 @@ namespace BITKit.Entities
public event Action<int> OnSetHealthPoint;
public event Action<bool> OnSetAlive;
public event Func<DamageMessage,int, int> OnDamage;
public event Func<DamageMessage,int, int> OnDamageFactory;
public int HealthPoint
{
@@ -82,11 +83,13 @@ namespace BITKit.Entities
private void OnGetDamage(DamageMessage damageMessage)
{
if (damageMessage.target != entity) return;
var damage = damageMessage.damage;
foreach (var x in OnDamage.CastAsFunc())
if (damageMessage.Target != entity) return;
if (IsAlive is false) return;
var damage = damageMessage.Damage;
foreach (var x in OnDamageFactory.CastAsFunc().Reverse())
{
damage = x.Invoke(damageMessage,damage);
damage = x.Invoke(damageMessage,damage);
if (damage <= 0) break;
}
AddHP(-damage);
}

View File

@@ -21,7 +21,7 @@ namespace BITKit.Entities
}
private void OnGetDamage(DamageMessage obj)
{
if (obj.target != entity) return;
if (obj.Target != entity) return;
DamageMessages.Enqueue(obj);
onGetDamage?.Invoke(obj);
foreach (var x in callbacks)

View File

@@ -17,6 +17,8 @@ namespace BITKit.Entities.Player
private IntervalUpdate cd = new(0.08f);
[Inject]
private IHealth _health;
[Inject]
private InputActionGroup _inputActionReference;
public override void OnStart()
{

View File

@@ -13,6 +13,7 @@ namespace BITKit.Entities
[SerializeField] private Animator animator;
[SerializeField] private Rigidbody[] rigidbodies;
[SerializeField] private Collider[] ragdollColliders;
[SerializeField] private Joint joint;
[SerializeField] private new Rigidbody rigidbody;
private CancellationToken _cancellationToken;
[Inject]
@@ -39,6 +40,11 @@ namespace BITKit.Entities
{
}
if (alive is false && joint is not null)
{
Destroy(joint);
}
}
public void OnSetHP(int hp)

View File

@@ -84,9 +84,16 @@ namespace BITKit.Entities
{
try
{
foreach (var x in entityComponents)
var monoBehaviours = GetComponentsInChildren<MonoBehaviour>(true);
foreach (var x in monoBehaviours)
{
foreach (var att in x.GetType().GetCustomAttributes<CustomTypeAttribute>())
foreach (var att in x
//.GetCustomAttributes<CustomTypeAttribute>()
.GetType()
//.GetCustomAttributes<CustomTypeAttribute>()
.GetCustomAttributes()
.OfType<CustomTypeAttribute>()
)
{
genericEvent.Set(att.Type,x);
genericEvent.Set(att.Type.FullName, x);
@@ -94,7 +101,7 @@ namespace BITKit.Entities
}
genericEvent.Set(x.GetType(),x);
}
foreach (var x in GetComponentsInChildren<MonoBehaviour>(true))
foreach (var x in monoBehaviours)
{
Inject(x);
}

View File

@@ -9,6 +9,7 @@ namespace BITKit.Entities
bool IsOvering { get; }
void AddOverride(object key);
void RemoveOverride(object key);
event Action<bool> OnOverride;
}
public interface IEntityOverrideCallback
{
@@ -17,10 +18,13 @@ namespace BITKit.Entities
[CustomType(typeof(IEntityOverride))]
public class EntityOverride : EntityComponent,IEntityOverride
{
[SerializeField,ReadOnly] private bool isOvering;
public bool IsOvering => _allowOverrideHandle;
private readonly ValidHandle _allowOverrideHandle = new();
public void AddOverride(object key) => _allowOverrideHandle.AddElement(key);
public void RemoveOverride(object key)=>_allowOverrideHandle.RemoveElement(key);
public event Action<bool> OnOverride;
public override void Initialize(IEntity _entity)
{
base.Initialize(_entity);
@@ -33,10 +37,8 @@ namespace BITKit.Entities
}
private void Override(bool @override)
{
foreach (var x in entity.GetCallbacks<IEntityOverrideCallback>())
{
x.OnEntryOverride(@override);
}
OnOverride?.Invoke(@override);
isOvering=@override;
}
}
}

View File

@@ -57,17 +57,16 @@ namespace BITKit
private void Init()
{
if (allowGlobalActivation)
BITInputSystem.AllowInput.AddListener(Listen);
allowInput.AddListener(Allow);
BITInputSystem.AllowInput.AddListener(ListenGlobalInput);
allowInput.AddListener(AllowInput);
}
private void Listen(bool allowInput)
private void ListenGlobalInput(bool allowInput)
{
this.allowInput.SetElements(lockFile, allowInput);
this.allowInput.SetDisableElements(lockFile, !allowInput);
}
private void Allow(bool allow)
private void AllowInput(bool allow)
{
foreach (var action in actions)
{
@@ -80,7 +79,6 @@ namespace BITKit
action.Disable();
}
}
isEnabled = allow;
}

View File

@@ -0,0 +1,26 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MyNamespace
{
public class JointBasedSync : MonoBehaviour
{
[SerializeField] private ConfigurableJoint joint;
[SerializeField] private Transform animate;
private Quaternion _initialRotation;
private void Start()
{
_initialRotation = joint.transform.localRotation;
}
private void FixedUpdate()
{
//joint.targetPosition = animate.localPosition;
joint.targetRotation = Quaternion.Inverse(animate.localRotation) * _initialRotation;
}
}
}

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using BITKit.Core.Tuple;
using UnityEngine;
namespace BITKit.Physics
@@ -19,6 +20,10 @@ namespace BITKit.Physics
[SerializeField] private float positionSpring;
[SerializeField] private float positionDamper;
[SerializeField] private float maximumForce;
[SerializeField] private Optional<UnityTuple<Rigidbody, Transform>> rootSync;
[SerializeField] private Optional<ConfigurableJointMotion> overrideMotion;
[SerializeField] private Optional<ConfigurableJointMotion> overrideAngularMotion;
private void Start()
{
@@ -33,16 +38,46 @@ namespace BITKit.Physics
var drive = new JointDrive
{
positionDamper = positionDamper,
positionSpring = positionSpring,
positionSpring =Mathf.Lerp(0,positionSpring,Blend),
maximumForce = maximumForce,
};
foreach (var jointConfigure in jointConfigures)
{
jointConfigure.joint.angularXDrive = drive;
jointConfigure.joint.angularYZDrive = drive;
jointConfigure.joint.targetRotation = Quaternion.Inverse(jointConfigure.animate.localRotation) *
jointConfigure.InitialRotation;
// jointConfigure.joint.targetRotation =
// Quaternion.Lerp(
// Quaternion.identity,
// Quaternion.Inverse(jointConfigure.animate.localRotation) * jointConfigure.InitialRotation,
// Blend
// );
jointConfigure.joint.targetRotation = Quaternion.Inverse(jointConfigure.animate.localRotation) *
jointConfigure.InitialRotation;
jointConfigure.joint.targetPosition = jointConfigure.animate.localPosition;
if (overrideAngularMotion.Allow)
{
jointConfigure.joint.angularXMotion = overrideAngularMotion.Value;
jointConfigure.joint.angularYMotion = overrideAngularMotion.Value;
jointConfigure.joint.angularZMotion = overrideAngularMotion.Value;
}
if (overrideMotion.Allow)
{
jointConfigure.joint.xMotion = overrideMotion.Value;
jointConfigure.joint.yMotion = overrideMotion.Value;
jointConfigure.joint.zMotion = overrideMotion.Value;
}
}
if (rootSync.Allow)
{
var root = rootSync.Value;
root.Item1.transform.localPosition = root.Item2.localPosition;
root.Item1.MoveRotation(root.Item2.rotation);
}
}
}

View File

@@ -17,5 +17,15 @@ namespace BITKit
catch(MissingReferenceException){}
}
public static async void Explosion(Vector3 position,float radius,LayerMask layerMask,float force)
{
await UniTask.DelayFrame(8);
foreach (var x in UnityEngine.Physics.OverlapSphere(position,radius,layerMask))
{
if(x.attachedRigidbody is null)continue;
x.attachedRigidbody.AddExplosionForce(force,position,radius);
//Debug.Log(x);
}
}
}
}

View File

@@ -24,6 +24,10 @@ namespace BITKit.UX
[SerializeField] private bool allowCursor;
[SerializeField] private bool allowInput;
[SerializeField] private bool autoEntry;
protected readonly InputActionGroup inputActionGroup = new()
{
allowGlobalActivation = false
};
public bool IsAnimate => isAnimate;
public bool IsValid => cancellationToken.IsCancellationRequested is false;
public string Index { get; private set; }
@@ -50,12 +54,14 @@ namespace BITKit.UX
{
OnEntryOrExit(true);
document.rootVisualElement.SetActive(true);
inputActionGroup.allowInput.AddElement(this);
OnEntry?.Invoke();
}
void IUXPanel.Exit()
{
if (IsValid is false) return;
OnEntryOrExit(false);
inputActionGroup.allowInput.RemoveElement(this);
try
{
document.rootVisualElement.SetActive(false);

View File

@@ -512,6 +512,12 @@ namespace BITKit
}
public static partial class UIToolkitExtensions
{
public static VisualElement Create(this VisualElement self, VisualTreeAsset asset)
{
var clone = asset.CloneTree();
self.Add(clone);
return clone;
}
public static T Create<T>(this VisualElement self, string name = Constant.EmetyString) where T : VisualElement, new()
{
var element = new T();
@@ -522,6 +528,16 @@ namespace BITKit
self.Add(element);
return element;
}
public static T Create<T>(this VisualElement self,Func<VisualElement> createFactory, string name = Constant.EmetyString) where T : VisualElement, new()
{
var element = createFactory.Invoke();
if (string.IsNullOrEmpty(name) is false)
{
element.name = name;
}
self.Add(element);
return element as T;
}
public static bool GetActive(this VisualElement self) => self.style.display.value == DisplayStyle.Flex;
public static void SetActive(this VisualElement self, bool active)
{