This commit is contained in:
CortexCore
2023-10-29 15:27:13 +08:00
parent c5f638d9d2
commit c7b6ddbf70
73 changed files with 2158 additions and 494 deletions

View File

@@ -26,6 +26,7 @@ namespace BITKit
}
public static class Time
{
public static float ElapsedTime { get; internal set; }
public static float DeltaTime { get; internal set; }
public static double TimeAsDouble { get; internal set; }

View File

@@ -18,6 +18,7 @@ namespace BITKit.Core.Entites
bool RegisterComponent<T>(T component);
IServiceProvider ServiceProvider { get; }
void Inject(object obj);
}
/// <summary>
/// 基本实体组件

View File

@@ -87,30 +87,30 @@ namespace BITKit
{
var currentIndex = m_index;
m_index = index;
if (currentIndex is not -1 && list.TryGetElementAt(currentIndex, out var element))
if (currentIndex is not -1 && list.TryGetElementAt(currentIndex, out var currentElement))
{
element.Exit();
currentElement.Exit();
try
{
await element.ExitAsync();
await currentElement.ExitAsync();
}
catch (OperationCanceledException)
{
}
element.IsEntered = false;
OnExit?.Invoke(element);
currentElement.IsEntered = false;
OnExit?.Invoke(currentElement);
}
if (index is not -1 && list.TryGetElementAt(index, out element))
if (index is not -1 && list.TryGetElementAt(index, out var nextElement))
{
element.IsEntered = true;
element.Entry();
nextElement.IsEntered = true;
nextElement.Entry();
try
{
await element.EntryAsync();
await nextElement.EntryAsync();
}
catch (OperationCanceledException){}
OnEntry?.Invoke(element);
OnEntry?.Invoke(nextElement);
}
}
completed = true;

View File

@@ -0,0 +1,7 @@
using System.Collections;
using System.Collections.Generic;
namespace BITKit.UniversalInputSystem
{
}

View File

@@ -3,6 +3,25 @@ using System.Collections.Generic;
namespace BITKit.StateMachine
{
public struct EmptyState:IState
{
public bool Enabled { get; set; }
public void Initialize()
{
}
public void OnStateEntry(IState old)
{
}
public void OnStateUpdate(float deltaTime)
{
}
public void OnStateExit(IState old, IState newState)
{
}
}
public interface IState
{
bool Enabled { get; set; }

View File

@@ -0,0 +1,53 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!74 &7400000
AnimationClip:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: Empty_Fire
serializedVersion: 7
m_Legacy: 0
m_Compressed: 0
m_UseHighQualityCurve: 1
m_RotationCurves: []
m_CompressedRotationCurves: []
m_EulerCurves: []
m_PositionCurves: []
m_ScaleCurves: []
m_FloatCurves: []
m_PPtrCurves: []
m_SampleRate: 60
m_WrapMode: 0
m_Bounds:
m_Center: {x: 0, y: 0, z: 0}
m_Extent: {x: 0, y: 0, z: 0}
m_ClipBindingConstant:
genericBindings: []
pptrCurveMapping: []
m_AnimationClipSettings:
serializedVersion: 2
m_AdditiveReferencePoseClip: {fileID: 0}
m_AdditiveReferencePoseTime: 0
m_StartTime: 0
m_StopTime: 1
m_OrientationOffsetY: 0
m_Level: 0
m_CycleOffset: 0
m_HasAdditiveReferencePose: 0
m_LoopTime: 1
m_LoopBlend: 0
m_LoopBlendOrientation: 0
m_LoopBlendPositionY: 0
m_LoopBlendPositionXZ: 0
m_KeepOriginalOrientation: 0
m_KeepOriginalPositionY: 1
m_KeepOriginalPositionXZ: 0
m_HeightFromFeet: 0
m_Mirror: 0
m_EditorCurves: []
m_EulerEditorCurves: []
m_HasGenericRootTransform: 0
m_HasMotionFloatCurves: 0
m_Events: []

View File

@@ -130,6 +130,7 @@ namespace BITKit
private void Update()
{
BITApp.Time.ElapsedTime = Time.time;
BITApp.Time.DeltaTime = Time.deltaTime;
BITApp.Time.TimeAsDouble = Time.timeAsDouble;
}

View File

@@ -1,12 +1,14 @@
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using BITKit.Animations;
namespace BITKit.Entities
{
public sealed class EntityAnimator : EntityComponent
{
[SerializeField] private Animator[] animators;
[SerializeField] private UnityAnimator[] animators;
[SerializeReference, SubclassSelector] private References[] animationKeyWords;
[SerializeReference, SubclassSelector] private References _rootVelocity;
[SerializeReference, SubclassSelector] private References[] boolParameters;
@@ -23,13 +25,13 @@ namespace BITKit.Entities
private void Play(string animationName)
{
if (enabled is false) return;
if (animationKeyWords.Length is 0 || keyWords.Contains(animationName))
{
animators.ForEach(x =>
{
if (!x.isActiveAndEnabled) return;
animationName = animationName.Replace(".", "_");
x.SetTrigger(animationName);
x.Play(animationName);
});
}
}
@@ -40,7 +42,7 @@ namespace BITKit.Entities
animators.ForEach(x =>
{
if (x.isActiveAndEnabled)
x.SetBool(boolPar, entity.Get<bool>(boolPar));
x.animator.SetBool(boolPar, entity.Get<bool>(boolPar));
});
}
foreach (var floatPar in floatParameters)
@@ -48,14 +50,18 @@ namespace BITKit.Entities
animators.ForEach(x =>
{
if (x.isActiveAndEnabled)
x.SetFloat(floatPar, entity.Get<float>(floatPar));
x.animator.SetFloat(floatPar, entity.Get<float>(floatPar));
});
}
}
private void OnAnimatorMove()
{
entity.Set(_rootVelocity, animators[0].velocity);
if (enabled is false) return;
if (_rootVelocity is not null && entity is not null)
entity.Set(_rootVelocity, animators[0].animator.velocity);
}
// ReSharper disable once UnusedMember.Local
private void AnimationEvent(string eventName)
{

View File

@@ -14,17 +14,14 @@ namespace BITKit
private readonly ValidHandle allowHeal = new();
[Inject]
private IHealth _health;
public override void Initialize(IEntity _entity)
{
base.Initialize(_entity);
_health.OnSetAlive += OnSetAlive;
_health.OnSetHealthPoint += OnSetHP;
_entity.RegisterCallback<IDamageCallback>(this);
}
public override void OnStart()
{
_health = entity.Get<IHealth>();
_health.OnSetAlive += OnSetAlive;
_health.OnSetHealthPoint += OnSetHP;
entity.RegisterCallback<IDamageCallback>(this);
}
private void Update()

View File

@@ -37,6 +37,7 @@ namespace BITKit
destroyCancellationToken.ThrowIfCancellationRequested();
Execute();
}
catch(MissingReferenceException){}
catch (OperationCanceledException){}
}
}

