This commit is contained in:
CortexCore
2023-11-15 23:54:54 +08:00
parent ee3ecec6cb
commit 3c837a4a33
356 changed files with 73756 additions and 26493 deletions

View File

@@ -11,10 +11,8 @@ using UnityEngine.InputSystem;
using BITKit.StateMachine;
using Cysharp.Threading.Tasks;
using Unity.Mathematics;
using UnityEditorInternal;
using UnityEngine.InputSystem.Interactions;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace BITFALL.Guns
@@ -62,6 +60,7 @@ namespace BITFALL.Guns
[Header(Constant.Header.Settings)]
[SerializeField] private Vector3 bulletInitialOffset;
[SerializeField] private SpringEulerAngle recoilSpring=new();
[SerializeField] private float recoilPositionWeight=1;
[Header(Constant.Header.Gameobjects)]
[SerializeField] private Transform initialSight;
@@ -83,6 +82,7 @@ namespace BITFALL.Guns
// 引用组件
[Header(Constant.Header.Components)]
[SerializeField] private LocationAdditive locationAdditive;
[SerializeField] private LocationAdditive selfViewAdditive;
// 引用预制体
[Header(Constant.Header.Prefabs)]
@@ -111,7 +111,7 @@ namespace BITFALL.Guns
#region
public override string AddressablePath => _gun.AddressablePath;
#endregion
public override void OnAwake()
{
base.OnAwake();
@@ -119,14 +119,26 @@ namespace BITFALL.Guns
inputActionGroup.RegisterCallback(aimAction, OnAim);
inputActionGroup.RegisterCallback(reloadAction, OnReload);
inputActionGroup.RegisterCallback(meleeAction, OnMelee);
inputActionGroup.RegisterCallback(steadyAimAction, OnSteadyAim);
if (breathingAdditive.Allow)
inputActionGroup.RegisterCallback(steadyAimAction, OnSteadyAim);
_movement.OnStateChanged += OnMovementStateChanged;
_movement.OnCommand += OnMovementCommand;
animator[0].onStateExit += (state) => { isHolstered = state is BITConstant.Player.Holster; };
animator[0].onStateExit += (state) =>
BITAppForUnity.AllowCursor.AddListener(OnAllowCursor);
destroyCancellationToken.Register(() =>
{
isHolstered = state is BITConstant.Player.Holster;
};
BITAppForUnity.AllowCursor.RemoveListener(OnAllowCursor);
});
}
private void OnAllowCursor(bool allow)
{
if (!allow) return;
expectFiring.Reset();
expectAiming.Reset();
}
private void OnSteadyAim(InputAction.CallbackContext obj)
@@ -262,15 +274,25 @@ namespace BITFALL.Guns
break;
}
animator.animator.SetBool(IsGrounded,_movement.IsGrounded);
animator.animator.SetFloat(BITConstant.Player.SqrMagnitude,_movement.LocomotionBasedVelocity.sqrMagnitude);
recoilSpring.Update(deltaTime,default);
locationAdditive.AddEuler(recoilSpring.value);
if (selfViewAdditive)
{
selfViewAdditive.AddEuler(recoilSpring.value);
selfViewAdditive.AddPosition(
new Vector3(
recoilSpring.value.y,
-recoilSpring.value.x,
recoilSpring.value.z
) *
recoilPositionWeight
);
}
if(AnimationProperties.TryGetValue(BITConstant.Player.Aim, out var _aim))
{
_equipService.Zoom.Allow = CurrentState is Aim;
@@ -331,6 +353,8 @@ namespace BITFALL.Guns
}
AllowRendering.SetDisableElements(64564,_equipService.AllowScope);
expectAiming.shouldBe = inputActionGroup.GetAction(aimAction).IsPressed();
}
public override void AnimationEvent(string eventName)
@@ -353,28 +377,25 @@ namespace BITFALL.Guns
public void Fire()
{
if (RequireBolt) return;
//如果启用了指针则不开火
if(BITAppForUnity.AllowCursor)
{
return;
}
//播放射击动画
UnityEntity.Invoke(Constant.Animation.Play, BITConstant.Player.Fire);
//调用BulletManager生成子弹
var _transform = transform;
var rotation = _transform.rotation;
BulletService.Spawn(new SpawnBullet
if (_gun.BuckShot.Allow)
{
initiator = Entity.Id,
pos = (_transform.position+rotation * bulletInitialOffset),
rot = rotation,
forward = _transform.forward,
initialDamage = _gun.InitialDamage,
startSpeed = _gun.InitialBulletSpeed,
InitialForce = _gun.InitialBulletForce,
});
InternalAddRecoil();
for (int i = 0; i < _gun.BuckShot.Value; i++)
{
InternalFire();
}
}
else
{
InternalFire();
InternalAddRecoil();
}
//开火模式逻辑判断
switch (_gun.FireMode)
@@ -395,6 +416,38 @@ namespace BITFALL.Guns
}
break;
}
if (_gun.FireMode is SemiFireMode {RequireBoltAction:true})
{
RequireBolt = true;
}
}
private void InternalFire()
{
//调用BulletManager生成子弹
var _transform = transform;
var rotation = _transform.rotation;
var random = UnityEngine.Random.insideUnitCircle;
if (_gun.TryGetProperty<ISpread>(out var spread))
{
random *= spread.Spread;
}
random *= recoilSpring.value.GetLength();
BulletService.Spawn(new SpawnBullet
{
Initiator = Entity.Id,
Position = (_transform.position+rotation * bulletInitialOffset),
Rotation = rotation,
Forward = rotation * (Vector3.forward + Vector3.up * random.y + Vector3.right * random.x),
InitialDamage = _gun.InitialDamage,
StartSpeed = _gun.InitialBulletSpeed,
InitialForce = _gun.InitialBulletForce,
});
}
private void InternalAddRecoil()
{
if (_gun.TryGetProperty<IRecoil>(out var _recoil))
{
var _newRecoil = new Vector3
@@ -406,14 +459,15 @@ namespace BITFALL.Guns
recoilSpring.value = _newRecoil;
_playerMovement.AddViewEuler(new float2(_newRecoil.x,_newRecoil.y));
}
if (_gun.FireMode is SemiFireMode {RequireBoltAction:true})
{
RequireBolt = true;
}
}
private void OnFire(InputAction.CallbackContext context)
{
//如果启用了指针则不开火
if(BITAppForUnity.AllowCursor)
{
expectFiring.Reset();
return;
}
switch (_gun.FireMode)
{
case AutoFireMode :
@@ -439,11 +493,17 @@ namespace BITFALL.Guns
}
private void OnAim(InputAction.CallbackContext context)
{
expectAiming.shouldBe = context.ReadValueAsButton();
//如果启用了指针则不开火
if(BITAppForUnity.AllowCursor)
{
expectAiming.Reset();
return;
}
//expectAiming.shouldBe = context.ReadValueAsButton();
}
}
#if UNITY_EDITOR
[CustomEditor(typeof(BITGun))]
public class BITGunInspector:BITInspector<BITGun>{}
#endif
// #if UNITY_EDITOR
// [CustomEditor(typeof(BITGun))]
// public class BITGunInspector:BITInspector<BITGun>{}
// #endif
}