This commit is contained in:
CortexCore
2024-03-29 00:58:24 +08:00
parent 967ad8eacf
commit 05315ef4a8
232 changed files with 53368 additions and 8539 deletions

View File

@@ -107,6 +107,7 @@ namespace BITKit
if (index is not -1 && list.TryGetElementAt(index, out var nextElement))
{
nextElement.IsEntered = true;
OnEntry?.Invoke(nextElement);
nextElement.Entry();
try
{
@@ -114,7 +115,7 @@ namespace BITKit
nextElement.Entered();
}
catch (OperationCanceledException){}
OnEntry?.Invoke(nextElement);
}
}
completed = true;

View File

@@ -99,7 +99,7 @@ namespace BITKit
}
else
{
AddPropertyInternal(addFactory.Invoke());
AddPropertyInternal(x =addFactory.Invoke());
//properties.Add(typeof(T).FullName, x = addFactory.Invoke());
return (T)x;
}

View File

@@ -10,13 +10,17 @@ namespace BITKit
private void OnAnimatorMove()
{
if (root)
root.SendMessage(nameof(OnAnimatorMove));
root.SendMessageUpwards(nameof(OnAnimatorMove),SendMessageOptions.DontRequireReceiver);
}
private void AIAnimationEvent(string actionName)
{
if (root)
root.SendMessage(nameof(AIAnimationEvent), actionName);
root.SendMessage(nameof(AIAnimationEvent), actionName,SendMessageOptions.DontRequireReceiver);
}
public void AnimationEvent(string eventName)
{
if(root)
root.SendMessage(nameof(AnimationEvent), eventName,SendMessageOptions.DontRequireReceiver);
}
}
}

View File

