From 577205fc12b9ace8a39fca9115296e19c31af2ee Mon Sep 17 00:00:00 2001 From: aicorr Date: Thu, 17 Apr 2025 08:42:39 +0530 Subject: [PATCH] Attempt to implement damage flash for enemy ships --- Content/Blueprints/BP_EnemySpaceship.uasset | 4 +- Content/Main.umap | 2 +- Content/Materials/xwingCanopy.uasset | 3 ++ Source/MyProject3/EnemySpaceship.cpp | 56 +++++++++++++++++++++ Source/MyProject3/EnemySpaceship.h | 18 +++++++ 5 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 Content/Materials/xwingCanopy.uasset diff --git a/Content/Blueprints/BP_EnemySpaceship.uasset b/Content/Blueprints/BP_EnemySpaceship.uasset index 3c84129..2067087 100644 --- a/Content/Blueprints/BP_EnemySpaceship.uasset +++ b/Content/Blueprints/BP_EnemySpaceship.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:39d36dfd7a1231daa0438638e93117111c923c4bf8602f92c7786d1b8c933943 -size 34382 +oid sha256:cc181eeea254267a65cf7dfce6b4cc02b874bb742c131df9a579995fdb90b1e2 +size 34669 diff --git a/Content/Main.umap b/Content/Main.umap index 778cf95..15882e4 100644 --- a/Content/Main.umap +++ b/Content/Main.umap @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:408f5437756de7b851860ade1c3ec06befec1293dfd15577b486889fcf521de6 +oid sha256:0e38f7383dfe31ff7727fa174a9bcbe4d73fa816d0dbf50687a1f57d727308af size 70812 diff --git a/Content/Materials/xwingCanopy.uasset b/Content/Materials/xwingCanopy.uasset new file mode 100644 index 0000000..e69998c --- /dev/null +++ b/Content/Materials/xwingCanopy.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:538a8780ccfe24b70af10489f891a267e9b419b927b4920fdc4cef4615cb993e +size 54020 diff --git a/Source/MyProject3/EnemySpaceship.cpp b/Source/MyProject3/EnemySpaceship.cpp index eb8b83e..4a3e182 100644 --- a/Source/MyProject3/EnemySpaceship.cpp +++ b/Source/MyProject3/EnemySpaceship.cpp @@ -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()) { diff --git a/Source/MyProject3/EnemySpaceship.h b/Source/MyProject3/EnemySpaceship.h index 61fad5d..6fdc2f2 100644 --- a/Source/MyProject3/EnemySpaceship.h +++ b/Source/MyProject3/EnemySpaceship.h @@ -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(); }; \ No newline at end of file