This commit is contained in:
CortexCore
2024-05-31 01:23:15 +08:00
parent c798b224be
commit 299082fe27
164 changed files with 3604 additions and 2018 deletions

View File

@@ -62,6 +62,7 @@ namespace BITKit.Entities
/// </summary>
public interface IEntityMovement:IStateMachine<IEntityMovementState>
{
Vector3 Size => Vector3.one;
float ReferenceSpeed => 2.5f;
Vector3 Position { get; set; }
Quaternion Rotation { get; set; }
@@ -138,5 +139,6 @@ namespace BITKit.Entities
void BeforeUpdateMovement(float deltaTime);
void AfterUpdateMovement(float deltaTime);
void ExecuteCommand<T>(T command);
void DrawGizmos();
}
}

View File

@@ -81,8 +81,8 @@ namespace BITKit.Entities
{
public override DamageMessage ReadBinary(BinaryReader reader)
{
UnityEntitiesService.TryGetEntity(reader.ReadUInt64(),out var initiator);
UnityEntitiesService.TryGetEntity(reader.ReadUInt64(),out var target);
UnityEntitiesService.TryGetEntity(reader.ReadInt32(),out var initiator);
UnityEntitiesService.TryGetEntity(reader.ReadInt32(),out var target);
return new DamageMessage()
{
Initiator = initiator as Entity,
@@ -114,6 +114,7 @@ namespace BITKit.Entities
IHealth Health { get; }
IUnityEntity UnityEntity { get; }
Rigidbody Rigidbody { get; }
Collider Collider { get; }
void GiveDamage(DamageMessage message);
}
public class DamageService:MonoBehaviour,IDamageService

View File

@@ -90,11 +90,6 @@ namespace BITKit.Entities
OnSetAlive?.Invoke(alive);
}
private void AddHP(int hp)
{
OnHealthPointChangedInternal(healthPoint, healthPoint += hp);
}
private void OnGetDamage(DamageMessage damageMessage)
{
if (damageMessage.Target != UnityEntity) return;
@@ -114,11 +109,23 @@ namespace BITKit.Entities
break;
}
}
if (Data.Get<bool>("god") is false)
AddHP(-damage);
damageMessage.Damage = damage;
if (Data.Get<bool>("god") is false)
{
healthPoint-=damage;
}
IsAlive = healthPoint >= 0;
OnSetHealthPoint?.Invoke(healthPoint);
OnDamageRelease?.Invoke(damageMessage);
if (!IsAlive)
{
OnSetAliveInternal(false);
}
}
}
#if UNITY_EDITOR

View File

@@ -27,10 +27,24 @@ namespace BITKit.Entities
}
public Rigidbody Rigidbody
{
get=>m_rigidbody;
get
{
EnsureConfigure();
return m_rigidbody;
}
set=>m_rigidbody=value;
}
public Collider Collider
{
get
{
EnsureConfigure();
return _collider;
}
}
private Collider _collider;
public void GiveDamage(DamageMessage message)
{
if (_unityEntity is not null)
@@ -53,6 +67,7 @@ namespace BITKit.Entities
_unityEntity = GetComponentInParent<IUnityEntity>(true);
_unityEntity?.Inject(this);
_initialized = true;
_collider = GetComponent<Collider>();
}
}
}

View File

@@ -3,7 +3,9 @@ using System.Collections.Generic;
using BITFALL.Rig;
using BITKit.Entities;
using Cysharp.Threading.Tasks.Triggers;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;
namespace BITKit
@@ -14,6 +16,7 @@ namespace BITKit
[SerializeField] private EntityHitbox[] hitboxes;
[SerializeReference,SubclassSelector] private IReference[] tagReferences;
#if UNITY_EDITOR
[BIT]
private void Build()
{
@@ -73,5 +76,6 @@ namespace BITKit
}
EditorUtility.SetDirty(this);
}
#endif
}
}

View File