@@ -119,7 +119,7 @@ namespace BITKit.Entities
}
public class DamageService:MonoBehaviour,IDamageService
{
internal static IDamageService Singleton { get; set; }
public static IDamageService Singleton { get;private set; }
private readonly Queue<DamageMessage> Messages = new();
[SerializeReference,SubclassSelector] private INetClient netClient;

View File

@@ -4,19 +4,52 @@ using UnityEngine;
using BITKit;
namespace BITKit.Entities
{
public class EntityHitbox : EntityBehavior,IDamagable
public class EntityHitbox : MonoBehaviour,IDamagable
{
public IHealth Health => _health;
public IHealth Health
{
get
{
EnsureConfigure();
return _health;
}
}
public IUnityEntity UnityEntity
{
get
{
EnsureConfigure();
return _unityEntity;
}
}
public Rigidbody Rigidbody
{
get=>m_rigidbody;
set=>m_rigidbody=value;
}
IUnityEntity IDamagable.UnityEntity => UnityEntity;
public Rigidbody Rigidbody => m_rigidbody;
public void GiveDamage(DamageMessage message)
{
UnityEntity.Invoke(message);
if (_unityEntity is not null)
UnityEntity.Invoke(message);
}
[SerializeField]private Rigidbody m_rigidbody;
[Inject]
[Inject(true)]
private IHealth _health;
private IUnityEntity _unityEntity;
private bool _initialized;
private void EnsureConfigure()
{
if (_initialized) return;
_unityEntity = GetComponentInParent<IUnityEntity>(true);
_unityEntity?.Inject(this);
_initialized = true;
}
}
}

View File

@@ -0,0 +1,77 @@
using System.Collections;
using System.Collections.Generic;
using BITFALL.Rig;
using BITKit.Entities;
using Cysharp.Threading.Tasks.Triggers;
using UnityEditor;
using UnityEngine;
namespace BITKit
{
public class EntityHitboxBuilder : MonoBehaviour
{
[SerializeField] private Collider[] colliders;
[SerializeField] private EntityHitbox[] hitboxes;
[SerializeReference,SubclassSelector] private IReference[] tagReferences;
[BIT]
private void Build()
{
foreach (var x in hitboxes)
{
DestroyImmediate(x.gameObject);
}
hitboxes = new EntityHitbox[colliders.Length];
for (var i = 0; i < colliders.Length; i++)
{
var collider = colliders[i];
var newGameObject = new GameObject($"Hitbox_{collider.gameObject.name}");
newGameObject.layer = gameObject.layer;
newGameObject.transform.SetParent(transform);
var transform1 = collider.transform;
newGameObject.transform.position = transform1.position;
newGameObject.transform.rotation = transform1.rotation;
var hitbox =hitboxes[i] = newGameObject.AddComponent<EntityHitbox>();
var tag = newGameObject.AddComponent<Tag>();
var tickOverride = newGameObject.AddComponent<TickOverrideTransform>();
tickOverride.Source = newGameObject.transform;
tickOverride.Target = collider.transform;
if (collider.TryGetComponent<Rigidbody>(out var newRigidbody))
{
hitbox.Rigidbody = newRigidbody;
}
switch (collider)
{
case BoxCollider boxCollider:
var box = newGameObject.AddComponent<BoxCollider>();
box.size = boxCollider.size;
box.center = boxCollider.center;
break;
case SphereCollider sphereCollider:
var sphere = newGameObject.AddComponent<SphereCollider>();
sphere.radius = sphereCollider.radius;
sphere.center = sphereCollider.center;
break;
case CapsuleCollider capsuleCollider:
var capsule = newGameObject.AddComponent<CapsuleCollider>();
capsule.radius = capsuleCollider.radius;
capsule.height = capsuleCollider.height;
capsule.direction = capsuleCollider.direction;
capsule.center = capsuleCollider.center;
break;
}
tag.SetTags(tagReferences);
EditorUtility.SetDirty(hitbox);
EditorUtility.SetDirty(tag);
}
EditorUtility.SetDirty(this);
}
}
}

View File

@@ -1,20 +0,0 @@
{
"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
}

View File

@@ -1,144 +0,0 @@
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
{
[CustomType(typeof(IEntityPhysics))]
public class EntityPhysics : EntityBehavior,IEntityPhysics
{
[SerializeField] private Animator animator;
[SerializeField] private Rigidbody[] rigidbodies;
[SerializeField] private Collider[] ragdollColliders;
[SerializeField] private Joint[] joints;
[SerializeField] private new Rigidbody rigidbody;
[Inject(true)]
private IHealth _health;
[Inject(true)] private IEntityOverride _override;
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;
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;
}
}
if (_override is not null)
_override.OnOverride += OnOverride;
}
private void OnOverride(bool obj)
{
EnsureConf();
}
private void OnSetAlive(bool alive)
{
EnsureConf();
}
private async void EnsureConf()
{
var allow = (_health, _override) switch
{
(not null,not null)=>_health.IsAlive || _override.IsOvering,
(not null,null)=>_health.IsAlive,
(null,not null)=>_override.IsOvering,
_=>false,
};
if (animator)
{
animator.enabled = allow;
}
try
{
await Task.Delay(10, destroyCancellationToken);
if (destroyCancellationToken.IsCancellationRequested) return;
foreach (var x in rigidbodies)
{
//x.isKinematic = alive;
x.gameObject.SetActive(!allow);
}
foreach (var x in ragdollColliders)
{
x.enabled = !allow;
}
OnSetPhysics?.Invoke(!allow);
}
catch (OperationCanceledException)
{
}
foreach (var joint in joints)
{
switch (joint)
{
case ConfigurableJoint configurableJoint:
configurableJoint.xMotion = allow ? _jointXMotions[joint] : ConfigurableJointMotion.Free;
configurableJoint.yMotion = allow ? _jointYMotions[joint] : ConfigurableJointMotion.Free;
configurableJoint.zMotion = allow ? _jointZMotions[joint] : ConfigurableJointMotion.Free;
configurableJoint.angularXMotion =
allow ? _jointAngularXMotions[joint] : ConfigurableJointMotion.Free;
configurableJoint.angularYMotion =
allow ? _jointAngularYMotions[joint] : ConfigurableJointMotion.Free;
configurableJoint.angularZMotion =
allow ? _jointAngularZMotions[joint] : ConfigurableJointMotion.Free;
break;
}
}
}
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);
}
}
}
}