View File

@@ -0,0 +1,4 @@
{
"name": "BITKit.Entities.InputSystem",
"references":[ "GUID:14fe60d984bf9f84eac55c6ea033a8f4", "GUID:f6155d9ae143f3949ac54e8355593d6c", "GUID:7efac18f239530141802fb139776f333", "GUID:709caf8d7fb6ef24bbba0ab9962a3ad0" ]
}

View File

@@ -0,0 +1,68 @@
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;
namespace BITKit.Entities.InputSystem
{
public class EntityInputSystem : EntityComponent
{
protected readonly InputActionGroup inputActionGroup = new()
{
allowGlobalActivation = true
};
[Inject(true)]
private IHealth _health;
[Inject(true)]
private IEntityOverride _override;
#if UNITY_EDITOR
[SerializeField,HideInInspector] internal bool Allow;
#endif
public override void Initialize(IEntity _entity)
{
base.Initialize(_entity);
_entity.AddService(inputActionGroup);
}
public override void OnAwake()
{
base.OnAwake();
inputActionGroup.allowInput.AddListener(x=>Allow=x);
if (_health is not null)
{
_health.OnSetAlive += x =>
{
inputActionGroup.allowInput.SetElements(_health,x);
};
}
if (_override is not null)
{
_override.OnOverride += x =>
{
inputActionGroup.allowInput.SetDisableElements(_override,x);
};
}
}
}
#if UNITY_EDITOR
[CustomEditor(typeof(EntityInputSystem))]
public sealed class EntityInputSystemInspector : BITInspector<EntityInputSystem>
{
public override VisualElement CreateInspectorGUI()
{
FillDefaultInspector();
var checkBox = root.Create<Toggle>();
checkBox.label = "Allow Input";
checkBox.SetEnabled(false);
checkBox.BindProperty(serializedObject.FindProperty(nameof(EntityInputSystem.Allow)));
return root;
}
}
#endif
}

View File

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

View File

