更改文件架构
This commit is contained in:
52
Packages/Common~/Scripts/Entity/Core/DamageSystem.cs
Normal file
52
Packages/Common~/Scripts/Entity/Core/DamageSystem.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using BITKit;
|
||||
using BITKit.SubSystems;
|
||||
namespace BITKit.Entities
|
||||
{
|
||||
|
||||
public interface IDamageType { }
|
||||
public record DamageMessage
|
||||
{
|
||||
public IEntity initiator;
|
||||
public IEntity target;
|
||||
public int damage;
|
||||
public IDamagable hit;
|
||||
public Location location;
|
||||
public IDamageType damageType;
|
||||
}
|
||||
public interface IDamageCallback
|
||||
{
|
||||
void OnGetDamage(DamageMessage message);
|
||||
}
|
||||
public interface IDamagable
|
||||
{
|
||||
IEntity Entity { get; }
|
||||
Rigidbody Rigidbody { get; }
|
||||
void GiveDamage(DamageMessage message);
|
||||
}
|
||||
[SubSystemConfig(isMainThread = true)]
|
||||
public class DamageSystem : SubBITSystem
|
||||
{
|
||||
static Queue<DamageMessage> messages = new();
|
||||
public static void Excute(DamageMessage damageMessage)
|
||||
{
|
||||
messages.Enqueue(damageMessage);
|
||||
}
|
||||
public override void OnCreate()
|
||||
{
|
||||
messages.Clear();
|
||||
}
|
||||
public override void OnFixedUpdate(float deltaTime)
|
||||
{
|
||||
while (messages.TryDequeue(out var damageMessage))
|
||||
{
|
||||
damageMessage.hit?.GiveDamage(damageMessage);
|
||||
damageMessage.initiator?.Invoke(damageMessage);
|
||||
//damageMessage.initiator?.Invoke(damageMessage);
|
||||
//damageMessage.target?.Invoke(damageMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
11
Packages/Common~/Scripts/Entity/Core/DamageSystem.cs.meta
Normal file
11
Packages/Common~/Scripts/Entity/Core/DamageSystem.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6745dae71c15a66439ec1aee5f3c275d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
36
Packages/Common~/Scripts/Entity/Core/EntitiesManager.cs
Normal file
36
Packages/Common~/Scripts/Entity/Core/EntitiesManager.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AddressableAssets;
|
||||
using BITKit;
|
||||
using System.Collections.Concurrent;
|
||||
namespace BITKit.Entities
|
||||
{
|
||||
public class EntitiesManager
|
||||
{
|
||||
[RuntimeInitializeOnLoadMethod]
|
||||
static void Reload()
|
||||
{
|
||||
Dictionary.Clear();
|
||||
}
|
||||
public static ConcurrentDictionary<int, IEntity> Dictionary = new();
|
||||
public static Func<int, IEntity> CreateFactory;
|
||||
public static IEntity Get(int id)
|
||||
{
|
||||
if (Dictionary.TryGetValue(id, out var entity))
|
||||
{
|
||||
return entity;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new System.NullReferenceException($"没有找到id为{id}的iEntity");
|
||||
}
|
||||
}
|
||||
public static IEntity GetOrAdd(int id)
|
||||
{
|
||||
return GetOrAdd(id, CreateFactory);
|
||||
}
|
||||
public static IEntity GetOrAdd(int id, Func<int, IEntity> createFacotry) => Dictionary.GetOrAdd(id, createFacotry);
|
||||
}
|
||||
}
|
11
Packages/Common~/Scripts/Entity/Core/EntitiesManager.cs.meta
Normal file
11
Packages/Common~/Scripts/Entity/Core/EntitiesManager.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 52fa20c3ba3f2664b9eb666a434b77de
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
147
Packages/Common~/Scripts/Entity/Core/Entity.cs
Normal file
147
Packages/Common~/Scripts/Entity/Core/Entity.cs
Normal file
@@ -0,0 +1,147 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Unity.VisualScripting;
|
||||
using UnityEngine.Events;
|
||||
using System.Linq;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace BITKit.Entities
|
||||
{
|
||||
|
||||
[Inspectable]
|
||||
public class Entity : MonoBehaviour, IEntity
|
||||
{
|
||||
public string addressablePath="Entity";
|
||||
GenericEvent genericEvent = new();
|
||||
Processor processor = new();
|
||||
public IEntityComponent[] entityComponents { get; set; }
|
||||
public int Id
|
||||
{
|
||||
get
|
||||
{
|
||||
if (id is 0)
|
||||
{
|
||||
id = Guid.NewGuid().GetHashCode();
|
||||
}
|
||||
return id;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (id != value)
|
||||
{
|
||||
id = value;
|
||||
EntitiesManager.Dictionary.TryRemove(id, out var _);
|
||||
EntitiesManager.GetOrAdd(id, x => this);
|
||||
}
|
||||
}
|
||||
}
|
||||
int id;
|
||||
|
||||
protected virtual void Awake()
|
||||
{
|
||||
entityComponents = GetComponentsInChildren<IEntityComponent>(true);
|
||||
entityComponents.ForEach(x => x.Initialize(this));
|
||||
}
|
||||
protected virtual void Start()
|
||||
{
|
||||
entityComponents.ForEach(x => x.OnAwake());
|
||||
entityComponents.ForEach(x => x.OnStart());
|
||||
EntitiesManager.Dictionary.AddOrUpdate(id, (x) => this, (x1, x2) => this);
|
||||
}
|
||||
protected virtual void OnDestroy()
|
||||
{
|
||||
entityComponents.ForEach(x => x.OnDestroyComponent());
|
||||
EntitiesManager.Dictionary.TryRemove(id, out var _);
|
||||
}
|
||||
protected virtual void Update()
|
||||
{
|
||||
entityComponents.ForEach(x => x.OnUpdate(Time.deltaTime));
|
||||
}
|
||||
protected virtual void FixedUpdate()
|
||||
{
|
||||
entityComponents.ForEach(x => x.OnFixedUpdate(Time.fixedDeltaTime));
|
||||
}
|
||||
protected virtual void LateUpdate()
|
||||
{
|
||||
entityComponents.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);
|
||||
public void RemoveListener<T>(Action<T> action) => genericEvent.RemoveListener<T>(action);
|
||||
public void AddListener<T>(string key, Action<T> action) => genericEvent.AddListener<T>(key, action);
|
||||
public void Invoke<T>(string key, T value) => genericEvent.Invoke<T>(key, value);
|
||||
public void Invoke<T>() where T : new() => genericEvent.Invoke<T>();
|
||||
public void RemoveListener<T>(string key, Action<T> action) => genericEvent.RemoveListener<T>(key, action);
|
||||
public T Get<T>(string key = Constant.System.Internal)
|
||||
{
|
||||
var value = genericEvent.Get<T>(key);
|
||||
if (value is null && typeof(T).IsAssignableFrom(typeof(Component)))
|
||||
{
|
||||
if (TryGetComponent<T>(out var component))
|
||||
{
|
||||
Set<T>(component);
|
||||
return component;
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
public int[] GetInts() => new int[] { id };
|
||||
public float[] GetFloats() => null;
|
||||
public byte[] GetBytes() => null;
|
||||
|
||||
|
||||
}
|
||||
#if UNITY_EDITOR
|
||||
[UnityEditor.CustomEditor(typeof(Entity))]
|
||||
public class EntityInspector : BITInspector<Entity>
|
||||
{
|
||||
public override VisualElement CreateInspectorGUI()
|
||||
{
|
||||
FillDefaultInspector();
|
||||
var label = CreateSubTitle("Identity");
|
||||
var idLabel = root.Create<Label>();
|
||||
idLabel.text = agent.Id.ToString();
|
||||
return root;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
11
Packages/Common~/Scripts/Entity/Core/Entity.cs.meta
Normal file
11
Packages/Common~/Scripts/Entity/Core/Entity.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 73333e0a8f0bc4a4b82c06db4c35a21f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
52
Packages/Common~/Scripts/Entity/Core/EntityComponent.cs
Normal file
52
Packages/Common~/Scripts/Entity/Core/EntityComponent.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Sirenix.OdinInspector;
|
||||
namespace BITKit.Entities
|
||||
{
|
||||
public interface IEntityComponent
|
||||
{
|
||||
bool isLocalPlayer { get; }
|
||||
IEntity entity { get; }
|
||||
void Initialize(IEntity entity);
|
||||
void OnAwake();
|
||||
void OnStart();
|
||||
void OnUpdate(float deltaTime);
|
||||
void OnFixedUpdate(float deltaTime);
|
||||
void OnLateUpdate(float deltaTime);
|
||||
void OnSetOverride(bool value);
|
||||
void OnDestroyComponent();
|
||||
void OnSpawn();
|
||||
void OnDespawn();
|
||||
void RegisterCallback() { }
|
||||
void UnRegisterCallback() { }
|
||||
|
||||
}
|
||||
public abstract partial class EntityComponent : MonoBehaviour, IEntityComponent
|
||||
{
|
||||
public IEntity entity { get; set; }
|
||||
public bool isLocalPlayer => entity.Get<bool>(nameof(isLocalPlayer));
|
||||
public bool isSpawned=> entity.Get<bool>(nameof(isSpawned));
|
||||
private IEntity mEntity;
|
||||
public virtual void Initialize(IEntity entity) { this.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 OnSetOverride(bool value) { }
|
||||
public virtual void OnDestroyComponent() { }
|
||||
public virtual void RegisterCallback() { }
|
||||
public virtual void UnRegisterCallback() { }
|
||||
public virtual void OnSpawn() { }
|
||||
public virtual void OnDespawn() { }
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
[UnityEditor.CustomEditor(typeof(EntityComponent), true)]
|
||||
public class EntityComponentInspector : BITInspector<EntityComponent>
|
||||
{
|
||||
|
||||
}
|
||||
#endif
|
||||
}
|
11
Packages/Common~/Scripts/Entity/Core/EntityComponent.cs.meta
Normal file
11
Packages/Common~/Scripts/Entity/Core/EntityComponent.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 50095eaaebad1544880c98d1de25686f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
34
Packages/Common~/Scripts/Entity/Core/EntityInputComponent.cs
Normal file
34
Packages/Common~/Scripts/Entity/Core/EntityInputComponent.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
using BITKit.UniversalInputSystem;
|
||||
namespace BITKit.Entities
|
||||
{
|
||||
public class EntityInputComponent : EntityComponent, BITController.IGameplayActions
|
||||
{
|
||||
public virtual void OnMovement(InputAction.CallbackContext context) { }
|
||||
public virtual void OnView(InputAction.CallbackContext context) { }
|
||||
public virtual void OnJump(InputAction.CallbackContext context) { }
|
||||
public virtual void OnCrouch(InputAction.CallbackContext context) { }
|
||||
public virtual void OnHoldCrouch(InputAction.CallbackContext context) { }
|
||||
public virtual void OnFire(InputAction.CallbackContext context) { }
|
||||
public virtual void OnAim(InputAction.CallbackContext context) { }
|
||||
public virtual void OnInteractive(InputAction.CallbackContext context) { }
|
||||
public virtual void OnAbility(InputAction.CallbackContext context) { }
|
||||
public virtual void OnMelee(InputAction.CallbackContext context) { }
|
||||
public virtual void OnRun(InputAction.CallbackContext context) { }
|
||||
public virtual void OnRunHold(InputAction.CallbackContext context) { }
|
||||
public virtual void OnReload(InputAction.CallbackContext context) { }
|
||||
public virtual void OnPrimary(InputAction.CallbackContext context) { }
|
||||
public virtual void OnSecondary(InputAction.CallbackContext context) { }
|
||||
public virtual void OnTertiary(InputAction.CallbackContext context) { }
|
||||
public virtual void OnQuaternary(InputAction.CallbackContext context) { }
|
||||
public virtual void OnQuinary(InputAction.CallbackContext context) { }
|
||||
public virtual void OnSenary(InputAction.CallbackContext context) { }
|
||||
public virtual void OnSeptenary(InputAction.CallbackContext context) { }
|
||||
public virtual void OnOctonary(InputAction.CallbackContext context) { }
|
||||
public virtual void OnNonary(InputAction.CallbackContext context) { }
|
||||
public virtual void OnDenary(InputAction.CallbackContext context) { }
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: dd044c1ce22e43f4eb9f753a9f8438cc
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
22
Packages/Common~/Scripts/Entity/Core/EntityOverride.cs
Normal file
22
Packages/Common~/Scripts/Entity/Core/EntityOverride.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
namespace BITKit.Entities
|
||||
{
|
||||
public class EntityOverride : EntityComponent
|
||||
{
|
||||
ValidHandle isOverriding = new();
|
||||
IEntityComponent[] components;
|
||||
public override void OnAwake()
|
||||
{
|
||||
components = GetComponentsInChildren<IEntityComponent>();
|
||||
isOverriding.AddListener(_isOverriding =>
|
||||
{
|
||||
components.ForEach(x =>
|
||||
{
|
||||
x.OnSetOverride(_isOverriding);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
11
Packages/Common~/Scripts/Entity/Core/EntityOverride.cs.meta
Normal file
11
Packages/Common~/Scripts/Entity/Core/EntityOverride.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8bf3c729cc99f0b47a6b0bd8f97fac73
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
42
Packages/Common~/Scripts/Entity/Core/IEntity.cs
Normal file
42
Packages/Common~/Scripts/Entity/Core/IEntity.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AddressableAssets;
|
||||
namespace BITKit.Entities
|
||||
{
|
||||
/// <summary>Entity接口,用于复杂实体</summary>
|
||||
public interface IEntity : IGenericEvent<string>, IDatabase, IProcessor, ICallback
|
||||
{
|
||||
public static IEntity LocalPlayer;
|
||||
public static Action<IEntity> OnSpawnLocalPlayer;
|
||||
IEntityComponent[] entityComponents { get; set; }
|
||||
int Id { get; set; }
|
||||
string AddressablePath => nameof(IEntity);
|
||||
}
|
||||
public class IEntityReader : NetMessageReader<IEntity>
|
||||
{
|
||||
public override IEntity ReadBinary(BinaryReader reader)
|
||||
{
|
||||
var id = reader.ReadInt32();
|
||||
var path = reader.ReadString();
|
||||
var entity = EntitiesManager.GetOrAdd(id, _id => Create(id, path));
|
||||
return entity;
|
||||
}
|
||||
public override void WriteBinary(BinaryWriter writer, IEntity value)
|
||||
{
|
||||
writer.Write(value.Id);
|
||||
writer.Write(value.AddressablePath);
|
||||
}
|
||||
IEntity Create(int id, string path)
|
||||
{
|
||||
var entity = Addressables
|
||||
.LoadAssetAsync<GameObject>(path)
|
||||
.WaitForCompletion()
|
||||
.GetComponent<IEntity>();
|
||||
entity.Id = id;
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
}
|
11
Packages/Common~/Scripts/Entity/Core/IEntity.cs.meta
Normal file
11
Packages/Common~/Scripts/Entity/Core/IEntity.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7cfd32bf242fb4d49be955f6f0558e5f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user