View File

@@ -134,8 +134,9 @@ namespace BITKit
}
if(UnityEngine.Physics.Raycast(EndPosition,Vector3.down,out _,1,layerMask) is false)
if(UnityEngine.Physics.Raycast(EndPosition,Vector3.down,out _,1.6f,layerMask) is false)
{
Debug.DrawRay(EndPosition, Vector3.down*1.6f, Color.red, 8);
reportBuilder.AppendLine("未检测到地面,跳过");
continue;
//Debug.DrawRay(EndPosition, Vector3.down, Color.red, 8);

View File

@@ -0,0 +1,141 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using BITKit;
using Unity.Burst;
using Unity.Collections;
using Unity.Jobs;
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.Jobs;
using UnityEngine.Pool;
namespace BITFALL.Rig{
public class TickOverrideTranformService : MonoBehaviour
{
//[BurstCompile]
// public struct CopyTransformJob : IJobParallelForTransform
// {
// [Unity.Collections.ReadOnly]
// public NativeArray<float3> positions;
// [Unity.Collections.ReadOnly]
// public NativeArray<quaternion> rotations;
//
//
// // The code actually running on the job
// public void Execute(int index, TransformAccess transform)
// {
// transform.SetPositionAndRotation(positions[index],rotations[index]);
// }
// }
public static void Register(int id,TickOverrideTransform tickOverrideTransform)
{
Dictionary.Add(id,tickOverrideTransform);
IsDirty = true;
}
public static void UnRegister(int id)
{
Dictionary.Remove(id);
IsDirty = true;
}
private static readonly Dictionary<int, TickOverrideTransform> Dictionary = new();
private static bool IsDirty;
private static Transform[] Sources;
private static Transform[] Targets;
[SerializeReference, SubclassSelector] private ITicker ticker;
// private TransformAccessArray m_AccessArray;
// private NativeArray<quaternion> _rotations;
// private NativeArray<float3> _positions;
// private JobHandle _jobHandle;
// private InitializationState _initializationState;
private void OnEnable()
{
ticker.Add(Tick);
}
private void OnDisable()
{
ticker.Remove(Tick);
}
// private void OnDestroy()
// {
// if (_initializationState is not InitializationState.Initializing) return;
// _jobHandle.Complete();
// _rotations.Dispose();
// _positions.Dispose();
// }
private void Tick(float obj)
{
// switch (_initializationState)
// {
// case InitializationState.Initializing when _jobHandle.IsCompleted:
// _jobHandle.Complete();
// _rotations.Dispose();
// _positions.Dispose();
// _initializationState = InitializationState.Initialized;
// break;
// case InitializationState.None:
// break;
// default:
// return;
// }
if (IsDirty)
{
var newLength = Dictionary.Count;
Sources = new Transform[newLength];
Targets = new Transform[newLength];
//_positions = new NativeArray<float3>(newLength, Allocator.Persistent);
//_rotations = new NativeArray<quaternion>(newLength, Allocator.Persistent);
var index = 0;
foreach (var x in Dictionary.Values)
{
Sources[index] = x.Source;
Targets[index] = x.Target;
index++;
}
}
var length =Sources?.Length ?? 0;
if(length is 0) return;
// m_AccessArray = new TransformAccessArray(length);
// m_AccessArray.SetTransforms(Sources);
for (var j = 0; j < length; j++)
{
Sources[j].SetPositionAndRotation(Targets[j].position,Targets[j].rotation);
}
// foreach (var x in Targets)
// {
// _positions[i] = x.position;
// _rotations[i] = x.rotation;
// i++;
// }
//
// foreach (var x in Sources)
// {
// x.position = _positions[i];
// x.rotation = _rotations[i];
// }
// var _job = new CopyTransformJob()
// {
// positions = _positions,
// rotations = _rotations
// };
// _jobHandle = _job.Schedule(m_AccessArray);
//
// _initializationState = InitializationState.Initializing;
}
}
}

View File

@@ -0,0 +1,34 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace BITFALL.Rig
{
public class TickOverrideTransform : MonoBehaviour
{
public Transform Source;
public Transform Target;
private int Id;
private void OnEnable()
{
Id = GetInstanceID();
TickOverrideTranformService.Register(Id,this);
}
private void OnDisable()
{
TickOverrideTranformService.UnRegister(Id);
}
#if UNITY_EDITOR
private void OnValidate()
{
if(!Source)Source = transform;
UnityEditor.EditorUtility.SetDirty(this);
}
#endif
}
}

View File

@@ -51,10 +51,10 @@ namespace BITKit.StateMachine
state.Initialize();
StateDictionary.Add(state.GetType(), state);
}
if (states.Count > 0)
{
TransitionState(states[0]);
}
// if (states.Count > 0)
// {
// TransitionState(states[0]);
// }
}
public void UpdateState(float deltaTime)
{

View File

@@ -16,5 +16,10 @@ namespace BITKit
public string[] GetTags() => CacheTags ??= reference?.Length > 0 ? reference.Select(x => x.Value).ToArray() : tags;
private string[] CacheTags;
public void SetTags(IReference[] newReference)
{
reference = newReference;
CacheTags = null;
}
}
}

