Skip to content

Commit

Permalink
Implement final support for handheld flashlights
Browse files Browse the repository at this point in the history
  • Loading branch information
Shtrecker committed Sep 29, 2024
1 parent e23931b commit 21ab0b1
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 55 deletions.
108 changes: 57 additions & 51 deletions src/xrGame/CustomDetector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,46 +204,18 @@ void CCustomDetector::OnStateSwitch(u32 S)
{
inherited::OnStateSwitch(S);

// TODO: Drombeys to Rawlik: Rework
if (pSettings->section_exist(m_HudLight.Section))
{
SetMultipleBonesStatus(m_HudLight.Section, "torch_cone_bones", m_HudLight.GetTorchActive());
}

float CurrentAnimTime = -1.0f;
bool TorchActive = false;

switch(S)
{
case eShowing:
{
m_sounds.PlaySound ("sndShow", Fvector().set(0,0,0), this, true, false);
if (m_bFastAnimMode)
{
PlayHUDMotion("anm_show_fast", !!(m_old_state != eHidden), this, S);
CurrentAnimTime = READ_IF_EXISTS(pSettings, r_float, HudSection(), "torch_enable_time_anm_show_fast", CurrentAnimTime);
}
else
{
PlayHUDMotion("anm_show", !!(m_old_state != eHidden), this, S);
CurrentAnimTime = READ_IF_EXISTS(pSettings, r_float, HudSection(), "torch_enable_time_anm_show", CurrentAnimTime);
}
TorchActive = true;
PlayHUDMotion(m_bFastAnimMode ? "anm_show_fast" : "anm_show", !!(m_old_state != eHidden), this, S);
SetPending (TRUE);
}break;
case eHiding:
{
m_sounds.PlaySound ("sndHide", Fvector().set(0,0,0), this, true, false);
if (m_bFastAnimMode)
{
PlayHUDMotion("anm_hide_fast", TRUE, this, S);
CurrentAnimTime = READ_IF_EXISTS(pSettings, r_float, HudSection(), "torch_disable_time_anm_hide_fast", CurrentAnimTime);
}
else
{
PlayHUDMotion("anm_hide", TRUE, this, S);
CurrentAnimTime = READ_IF_EXISTS(pSettings, r_float, HudSection(), "torch_disable_time_anm_hide", CurrentAnimTime);
}
PlayHUDMotion(m_bFastAnimMode ? "anm_hide_fast" : "anm_hide", TRUE, this, S);
SetPending (TRUE);
SetHideDetStateInWeapon();
}break;
Expand Down Expand Up @@ -299,21 +271,6 @@ void CCustomDetector::OnStateSwitch(u32 S)
}break;
}

if (pSettings->section_exist(m_HudLight.Section))
{
if (CurrentAnimTime > 0.0f)
{
Device.callback(u32(CurrentAnimTime * 1000),
[this, TorchActive]()
{
m_HudLight.SwitchTorchlight(TorchActive);

// TODO Hozar to Rawlik: Redo the hiding of dice not on realtime
SetMultipleBonesStatus(m_HudLight.Section, "torch_cone_bones", m_HudLight.GetTorchActive());
});
}
}

m_old_state=S;
}

Expand Down Expand Up @@ -453,8 +410,6 @@ void CCustomDetector::Load(LPCSTR section)

m_sounds.LoadSound( section, "snd_draw", "sndShow");
m_sounds.LoadSound( section, "snd_holster", "sndHide");

m_HudLight.SwitchTorchlight(true);
}


Expand Down Expand Up @@ -546,13 +501,64 @@ extern u32 hud_adj_mode;

