1
This commit is contained in:
@@ -5,29 +5,27 @@ using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using BITKit.Core.Entites;
|
||||
using BITKit.Entities;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine.UIElements;
|
||||
// ReSharper disable RedundantTypeArgumentsOfMethod
|
||||
|
||||
namespace BITKit.Entities
|
||||
{
|
||||
public class Entity : MonoBehaviour, IEntity
|
||||
public class Entity : MonoBehaviour, IUnityEntity
|
||||
{
|
||||
private readonly GenericEvent genericEvent = new();
|
||||
private readonly Processor processor = new();
|
||||
public IEntityComponent[] entityComponents { get; set; }
|
||||
public ulong Id { get; private set; }
|
||||
public CancellationToken CancellationToken => _cancellationToken;
|
||||
public CancellationToken CancellationToken { get; private set; }
|
||||
public IEntityBehavior[] Behaviors { get;private set; }
|
||||
public IEntityComponent[] Components => Behaviors.Cast<IEntityComponent>().ToArray();
|
||||
|
||||
Core.Entites.IEntityComponent[] Core.Entites.IEntity.Components => _components;
|
||||
|
||||
bool Core.Entites.IEntity.RegisterComponent<T>(T component)
|
||||
bool Entities.IEntity.RegisterComponent<T>(T component)
|
||||
{
|
||||
throw new InvalidOperationException("Unity Entity can't register component");
|
||||
}
|
||||
|
||||
IServiceProvider Core.Entites.IEntity.ServiceProvider=> throw new InvalidOperationException("Unity Entity can't register component");
|
||||
IServiceProvider Entities.IEntity.ServiceProvider=> throw new InvalidOperationException("Unity Entity can't register component");
|
||||
public void Inject(object obj)
|
||||
{
|
||||
foreach (var fieldInfo in obj
|
||||
@@ -44,7 +42,7 @@ namespace BITKit.Entities
|
||||
{
|
||||
case null:
|
||||
break;
|
||||
case Core.Entites.IEntityComponent entityComponent:
|
||||
case Entities.IEntityComponent entityComponent:
|
||||
if(entityComponent.Entity.Id == Id)
|
||||
continue;
|
||||
break;
|
||||
@@ -61,23 +59,37 @@ namespace BITKit.Entities
|
||||
}
|
||||
else if(attribute?.CanBeNull is false)
|
||||
{
|
||||
BIT4Log.Warning<Entity>($"{name}未找到{type.FullName}");
|
||||
BIT4Log.Warning<Entity>(genericEvent.GetDiagnostics());
|
||||
BIT4Log.Warning<Entity>($"{name}未找到{obj.GetType().Name}需要的{type.FullName}");
|
||||
//BIT4Log.Warning<Entity>(genericEvent.GetDiagnostics());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private CancellationToken _cancellationToken;
|
||||
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 bool isInitialized;
|
||||
private Core.Entites.IEntityComponent[] _components => entityComponents.Cast<Core.Entites.IEntityComponent>().ToArray();
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
Id = (ulong)Guid.NewGuid().GetHashCode();
|
||||
_cancellationToken = gameObject.GetCancellationTokenOnDestroy();
|
||||
Set(_cancellationToken);
|
||||
entityComponents = GetComponentsInChildren<IEntityComponent>(true).Distinct().ToArray();
|
||||
|
||||
CancellationToken = gameObject.GetCancellationTokenOnDestroy();
|
||||
Set(CancellationToken);
|
||||
Behaviors = GetComponentsInChildren<IEntityBehavior>(true).Distinct().ToArray();
|
||||
UnityEntitiesService.Register(this);
|
||||
}
|
||||
private void Start()
|
||||
@@ -85,6 +97,7 @@ namespace BITKit.Entities
|
||||
try
|
||||
{
|
||||
var monoBehaviours = GetComponentsInChildren<MonoBehaviour>(true);
|
||||
Behaviors.ForEach(x => x.Initialize(this));
|
||||
foreach (var x in monoBehaviours)
|
||||
{
|
||||
foreach (var att in x
|
||||
@@ -95,9 +108,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,10 +116,10 @@ namespace BITKit.Entities
|
||||
{
|
||||
Inject(x);
|
||||
}
|
||||
entityComponents.ForEach(x => x.Initialize(this));
|
||||
|
||||
|
||||
entityComponents.ForEach(x => x.OnAwake());
|
||||
entityComponents.ForEach(x => x.OnStart());
|
||||
Behaviors.ForEach(x => x.OnAwake());
|
||||
Behaviors.ForEach(x => x.OnStart());
|
||||
isInitialized = true;
|
||||
}
|
||||
catch (Exception e)
|
||||
@@ -122,21 +133,21 @@ namespace BITKit.Entities
|
||||
{
|
||||
if (isInitialized)
|
||||
{
|
||||
entityComponents.ForEach(x => x.OnDestroyComponent());
|
||||
Behaviors.ForEach(x => x.OnDestroyComponent());
|
||||
}
|
||||
UnityEntitiesService.UnRegister(this);
|
||||
}
|
||||
private void Update()
|
||||
{
|
||||
entityComponents.ForEach(x => x.OnUpdate(Time.deltaTime));
|
||||
Behaviors.ForEach(x => x.OnUpdate(Time.deltaTime));
|
||||
}
|
||||
private void FixedUpdate()
|
||||
{
|
||||
entityComponents.ForEach(x => x.OnFixedUpdate(Time.fixedDeltaTime));
|
||||
Behaviors.ForEach(x => x.OnFixedUpdate(Time.fixedDeltaTime));
|
||||
}
|
||||
private void LateUpdate()
|
||||
{
|
||||
entityComponents.ForEach(x => x.OnLateUpdate(Time.deltaTime));
|
||||
Behaviors.ForEach(x => x.OnLateUpdate(Time.deltaTime));
|
||||
}
|
||||
public void AddListener<T>(Action<T> action) => genericEvent.AddListener<T>(action);
|
||||
public void Invoke<T>(T value) => genericEvent.Invoke<T>(value);
|
||||
@@ -155,42 +166,6 @@ namespace BITKit.Entities
|
||||
}
|
||||
public void Set<T>(T value) => genericEvent.Set<T>(value);
|
||||
public void Set<T>(string key = Constant.System.Internal, T value = default) => genericEvent.Set<T>(key, value);
|
||||
public T GetContext<T>(T value = default) => processor.GetContext<T>(value);
|
||||
public void AddProcessor<T>(Func<T, T> func) => processor.AddProcessor<T>(func);
|
||||
public void RemoveProcessor<T>(Func<T, T> func) => processor.RemoveProcessor<T>(func);
|
||||
public T GetContext<T>(string key, T value) => processor.GetContext<T>(value);
|
||||
public void AddProcessor<T>(string key, Func<T, T> func) => processor.AddProcessor<T>(key, func);
|
||||
public void RemoveProcessor<T>(string key, Func<T, T> func) => processor.RemoveProcessor<T>(key, func);
|
||||
|
||||
public void RegisterCallback<T>(T t)
|
||||
{
|
||||
var value = GetCallbacks<T>() as List<T>;
|
||||
value!.Add(t);
|
||||
}
|
||||
|
||||
public void UnRegisterCallback<T>(T t)
|
||||
{
|
||||
var value = GetCallbacks<T>() as List<T>;
|
||||
value!.Remove(t);
|
||||
}
|
||||
|
||||
public IEnumerable<T> GetCallbacks<T>()
|
||||
{
|
||||
var value = Get<List<T>>(nameof(ICallback)).CreateOrAddIfEmety(() =>
|
||||
{
|
||||
List<T> newList = new();
|
||||
Set<List<T>>(nameof(ICallback), newList);
|
||||
|
||||
return newList;
|
||||
});
|
||||
if (value is null)
|
||||
{
|
||||
Debug.LogWarning("List is Null");
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
#if UNITY_EDITOR
|
||||
[UnityEditor.CustomEditor(typeof(Entity))]
|
||||
|
38
Src/Unity/Scripts/Entity/Core/EntityBehavior.cs
Normal file
38
Src/Unity/Scripts/Entity/Core/EntityBehavior.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using System;
|
||||
using BITKit.Entities;
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor;
|
||||
#endif
|
||||
using UnityEngine;
|
||||
|
||||
namespace BITKit.Entities
|
||||
{
|
||||
public abstract class EntityBehavior : MonoBehaviour, IEntityBehavior
|
||||
{
|
||||
public IEntity Entity { get; set; }
|
||||
public IUnityEntity UnityEntity { get; private set; }
|
||||
protected Transform Transform { get; private set; }
|
||||
private IUnityEntity _mUnityEntity;
|
||||
public virtual void Initialize(IEntity _entity)
|
||||
{
|
||||
Transform = transform;
|
||||
UnityEntity = _entity as IUnityEntity;
|
||||
Entity = _entity;
|
||||
}
|
||||
public virtual void OnAwake() { }
|
||||
public virtual void OnStart() { }
|
||||
public virtual void OnUpdate(float deltaTime) { }
|
||||
public virtual void OnFixedUpdate(float deltaTime) { }
|
||||
public virtual void OnLateUpdate(float deltaTime) { }
|
||||
public virtual void OnDestroyComponent() { }
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
[CanEditMultipleObjects]
|
||||
[CustomEditor(typeof(EntityBehavior), true)]
|
||||
public class EntityComponentInspector : BITInspector<EntityBehavior>
|
||||
{
|
||||
|
||||
}
|
||||
#endif
|
||||
}
|
@@ -1,47 +0,0 @@
|
||||
using System;
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor;
|
||||
#endif
|
||||
using UnityEngine;
|
||||
|
||||
namespace BITKit.Entities
|
||||
{
|
||||
public interface IEntityComponent:BITKit.Core.Entites.IEntityComponent
|
||||
{
|
||||
IEntity entity { get; }
|
||||
void Initialize(IEntity _entity);
|
||||
void OnUpdate(float deltaTime);
|
||||
void OnFixedUpdate(float deltaTime);
|
||||
void OnLateUpdate(float deltaTime);
|
||||
void OnDestroyComponent();
|
||||
}
|
||||
public abstract class EntityComponent : MonoBehaviour, IEntityComponent
|
||||
{
|
||||
public IEntity entity { get; private set; }
|
||||
protected Transform Transform { get; private set; }
|
||||
private IEntity mEntity;
|
||||
public virtual void Initialize(IEntity _entity)
|
||||
{
|
||||
Transform = transform;
|
||||
entity = _entity;
|
||||
Entity = _entity;
|
||||
}
|
||||
public virtual void OnAwake() { }
|
||||
public virtual void OnStart() { }
|
||||
public virtual void OnUpdate(float deltaTime) { }
|
||||
public virtual void OnFixedUpdate(float deltaTime) { }
|
||||
public virtual void OnLateUpdate(float deltaTime) { }
|
||||
public virtual void OnDestroyComponent() { }
|
||||
public virtual Type BaseType => GetType();
|
||||
public Core.Entites.IEntity Entity { get; set; }
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
[CanEditMultipleObjects]
|
||||
[CustomEditor(typeof(EntityComponent), true)]
|
||||
public class EntityComponentInspector : BITInspector<EntityComponent>
|
||||
{
|
||||
|
||||
}
|
||||
#endif
|
||||
}
|
@@ -1,19 +0,0 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
namespace BITKit.Entities
|
||||
{
|
||||
public class EntityIdComponent : EntityComponent
|
||||
{
|
||||
public ulong Id;
|
||||
public string Name;
|
||||
public override void Initialize(IEntity _entity)
|
||||
{
|
||||
if (Id is 0) Id = (ulong)Guid.NewGuid().GetHashCode();
|
||||
base.Initialize(_entity);
|
||||
_entity.Set(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5ca4ab5daca5d054caf8c917c9ca6aa7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -11,12 +11,8 @@ namespace BITKit.Entities
|
||||
void RemoveOverride(object key);
|
||||
event Action<bool> OnOverride;
|
||||
}
|
||||
public interface IEntityOverrideCallback
|
||||
{
|
||||
void OnEntryOverride(bool @override);
|
||||
}
|
||||
[CustomType(typeof(IEntityOverride))]
|
||||
public class EntityOverride : EntityComponent,IEntityOverride
|
||||
public class EntityOverride : EntityBehavior,IEntityOverride
|
||||
{
|
||||
[SerializeField,ReadOnly] private bool isOvering;
|
||||
public bool IsOvering => _allowOverrideHandle;
|
||||
@@ -25,10 +21,10 @@ namespace BITKit.Entities
|
||||
public void RemoveOverride(object key)=>_allowOverrideHandle.RemoveElement(key);
|
||||
public event Action<bool> OnOverride;
|
||||
|
||||
public override void Initialize(IEntity _entity)
|
||||
public override void Initialize(IEntity entity)
|
||||
{
|
||||
base.Initialize(_entity);
|
||||
_entity.Set<IEntityOverride>(this);
|
||||
base.Initialize(entity);
|
||||
UnityEntity.Set<IEntityOverride>(this);
|
||||
}
|
||||
public override void OnAwake()
|
||||
{
|
||||
|
@@ -2,36 +2,38 @@ using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using BITKit.Core.Entites;
|
||||
using BITKit.Entities;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AddressableAssets;
|
||||
namespace BITKit.Entities
|
||||
{
|
||||
/// <summary>Entity接口,用于复杂实体</summary>
|
||||
public interface IEntity :BITKit.Core.Entites.IEntity,IGenericEvent<string>, IDatabase, IProcessor, ICallback
|
||||
public interface IUnityEntity :Entities.IEntity,IGenericEvent<string>, IDatabase
|
||||
{
|
||||
IEntityComponent[] entityComponents { get; set; }
|
||||
void AddService<T>(T service);
|
||||
void AddService(object service);
|
||||
void AddService(Type type, object service);
|
||||
}
|
||||
public class IEntityReader : NetMessageReader<IEntity>
|
||||
public class IEntityReader : NetMessageReader<IUnityEntity>
|
||||
{
|
||||
public override IEntity ReadBinary(BinaryReader reader)
|
||||
public override IUnityEntity ReadBinary(BinaryReader reader)
|
||||
{
|
||||
var id = reader.ReadInt32();
|
||||
var path = reader.ReadString();
|
||||
var entity = DI.Get<IEntitiesService>().Entities[id];
|
||||
return (IEntity)entity;
|
||||
return (IUnityEntity)entity;
|
||||
}
|
||||
public override void WriteBinary(BinaryWriter writer, IEntity value)
|
||||
public override void WriteBinary(BinaryWriter writer, IUnityEntity value)
|
||||
{
|
||||
writer.Write(value.Id);
|
||||
writer.Write(value.Id);
|
||||
}
|
||||
private IEntity Create(int id, string path)
|
||||
private IUnityEntity Create(int id, string path)
|
||||
{
|
||||
var entity = Addressables
|
||||
.LoadAssetAsync<GameObject>(path)
|
||||
.WaitForCompletion()
|
||||
.GetComponent<IEntity>();
|
||||
.GetComponent<IUnityEntity>();
|
||||
return entity;
|
||||
}
|
||||
}
|
@@ -7,7 +7,7 @@ using UnityEngine;
|
||||
|
||||
namespace BITKit.Entities
|
||||
{
|
||||
public class StateBasedComponent<T> : EntityComponent,IStateMachine<T> where T : IState
|
||||
public class StateBasedBehavior<T> : EntityBehavior,IStateMachine<T> where T : IState
|
||||
{
|
||||
[SerializeField] private MonoStateMachine<T> stateMachine;
|
||||
public bool Enabled
|
||||
@@ -28,9 +28,9 @@ namespace BITKit.Entities
|
||||
remove => stateMachine.OnStateChanged -= value;
|
||||
}
|
||||
public IDictionary<Type, T> StateDictionary => stateMachine.StateDictionary;
|
||||
public override void Initialize(IEntity _entity)
|
||||
public override void Initialize(IEntity entity)
|
||||
{
|
||||
base.Initialize(_entity);
|
||||
base.Initialize(entity);
|
||||
if (stateMachine is null)
|
||||
{
|
||||
Debug.LogWarning(GetType().Name);
|
||||
|
@@ -4,13 +4,12 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using BITKit;
|
||||
using BITKit.Core.Entites;
|
||||
using BITKit.Entities;
|
||||
using UnityEngine;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine.Pool;
|
||||
using IEntity = BITKit.Core.Entites.IEntity;
|
||||
using IEntityComponent = BITKit.Core.Entites.IEntityComponent;
|
||||
using IEntity = BITKit.Entities.IEntity;
|
||||
using IEntityComponent = BITKit.Entities.IEntityComponent;
|
||||
[Serializable]
|
||||
public class UnityEntitiesServiceSingleton:IEntitiesService
|
||||
{
|
||||
|
Reference in New Issue
Block a user