[ADD] Display post process material on screen with radius depending on damages amount taken over time

This commit is contained in:
Emilie Schott 2025-03-09 22:23:36 +01:00
parent 89f514a0e4
commit 8bb7542dfd
7 changed files with 126 additions and 17 deletions

Binary file not shown.

BIN
Content/Legumix/Levels/LVL_GYM_00.umap (Stored with Git LFS)

Binary file not shown.

BIN
Content/Legumix/Player/BP_Play.uasset (Stored with Git LFS)

Binary file not shown.

Binary file not shown.

View File

@ -28,9 +28,16 @@ ALMPlayer::ALMPlayer()
SpreadStream = FRandomStream(FMath::Rand()); SpreadStream = FRandomStream(FMath::Rand());
HealthComponent = CreateDefaultSubobject<ULMHealthComponent>(TEXT("HealthComponent")); HealthComponent = CreateDefaultSubobject<ULMHealthComponent>(TEXT("HealthComponent"));
HitPeriodLengthInSeconds = 0.f; HitPeriodLengthInSeconds = 15.f;
TimeElapsedSinceLastDamageInSeconds = 0.f; bHitPeriodInProgress = false;
TimeOfTheLastDamage = 0.f;
HealthAtTheCurrentHitPeriodBeginning = 100.f;
CumulatedDamagesOnCurrentHitPeriod = 0.f; CumulatedDamagesOnCurrentHitPeriod = 0.f;
PlayerViewOcclusionPercent = 0.f;
NextPlayerViewOcclusionPercent = 0.f;
PlayerViewOcclusionPercent = 0.f;
AlphaLerpForPlayerViewOcclusionPercent = 0.f;
} }
void ALMPlayer::BeginPlay() void ALMPlayer::BeginPlay()
@ -41,9 +48,41 @@ void ALMPlayer::BeginPlay()
GetCamera()->SetFieldOfView(ULMUserSettings::GetLegumixUserSettings()->FieldOfView); GetCamera()->SetFieldOfView(ULMUserSettings::GetLegumixUserSettings()->FieldOfView);
HealthComponent->OnHealthChanged.AddDynamic(this, &ALMPlayer::SetDisplayDamageParameters);
} }
void ALMPlayer::Tick(float DeltaTime)
{
if(bHitPeriodInProgress == true)
{
double TimeElapsedSinceLastDamage = GetWorld()->GetTimeSeconds() - TimeOfTheLastDamage;
if(TimeElapsedSinceLastDamage > HitPeriodLengthInSeconds)
{
if(bHitPeriodInProgress == true)
{
bHitPeriodInProgress = false;
PlayerViewOcclusionPercent = NextPlayerViewOcclusionPercent;
NextPlayerViewOcclusionPercent = 0.f;
AlphaLerpForPlayerViewOcclusionPercent = 0.f;
TimeOfTheLastDamage = 0.f;
CumulatedDamagesOnCurrentHitPeriod = 0.f;
}
}
}
if(PlayerViewOcclusionPercent != NextPlayerViewOcclusionPercent)
{
AlphaLerpForPlayerViewOcclusionPercent = FMath::Clamp(AlphaLerpForPlayerViewOcclusionPercent + DeltaTime, 0, 1);
PlayerViewOcclusionPercent = FMath::Lerp(PlayerViewOcclusionPercent, NextPlayerViewOcclusionPercent, AlphaLerpForPlayerViewOcclusionPercent);
DamageDynamicMaterialInstance->SetScalarParameterValue("PlayerViewOcclusionPercent", PlayerViewOcclusionPercent);
}
else if(PlayerViewOcclusionPercent == NextPlayerViewOcclusionPercent)
{
AlphaLerpForPlayerViewOcclusionPercent = 0;
}
}
bool ALMPlayer::PickUpAmmo(ALMAmmo* Ammo) bool ALMPlayer::PickUpAmmo(ALMAmmo* Ammo)
{ {
if (GEngine) if (GEngine)
@ -163,3 +202,25 @@ USkeletalMeshComponent* ALMPlayer::GetArms()
return ArmsMesh; return ArmsMesh;
} }
void ALMPlayer::SetDisplayDamageParameters(float Health, float Damage)
{
if (bHitPeriodInProgress == false)
{
HealthAtTheCurrentHitPeriodBeginning = Health;
bHitPeriodInProgress = true;
}
TimeOfTheLastDamage = GetWorld()->GetTimeSeconds();
CumulatedDamagesOnCurrentHitPeriod += Damage;
SetPlayerViewOcclusionPercent();
}
void ALMPlayer::SetPlayerViewOcclusionPercent()
{
AlphaLerpForPlayerViewOcclusionPercent = 0;
PlayerViewOcclusionPercent = NextPlayerViewOcclusionPercent;
NextPlayerViewOcclusionPercent = CumulatedDamagesOnCurrentHitPeriod / HealthAtTheCurrentHitPeriodBeginning;
}

