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

97 lines
3.0 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using Animancer;
using BITFALL.Entities.Equipment;
using BITFALL.Entities.Inventory;
using BITFALL.Items;
using BITKit;
using BITKit.Entities;
using BITKit.StateMachine;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Interactions;
namespace BITFALL.Player.Equip
{
[Serializable]
public struct AllowQuickKnife:IProperty{}
public interface IThrowState:IState{}
public class ThrowController : BITEquipBase<IThrowState>
{
public static void Throw(Transform throwPoint,int throwForce, IBasicItem item)
{
if (item.GetAssetable() is not AssetableThrow assetableThrow) return;
var instance =Instantiate(assetableThrow.Prefab,
throwPoint.position + throwPoint.forward * 0.016f,
throwPoint.rotation) ;
if (!instance.TryGetComponent<Rigidbody>(out var _rigidbody)) return;
var forward = throwPoint.forward;
_rigidbody.AddForce(forward * throwForce, ForceMode.VelocityChange);
_rigidbody.AddTorque(forward * throwForce, ForceMode.VelocityChange);
}
[Header(nameof(throwPoint))]
[SerializeField] private Transform throwPoint;
[SerializeField] private int throwForce;
[SerializeField] private InputActionReference throwAction;
[Inject] private IPlayerEquipSelector _equipSelector;
[Inject] private IEntityEquipmentContainer _equipmentContainer;
[Header(Constant.Header.Animations)]
[SerializeField] private AnimationClip drawClip;
[SerializeField] private AnimationClip throwClip;
private AnimancerState currentState;
private bool isThrowing;
public override void Entry()
{
base.Entry();
isThrowing = false;
inputActionGroup.RegisterCallback(throwAction, OnThrow);
currentState = AnimancerComponent.Play(drawClip);
currentState.Events.OnEnd = () =>
{
currentState.Events.OnEnd = null;
if (inputActionGroup.GetAction(throwAction).IsPressed())
{
currentState.Events.OnEnd = null;
}
else
{
isThrowing = true;
currentState = AnimancerComponent.Play(throwClip);
currentState.Events.OnEnd =()=>
{
currentState.Events.OnEnd = null;
_equipSelector.Cancel();
};
}
};
}
private void OnThrow(InputAction.CallbackContext obj)
{
if (obj is not { interaction: HoldInteraction, canceled: true } || isThrowing) return;
isThrowing = true;
currentState = AnimancerComponent.Play(throwClip);
currentState.Events.OnEnd =()=>
{
currentState.Events.OnEnd = null;
_equipSelector.Cancel();
};
}
public override void Exit()
{
base.Exit();
inputActionGroup.UnRegisterCallback(throwAction, OnThrow);
AnimancerComponent.Stop();
}
public override void AnimationEvent(string eventName)
{
if (eventName is not BITConstant.Player.Throw) return;
if(item.GetAssetable().TryGetProperty<EquipmentAsSlot>(out var slot) is false)return;
if(_equipmentContainer.TryUseEquip(slot.slot) is false) return;
Throw(throwPoint, throwForce, item);
}
}
}