View File

@@ -146,6 +146,10 @@ namespace BITKit
{
allowUpdateTime = currentTime + Interval + interval;
}
else
{
allowUpdateTime += interval;
}
}
else
{

View File

@@ -23,6 +23,7 @@ namespace BITKit.GameEditor
private void Rebuild()
{
rootVisualElement.Clear();
var obj =AssetDatabase.LoadAssetAtPath<AssetCacheScriptableObject>("Assets/Artists/Configs/Asset Cache.asset");
if (obj is null)
{
@@ -32,34 +33,41 @@ namespace BITKit.GameEditor
//button.clicked += Rebuild;
return;
}
rootVisualElement.Bind(new SerializedObject(obj));
//BITInspectorExtensions.FillDefaultInspector(rootVisualElement,new SerializedObject(obj),true);
var toggle = rootVisualElement.Create<Toggle>();
toggle.bindingPath = "isEditable";
toggle.label = "Edit";
// toggle.RegisterValueChangedCallback(_ =>
// {
// Rebuild();
// });
var rebuildButton = rootVisualElement.Create<Button>();
rebuildButton.text = "Rebuild";
rebuildButton.clicked += Rebuild;
var scroll = rootVisualElement.Create<ScrollView>();
scroll.style.flexGrow = 1;
if (obj.IsEditable)
{
BITInspectorExtensions.FillDefaultInspector(rootVisualElement,new SerializedObject(obj),true);
}
else
{
var defaultList = scroll.Create<Foldout>();
defaultList.text = "Default";
foreach (var x in obj.Assets)
{
var field = scroll.Create<ObjectField>();
var field = defaultList.Create<ObjectField>();
//field.label = x.name;
field.style.opacity = 1;
field.value = x;
field.SetEnabled(false);
}
}
foreach (var folder in obj.Folders)
{
var foldout = scroll.Create<Foldout>();
foldout.text = folder.Key;
foreach (var x in folder.Value)
{
var field = foldout.Create<ObjectField>();
//field.label = x.name;
field.style.opacity = 1;
field.value = x;
field.SetEnabled(false);
}
}
}
}

View File

