1
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using System.Linq;
|
||||
using System.Net.Configuration;
|
||||
using BITKit.Animations;
|
||||
|
||||
namespace BITKit.Entities
|
||||
@@ -14,9 +15,15 @@ namespace BITKit.Entities
|
||||
[SerializeReference, SubclassSelector] private References[] boolParameters;
|
||||
[SerializeReference, SubclassSelector] private References[] floatParameters;
|
||||
private List<string> keyWords;
|
||||
|
||||
private ConstantHash[] _boolParameterHashes;
|
||||
private ConstantHash[] _floatParameterHashes;
|
||||
public override void OnAwake()
|
||||
{
|
||||
keyWords = animationKeyWords.Select(x => x.Get()).ToList();
|
||||
|
||||
_boolParameterHashes = boolParameters.Select(x =>new ConstantHash(x.Get())).ToArray();
|
||||
_floatParameterHashes = floatParameters.Select(x =>new ConstantHash(x.Get())).ToArray();
|
||||
}
|
||||
public override void OnStart()
|
||||
{
|
||||
@@ -35,26 +42,30 @@ namespace BITKit.Entities
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnFixedUpdate(float deltaTime)
|
||||
{
|
||||
foreach (var boolPar in boolParameters)
|
||||
bool cacheBool;
|
||||
float cacheFloat;
|
||||
foreach (var boolPar in _boolParameterHashes)
|
||||
{
|
||||
animators.ForEach(x =>
|
||||
{
|
||||
if (x.isActiveAndEnabled)
|
||||
x.animator.SetBool(boolPar, UnityEntity.Get<bool>(boolPar));
|
||||
});
|
||||
foreach (var x in animators)
|
||||
{
|
||||
if (!x.isActiveAndEnabled) return;
|
||||
UnityEntity.GetDirect<bool>(boolPar, out cacheBool);
|
||||
x.animator.SetBool(boolPar.HashCode, cacheBool);
|
||||
}
|
||||
}
|
||||
foreach (var floatPar in floatParameters)
|
||||
foreach (var floatPar in _floatParameterHashes)
|
||||
{
|
||||
animators.ForEach(x =>
|
||||
{
|
||||
if (x.isActiveAndEnabled)
|
||||
x.animator.SetFloat(floatPar, UnityEntity.Get<float>(floatPar));
|
||||
});
|
||||
foreach (var x in animators)
|
||||
{
|
||||
if (!x.isActiveAndEnabled) return;
|
||||
UnityEntity.GetDirect<float>(floatPar, out cacheFloat);
|
||||
x.animator.SetFloat(floatPar.HashCode, cacheFloat);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnAnimatorMove()
|
||||
{
|
||||
if (enabled is false) return;
|
||||
|
@@ -10,7 +10,7 @@ namespace BITKit.Entities.Player.Character
|
||||
[SerializeField] private Renderer[] fpvRenderer = Array.Empty<Renderer>();
|
||||
[SerializeField] private Renderer[] tpvRenderer = Array.Empty<Renderer>();
|
||||
[Header(Constant.Header.Reference)]
|
||||
[SerializeReference, SubclassSelector] public References _getDamage;
|
||||
[SerializeReference, SubclassSelector] public IReference getDamage;
|
||||
public override void OnStart()
|
||||
{
|
||||
var heal = UnityEntity.Get<IHealth>();
|
||||
@@ -33,7 +33,7 @@ namespace BITKit.Entities.Player.Character
|
||||
}
|
||||
private void OnSetHP(int hp)
|
||||
{
|
||||
UnityEntity.Invoke<string>(Constant.Animation.Play, _getDamage);
|
||||
UnityEntity.Invoke<string>(Constant.Animation.Play, getDamage.Value);
|
||||
}
|
||||
private void SetFPV(bool isFpv)
|
||||
{
|
||||
|
@@ -1,132 +0,0 @@
|
||||
using System;
|
||||
using BITKit.Entities;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AI;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
namespace BITKit
|
||||
{
|
||||
public class NavAgentMovement: StateBasedBehavior<IEntityMovementState>,IEntityMovement
|
||||
{
|
||||
#region 属性
|
||||
[SerializeField] private NavMeshAgent agent;
|
||||
[SerializeField] private new Rigidbody rigidbody;
|
||||
|
||||
[Inject] private IHealth _health;
|
||||
|
||||
[Inject(true)] private IEntityOverride _override;
|
||||
#endregion
|
||||
|
||||
public override void OnStart()
|
||||
{
|
||||
if (_override is not null)
|
||||
{
|
||||
_override.OnOverride += (x) =>
|
||||
{
|
||||
agent.isStopped = x;
|
||||
};
|
||||
}
|
||||
|
||||
_health.OnSetAlive += OnSetAlive;
|
||||
_health.OnSetHealthPoint += OnSetHP;
|
||||
|
||||
}
|
||||
|
||||
#region 接口实现
|
||||
|
||||
public Vector3 Position
|
||||
{
|
||||
get=>transform.position;
|
||||
set=>transform.position=value;
|
||||
}
|
||||
|
||||
public Quaternion Rotation
|
||||
{
|
||||
get=>transform.rotation;
|
||||
set=>transform.rotation=value;
|
||||
}
|
||||
|
||||
public Vector3 Forward => transform.forward;
|
||||
public Vector3 ViewCenter { get; set; }
|
||||
public Quaternion ViewRotation { get; set; }
|
||||
public Vector3 LocomotionBasedVelocity { get; private set; }
|
||||
public Vector3 Velocity { get; private set; }
|
||||
public Vector3 GroundVelocity { get; private set; }
|
||||
public Vector3 AngularVelocity { get; private set; }
|
||||
public bool IsGrounded { get; private set; }
|
||||
private bool isDead;
|
||||
private Vector3 recordPosition;
|
||||
private Quaternion recordRotation;
|
||||
private IEntityMovement _entityMovementImplementation;
|
||||
#endregion
|
||||
public override void OnUpdate(float deltaTime)
|
||||
{
|
||||
Velocity = agent.velocity;
|
||||
var _groundVelocity = Velocity;
|
||||
_groundVelocity.y = 0;
|
||||
GroundVelocity = _groundVelocity;
|
||||
IsGrounded = agent.isOnOffMeshLink is false;
|
||||
|
||||
UnityEntity.Set<bool>("IsMoving",Velocity.sqrMagnitude>=0.16f);
|
||||
UnityEntity.Set<float>("SqrMagnitude",Velocity.sqrMagnitude);
|
||||
|
||||
if (!isDead) return;
|
||||
|
||||
recordPosition = rigidbody.position;
|
||||
recordRotation = rigidbody.rotation * transform.rotation;
|
||||
}
|
||||
public void SyncMovement(Vector3 velocity, Vector3 position, Quaternion rotation, bool isGrounded)
|
||||
{
|
||||
|
||||
}
|
||||
public void Movement(Vector3 relativeVector)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void Movement(InputAction.CallbackContext context)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void ExecuteCommand<T>(T command)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public event Action<object> OnCommand;
|
||||
|
||||
public void OnSetAlive(bool alive)
|
||||
{
|
||||
switch (alive)
|
||||
{
|
||||
case false:
|
||||
isDead = true;
|
||||
break;
|
||||
case true when isDead:
|
||||
{
|
||||
var _transform = transform;
|
||||
_transform.position = new Vector3()
|
||||
{
|
||||
x=recordPosition.x,
|
||||
y=0,
|
||||
z=recordPosition.x,
|
||||
};
|
||||
rigidbody.position = recordPosition;
|
||||
|
||||
_transform.rotation *= recordRotation;
|
||||
|
||||
rigidbody.rotation = recordRotation;
|
||||
|
||||
isDead = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void OnSetHP(int hp)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -16,6 +16,8 @@ namespace BITKit.Entities
|
||||
public event Action<DamageMessage> OnEntityKilled;
|
||||
void Execute(DamageMessage damageMessage);
|
||||
}
|
||||
public struct MeleeDamageMessage:IDamageType{}
|
||||
public struct BulletDamageMessage:IDamageType{}
|
||||
[Serializable]
|
||||
public class DamageServiceSingleton:IDamageService
|
||||
{
|
||||
@@ -69,6 +71,7 @@ namespace BITKit.Entities
|
||||
public Location Location;
|
||||
public IDamageType DamageType;
|
||||
}
|
||||
|
||||
public interface IDamageCallback
|
||||
{
|
||||
void OnGetDamage(DamageMessage message);
|
||||
|
@@ -1,7 +1,9 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor;
|
||||
using UnityEditor.UIElements;
|
||||
#endif
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
@@ -18,9 +20,7 @@ namespace BITKit.Entities.InputSystem
|
||||
[Inject(true)]
|
||||
private IEntityOverride _override;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
[SerializeField,HideInInspector] internal bool Allow;
|
||||
#endif
|
||||
public override void Initialize(IEntity _entity)
|
||||
{
|
||||
base.Initialize(_entity);
|
||||
@@ -46,6 +46,18 @@ namespace BITKit.Entities.InputSystem
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnStart()
|
||||
{
|
||||
base.OnStart();
|
||||
inputActionGroup.allowInput.Invoke();
|
||||
}
|
||||
|
||||
public override void OnDestroyComponent()
|
||||
{
|
||||
base.OnDestroyComponent();
|
||||
inputActionGroup.Dispose();
|
||||
}
|
||||
}
|
||||
#if UNITY_EDITOR
|
||||
[CustomEditor(typeof(EntityInputSystem))]
|
||||
|
@@ -31,20 +31,27 @@ namespace BITKit.Entities.Player
|
||||
public override void OnUpdate(float deltaTime)
|
||||
{
|
||||
//if (sensor.Get().TryGetAny(x=>x.TryGetComponentAny<ISelectable>(out _),out var detected))
|
||||
if (sensor.Get().TryGetAny(x=>x.GetComponentInParent<ISelectable>() is not null,out var detected))
|
||||
try
|
||||
{
|
||||
if (detected.TryGetComponentAny<ISelectable>(out var _detected))
|
||||
if (sensor.Get().TryGetAny(x=>x.GetComponentInParent<ISelectable>() is not null,out var detected))
|
||||
{
|
||||
if (_detected == selected)
|
||||
if (detected.TryGetComponentAny<ISelectable>(out var _detected))
|
||||
{
|
||||
if (_detected == selected)
|
||||
{
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
TryDeSelected();
|
||||
|
||||
Detected(_detected);
|
||||
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
TryDeSelected();
|
||||
|
||||
Detected(_detected);
|
||||
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -52,10 +59,14 @@ namespace BITKit.Entities.Player
|
||||
TryDeSelected();
|
||||
}
|
||||
}
|
||||
else
|
||||
catch(MissingReferenceException e)
|
||||
{}
|
||||
catch (Exception e)
|
||||
{
|
||||
TryDeSelected();
|
||||
Console.WriteLine(e);
|
||||
throw;
|
||||
}
|
||||
|
||||
}
|
||||
private void TryDeSelected()
|
||||
{
|
||||
|
@@ -15,7 +15,6 @@ namespace BITKit.Entities
|
||||
[SerializeField] private Collider[] ragdollColliders;
|
||||
[SerializeField] private Joint[] joints;
|
||||
[SerializeField] private new Rigidbody rigidbody;
|
||||
private CancellationToken _cancellationToken;
|
||||
[Inject]
|
||||
private IHealth _health;
|
||||
private readonly Dictionary<Joint,ConfigurableJointMotion> _jointXMotions=new();
|
||||
@@ -29,7 +28,6 @@ namespace BITKit.Entities
|
||||
{
|
||||
_health.OnSetAlive += OnSetAlive;
|
||||
_health.OnSetHealthPoint += OnSetHP;
|
||||
_cancellationToken = UnityEntity.Get<CancellationToken>();
|
||||
foreach (var x in joints)
|
||||
{
|
||||
switch (x)
|
||||
@@ -53,7 +51,7 @@ namespace BITKit.Entities
|
||||
animator.enabled = alive;
|
||||
try
|
||||
{
|
||||
await Task.Delay(10, _cancellationToken);
|
||||
await Task.Delay(10, destroyCancellationToken);
|
||||
rigidbodies.ForEach(x => { x.isKinematic = alive; });
|
||||
ragdollColliders.ForEach(x => { x.enabled = !alive; });
|
||||
OnSetPhysics?.Invoke(!alive);
|
||||
|
@@ -1,3 +1,4 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@@ -19,9 +20,18 @@ namespace BITKit.Entities.VFX
|
||||
|
||||
private void Play(string animationName)
|
||||
{
|
||||
if (isActiveAndEnabled is false) return;
|
||||
if (keyWords.Contains(animationName) is false) return;
|
||||
vfxPlayer.Execute();
|
||||
try
|
||||
{
|
||||
if (isActiveAndEnabled is false) return;
|
||||
if (keyWords.Contains(animationName) is false) return;
|
||||
vfxPlayer.Execute();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogWarning(transform.name);
|
||||
Debug.LogException(e);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -156,6 +156,21 @@ namespace BITKit.Entities
|
||||
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 void SetDirect(int key, object value)
|
||||
{
|
||||
genericEvent.SetDirect(key, value);
|
||||
}
|
||||
|
||||
public void GetDirect(int key, out object value)
|
||||
{
|
||||
genericEvent.GetDirect(key, out value);
|
||||
}
|
||||
|
||||
public void GetDirect<T>(int key, out T value)
|
||||
{
|
||||
genericEvent.GetDirect(key, out value);
|
||||
}
|
||||
|
||||
public T Get<T>(string key = Constant.System.Internal)
|
||||
{
|
||||
var value = genericEvent.Get<T>(key);
|
||||
|
@@ -4,7 +4,6 @@ using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using BITKit.Entities;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AddressableAssets;
|
||||
namespace BITKit.Entities
|
||||
{
|
||||
/// <summary>Entity接口,用于复杂实体</summary>
|
||||
@@ -14,27 +13,27 @@ namespace BITKit.Entities
|
||||
void AddService(object service);
|
||||
void AddService(Type type, object service);
|
||||
}
|
||||
public class IEntityReader : NetMessageReader<IUnityEntity>
|
||||
{
|
||||
public override IUnityEntity ReadBinary(BinaryReader reader)
|
||||
{
|
||||
var id = reader.ReadInt32();
|
||||
var path = reader.ReadString();
|
||||
var entity = DI.Get<IEntitiesService>().Entities[id];
|
||||
return (IUnityEntity)entity;
|
||||
}
|
||||
public override void WriteBinary(BinaryWriter writer, IUnityEntity value)
|
||||
{
|
||||
writer.Write(value.Id);
|
||||
writer.Write(value.Id);
|
||||
}
|
||||
private IUnityEntity Create(int id, string path)
|
||||
{
|
||||
var entity = Addressables
|
||||
.LoadAssetAsync<GameObject>(path)
|
||||
.WaitForCompletion()
|
||||
.GetComponent<IUnityEntity>();
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
// public class IEntityReader : NetMessageReader<IUnityEntity>
|
||||
// {
|
||||
// public override IUnityEntity ReadBinary(BinaryReader reader)
|
||||
// {
|
||||
// var id = reader.ReadInt32();
|
||||
// var path = reader.ReadString();
|
||||
// var entity = DI.Get<IEntitiesService>().Entities[id];
|
||||
// return (IUnityEntity)entity;
|
||||
// }
|
||||
// public override void WriteBinary(BinaryWriter writer, IUnityEntity value)
|
||||
// {
|
||||
// writer.Write(value.Id);
|
||||
// writer.Write(value.Id);
|
||||
// }
|
||||
// private IUnityEntity Create(int id, string path)
|
||||
// {
|
||||
// var entity = Addressables
|
||||
// .LoadAssetAsync<GameObject>(path)
|
||||
// .WaitForCompletion()
|
||||
// .GetComponent<IUnityEntity>();
|
||||
// return entity;
|
||||
// }
|
||||
// }
|
||||
}
|
26
Src/Unity/Scripts/Entity/Core/UnityIdComponent.cs
Normal file
26
Src/Unity/Scripts/Entity/Core/UnityIdComponent.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace BITKit.Entities
|
||||
{
|
||||
public interface IdComponent
|
||||
{
|
||||
ulong Id { get; }
|
||||
string Name { get; }
|
||||
}
|
||||
public class UnityIdComponent : EntityBehavior,IdComponent
|
||||
{
|
||||
[SerializeField] private ulong id;
|
||||
[SerializeField] private string unityName;
|
||||
public ulong Id => id;
|
||||
public string Name => unityName;
|
||||
public override void Initialize(IEntity _entity)
|
||||
{
|
||||
base.Initialize(_entity);
|
||||
id = _entity.Id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,5 +1,5 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c9f4b115cecfadd40b191dc35f29ef08
|
||||
guid: 7deed5246427534489a19e5325df9a8d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
@@ -18,6 +18,8 @@ namespace BITKit.Entities.Editor
|
||||
}
|
||||
private Label _timeLabel;
|
||||
private VisualElement _container;
|
||||
private VisualElement _idContainer;
|
||||
private VisualElement _nameContainer;
|
||||
private void OnEnable()
|
||||
{
|
||||
_timeLabel = rootVisualElement.Create<Label>();
|
||||
@@ -25,15 +27,25 @@ namespace BITKit.Entities.Editor
|
||||
rootVisualElement.styleSheets.Add(BITEditorUtils.InspectorStyleSheet);
|
||||
rootVisualElement.styleSheets.Add(BITEditorUtils.Style);
|
||||
rootVisualElement.AddToClassList("pa-8");
|
||||
|
||||
_idContainer = _container.Create<VisualElement>();
|
||||
_nameContainer = _container.Create<VisualElement>();
|
||||
|
||||
_container.style.flexDirection = FlexDirection.Row;
|
||||
_idContainer.style.flexDirection = FlexDirection.Column;
|
||||
_nameContainer.style.flexDirection = FlexDirection.Column;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
_timeLabel.text = DateTime.Now.ToString(CultureInfo.InvariantCulture);
|
||||
_container.Clear();
|
||||
_nameContainer.Clear();
|
||||
_idContainer.Clear();
|
||||
foreach (var x in UnityEntitiesService.Entities)
|
||||
{
|
||||
_container.Create<Label>().text = $"{x.Id}\t{x}";
|
||||
//_container.Create<Label>().text = $"{x.Id}\t{x}";
|
||||
_idContainer.Create<Label>().text = $"{x.Id}";
|
||||
_nameContainer.Create<Label>().text = $"{x}";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user