Fix damage flash implementation

This commit is contained in:
2025-04-16 22:44:01 +05:30
parent 7f08808b71
commit 44aee655de
4 changed files with 57 additions and 27 deletions

Binary file not shown.

Binary file not shown.

View File

@@ -111,6 +111,9 @@ void ASpaceshipPawn::BeginPlay()
CurrentHealth = MaxHealth; CurrentHealth = MaxHealth;
CurrentShield = MaxShield; CurrentShield = MaxShield;
LastDamageTime = 0.0f; LastDamageTime = 0.0f;
// Initialize the dynamic material
InitializeDynamicMaterial();
} }
void ASpaceshipPawn::Tick(float DeltaTime) void ASpaceshipPawn::Tick(float DeltaTime)
@@ -486,41 +489,54 @@ float ASpaceshipPawn::TakeDamage(float DamageAmount, FDamageEvent const& DamageE
return DamageToApply; return DamageToApply;
} }
void ASpaceshipPawn::InitializeDynamicMaterial()
{
if (ShipMesh)
{
// Store the original material
OriginalMaterial = ShipMesh->GetMaterial(0);
if (OriginalMaterial)
{
// Create dynamic material instance only once
DynamicMaterialInstance = UMaterialInstanceDynamic::Create(OriginalMaterial, this);
if (DynamicMaterialInstance)
{
// Apply the dynamic material instance to the mesh
ShipMesh->SetMaterial(0, DynamicMaterialInstance);
// Initialize flash color to transparent
DynamicMaterialInstance->SetVectorParameterValue("FlashColor", FLinearColor(0.0f, 0.0f, 0.0f, 0.0f));
}
}
}
}
void ASpaceshipPawn::ApplyDamageFlash() void ASpaceshipPawn::ApplyDamageFlash()
{ {
// Apply material flash effect to indicate damage // Only set the flash color if we have a valid dynamic material instance
if (ShipMesh && ShipMesh->GetMaterial(0)) if (DynamicMaterialInstance)
{ {
UMaterialInstanceDynamic* DynamicMaterial = UMaterialInstanceDynamic::Create(ShipMesh->GetMaterial(0), this); // Set the flash color
if (DynamicMaterial) DynamicMaterialInstance->SetVectorParameterValue("FlashColor", DamageFlashColor);
{
// Assuming the material has a parameter named "FlashColor"
DynamicMaterial->SetVectorParameterValue("FlashColor", DamageFlashColor);
ShipMesh->SetMaterial(0, DynamicMaterial);
// Set timer to reset the flash // Set timer to reset the flash
GetWorldTimerManager().SetTimer( GetWorldTimerManager().SetTimer(
DamageFlashTimerHandle, DamageFlashTimerHandle,
this, this,
&ASpaceshipPawn::ResetDamageFlash, &ASpaceshipPawn::ResetDamageFlash,
DamageFlashDuration, DamageFlashDuration,
false false
); );
}
} }
} }
void ASpaceshipPawn::ResetDamageFlash() void ASpaceshipPawn::ResetDamageFlash()
{ {
// Reset material flash effect // Reset the flash color only if we have a valid dynamic material instance
if (ShipMesh && ShipMesh->GetMaterial(0)) if (DynamicMaterialInstance)
{ {
UMaterialInstanceDynamic* DynamicMaterial = Cast<UMaterialInstanceDynamic>(ShipMesh->GetMaterial(0)); DynamicMaterialInstance->SetVectorParameterValue("FlashColor", FLinearColor(0.0f, 0.0f, 0.0f, 0.0f));
if (DynamicMaterial)
{
// Reset the flash color (assuming transparent black means no flash)
DynamicMaterial->SetVectorParameterValue("FlashColor", FLinearColor(0.0f, 0.0f, 0.0f, 0.0f));
}
} }
} }

View File

@@ -176,6 +176,14 @@ protected:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Effects") UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Effects")
FLinearColor DamageFlashColor = FLinearColor(1.0f, 0.0f, 0.0f, 0.5f); FLinearColor DamageFlashColor = FLinearColor(1.0f, 0.0f, 0.0f, 0.5f);
// Store the original material
UPROPERTY()
UMaterialInterface* OriginalMaterial;
// Store the dynamic material instance
UPROPERTY()
UMaterialInstanceDynamic* DynamicMaterialInstance;
private: private:
// Movement state // Movement state
float CurrentThrottleInput; float CurrentThrottleInput;
@@ -213,6 +221,9 @@ private:
FTimerHandle DamageFlashTimerHandle; FTimerHandle DamageFlashTimerHandle;
float LastDamageTime; float LastDamageTime;
// Initialize dynamic material
void InitializeDynamicMaterial();
void StartShieldRecharge(); void StartShieldRecharge();
void RechargeShield(); void RechargeShield();
bool IsDead() const { return CurrentHealth <= 0.0f; } bool IsDead() const { return CurrentHealth <= 0.0f; }