@@ -1,5 +1,6 @@
using System.Collections;
using System.Collections.Generic;
using AYellowpaper.SerializedCollections;
#if UNITY_EDITOR
using UnityEditor;
using UnityEditor.UIElements;
@@ -11,40 +12,9 @@ namespace BITKit.GameEditor
{
public class AssetCacheScriptableObject : ScriptableObject
{
[SerializeField] private bool isEditable;
[SerializeField] private Object[] _assets;
public bool IsEditable => isEditable;
[SerializeField] private SerializedDictionary<string, Object[]> folders;
public Object[] Assets => _assets;
public Dictionary<string, Object[]> Folders=>folders;
}
#if UNITY_EDITOR
[CustomEditor(typeof(AssetCacheScriptableObject))]
public class AssetCacheScriptableObjectEditor : BITInspector<AssetCacheScriptableObject>
{
public override VisualElement CreateInspectorGUI()
{
root.root.Clear();
if (agent.IsEditable)
{
return base.CreateInspectorGUI();
}
var toggle = root.Create<Toggle>();
toggle.bindingPath = "isEditable";
toggle.label = "Edit";
toggle.RegisterValueChangedCallback(x => CreateInspectorGUI());
foreach (var x in agent.Assets)
{
var field = root.Create<ObjectField>();
//field.label = x.name;
field.style.opacity = 1;
field.value = x;
field.SetEnabled(false);
}
return root;
}
}
#endif
}

View File

@@ -6,7 +6,7 @@
"GUID:4988cf9794f41d64c884876ab6574b89",
"GUID:6ef4ed8ff60a7aa4bb60a8030e6f4008",
"GUID:e6234d6c1f7bf4e4db20eddc411c00b8",
"GUID:49b49c76ee64f6b41bf28ef951cb0e50"
"GUID:d525ad6bd40672747bde77962f1c401e"
],
"includePlatforms": [],
"excludePlatforms": [],

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using BITKit.UX;
using UnityEditor;
@@ -22,13 +23,7 @@ namespace BITKit.GameEditor
private void OnEnable()
{
var paths = AssetDatabase.FindAssets($"t:{typeof(T).Name}");
var allItem =
(
from path in paths.Select(AssetDatabase.GUIDToAssetPath)
select AssetDatabase.LoadAssetAtPath<ScriptableObject>(path)
).ToArray();
List.AddRange(allItem.Cast<T>());
RebuildList();
var toolbarContainer = rootVisualElement.Create<VisualElement>();
toolbarContainer.style.alignSelf = Align.Auto;
@@ -38,6 +33,11 @@ namespace BITKit.GameEditor
var refreshButton = toolbarContainer.Create<Button>();
refreshButton.text = "刷新";
refreshButton.clicked += () =>
{
RebuildList();
_listView.Rebuild();
};
var container = rootVisualElement.Create<VisualElement>();
@@ -116,7 +116,28 @@ namespace BITKit.GameEditor
icon.style.width = 24;
icon.style.height = 24;
container.AddManipulator(new ContextualMenuManipulator((evt) =>
{
evt.menu.AppendAction("复制", Copy, DropdownMenuAction.AlwaysEnabled);
//evt.menu.AppendAction("Second menu item", (x) => Debug.Log("Second!!!!"), DropdownMenuAction.AlwaysEnabled);
}));
return container;
void Copy(DropdownMenuAction dropdownMenuAction)
{
if (container.userData is not ScriptableObject scriptableObject) return;
var path = AssetDatabase.GetAssetPath(scriptableObject);
var folder = Path.GetDirectoryName(path)!;
path = Path.Combine(folder, $"{scriptableObject.name}Copy.asset");
var newObject = CreateInstance<T>();
AssetDatabase.CreateAsset(newObject, path);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
Debug.Log($"复制成功:{path}");
}
}
protected virtual void BindItem(VisualElement arg1, int arg2)
{
@@ -124,12 +145,24 @@ namespace BITKit.GameEditor
{
var item = List[arg2];
arg1.Q<Label>().text = item.name;
arg1.userData = item;
}
catch (Exception e)
{
Debug.LogException(e);
}
}
protected void RebuildList()
{
List.Clear();
var paths = AssetDatabase.FindAssets($"t:{typeof(T).Name}");
var allItem =
(
from path in paths.Select(AssetDatabase.GUIDToAssetPath)
select AssetDatabase.LoadAssetAtPath<ScriptableObject>(path)
).ToArray();
List.AddRange(allItem.Cast<T>());
}
protected virtual void CreateScriptableObject(string name)
{