@@ -1,3 +1,16 @@
{
"name": "BITKit.Entities.Physics"
}
"name": "BITKit.Entities.Physics",
"rootNamespace": "",
"references": [
"GUID:14fe60d984bf9f84eac55c6ea033a8f4"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

View File

@@ -10,6 +10,7 @@ namespace BITKit.Entities.Physics
/// </summary>
public interface IEntityPhysics
{
ValidHandle DisablePhysics { get; }
Vector3 Center { get; }
bool IsPhysics { get; }
Vector3 Velocity { get; set; }

View File

@@ -20,7 +20,7 @@ namespace BITKit.Entities
private readonly GenericEvent genericEvent = new();
public ulong Id { get; set; }
public int Id { get; set; }
public CancellationToken CancellationToken { get; private set; }
public IEntityBehavior[] Behaviors { get;private set; }
public IEntityComponent[] Components => Behaviors.Cast<IEntityComponent>().ToArray();
@@ -33,9 +33,43 @@ namespace BITKit.Entities
IServiceProvider Entities.IEntity.ServiceProvider=> throw new InvalidOperationException("Unity Entity can't register component");
public void Inject(object obj)
{
foreach (var propertyInfo in obj
.GetType()
.GetProperties(ReflectionHelper.Flags)
.Where(x=>x.GetCustomAttribute<InjectAttribute>(true) is not null))
{
var type = propertyInfo.PropertyType;
var attribute = propertyInfo.GetCustomAttribute<InjectAttribute>();
var currentValue = propertyInfo.GetValue(obj);
try
{
switch (currentValue)
{
case null:
break;
case IEntityComponent entityComponent:
if(entityComponent.Entity.Id == Id)
continue;
break;
case MonoBehaviour { destroyCancellationToken: { IsCancellationRequested: false } }:
continue;
case not null:
continue;
}
}
catch (MissingReferenceException){}
if(genericEvent.TryGetObjectDirect(type.FullName,out var value))
{
propertyInfo.SetValue(obj,value,null);
}
else if(attribute?.CanBeNull is false)
{
BIT4Log.Warning<Entity>($"{name}未找到{obj.GetType().Name}需要的{type.FullName}");
}
}
foreach (var fieldInfo in obj
.GetType()
.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
.GetFields(ReflectionHelper.Flags)
.Where(fieldInfo=>fieldInfo.GetCustomAttribute<InjectAttribute>(true) is not null))
{
var type = fieldInfo.FieldType;
@@ -97,7 +131,7 @@ namespace BITKit.Entities
private void Awake()
{
if (Id.IsDefault())
Id = (ulong)Guid.NewGuid().GetHashCode();
Id = GetInstanceID();
CancellationToken = gameObject.GetCancellationTokenOnDestroy();
Set(CancellationToken);
}

View File

@@ -48,6 +48,13 @@ namespace BITKit.Entities
stateMachine.Initialize();
}
}
public override void OnDestroyComponent()
{
base.OnDestroyComponent();
CurrentState?.OnStateExit(CurrentState,null);
}
void IStateMachine<T>.Initialize()
{
stateMachine.Initialize();

View File

@@ -38,10 +38,10 @@ public class UnityEntitiesServiceSingleton:IEntitiesService
public CancellationToken CancellationToken => UnityEntitiesService.CancellationToken;
public IEntity Get(ulong id) => UnityEntitiesService.Get(id);
public bool TryGetEntity(ulong id, out IEntity entity) => UnityEntitiesService.TryGetEntity(id, out entity);
public IEntity Get(int id) => UnityEntitiesService.Get(id);
public bool TryGetEntity(int id, out IEntity entity) => UnityEntitiesService.TryGetEntity(id, out entity);
public IEntity GetOrAdd(ulong id, Func<ulong, IEntity> factory)=>UnityEntitiesService.GetOrAdd(id,factory);
public IEntity GetOrAdd(int id, Func<int, IEntity> factory)=>UnityEntitiesService.GetOrAdd(id,factory);
public IEntity[] Query<T>()
@@ -85,7 +85,7 @@ public class UnityEntitiesService : MonoBehaviour,IEntitiesService
return false;
}
public static CancellationToken CancellationToken;
private static readonly ConcurrentDictionary<ulong,IEntity> Dictionary=new();
private static readonly ConcurrentDictionary<int,IEntity> Dictionary=new();
private static readonly Queue<IEntity> RegisterQueue = new();
private static readonly Queue<IEntity> UnRegisterQueue = new();
private void Awake()
@@ -126,18 +126,18 @@ public class UnityEntitiesService : MonoBehaviour,IEntitiesService
}
CancellationToken IEntitiesService.CancellationToken => CancellationToken;
public static IEntity Get(ulong id)=>Dictionary[id];
IEntity IEntitiesService.Get(ulong id)=>Get(id);
public static bool TryGetEntity(ulong id, out IEntity entity)
public static IEntity Get(int id)=>Dictionary[id];
IEntity IEntitiesService.Get(int id)=>Get(id);
public static bool TryGetEntity(int id, out IEntity entity)
{
return Dictionary.TryGetValue(id, out entity);
}
bool IEntitiesService.TryGetEntity(ulong id, out IEntity entity)=>TryGetEntity(id,out entity);
public static IEntity GetOrAdd(ulong id, Func<ulong, IEntity> factory)
bool IEntitiesService.TryGetEntity(int id, out IEntity entity)=>TryGetEntity(id,out entity);
public static IEntity GetOrAdd(int id, Func<int, IEntity> factory)
{
return Dictionary.GetOrAdd(id, factory);
}
IEntity IEntitiesService.GetOrAdd(ulong id, Func<ulong, IEntity> factory)=>GetOrAdd(id,factory);
IEntity IEntitiesService.GetOrAdd(int id, Func<int, IEntity> factory)=>GetOrAdd(id,factory);
IEntity[] IEntitiesService.Query<T>()=>Query<T>();
public static IEntity[] Query<T>()

View File

@@ -7,14 +7,14 @@ namespace BITKit.Entities
{
public interface IdComponent
{
ulong Id { get; }
int Id { get; }
string Name { get; }
}
public class UnityIdComponent : EntityBehavior,IdComponent
{
[SerializeField] private ulong id;
[SerializeField] private int id;
[SerializeField] private string unityName;
public ulong Id => id;
public int Id => id;
public string Name => unityName;
public override void Initialize(IEntity _entity)
{

View File

@@ -19,7 +19,7 @@ namespace BITKit.Entities
public struct EntitiesNetSyncBatchCommand
{
public int Length;
public ulong[] Ids;
public int[] Ids;
public ulong[] AddressablePaths;
public EntitiesNetSyncCommand[] Commands;
}
@@ -81,7 +81,7 @@ namespace BITKit.Entities
{
using var ms = new MemoryStream(obj.Data);
using var reader = new BinaryReader(ms);
var id = reader.ReadUInt64();
var id = reader.ReadInt32();
var path = reader.ReadUInt64();
entitiesService.GetOrAdd(id, x => AddEntity(id, path)).TryGetComponent<IEntityBinaryHeader>(out var header);
header.Deserialize(reader);
@@ -100,7 +100,7 @@ namespace BITKit.Entities
header.Deserialize(reader);
}
}
private static IEntity AddEntity(ulong id,ulong addressableId)
private static IEntity AddEntity(int id,ulong addressableId)
{
var entity = AddressableHelper.Get<GameObject>(addressableId);
var instance = Instantiate(entity).GetComponent<Entity>();
@@ -146,7 +146,7 @@ namespace BITKit.Entities
var batchCommand = new EntitiesNetSyncBatchCommand()
{
Length = headers.Length,
Ids = new ulong[headers.Length],
Ids = new int[headers.Length],
AddressablePaths = new ulong[headers.Length],
Commands = new EntitiesNetSyncCommand[headers.Length]
};

View File

@@ -15,8 +15,8 @@ namespace BITKit.Entities.Player
[SerializeField] private MonoStateMachine<T> stateMachine;
public override void OnAwake()
{
stateMachine?.Initialize();
base.OnAwake();
stateMachine?.Initialize();
}
public bool Enabled