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

Binary file not shown.

Binary file not shown.

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())
{

View File

@@ -109,6 +109,20 @@ protected:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "AI")
float FlankingFrequency = 0.6f; // Increased chance of flanking (was 0.4f)
UPROPERTY()
UMaterialInterface* OriginalMaterial;
UPROPERTY()
UMaterialInstanceDynamic* DynamicMaterialInstance;
UPROPERTY(EditAnywhere, Category = "Effects")
FLinearColor DamageFlashColor = FLinearColor(1.0f, 0.0f, 0.0f, 1.0f);
UPROPERTY(EditAnywhere, Category = "Effects")
float DamageFlashDuration = 0.1f;
FTimerHandle DamageFlashTimerHandle;
public:
virtual void Tick(float DeltaTime) override;
@@ -164,4 +178,8 @@ private:
float LastFlankUpdateTime;
FVector CurrentFlankTarget;
bool bInitializedFlank;
void InitializeDynamicMaterial();
void ApplyDamageFlash();
void ResetDamageFlash();
};