Attempt to implement damage flash for enemy ships

This commit is contained in:
2025-04-17 08:42:39 +05:30
parent 322f1ac061
commit 577205fc12
5 changed files with 80 additions and 3 deletions

View File

@@ -61,6 +61,8 @@ void AEnemySpaceship::BeginPlay()
bInitializedFlank = false;
LastFlankUpdateTime = 0.0f;
CurrentFlankTarget = GetActorLocation();
InitializeDynamicMaterial();
}
// Modify Tick function to include flanking behavior
@@ -507,6 +509,57 @@ void AEnemySpaceship::ResetFire()
bCanFire = true;
}
void AEnemySpaceship::InitializeDynamicMaterial()
{
if (EnemyMesh)
{
// Store the original material
OriginalMaterial = EnemyMesh->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
EnemyMesh->SetMaterial(0, DynamicMaterialInstance);
// Initialize flash color to transparent
DynamicMaterialInstance->SetVectorParameterValue("FlashColor", FLinearColor(0.0f, 0.0f, 0.0f, 0.0f));
}
}
}
}
void AEnemySpaceship::ApplyDamageFlash()
{
// Only set the flash color if we have a valid dynamic material instance
if (DynamicMaterialInstance)
{
// Set the flash color
DynamicMaterialInstance->SetVectorParameterValue("FlashColor", DamageFlashColor);
// Set timer to reset the flash
GetWorldTimerManager().SetTimer(
DamageFlashTimerHandle,
this,
&AEnemySpaceship::ResetDamageFlash,
DamageFlashDuration,
false
);
}
}
void AEnemySpaceship::ResetDamageFlash()
{
// Reset the flash color only if we have a valid dynamic material instance
if (DynamicMaterialInstance)
{
DynamicMaterialInstance->SetVectorParameterValue("FlashColor", FLinearColor(0.0f, 0.0f, 0.0f, 0.0f));
}
}
float AEnemySpaceship::TakeDamage(float DamageAmount, FDamageEvent const& DamageEvent,
AController* EventInstigator, AActor* DamageCauser)
{
@@ -521,6 +574,9 @@ float AEnemySpaceship::TakeDamage(float DamageAmount, FDamageEvent const& Damage
FString::Printf(TEXT("Enemy Hit! Health: %f"), CurrentHealth));
}
// Apply visual damage effects
ApplyDamageFlash();
// When damaged, prefer retreat or strafe behaviors temporarily
if (FMath::RandBool())
{