manually update aim anim ; don't play anim while firing, but pause it ; stop firing when weapon is ready to fire, not when anim reaches certain time ;

This commit is contained in:
in0finite 2020-03-24 22:01:29 +01:00
parent 212d1187ff
commit bc05cb0b4c
2 changed files with 64 additions and 5 deletions

View file

@ -18,11 +18,17 @@ namespace SanAndreasUnity.Behaviours.Peds.States
BaseVehicleState.PreparePedForVehicle(m_ped, this.CurrentVehicle, this.CurrentVehicleSeat);
UpdateAnimsInternal();
// we should not update firing from here, because it can cause stack overflow
this.UpdateAnimsInternal(false);
}
protected override void UpdateAnimsInternal()
{
this.UpdateAnimsInternal(true);
}
void UpdateAnimsInternal(bool bUpdateFiring)
{
if (this.CurrentVehicleSeat != null)
{
@ -30,11 +36,14 @@ namespace SanAndreasUnity.Behaviours.Peds.States
m_model.PlayAnim(animId);
m_model.LastAnimState.wrapMode = WrapMode.ClampForever;
if (m_ped.CurrentWeapon != null)
if (bUpdateFiring)
{
m_ped.CurrentWeapon.AimAnimState = m_model.LastAnimState;
if (m_ped.CurrentWeapon != null)
{
m_ped.CurrentWeapon.AimAnimState = m_model.LastAnimState;
BaseAimMovementState.UpdateAimAnim(m_ped, m_model.LastAnimState, this.AimAnimMaxTime, this.AimAnimFireMaxTime, () => BaseAimMovementState.TryFire(m_ped));
this.UpdateAimAnim(() => BaseAimMovementState.TryFire(m_ped));
}
}
m_model.VehicleParentOffset = m_model.GetAnim(animId.AnimName).RootEnd;
@ -80,6 +89,51 @@ namespace SanAndreasUnity.Behaviours.Peds.States
}
void UpdateAimAnim(System.Func<bool> tryFireFunc)
{
var ped = m_ped;
var weapon = ped.CurrentWeapon;
var state = m_model.LastAnimState;
float aimAnimMaxTime = this.AimAnimMaxTime;
if (state.time >= aimAnimMaxTime)
{
// keep the anim at max time
state.time = aimAnimMaxTime;
ped.AnimComponent.Sample();
state.enabled = false;
if (ped.IsFiring)
{
// check if weapon finished firing
if (weapon != null && weapon.TimeSinceFired >= (weapon.AimAnimFireMaxTime - weapon.AimAnimMaxTime))
{
if (Net.NetStatus.IsServer)
{
ped.StopFiring();
}
}
}
else
{
// check if we should start firing
if (ped.IsFireOn && tryFireFunc())
{
// we started firing
}
else
{
// we should remain in aim state
}
}
}
}
public virtual void StartFiring()
{
// switch to firing state

View file

@ -113,6 +113,9 @@ namespace SanAndreasUnity.Behaviours
//public bool IsInsideFireAnim { get { return this.AimAnimState != null && this.AimAnimState.enabled && this.AimAnimState.time > this.AimAnimMaxTime; } }
public Transform GunFlash { get; private set; }
public float LastTimeWhenFired { get; protected set; } = float.NegativeInfinity;
public float TimeSinceFired => Time.time - this.LastTimeWhenFired;
// weapon sounds are located in SFX -> GENRL -> BANK 137
// these indexes represent indexes of sounds in that bank
@ -613,7 +616,9 @@ namespace SanAndreasUnity.Behaviours
public virtual void FireProjectile (Vector3 firePos, Vector3 fireDir)
{
this.LastTimeWhenFired = Time.time;
// raycast against all (non-breakable ?) objects
RaycastHit hit;