1
This commit is contained in:
@@ -62,6 +62,14 @@ namespace BITKit.Entities
|
||||
/// </summary>
|
||||
public interface IEntityMovement:IStateMachine<IEntityMovementState>
|
||||
{
|
||||
/// <summary>
|
||||
/// 视角中心,通常是摄像机的位置
|
||||
/// </summary>
|
||||
Vector3 ViewCenter { get; }
|
||||
/// <summary>
|
||||
/// 视角旋转,通常是摄像机的旋转
|
||||
/// </summary>
|
||||
Quaternion ViewRotation { get; }
|
||||
/// <summary>
|
||||
/// 基于运动的速度,是相对于标准化移动速度的相对速度
|
||||
/// </summary>
|
||||
@@ -75,6 +83,10 @@ namespace BITKit.Entities
|
||||
/// </summary>
|
||||
Vector3 GroundVelocity { get; }
|
||||
/// <summary>
|
||||
/// 旋转速度
|
||||
/// </summary>
|
||||
Vector3 AngularVelocity { get; }
|
||||
/// <summary>
|
||||
/// 是否在地面上
|
||||
/// </summary>
|
||||
bool IsGrounded { get; }
|
||||
@@ -96,8 +108,16 @@ namespace BITKit.Entities
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
void Movement(InputAction.CallbackContext context);
|
||||
|
||||
/// <summary>
|
||||
/// 执行命令
|
||||
/// </summary>
|
||||
/// <param name="command"></param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
void ExecuteCommand<T>(T command=default);
|
||||
/// <summary>
|
||||
/// 执行命令的回调
|
||||
/// </summary>
|
||||
event Action<object> OnCommand;
|
||||
}
|
||||
public interface IEntityMovementState:IState
|
||||
{
|
||||
@@ -107,80 +127,4 @@ namespace BITKit.Entities
|
||||
void AfterUpdateMovement(float deltaTime);
|
||||
void ExecuteCommand<T>(T command);
|
||||
}
|
||||
public interface IPlayerMovementCommand{}
|
||||
[Serializable]
|
||||
public class MonoMovementProxy:IEntityMovement
|
||||
{
|
||||
[SerializeField] private MonoBehaviour monoBehaviour;
|
||||
private IEntityMovement _entityMovementImplementation=>monoBehaviour as IEntityMovement;
|
||||
|
||||
public Vector3 LocomotionBasedVelocity=>_entityMovementImplementation.LocomotionBasedVelocity;
|
||||
public Vector3 Velocity => _entityMovementImplementation.Velocity;
|
||||
|
||||
public Vector3 GroundVelocity => _entityMovementImplementation.GroundVelocity;
|
||||
|
||||
public bool IsGrounded => _entityMovementImplementation.IsGrounded;
|
||||
|
||||
public void SyncMovement(Vector3 velocity, Vector3 position, Quaternion rotation, bool isGrounded)
|
||||
{
|
||||
_entityMovementImplementation.SyncMovement(velocity, position, rotation, isGrounded);
|
||||
}
|
||||
|
||||
public void Movement(Vector3 relativeVector)
|
||||
{
|
||||
_entityMovementImplementation.Movement(relativeVector);
|
||||
}
|
||||
|
||||
public void Movement(InputAction.CallbackContext context)
|
||||
{
|
||||
_entityMovementImplementation.Movement(context);
|
||||
}
|
||||
|
||||
public void ExecuteCommand<T>(T command)=>_entityMovementImplementation.ExecuteCommand(command);
|
||||
|
||||
public bool Enabled
|
||||
{
|
||||
get => _entityMovementImplementation.Enabled;
|
||||
set => _entityMovementImplementation.Enabled = value;
|
||||
}
|
||||
|
||||
public IEntityMovementState CurrentState
|
||||
{
|
||||
get => _entityMovementImplementation.CurrentState;
|
||||
set => _entityMovementImplementation.CurrentState = value;
|
||||
}
|
||||
|
||||
public event Action<IEntityMovementState, IEntityMovementState> OnStateChanged
|
||||
{
|
||||
add => _entityMovementImplementation.OnStateChanged += value;
|
||||
remove => _entityMovementImplementation.OnStateChanged -= value;
|
||||
}
|
||||
|
||||
public IDictionary<Type, IEntityMovementState> StateDictionary => _entityMovementImplementation.StateDictionary;
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
_entityMovementImplementation.Initialize();
|
||||
}
|
||||
|
||||
public void UpdateState(float deltaTime)
|
||||
{
|
||||
_entityMovementImplementation.UpdateState(deltaTime);
|
||||
}
|
||||
|
||||
public void DisposeState()
|
||||
{
|
||||
_entityMovementImplementation.DisposeState();
|
||||
}
|
||||
|
||||
public void TransitionState<State>() where State : IEntityMovementState
|
||||
{
|
||||
_entityMovementImplementation.TransitionState<State>();
|
||||
}
|
||||
|
||||
public void TransitionState(IEntityMovementState state)
|
||||
{
|
||||
_entityMovementImplementation.TransitionState(state);
|
||||
}
|
||||
}
|
||||
}
|
@@ -14,9 +14,12 @@ namespace BITKit
|
||||
#endregion
|
||||
#region 接口实现
|
||||
|
||||
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;
|
||||
@@ -55,6 +58,8 @@ namespace BITKit
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public event Action<object> OnCommand;
|
||||
|
||||
public void OnSetAlive(bool alive)
|
||||
{
|
||||
switch (alive)
|
||||
|
@@ -90,9 +90,6 @@ namespace BITKit.Entities
|
||||
{
|
||||
OnSetAliveInternal(IsAlive = _isAlive);
|
||||
}
|
||||
|
||||
//entity.Invoke<int>(_onSetHP, newHP);
|
||||
//entity.Set<int>("HP", newHP);
|
||||
foreach (var x in entity.GetCallbacks<IHealthCallback>())
|
||||
{
|
||||
x.OnSetHP(newHP);
|
||||
@@ -108,20 +105,17 @@ namespace BITKit.Entities
|
||||
|
||||
private void OnSetAliveInternal(bool alive)
|
||||
{
|
||||
IsAlive = alive;
|
||||
foreach (var x in entity.GetCallbacks<IHealthCallback>())
|
||||
{
|
||||
x.OnSetAlive(alive);
|
||||
}
|
||||
|
||||
foreach (var x in additiveCallback)
|
||||
{
|
||||
x.OnSetAlive(alive);
|
||||
}
|
||||
|
||||
//entity.Invoke<bool>(_onSetAlive, alive);
|
||||
//entity.Set<bool>(_isAlive, alive);
|
||||
onSetAlive.Invoke(alive);
|
||||
OnSetAlive?.Invoke(alive);
|
||||
onSetAlive.Invoke(alive);
|
||||
}
|
||||
|
||||
private void AddHP(int hp)
|
||||
|
@@ -68,7 +68,7 @@ namespace BITKit.Entities.Player
|
||||
}
|
||||
public void Interactive(InputAction.CallbackContext context)
|
||||
{
|
||||
if (context.interaction is not TapInteraction || !context.performed) return;
|
||||
if (context.interaction is not PressInteraction || !context.performed) return;
|
||||
var _selected = selected;
|
||||
if (_selected is not MonoBehaviour monoBehaviour) return;
|
||||
if (monoBehaviour.TryGetComponentAny<IAction>(out var action))
|
||||
|
@@ -31,8 +31,7 @@ namespace BITKit.Entities
|
||||
entityComponents = GetComponentsInChildren<IEntityComponent>(true).Distinct().ToArray();
|
||||
foreach (var x in entityComponents)
|
||||
{
|
||||
var att = x.GetType().GetCustomAttribute<CustomTypeAttribute>();
|
||||
if (att is not null)
|
||||
foreach (var att in x.GetType().GetCustomAttributes<CustomTypeAttribute>())
|
||||
{
|
||||
genericEvent.Set(att.Type, x);
|
||||
}
|
||||
|
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"name": "BITFALL.Entities.EquipSelector.Runtime",
|
||||
"rootNamespace": "",
|
||||
"references": [
|
||||
"GUID:14fe60d984bf9f84eac55c6ea033a8f4",
|
||||
"GUID:709caf8d7fb6ef24bbba0ab9962a3ad0",
|
||||
"GUID:b355af20142c0c541ba9588ab1d0f64e",
|
||||
"GUID:75469ad4d38634e559750d17036d5f7c",
|
||||
"GUID:30cdc242b1ac6a944a460f4ab0b77b88",
|
||||
"GUID:677cd05ca06c46b4395470200b1acdad",
|
||||
"GUID:7efac18f239530141802fb139776f333",
|
||||
"GUID:84d565da37ad40546a118cfb3c3509f3",
|
||||
"GUID:42a9827d94e00374aa52e51f0a1b035c",
|
||||
"GUID:d525ad6bd40672747bde77962f1c401e",
|
||||
"GUID:49b49c76ee64f6b41bf28ef951cb0e50",
|
||||
"GUID:9354affc93e0f3e4a904785e7d4c0f59"
|
||||
],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": false,
|
||||
"precompiledReferences": [],
|
||||
"autoReferenced": true,
|
||||
"defineConstraints": [],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
@@ -0,0 +1,164 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using BITKit;
|
||||
using BITKit.Entities;
|
||||
using UnityEngine.InputSystem;
|
||||
using static UnityEditor.Progress;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using BITKit.Entities.Player;
|
||||
using UnityEngine.InputSystem.Interactions;
|
||||
using Debug = UnityEngine.Debug;
|
||||
|
||||
namespace BITFALL
|
||||
{
|
||||
[CustomType(typeof(IPlayerEquipSelector))]
|
||||
public class PlayerEquipSelector : EntityComponent,TaskSubscriber<IBasicItem>,IEntityInventoryCallback,IPlayerEquipSelector
|
||||
{
|
||||
[Header(Constant.Header.Components)]
|
||||
public EntityEquipment equipment;
|
||||
|
||||
[Header(Constant.Header.InternalVariables)]
|
||||
private readonly Dictionary<int, IBasicItem> equips=new();
|
||||
private IBasicItemContainer inventory;
|
||||
|
||||
public event Action<IBasicItem> OnEquip;
|
||||
public event Action<IBasicItem> OnDeEquip;
|
||||
public event Action<IDictionary<int, IBasicItem>> OnUpdateEquip;
|
||||
|
||||
private IBasicItem currentEquip;
|
||||
public override void OnAwake()
|
||||
{
|
||||
var health = entity.Get<IHealth>();
|
||||
health.OnSetAlive += OnSetAlive;
|
||||
OnDeEquip += DeEquip;
|
||||
OnEquip += Equip;
|
||||
}
|
||||
public override void OnStart()
|
||||
{
|
||||
base.OnStart();
|
||||
|
||||
entity.RegisterCallback<TaskSubscriber<IBasicItem>>(this);
|
||||
|
||||
inventory = entity.Get<IBasicItemContainer>();
|
||||
}
|
||||
public void OnPrimary(InputAction.CallbackContext context)
|
||||
{
|
||||
if (context is not {interaction:PressInteraction ,performed:true}) return;
|
||||
Equip(1);
|
||||
}
|
||||
public void OnSecondary(InputAction.CallbackContext context)
|
||||
{
|
||||
if (context is not {interaction:PressInteraction ,performed:true}) return;
|
||||
Equip(2);
|
||||
}
|
||||
public void OnTertiary(InputAction.CallbackContext context)
|
||||
{
|
||||
if (context is not {interaction:PressInteraction ,performed:true}) return;
|
||||
if (Equip(3) is false)
|
||||
{
|
||||
Equip(-1);
|
||||
}
|
||||
}
|
||||
public void OnQuaternary(InputAction.CallbackContext context)
|
||||
{
|
||||
if (context is not {interaction:PressInteraction ,performed:true}) return;
|
||||
Equip(4);
|
||||
}
|
||||
public void OnHolster(InputAction.CallbackContext context)
|
||||
{
|
||||
if (context is not {interaction:PressInteraction ,performed:true}) return;
|
||||
Equip(-1);
|
||||
}
|
||||
private void OnSetAlive(bool alive)
|
||||
{
|
||||
if (alive) return;
|
||||
foreach (var x in equips.ToArray())
|
||||
{
|
||||
inventory.Add(x.Value);
|
||||
}
|
||||
equips.Clear();
|
||||
UpdateEquip();
|
||||
Equip(-1);
|
||||
}
|
||||
int TaskSubscriber<IBasicItem>.Priority => 0;
|
||||
|
||||
bool TaskSubscriber<IBasicItem>.TryExecute(IBasicItem value)
|
||||
{
|
||||
var asset = value.GetAssetable();
|
||||
if (IsSupportItem(value) is false) return false;
|
||||
switch (asset)
|
||||
{
|
||||
case var _ when asset.TryGetProperty<EquipmentAsWeapon>(out _):
|
||||
if (equips.TryAdd(1, value) || equips.TryAdd(2,value))
|
||||
{
|
||||
OnEquip?.Invoke(value);
|
||||
UpdateEquip();
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
public void OnAdd(IBasicItem item)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void OnRemove(IBasicItem item)
|
||||
{
|
||||
if (IsSupportItem(item) is false)
|
||||
{
|
||||
UpdateEquip();
|
||||
}
|
||||
}
|
||||
private bool IsSupportItem(IBasicItem item)
|
||||
{
|
||||
return equipment.equips.list.Any(x => x.AddressablePath == item.AddressablePath);
|
||||
}
|
||||
private void UpdateEquip()
|
||||
{
|
||||
OnUpdateEquip?.Invoke(new Dictionary<int, IBasicItem>(equips));
|
||||
}
|
||||
|
||||
public bool TryDeEquip(IBasicItem item)
|
||||
{
|
||||
if (item is null) return false;
|
||||
if (equips.Any(x => x.Value.AddressablePath == item.AddressablePath) is false) return false;
|
||||
var index = equips.Single(x=>x.Value.AddressablePath==item.AddressablePath).Key;
|
||||
if (equips.TryRemove(index) is false) return false;
|
||||
if (!inventory.Add(item)) return false;
|
||||
OnDeEquip?.Invoke(item);
|
||||
UpdateEquip();
|
||||
return true;
|
||||
}
|
||||
|
||||
private void Equip(IBasicItem item)
|
||||
{
|
||||
if (item is null)
|
||||
{
|
||||
equipment.equips.Entry(-1);
|
||||
}
|
||||
else
|
||||
{
|
||||
equipment.equips.Entry(x=>x.AddressablePath == item.AddressablePath);
|
||||
}
|
||||
}
|
||||
|
||||
private bool Equip(int index)
|
||||
{
|
||||
if (!equips.TryGetValue(index, out var x) && index is not -1) return false;
|
||||
if (index is -1)
|
||||
{
|
||||
OnEquip?.Invoke(x);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
private void DeEquip(IBasicItem item)
|
||||
{
|
||||
equipment.equips.Entry(-1);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user