From c216c8e69099b41a866fd7e70e944c950b76ad4b Mon Sep 17 00:00:00 2001 From: JKuijperM Date: Mon, 3 Apr 2023 18:31:24 +0200 Subject: [PATCH] Work in the life system Added counter for player lifes that decreases when the zombie collisions with an obstacle. Also, added a effect that changes the material when the collision is produced. --- Content/Blueprints/BP_ZombieCharacter.uasset | 4 +- Content/Blueprints/Timelines/C_Flash.uasset | 3 + .../Zombies/Skins/MI_MaleZombie.uasset | 3 + .../Zombies/Skins/M_MaleZombie.uasset | 3 + Source/EndlessZombie/Obstacle.cpp | 12 ++-- Source/EndlessZombie/Obstacle.h | 3 + Source/EndlessZombie/ZombieCharacter.cpp | 56 ++++++++++++++++++- Source/EndlessZombie/ZombieCharacter.h | 26 +++++++-- 8 files changed, 99 insertions(+), 11 deletions(-) create mode 100644 Content/Blueprints/Timelines/C_Flash.uasset create mode 100644 Content/Characters/Zombies/Skins/MI_MaleZombie.uasset create mode 100644 Content/Characters/Zombies/Skins/M_MaleZombie.uasset diff --git a/Content/Blueprints/BP_ZombieCharacter.uasset b/Content/Blueprints/BP_ZombieCharacter.uasset index 1cd6b08..b4a8f6b 100644 --- a/Content/Blueprints/BP_ZombieCharacter.uasset +++ b/Content/Blueprints/BP_ZombieCharacter.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:51c2a5da66d7a584e0b591a3262e7c49d15cd71dfe53e23254247a3e78962698 -size 35097 +oid sha256:0436199e5aea693e96a0293f4468d62e16194ae56ffd4efd4b43a94de2d6dd09 +size 103422 diff --git a/Content/Blueprints/Timelines/C_Flash.uasset b/Content/Blueprints/Timelines/C_Flash.uasset new file mode 100644 index 0000000..abb32bd --- /dev/null +++ b/Content/Blueprints/Timelines/C_Flash.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9741b8174cf3fba8bc9c7b745d7dd0e27b9a4a8e40e19c7ee458433e18ccfc0f +size 1889 diff --git a/Content/Characters/Zombies/Skins/MI_MaleZombie.uasset b/Content/Characters/Zombies/Skins/MI_MaleZombie.uasset new file mode 100644 index 0000000..7e05db9 --- /dev/null +++ b/Content/Characters/Zombies/Skins/MI_MaleZombie.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a1ca4bdd23213b34e8672d49214919b0255e0c1e1522ab029ac41c96e0803494 +size 10433 diff --git a/Content/Characters/Zombies/Skins/M_MaleZombie.uasset b/Content/Characters/Zombies/Skins/M_MaleZombie.uasset new file mode 100644 index 0000000..dbdb287 --- /dev/null +++ b/Content/Characters/Zombies/Skins/M_MaleZombie.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ee4cbb291144ca2222b925547d1f38556ef6dd78d53a90d51fe1f07ff5ecf57c +size 16268 diff --git a/Source/EndlessZombie/Obstacle.cpp b/Source/EndlessZombie/Obstacle.cpp index 637c091..e6372e8 100644 --- a/Source/EndlessZombie/Obstacle.cpp +++ b/Source/EndlessZombie/Obstacle.cpp @@ -40,10 +40,14 @@ void AObstacle::Tick(float DeltaTime) void AObstacle::CollisionBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) { - if (OtherActor->IsA(AZombieCharacter::StaticClass())) - { - AZombieCharacter* ZombieCharacter = Cast(OtherActor); - ZombieCharacter->Die(); + if (!bIsCollisioned) { + if (OtherActor->IsA(AZombieCharacter::StaticClass())) + { + AZombieCharacter* ZombieCharacter = Cast(OtherActor); + ZombieCharacter->ObstacleCollision(); + bIsCollisioned = true; + StaticMesh->SetCollisionProfileName("NoCollision"); + } } } diff --git a/Source/EndlessZombie/Obstacle.h b/Source/EndlessZombie/Obstacle.h index b808fc8..b224de0 100644 --- a/Source/EndlessZombie/Obstacle.h +++ b/Source/EndlessZombie/Obstacle.h @@ -34,4 +34,7 @@ public: UFUNCTION() void CollisionBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult); +private: + bool bIsCollisioned = false; + }; diff --git a/Source/EndlessZombie/ZombieCharacter.cpp b/Source/EndlessZombie/ZombieCharacter.cpp index eeeb598..4be5c58 100644 --- a/Source/EndlessZombie/ZombieCharacter.cpp +++ b/Source/EndlessZombie/ZombieCharacter.cpp @@ -1,6 +1,5 @@ // Copyright Jorge Kuijper. All Rights Reserved. - #include "ZombieCharacter.h" #include "EndlessZombieGameMode.h" #include "Camera/CameraComponent.h" @@ -52,6 +51,7 @@ AZombieCharacter::AZombieCharacter() FollowCamera->bUsePawnControlRotation = false; // Camera does not rotate relative to arm FollowCamera->SetWorldRotation(FQuat(FRotator(-15.f, 0.f, 0.f))); + bReadyState = true; } // Called when the game starts or when spawned @@ -66,6 +66,19 @@ void AZombieCharacter::BeginPlay() Subsystem->AddMappingContext(DefaultMappingContext, 0); } } + + DynamicFlashMaterial = GetMesh()->CreateDynamicMaterialInstance(0); + + if (FlashCurve) + { + FOnTimelineFloat TimelineCallback; + FOnTimelineEventStatic TimelineFisihedCallback; + + TimelineCallback.BindUFunction(this, FName("ControlFlashing")); + TimelineFisihedCallback.BindUFunction(this, FName("SetState")); + FlashTimeline.AddInterpFloat(FlashCurve, TimelineCallback); + FlashTimeline.SetTimelineFinishedFunc(TimelineFisihedCallback); + } } // Called every frame @@ -73,6 +86,8 @@ void AZombieCharacter::Tick(float DeltaTime) { Super::Tick(DeltaTime); + FlashTimeline.TickTimeline(DeltaTime); + if (!bDied) { MoveForwardConstant(DeltaTime); @@ -168,6 +183,45 @@ void AZombieCharacter::Die() bDied = true; } +void AZombieCharacter::ObstacleCollision() +{ + iPlayerLife--; + if (iPlayerLife > 0) + { + PlayFlashEffect(); + } + else + { + Die(); + } +} + +void AZombieCharacter::PlayFlashEffect(float fMultiplier, FLinearColor Color, float fPlayRate) +{ + if (DynamicFlashMaterial) + { + fGlobalMultiplier = fMultiplier; + DynamicFlashMaterial->SetScalarParameterValue("FlashMultiplier", fMultiplier); + DynamicFlashMaterial->SetVectorParameterValue("FlashColor", Color); + FlashTimeline.SetPlayRate(fPlayRate); + FlashTimeline.PlayFromStart(); + } +} + +void AZombieCharacter::ControlFlashing() +{ + fTimelineValue = FlashTimeline.GetPlaybackPosition(); + float fFlashCurve = FlashCurve->GetFloatValue(fTimelineValue); + + float alpha = FMath::Lerp(0.f, fGlobalMultiplier, fFlashCurve); + DynamicFlashMaterial->SetScalarParameterValue("FlashMultiplier", alpha); +} + +void AZombieCharacter::SetState() +{ + bReadyState = true; +} + void AZombieCharacter::MoveForwardConstant(float DeltaTime) { if (Controller) diff --git a/Source/EndlessZombie/ZombieCharacter.h b/Source/EndlessZombie/ZombieCharacter.h index d5b6a51..35f846b 100644 --- a/Source/EndlessZombie/ZombieCharacter.h +++ b/Source/EndlessZombie/ZombieCharacter.h @@ -3,8 +3,8 @@ #pragma once #include "CoreMinimal.h" -#include "GameFramework/Character.h" #include "InputActionValue.h" +#include "GameFramework/Character.h" #include "Components/TimelineComponent.h" #include "ZombieCharacter.generated.h" @@ -70,6 +70,8 @@ public: float fBaseSpeed = 5.f; UPROPERTY(EditAnywhere, Category = "Movement") float fAcceleration = .001f; + UPROPERTY(EditAnywhere, Category = "Life") + int iPlayerLife = 3; bool bCanTurn = false; FRotator rDesireRotation = FRotator(0.f, 0.f, 0.f); @@ -77,13 +79,29 @@ public: float fTurnSpeed = 10.f; void Die(); + void ObstacleCollision(); + void PlayFlashEffect(float fMultiplier = 1.f, FLinearColor Color = FLinearColor(1.f, 1.f, 1.f, 1.f), float fPlayRate = 5.f); + + // Properties for the timeline to turn right + UFUNCTION() + void ControlFlashing(); + UFUNCTION() + void SetState(); + UPROPERTY(EditAnywhere, Category = "Life") + UCurveFloat* FlashCurve; + bool bReadyState; + float fCurveValue; + float fDistanceValue; + float fTimelineValue; + FTimeline FlashTimeline; private: void MoveForwardConstant(float DeltaTime); - - bool bDied = false; - void TurnCorner(float DeltaTime); + bool bDied = false; + float fGlobalMultiplier = 1.f; + + UMaterialInstanceDynamic* DynamicFlashMaterial; };