138 lines
3.8 KiB
C#
138 lines
3.8 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
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;
|
|
public virtual bool Enabled { get; set; }
|
|
|
|
public virtual void Initialize()
|
|
{
|
|
|
|
}
|
|
|
|
public virtual void OnStateEntry(IState old)
|
|
{
|
|
}
|
|
|
|
public virtual void OnStateUpdate(float deltaTime)
|
|
{
|
|
}
|
|
|
|
public virtual void OnStateExit(IState old, IState newState)
|
|
{
|
|
}
|
|
}
|
|
|
|
public class UniversalUseController : BITEquipBase<IUseState>
|
|
{
|
|
[Inject] private IPlayerInventory _playerInventory;
|
|
[Inject] private IEntityInventory _inventory;
|
|
[Inject] private IPlayerEquipSelector _playerEquipSelector;
|
|
[Inject] private IEntityEquipmentContainer _equipmentContainer;
|
|
public bool IClosed { get; set; }
|
|
public override void OnAwake()
|
|
{
|
|
base.OnAwake();
|
|
animator[0].onStateEnter += (x) =>
|
|
{
|
|
if (IsEntered is false) return;
|
|
switch (x)
|
|
{
|
|
case BITConstant.Player.Draw:
|
|
TransitionState<Draw>();
|
|
break;
|
|
case BITConstant.Player.Use:
|
|
TransitionState<Use>();
|
|
break;
|
|
case BITConstant.Player.Exit:
|
|
TransitionState<Exit>();
|
|
break;
|
|
}
|
|
};
|
|
animator[0].onStateExit += x =>
|
|
{
|
|
if (IsEntered is false) return;
|
|
switch (x)
|
|
{
|
|
case BITConstant.Player.Exit:
|
|
IClosed = true;
|
|
if (IsEntered)
|
|
{
|
|
_playerEquipSelector.Cancel();
|
|
}
|
|
break;
|
|
}
|
|
};
|
|
}
|
|
|
|
public override void Entry()
|
|
{
|
|
IClosed = false;
|
|
base.Entry();
|
|
TransitionState<Draw>();
|
|
}
|
|
|
|
public override void Exit()
|
|
{
|
|
base.Exit();
|
|
TransitionState<Exit>();
|
|
}
|
|
|
|
public override async UniTask ExitAsync()
|
|
{
|
|
while (destroyCancellationToken.IsCancellationRequested is false)
|
|
{
|
|
if (IClosed)
|
|
{
|
|
break;
|
|
}
|
|
|
|
if (destroyCancellationToken.IsCancellationRequested)
|
|
{
|
|
break;
|
|
}
|
|
|
|
await UniTask.NextFrame();
|
|
}
|
|
|
|
await base.ExitAsync();
|
|
}
|
|
|
|
public override void AnimationEvent(string eventName)
|
|
{
|
|
base.AnimationEvent(eventName);
|
|
|
|
switch (eventName)
|
|
{
|
|
case BITConstant.Player.Use when CurrentState is Use:
|
|
if (item.TryGetProperty<EquipmentAsSlot>(out var asSlot))
|
|
{
|
|
_equipmentContainer.TryUseEquip(asSlot.slot);
|
|
}
|
|
else
|
|
{
|
|
_inventory.UseItem(Item);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
} |