BITFALL/Assets/Artists/Scripts/Equip/UniversalUseController.cs

138 lines
3.8 KiB
C#
Raw Normal View History

2023-10-20 19:31:12 +08:00
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using BITFALL.Entities.Equipment.Melee;
using BITFALL.Entities.Equipment.Universal.States;
2023-10-20 22:46:14 +08:00
using BITFALL.Entities.Inventory;
2023-10-20 19:31:12 +08:00
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
{
2023-10-20 22:46:14 +08:00
public interface IUseState : IState
{
}
2023-10-20 19:31:12 +08:00
public abstract class UseState : IUseState
{
[SerializeField] protected UniversalUseController useController;
public virtual bool Enabled { get; set; }
2023-10-20 22:46:14 +08:00
2023-10-20 19:31:12 +08:00
public virtual void Initialize()
{
2023-10-20 22:46:14 +08:00
2023-10-20 19:31:12 +08:00
}
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>
{
2023-10-20 22:46:14 +08:00
[Inject] private IPlayerInventory _playerInventory;
[Inject] private IEntityInventory _inventory;
[Inject] private IPlayerEquipSelector _playerEquipSelector;
2023-10-24 23:37:59 +08:00
[Inject] private IEntityEquipmentContainer _equipmentContainer;
2023-10-20 22:46:14 +08:00
public bool IClosed { get; set; }
public override void OnAwake()
2023-10-20 19:31:12 +08:00
{
2023-10-20 22:46:14 +08:00
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;
}
};
2023-10-20 19:31:12 +08:00
}
2023-10-20 22:46:14 +08:00
2023-10-20 19:31:12 +08:00
public override void Entry()
{
2023-10-20 22:46:14 +08:00
IClosed = false;
2023-10-20 19:31:12 +08:00
base.Entry();
TransitionState<Draw>();
}
public override void Exit()
{
base.Exit();
TransitionState<Exit>();
}
public override async UniTask ExitAsync()
{
while (destroyCancellationToken.IsCancellationRequested is false)
{
2023-10-20 22:46:14 +08:00
if (IClosed)
2023-10-20 19:31:12 +08:00
{
break;
}
2023-10-20 22:46:14 +08:00
2023-10-20 19:31:12 +08:00
if (destroyCancellationToken.IsCancellationRequested)
{
break;
}
2023-10-20 22:46:14 +08:00
2023-10-20 19:31:12 +08:00
await UniTask.NextFrame();
}
2023-10-20 22:46:14 +08:00
await base.ExitAsync();
}
public override void AnimationEvent(string eventName)
{
base.AnimationEvent(eventName);
2023-10-24 23:37:59 +08:00
2023-10-20 22:46:14 +08:00
switch (eventName)
{
case BITConstant.Player.Use when CurrentState is Use:
2023-10-24 23:37:59 +08:00
if (item.TryGetProperty<EquipmentAsSlot>(out var asSlot))
{
_equipmentContainer.TryUseEquip(asSlot.slot);
}
else
2023-10-20 22:46:14 +08:00
{
2023-10-24 23:37:59 +08:00
_inventory.UseItem(Item);
2023-10-20 22:46:14 +08:00
}
break;
}
2023-10-20 19:31:12 +08:00
}
}
}