void CCustomDetector::UpdateCL()
{
inherited::UpdateCL();

if (m_HudLight.GetTorchActive() && !IsWorking())
if (m_HudLight.GetTorchInstalled())
{
m_HudLight.SwitchTorchlight(false);
if (Actor() != nullptr && Actor()->GetDetector() != nullptr && m_HudLight.GetTorchInstalled())
{
if (!psHUD_Flags.test(HUD_WEAPON_RT2))
{
// Hud рендера оружия отключен - возможно, сценка
m_HudLight.SwitchTorchlight(false);
}
else
{
float light_time_treshold_f = 0.0f;
u32 light_cur_time = 0;

if (GetState() == eShowing)
{
light_time_treshold_f = READ_IF_EXISTS(pSettings, r_float, HudSection(), ("torch_enable_time_" + GetActualCurrentAnim()).c_str(), 0.0f) * 1000.0f;

if (light_time_treshold_f >= 0.0f)
{
light_cur_time = Device.GetTimeDeltaSafe(m_dwMotionStartTm, m_dwMotionCurrTm);
m_HudLight.SwitchTorchlight(light_cur_time >= light_time_treshold_f);
}
else
{
light_time_treshold_f *= -1.0f;
light_cur_time = Device.GetTimeDeltaSafe(m_dwMotionCurrTm, m_dwMotionEndTm);
m_HudLight.SwitchTorchlight(light_cur_time <= light_time_treshold_f);
}
}
else if (GetState() == eHiding)
{
light_time_treshold_f = READ_IF_EXISTS(pSettings, r_float, HudSection(), ("torch_disable_time_" + GetActualCurrentAnim()).c_str(), 0.0f) * 1000.0f;

if (light_time_treshold_f >= 0.0f)
{
light_cur_time = Device.GetTimeDeltaSafe(m_dwMotionStartTm, m_dwMotionCurrTm);
m_HudLight.SwitchTorchlight(!(light_cur_time >= light_time_treshold_f));
}
else
{
light_time_treshold_f *= -1.0f;
light_cur_time = Device.GetTimeDeltaSafe(m_dwMotionCurrTm, m_dwMotionEndTm);
m_HudLight.SwitchTorchlight(!(light_cur_time <= light_time_treshold_f));
}
}
else
m_HudLight.SwitchTorchlight(true);
}
}
else if (m_HudLight.RenderLight != nullptr)
m_HudLight.SwitchTorchlight(false);

SetMultipleBonesStatus(m_HudLight.Section, "torch_cone_bones", m_HudLight.GetTorchActive());
}

inherited::UpdateCL();

if (H_Parent() != Level().CurrentEntity())
return;

Expand Down
29 changes: 25 additions & 4 deletions src/xrGame/HudLightTorch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#include "player_hud.h"
#include "HudItem.h"
#include "physic_item.h"
#include "CustomDetector.h"
#include "Weapon.h"
#include "Inventory.h"

void TransformToHudTemp(Fvector& pos) {
Fmatrix inv_v;
Expand Down Expand Up @@ -74,6 +77,10 @@ void HudLightTorch::NewTorchlight(const char* section) {
LightOffset.y = READ_IF_EXISTS(pSettings, r_float, section, "torch_attach_offset_y", 0.0f);
LightOffset.z = READ_IF_EXISTS(pSettings, r_float, section, "torch_attach_offset_z", 0.0f);

AimOffset.x = READ_IF_EXISTS(pSettings, r_float, section, "torch_aim_attach_offset_x", 0.0f);
AimOffset.y = READ_IF_EXISTS(pSettings, r_float, section, "torch_aim_attach_offset_y", 0.0f);
AimOffset.z = READ_IF_EXISTS(pSettings, r_float, section, "torch_aim_attach_offset_z", 0.0f);

LightWorldOffset.x = READ_IF_EXISTS(pSettings, r_float, section, "torch_world_attach_offset_x", 0.0f);
LightWorldOffset.y = READ_IF_EXISTS(pSettings, r_float, section, "torch_world_attach_offset_y", 0.0f);
LightWorldOffset.z = READ_IF_EXISTS(pSettings, r_float, section, "torch_world_attach_offset_z", 0.0f);
Expand Down Expand Up @@ -106,8 +113,6 @@ void HudLightTorch::NewTorchlight(const char* section) {
{
LightDirBoneName = pSettings->r_string(section, "light_dir_bone");
}

SwitchTorchlight(IsRenderLight);
}

void HudLightTorch::SwitchTorchlight(bool isActive) {
Expand Down Expand Up @@ -152,8 +157,24 @@ void HudLightTorch::UpdateTorchFromObject(CHudItem* item) const {
xform = item->HudItemData()->m_item_transform;
kin = item->HudItemData()->m_model;
lightBoneId = kin->LL_BoneID(LightBone);
kin->LL_GetTransform(lightBoneId).transform_tiny(lightPos, LightOffset);
kin->LL_GetTransform(lightBoneId).transform_tiny(omniPos, OmniOffset);

Fvector3 curr_light_offset = LightOffset;
Fvector3 curr_omni_offset = OmniOffset;

if (smart_cast<CCustomDetector*>(item) != nullptr)
{
CWeapon* wpn = smart_cast<CWeapon*>(Actor()->inventory().ActiveItem());
if (wpn != nullptr && wpn->WpnCanShoot() && wpn->GetAimFactor() > 0.001f)
{
Fvector3 aim_offset = AimOffset;
aim_offset.mul(wpn->GetAimFactor());
curr_light_offset.add(aim_offset);
curr_omni_offset.add(aim_offset);
}
}

kin->LL_GetTransform(lightBoneId).transform_tiny(lightPos, curr_light_offset);
kin->LL_GetTransform(lightBoneId).transform_tiny(omniPos, curr_omni_offset);

if (IsLightDirByBone)
{
Expand Down
1 change: 1 addition & 0 deletions src/xrGame/HudLightTorch.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class HudLightTorch
const char* LightDirBoneName = {};
bool IsLightDirByBone = false;
Fvector3 LightOffset;
Fvector3 AimOffset;
Fvector3 LightWorldOffset;
Fvector3 OmniOffset;
Fvector3 OmniWorldOffset;
Expand Down

0 comments on commit 21ab0b1

Please sign in to comment.