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.
This commit is contained in:
JKuijperM 2023-04-03 18:31:24 +02:00
parent 64d66c4982
commit c216c8e690
8 changed files with 99 additions and 11 deletions

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:51c2a5da66d7a584e0b591a3262e7c49d15cd71dfe53e23254247a3e78962698
size 35097
oid sha256:0436199e5aea693e96a0293f4468d62e16194ae56ffd4efd4b43a94de2d6dd09
size 103422

View File

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:9741b8174cf3fba8bc9c7b745d7dd0e27b9a4a8e40e19c7ee458433e18ccfc0f
size 1889

View File

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:a1ca4bdd23213b34e8672d49214919b0255e0c1e1522ab029ac41c96e0803494
size 10433

View File

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:ee4cbb291144ca2222b925547d1f38556ef6dd78d53a90d51fe1f07ff5ecf57c
size 16268

View File

@ -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<AZombieCharacter>(OtherActor);
ZombieCharacter->Die();
if (!bIsCollisioned) {
if (OtherActor->IsA(AZombieCharacter::StaticClass()))
{
AZombieCharacter* ZombieCharacter = Cast<AZombieCharacter>(OtherActor);
ZombieCharacter->ObstacleCollision();
bIsCollisioned = true;
StaticMesh->SetCollisionProfileName("NoCollision");
}
}
}

View File

@ -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;
};

View File

@ -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)

View File

@ -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;
};