View File

@ -23,6 +23,8 @@ public:
void Kill(); void Kill();
public: public:
FORCEINLINE float GetHealth() const { return Health; }
/** /**
* Applies an amount of damage to the component. * Applies an amount of damage to the component.
* @param Damage The damage to apply the Health to. * @param Damage The damage to apply the Health to.

View File

@ -11,6 +11,7 @@
class UCameraComponent; class UCameraComponent;
class ULMWeaponManager; class ULMWeaponManager;
class ULMHealthComponent; class ULMHealthComponent;
class UMaterialInstanceDynamic;
class ALMAmmo; class ALMAmmo;
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnRequestUnpauseSignature); DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnRequestUnpauseSignature);
@ -48,6 +49,9 @@ protected:
// Called when the game starts or when spawned // Called when the game starts or when spawned
virtual void BeginPlay() override; virtual void BeginPlay() override;
// Called every frame
virtual void Tick(float DeltaTime) override;
public: public:
UFUNCTION(BlueprintCallable) UFUNCTION(BlueprintCallable)
ULMWeaponManager* GetWeaponManager() { return WeaponManager; } ULMWeaponManager* GetWeaponManager() { return WeaponManager; }
@ -113,34 +117,76 @@ private:
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Legumix, meta = (AllowPrivateAccess = true)) UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Legumix, meta = (AllowPrivateAccess = true))
TObjectPtr<ULMHealthComponent> HealthComponent; TObjectPtr<ULMHealthComponent> HealthComponent;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Legumix, meta = (AllowPrivateAccess = true))
TObjectPtr<UMaterialInstanceDynamic> DamageDynamicMaterialInstance;
/** /**
* Length of the period in which the damages HUD will be displayed on player screen. * Is a hit period in progress
*/
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Legumix, meta = (AllowPrivateAccess = true))
bool bHitPeriodInProgress;
/**
* Length of the period in which the damage texture effect will be displayed on player screen.
*/ */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Legumix, meta = (AllowPrivateAccess = true)) UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Legumix, meta = (AllowPrivateAccess = true))
double HitPeriodLengthInSeconds; double HitPeriodLengthInSeconds;
/** /**
* Time elapsed since last damage taken. * Time of last damage taken.
* If it's superior to the HitPeriodLengthInSeconds variable, ends the Hit period * If the time elapsed since is superior to the HitPeriodLengthInSeconds variable, ends the Hit period
* and so the displaying of damages HUD on player screen * and so the displaying the damage texture effect on player screen
*/ */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Legumix, meta = (AllowPrivateAccess = true)) UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Legumix, meta = (AllowPrivateAccess = true))
double TimeElapsedSinceLastDamageInSeconds; double TimeOfTheLastDamage;
/** /**
* Cumulated damages taken by the player on the current hit period * Cumulated damages taken by the player on the current hit period
* To calculate view occlusion percent for the damage HUD * To calculate view occlusion percent for the damage texture effect
*/ */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Legumix, meta = (AllowPrivateAccess = true)) UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Legumix, meta = (AllowPrivateAccess = true))
float CumulatedDamagesOnCurrentHitPeriod; float CumulatedDamagesOnCurrentHitPeriod;
/** /**
* Player health at the current hit period beginning * Player health at the current hit period beginning
* To calculate view occlusion percent for the damage HUD * To calculate view occlusion percent for the damage texture effect
*/ */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Legumix, meta = (AllowPrivateAccess = true)) UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Legumix, meta = (AllowPrivateAccess = true))
float HealthAtTheCurrentHitPeriodBeginning; float HealthAtTheCurrentHitPeriodBeginning;
/**
* Percent of the player screen occupied with damage texture effect to reach
*/
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Legumix, meta = (AllowPrivateAccess = true))
float NextPlayerViewOcclusionPercent;
/**
* Previous percent of the player screen occupied with damage texture effect
* mandatory to make interpolation between this core and the current one
*/
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Legumix, meta = (AllowPrivateAccess = true))
float PlayerViewOcclusionPercent;
/**
* Percent of the player screen occupied with damage texture effect
*/
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category=Legumix, meta = (AllowPrivateAccess = true))
float AlphaLerpForPlayerViewOcclusionPercent;
/**
* Actualize all parameters needed to calculate PlayerViewOcclusionPercent
*/
UFUNCTION(BlueprintCallable, Category=PostProcess)
void SetDisplayDamageParameters(float Health, float Damage);
/**
* Determine Percent of the player screen occupied with damage texture effect
*/
UFUNCTION(BlueprintCallable, Category=PostProcess)
void SetPlayerViewOcclusionPercent();
private: private:
#if WITH_EDITORONLY_DATA #if WITH_EDITORONLY_DATA
/** If set, bullet debug will be drawn. */ /** If set, bullet debug will be drawn. */