@@ -0,0 +1,20 @@
{
"name": "BITKit.Entities.VFX",
"rootNamespace": "",
"references": [
"GUID:14fe60d984bf9f84eac55c6ea033a8f4",
"GUID:709caf8d7fb6ef24bbba0ab9962a3ad0",
"GUID:ea5474181b324dd49a5976cd68f44f18",
"GUID:d525ad6bd40672747bde77962f1c401e",
"GUID:49b49c76ee64f6b41bf28ef951cb0e50"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

View File

@@ -0,0 +1,27 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace BITKit.Entities.VFX
{
public class EntityVFXPlayer : EntityComponent
{
[SerializeReference,SubclassSelector] private IReference[] vfxReferences;
[SerializeField] private VFXPlayer vfxPlayer;
private readonly List<string> keyWords=new();
public override void OnAwake()
{
base.OnAwake();
keyWords.AddRange(vfxReferences.Select(x=>x.Value));
entity.AddListener<string>(Constant.Animation.Play, Play);
}
private void Play(string animationName)
{
if (isActiveAndEnabled is false) return;
if (keyWords.Contains(animationName) is false) return;
vfxPlayer.Execute();
}
}
}

View File

@@ -62,11 +62,28 @@ namespace BITKit.Entities
else if(attribute?.CanBeNull is false)
{
BIT4Log.Warning<Entity>($"{name}未找到{obj.GetType().Name}需要的{type.FullName}");
BIT4Log.Warning<Entity>(genericEvent.GetDiagnostics());
//BIT4Log.Warning<Entity>(genericEvent.GetDiagnostics());
}
}
}
public void AddService<T>(T service)
{
AddService(typeof(T),service);
}
public void AddService(object service)
{
AddService(service.GetType(),service);
}
public void AddService(Type type, object service)
{
genericEvent.Set(type,service);
genericEvent.Set(type.FullName, service);
genericEvent.SetDirect(type.FullName,service);
}
private CancellationToken _cancellationToken;
private bool isInitialized;
private Core.Entites.IEntityComponent[] _components => entityComponents.Cast<Core.Entites.IEntityComponent>().ToArray();
@@ -85,6 +102,7 @@ namespace BITKit.Entities
try
{
var monoBehaviours = GetComponentsInChildren<MonoBehaviour>(true);
entityComponents.ForEach(x => x.Initialize(this));
foreach (var x in monoBehaviours)
{
foreach (var att in x
@@ -95,9 +113,7 @@ namespace BITKit.Entities
.OfType<CustomTypeAttribute>()
)
{
genericEvent.Set(att.Type,x);
genericEvent.Set(att.Type.FullName, x);
genericEvent.SetDirect(att.Type.FullName,x);
AddService(att.Type, x);
}
genericEvent.Set(x.GetType(),x);
}
@@ -105,7 +121,7 @@ namespace BITKit.Entities
{
Inject(x);
}
entityComponents.ForEach(x => x.Initialize(this));
entityComponents.ForEach(x => x.OnAwake());
entityComponents.ForEach(x => x.OnStart());

View File

@@ -11,6 +11,9 @@ namespace BITKit.Entities
public interface IEntity :BITKit.Core.Entites.IEntity,IGenericEvent<string>, IDatabase, IProcessor, ICallback
{
IEntityComponent[] entityComponents { get; set; }
void AddService<T>(T service);
void AddService(object service);
void AddService(Type type, object service);
}
public class IEntityReader : NetMessageReader<IEntity>
{

View File

@@ -0,0 +1,14 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace BITKit
{
public static class GameObjectExtensions
{
public static T GetOrAddComponent<T>(this GameObject gameObject) where T : Component
{
return gameObject.TryGetComponent<T>(out var t) ? t : gameObject.AddComponent<T>();
}
}
}

View File

@@ -28,7 +28,11 @@ namespace BITKit.Physics
{
foreach (var x in jointConfigures)
{
x.InitialRotation=x.animate.localRotation;
x.InitialRotation= x.animate.localRotation;
if (x.animate.localRotation != x.joint.transform.localRotation)
{
BIT4Log.Log<PhysicsBasedAnimation>($"{x.animate.name}初始旋转与关节旋转不一致,将会导致动画不正确");
}
}
}
private void FixedUpdate()

View File

@@ -134,6 +134,13 @@ namespace BITKit
protected static void FillDefaultInspector(VisualElement container, SerializedObject serializedObject, bool hideScript)
{
container.Clear();
if (serializedObject.targetObject is null)
{
var label = container.Create<Label>();
label.text = "Null";
return;
}
var property = serializedObject.GetIterator();
if (!property.NextVisible(true)) return; // Expand first child.
do

View File

@@ -16,22 +16,43 @@ namespace BITKit
{
pool = new ObjectPool<T>(Spawn, OnGet, OnReturn, OnDestroy, maxSize: 16);
}
[Header(Constant.Header.Settings)]
[SerializeField] private int defaultCapacity = 16;
[Header(Constant.Header.Prefabs)]
public T prefab;
[SerializeField] private T prefab;
[Header(Constant.Header.Gameobjects)]
public Transform root;
[SerializeField] private Transform root;
private ObjectPool<T> pool;
private readonly List<T> _list=new();
private int _offset;
public T Get(T element = null, Transform _root = null)
{
if (_list.Count == defaultCapacity)
{
return _list[_offset = _offset/_list.Count+1];
}
if (element is not null)
prefab = element;
if (_root is not null)
root = _root;
return pool.Get();
var instance = pool.Get();
_list.Add(instance);
return instance;
}
public void Return(T element)
{
pool.Release(element);
_list.Remove(element);
}
public void Return(T element) => pool.Release(element);
private T Spawn() => Object.Instantiate(prefab, root);
private void OnGet(T element) => element.gameObject.SetActive(true);
private void OnReturn(T element) => element.gameObject.SetActive(false);

View File

@@ -22,14 +22,6 @@ namespace BITKit
sinleton = this;
DI.Register(this);
}
public Transform Spawn(VFXMessage message)
{
if (Data.TryGetValue<Transform>(message.path, out var vfx))
{
return Instantiate(vfx, message.location, message.location);
}
return null;
}
public Transform Spawn(Location location, params string[] keyWords)
{
if (TryMatch(out var prefab, keyWords))