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

97 lines
3.0 KiB
C#
Raw Normal View History

2024-03-31 23:34:22 +08:00
using System;
2023-10-24 23:37:59 +08:00
using System.Collections;
using System.Collections.Generic;
2024-03-29 00:58:24 +08:00
using Animancer;
2023-10-24 23:37:59 +08:00
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;
2024-03-29 00:58:24 +08:00
namespace BITFALL.Player.Equip
2023-10-24 23:37:59 +08:00
{
2024-03-31 23:34:22 +08:00
[Serializable]
public struct AllowQuickKnife:IProperty{}
2023-10-24 23:37:59 +08:00
public interface IThrowState:IState{}
public class ThrowController : BITEquipBase<IThrowState>
{
2024-03-29 00:58:24 +08:00
public static void Throw(Transform throwPoint,int throwForce, IBasicItem item)
{
2024-04-06 16:33:57 +08:00
if (item.GetAssetable() is not ScriptableThrow assetableThrow) return;
2024-03-29 00:58:24 +08:00
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);
}
2023-10-24 23:37:59 +08:00
[Header(nameof(throwPoint))]
[SerializeField] private Transform throwPoint;
2024-03-29 00:58:24 +08:00
[SerializeField] private int throwForce;
2023-10-24 23:37:59 +08:00
[SerializeField] private InputActionReference throwAction;
[Inject] private IPlayerEquipSelector _equipSelector;
[Inject] private IEntityEquipmentContainer _equipmentContainer;
2024-03-29 00:58:24 +08:00
[Header(Constant.Header.Animations)]
[SerializeField] private AnimationClip drawClip;
[SerializeField] private AnimationClip throwClip;
private AnimancerState currentState;
private bool isThrowing;
2023-10-24 23:37:59 +08:00
public override void Entry()
{
base.Entry();
2024-03-29 00:58:24 +08:00
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();
};
}
};
2023-10-24 23:37:59 +08:00
}
private void OnThrow(InputAction.CallbackContext obj)
{
2024-03-29 00:58:24 +08:00
if (obj is not { interaction: HoldInteraction, canceled: true } || isThrowing) return;
isThrowing = true;
currentState = AnimancerComponent.Play(throwClip);
currentState.Events.OnEnd =()=>
2023-10-24 23:37:59 +08:00
{
2024-03-29 00:58:24 +08:00
currentState.Events.OnEnd = null;
_equipSelector.Cancel();
};
2023-10-24 23:37:59 +08:00
}
2024-03-29 00:58:24 +08:00
public override void Exit()
{
base.Exit();
inputActionGroup.UnRegisterCallback(throwAction, OnThrow);
AnimancerComponent.Stop();
}
2023-10-24 23:37:59 +08:00
public override void AnimationEvent(string eventName)
{
if (eventName is not BITConstant.Player.Throw) return;
2024-03-29 00:58:24 +08:00
if(item.GetAssetable().TryGetProperty<EquipmentAsSlot>(out var slot) is false)return;
if(_equipmentContainer.TryUseEquip(slot.slot) is false) return;
Throw(throwPoint, throwForce, item);
2023-10-24 23:37:59 +08:00
}
}
}