|
class RecoilBase |
|
{ |
|
bool m_DebugMode; |
|
|
|
Weapon_Base m_Weapon; |
|
PlayerBase m_Player; |
|
protected bool m_DeleteRequested; |
|
protected float m_Time; |
|
protected float m_ReloadTime; |
|
protected vector m_RecoilModifier; |
|
protected bool m_IsClient; |
|
|
|
float m_MouseOffsetRangeMin; |
|
float m_MouseOffsetRangeMax; |
|
float m_MouseOffsetRelativeTime = 1; |
|
float m_HandsOffsetRelativeTime = 1; |
|
float m_CamOffsetRelativeTime = 1; |
|
float m_CamOffsetDistance = 0.05; |
|
float m_MouseOffsetDistance; |
|
float m_TimeNormalized; |
|
|
|
protected vector m_MouseOffsetTarget; |
|
protected vector m_MouseOffsetTargetAccum; |
|
protected float m_Angle; |
|
|
|
|
|
protected ref array<vector> m_HandsCurvePoints; |
|
|
|
void RecoilBase(Weapon_Base weapon) |
|
{ |
|
m_Weapon = weapon; |
|
|
|
m_DebugMode = GetDayZGame().IsAimLogEnabled(); |
|
m_Player = PlayerBase.Cast(weapon.GetHierarchyRootPlayer()); |
|
m_HandsCurvePoints = new array<vector>; |
|
Init(); |
|
PostInit(weapon); |
|
} |
|
|
|
void Init(); |
|
|
|
Weapon_Base GetWeapon() |
|
{ |
|
return m_Weapon; |
|
} |
|
|
|
void PostInit(Weapon_Base weapon) |
|
{ |
|
int muzzleIndex = weapon.GetCurrentMuzzle(); |
|
m_Angle = m_Player.GetRandomGeneratorSyncManager().GetRandomInRange(RandomGeneratorSyncUsage.RGSRecoil, m_MouseOffsetRangeMin, m_MouseOffsetRangeMax); |
|
m_RecoilModifier = GetRecoilModifier( GetWeapon() ); |
|
if(m_DebugMode) Print(m_Angle); |
|
|
|
m_ReloadTime = weapon.GetReloadTime(muzzleIndex); |
|
m_MouseOffsetTarget = vector.YawToVector(m_Angle); |
|
m_MouseOffsetTarget = m_MouseOffsetTarget * m_MouseOffsetDistance; |
|
m_IsClient = !GetGame().IsDedicatedServer(); |
|
m_CamOffsetDistance *= m_RecoilModifier[2]; |
|
} |
|
|
|
|
|
void Destroy() |
|
{ |
|
m_DeleteRequested = true; |
|
} |
|
|
|
|
|
void Update( SDayZPlayerAimingModel pModel, out float axis_mouse_x, out float axis_mouse_y, out float axis_hands_x, out float axis_hands_y, float pDt ) |
|
{ |
|
if( m_DeleteRequested ) |
|
{ |
|
delete this; |
|
} |
|
|
|
m_TimeNormalized = Math.InverseLerp(0, m_ReloadTime, m_Time); |
|
m_TimeNormalized = Math.Clamp(m_TimeNormalized, 0,0.99); |
|
|
|
ApplyMouseOffset(pDt, axis_mouse_x, axis_mouse_y); |
|
ApplyHandsOffset(pDt, axis_hands_x, axis_hands_y); |
|
if(m_IsClient) |
|
ApplyCamOffset(pModel); |
|
#ifdef DEVELOPER |
|
if(m_DebugMode) |
|
PrintString("RecoilBase | BEFORE | axis_mouse_y: " + axis_mouse_y.ToString()); |
|
#endif |
|
axis_mouse_x = axis_mouse_x * m_RecoilModifier[0]; |
|
axis_mouse_y = axis_mouse_y * m_RecoilModifier[1]; |
|
|
|
axis_hands_x = axis_hands_x * m_RecoilModifier[0]; |
|
axis_hands_y = axis_hands_y * m_RecoilModifier[1]; |
|
|
|
#ifdef DEVELOPER |
|
if(m_DebugMode) |
|
{ |
|
PrintString("RecoilBase | AFTER | axis_mouse_y: " + axis_mouse_y.ToString()); |
|
} |
|
#endif |
|
m_Time += pDt; |
|
|
|
if( m_Time >= m_ReloadTime ) |
|
{ |
|
Destroy(); |
|
} |
|
} |
|
|
|
void ApplyCamOffset(SDayZPlayerAimingModel pModel) |
|
{ |
|
float time_rel = Math.Clamp(Math.InverseLerp(0, m_CamOffsetRelativeTime, m_TimeNormalized), 0, 1); |
|
float offset = 0; |
|
float time = Easing.EaseOutBack(time_rel); |
|
if(time == 1) |
|
{ |
|
offset = 0; |
|
} |
|
else |
|
{ |
|
offset = Math.Lerp(0,m_CamOffsetDistance,time); |
|
} |
|
|
|
pModel.m_fCamPosOffsetZ = offset; |
|
|
|
m_Player.GetCurrentCamera().SendRecoilOffsetZ(offset); |
|
} |
|
|
|
void ApplyHandsOffset(float pDt, out float pRecResultX, out float pRecResultY) |
|
{ |
|
float relative_time = m_TimeNormalized / Math.Clamp(m_HandsOffsetRelativeTime, 0.001,1); |
|
vector pos_on_curve = GetPositionOnCurve(m_HandsCurvePoints, relative_time); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pRecResultX = pos_on_curve[0]; |
|
pRecResultY = pos_on_curve[1]; |
|
} |
|
|
|
void ApplyMouseOffset(float pDt, out float pRecResultX, out float pRecResultY) |
|
{ |
|
#ifdef DEVELOPER |
|
if(m_DebugMode) |
|
{ |
|
bool b1 = m_MouseOffsetTargetAccum[1] < m_MouseOffsetTarget[1]; |
|
PrintString( "RecoilBase | ApplyMouseOffset processing: " + b1 ); |
|
PrintString( "RecoilBase | m_MouseOffsetTargetAccum : " + m_MouseOffsetTargetAccum.ToString() ); |
|
PrintString( "RecoilBase | m_MouseOffsetTarget : " + m_MouseOffsetTarget.ToString() ); |
|
} |
|
#endif |
|
|
|
if( m_MouseOffsetTargetAccum[1] < m_MouseOffsetTarget[1] ) |
|
{ |
|
float relative_delta = pDt / m_ReloadTime / Math.Clamp(m_MouseOffsetRelativeTime, 0.001,1); |
|
|
|
float delta_mouse_offset_x = m_MouseOffsetTarget[0] * relative_delta; |
|
float delta_mouse_offset_y = m_MouseOffsetTarget[1] * relative_delta; |
|
|
|
if( ( m_MouseOffsetTargetAccum[1] + delta_mouse_offset_y) > m_MouseOffsetTarget[1] ) |
|
{ |
|
delta_mouse_offset_x = m_MouseOffsetTarget[0] - m_MouseOffsetTargetAccum[0]; |
|
delta_mouse_offset_y = m_MouseOffsetTarget[1] - m_MouseOffsetTargetAccum[1]; |
|
} |
|
|
|
m_MouseOffsetTargetAccum[0] = m_MouseOffsetTargetAccum[0] + delta_mouse_offset_x; |
|
m_MouseOffsetTargetAccum[1] = m_MouseOffsetTargetAccum[1] + delta_mouse_offset_y; |
|
|
|
pRecResultX = delta_mouse_offset_x; |
|
pRecResultY = delta_mouse_offset_y; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
#ifdef DEVELOPER |
|
if(m_DebugMode) |
|
{ |
|
PrintString( "RecoilBase | pRecResultY: " + pRecResultY ); |
|
} |
|
#endif |
|
} |
|
|
|
vector GetRecoilModifier(Weapon_Base weapon) |
|
{ |
|
if( weapon ) |
|
{ |
|
return weapon.GetPropertyModifierObject().m_RecoilModifiers; |
|
} |
|
else return "1 1 1"; |
|
} |
|
|
|
vector GetPositionOnCurve(array<vector> points, float time) |
|
{ |
|
return Math3D.Curve(ECurveType.CatmullRom, time, points); |
|
} |
|
} |