103 lines
3.0 KiB
C#
103 lines
3.0 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Animancer;
|
|
using BITFALL.Entities.Equipment.Melee;
|
|
using BITFALL.Entities.Equipment.Universal.States;
|
|
using BITFALL.Entities.Inventory;
|
|
using BITFALL.Player.Inventory;
|
|
using BITKit;
|
|
using BITKit.Entities;
|
|
using BITKit.StateMachine;
|
|
using Cysharp.Threading.Tasks;
|
|
using UnityEngine;
|
|
using Draw = BITFALL.Entities.Equipment.Universal.States.Draw;
|
|
|
|
namespace BITFALL.Entities.Equipment.Universal
|
|
{
|
|
public interface IUseState : IState
|
|
{
|
|
}
|
|
|
|
public abstract class UseState : IUseState
|
|
{
|
|
[SerializeField] protected UniversalUseController useController;
|
|
[SerializeField] protected AnimancerComponent animancerComponent;
|
|
[SerializeField] protected AnimationClip[] clips;
|
|
public virtual bool Enabled { get; set; }
|
|
protected AnimancerState currentState;
|
|
|
|
public virtual void Initialize()
|
|
{
|
|
|
|
}
|
|
public virtual void OnStateEntry(IState old)
|
|
{
|
|
PlayAnimation();
|
|
}
|
|
public virtual void PlayAnimation()
|
|
{
|
|
if (clips?.Length is 0) return;
|
|
var clip = clips.Random();
|
|
currentState = animancerComponent.Play(clip);
|
|
currentState.Events.OnEnd = OnPlayEnd;
|
|
}
|
|
|
|
public virtual void OnStateUpdate(float deltaTime)
|
|
{
|
|
}
|
|
public virtual void OnStateExit(IState old, IState newState)
|
|
{
|
|
}
|
|
|
|
protected virtual void OnPlayEnd()
|
|
{
|
|
if (currentState is { IsValid: true })
|
|
currentState.Events.OnEnd = null;
|
|
}
|
|
}
|
|
|
|
public class UniversalUseController : BITEquipBase<IUseState>
|
|
{
|
|
public struct Used:IProperty{}
|
|
/// <summary>
|
|
/// 是使用完就释放,还是连续使用
|
|
/// </summary>
|
|
[SerializeField] private bool release;
|
|
[Inject] internal IPlayerInventory _playerInventory;
|
|
[Inject] internal IPlayerEquipSelector _playerEquipSelector;
|
|
[Inject] internal IEntityEquipmentContainer _equipmentContainer;
|
|
public bool Release => release;
|
|
public override void Entry()
|
|
{
|
|
base.Entry();
|
|
TransitionState<Draw>();
|
|
}
|
|
public override void Exit()
|
|
{
|
|
base.Exit();
|
|
TransitionState<Exit>();
|
|
}
|
|
public override void AnimationEvent(string eventName)
|
|
{
|
|
base.AnimationEvent(eventName);
|
|
|
|
switch (eventName)
|
|
{
|
|
case BITConstant.Player.Use when CurrentState is Use:
|
|
var current = Item;
|
|
if (item.TryGetProperty<EquipmentAsSlot>(out var asSlot))
|
|
{
|
|
_equipmentContainer.TryUseEquip(asSlot.slot);
|
|
}
|
|
else
|
|
{
|
|
_inventory.UseItem(Item);
|
|
}
|
|
if (current?.Id == Item?.Id)
|
|
Item = null;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
} |