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

91 lines
2.5 KiB
C#

using System.Collections;
using System.Collections.Generic;
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.Throws
{
public interface IThrowState:IState{}
public class ThrowController : BITEquipBase<IThrowState>
{
[Header(nameof(throwPoint))]
[SerializeField] private Transform throwPoint;
[SerializeField] private float throwForce;
[SerializeField] private InputActionReference throwAction;
private AssetableThrow _assetableThrow=>(AssetableThrow)item;
[Inject] private IPlayerEquipSelector _equipSelector;
[Inject] private IEntityInventory _inventory;
[Inject] private IEntityEquipmentContainer _equipmentContainer;
private bool isHolding;
public override void OnAwake()
{
inputActionGroup.RegisterCallback(throwAction, OnThrow);
base.OnAwake();
animator[0].onStateExit += OnStateExit;
}
private void OnStateExit(string obj)
{
if (IsEntered is false) return;
switch (obj)
{
case BITConstant.Player.Draw:
if (isHolding is false)
{
animator.Play(BITConstant.Player.Throw);
}
break;
case BITConstant.Player.Throw:
_equipSelector.Cancel();
break;
}
}
public override void Entry()
{
isHolding = false;
base.Entry();
animator.Play(BITConstant.Player.Draw);
}
private void OnThrow(InputAction.CallbackContext obj)
{
switch (obj)
{
case { interaction: HoldInteraction, performed: true }:
isHolding = true;
break;
case { interaction: HoldInteraction, canceled: true }:
if (animator[0].stateName == BITConstant.Player.Idle)
animator.Play(BITConstant.Player.Throw);
break;
}
// if(obj is not {interaction:TapInteraction, performed:true}) return;
// switch (animator[0].stateName)
// {
// case BITConstant.Player.Idle:
// animator.Play(BITConstant.Player.Throw);
// break;
// }
}
public override void AnimationEvent(string eventName)
{
if (IsEntered is false) return;
if (eventName is not BITConstant.Player.Throw) return;
if (!_equipmentContainer.TryUseEquip<EquipmentAsThrow>()) return;
var instance =Instantiate(_assetableThrow.Prefab,
throwPoint.position + throwPoint.forward * 0.016f,
throwPoint.rotation) ;
if (!instance.TryGetComponent<Rigidbody>(out var _rigidbody)) return;
_rigidbody.AddForce(throwPoint.forward * throwForce, ForceMode.VelocityChange);
}
}
}