mirror of
https://github.com/Ed94/Cog.git
synced 2026-07-16 14:31:29 -07:00
moved metric to CogDebug
This commit is contained in:
@@ -0,0 +1,163 @@
|
||||
#include "CogDebugMetric.h"
|
||||
|
||||
#include "CogDebugSettings.h"
|
||||
#include "CogInterfaceFilteredActor.h"
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
float FCogDebugMetric::MaxDurationSetting = 0.0f;
|
||||
float FCogDebugMetric::RestartDelaySetting = 5.0f;
|
||||
bool FCogDebugMetric::IsVisible = false;
|
||||
TMap<FName, FCogDebugMetricEntry> FCogDebugMetric::Metrics;
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// FCogDebugMetric
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
void FCogDebugMetric::AddMetric(const FCogDebugMetricParams& Params)
|
||||
{
|
||||
if (Cast<ICogInterfacesFilteredActor>(Params.WorldContextObject))
|
||||
{
|
||||
if (Params.WorldContextObject != FCogDebugSettings::GetSelection())
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
FCogDebugMetricEntry& Entry = Metrics.FindOrAdd(Params.Name);
|
||||
Entry.Add(Params);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
void FCogDebugMetric::AddMetric(const UObject* WorldContextObject, FName Name, float MitigatedValue, float UnmitigatedValue, bool IsCritical)
|
||||
{
|
||||
FCogDebugMetricParams Params;
|
||||
Params.WorldContextObject = WorldContextObject;
|
||||
Params.Name = Name;
|
||||
Params.MitigatedValue = MitigatedValue;
|
||||
Params.UnmitigatedValue = UnmitigatedValue;
|
||||
Params.IsCritical = IsCritical;
|
||||
AddMetric(Params);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
void FCogDebugMetric::Tick(float DeltaSeconds)
|
||||
{
|
||||
for (auto& Entry : Metrics)
|
||||
{
|
||||
FCogDebugMetricEntry& Metric = Entry.Value;
|
||||
Metric.Tick(DeltaSeconds);
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
void FCogDebugMetric::Reset()
|
||||
{
|
||||
for (auto& Entry : Metrics)
|
||||
{
|
||||
FCogDebugMetricEntry& Metric = Entry.Value;
|
||||
Metric.Reset();
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// FCogMetricInstance
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
void FCogDebugMetricValue::Reset()
|
||||
{
|
||||
Last = 0.0f;
|
||||
Min = 0.0f;
|
||||
Max = 0.0f;
|
||||
PerSecond = 0.0f;
|
||||
PerFrame = 0.0f;
|
||||
Total = 0.0f;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
void FCogDebugMetricValue::AddMetric(const float Metric)
|
||||
{
|
||||
Last = Metric;
|
||||
Min = Min == 0.0f ? Metric : FMath::Min(Min, Metric);
|
||||
Max = FMath::Max(Max, Metric);
|
||||
PerFrame += Metric;
|
||||
Total += Metric;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
void FCogDebugMetricValue::UpdateMetricPerSecond(const float Duration)
|
||||
{
|
||||
PerSecond = Duration > 1.0f ? Total / Duration : Total;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
//
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
void FCogDebugMetricEntry::Reset()
|
||||
{
|
||||
Count = 0;
|
||||
Crits = 0;
|
||||
TotalCritChances = 0.0f;
|
||||
|
||||
IsInProgress = false;
|
||||
Timer = 0.0f;
|
||||
RestartTimer = 0.0f;
|
||||
|
||||
Mitigated.Reset();
|
||||
Unmitigated.Reset();
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
void FCogDebugMetricEntry::Add(const FCogDebugMetricParams& Params)
|
||||
{
|
||||
// If the max duration is reached, stop adding
|
||||
if (FCogDebugMetric::MaxDurationSetting != 0 && Timer >= FCogDebugMetric::MaxDurationSetting)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
IsInProgress = true;
|
||||
Count++;
|
||||
Crits += Params.IsCritical ? 1 : 0;
|
||||
|
||||
Mitigated.AddMetric(Params.MitigatedValue);
|
||||
Unmitigated.AddMetric(Params.UnmitigatedValue);
|
||||
Mitigated.UpdateMetricPerSecond(Timer);
|
||||
Unmitigated.UpdateMetricPerSecond(Timer);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
void FCogDebugMetricEntry::Tick(const float DeltaSeconds)
|
||||
{
|
||||
if (IsInProgress)
|
||||
{
|
||||
// If the max duration is reached, stop increasing time.
|
||||
if (FCogDebugMetric::MaxDurationSetting <= 0 || Timer < FCogDebugMetric::MaxDurationSetting)
|
||||
{
|
||||
Timer += DeltaSeconds;
|
||||
}
|
||||
else
|
||||
{
|
||||
IsInProgress = false;
|
||||
Timer = FCogDebugMetric::MaxDurationSetting;
|
||||
Mitigated.UpdateMetricPerSecond(Timer);
|
||||
Unmitigated.UpdateMetricPerSecond(Timer);
|
||||
}
|
||||
}
|
||||
|
||||
if (FCogDebugMetric::RestartDelaySetting > 0.0f)
|
||||
{
|
||||
if (Unmitigated.PerFrame == 0.0f)
|
||||
{
|
||||
RestartTimer += DeltaSeconds;
|
||||
if (RestartTimer > FCogDebugMetric::RestartDelaySetting)
|
||||
{
|
||||
Reset();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
RestartTimer = 0.0f;
|
||||
}
|
||||
}
|
||||
|
||||
Mitigated.PerFrame = 0.0f;
|
||||
Unmitigated.PerFrame = 0.0f;
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
#pragma once
|
||||
|
||||
#include "CoreMinimal.h"
|
||||
#include "CogDebugDefines.h"
|
||||
|
||||
#ifdef ENABLE_COG
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
struct COGDEBUG_API FCogDebugMetricParams
|
||||
{
|
||||
TObjectPtr<const UObject> WorldContextObject = nullptr;
|
||||
|
||||
FName Name;
|
||||
|
||||
float MitigatedValue = 0;
|
||||
|
||||
float UnmitigatedValue = 0;
|
||||
|
||||
bool IsCritical = false;
|
||||
};
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
struct COGDEBUG_API FCogDebugMetricValue
|
||||
{
|
||||
void Reset();
|
||||
|
||||
void AddMetric(const float Damage);
|
||||
|
||||
void UpdateMetricPerSecond(const float Duration);
|
||||
|
||||
float Last = 0.0f;
|
||||
|
||||
float Min = 0.0f;
|
||||
|
||||
float Max = 0.0f;
|
||||
|
||||
float PerFrame = 0.0f;
|
||||
|
||||
float PerSecond = 0.0f;
|
||||
|
||||
float Total = 0.0f;
|
||||
};
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
struct COGDEBUG_API FCogDebugMetricEntry
|
||||
{
|
||||
public:
|
||||
|
||||
void Add(const FCogDebugMetricParams& Params);
|
||||
|
||||
void Tick(const float DeltaSeconds);
|
||||
|
||||
void Reset();
|
||||
|
||||
int Count = 0;
|
||||
|
||||
int Crits = 0;
|
||||
|
||||
bool IsInProgress = false;
|
||||
|
||||
float TotalCritChances = 0.0f;
|
||||
|
||||
float Timer = 0.0f;
|
||||
|
||||
float RestartTimer = 0.0f;
|
||||
|
||||
FCogDebugMetricValue Mitigated;
|
||||
|
||||
FCogDebugMetricValue Unmitigated;
|
||||
};
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
class COGDEBUG_API FCogDebugMetric
|
||||
{
|
||||
public:
|
||||
|
||||
static void Tick(float DeltaSeconds);
|
||||
|
||||
static void AddMetric(const FCogDebugMetricParams& Params);
|
||||
|
||||
static void AddMetric(const UObject* WorldContextObject, FName Name, float MitigatedValue, float UnmitigatedValue, bool IsCritical);
|
||||
|
||||
static void Reset();
|
||||
|
||||
static bool IsVisible;
|
||||
|
||||
static float MaxDurationSetting;
|
||||
|
||||
static float RestartDelaySetting;
|
||||
|
||||
static TMap<FName, FCogDebugMetricEntry> Metrics;
|
||||
};
|
||||
|
||||
#endif //ENABLE_COG
|
||||
|
||||
Reference in New Issue
Block a user