From 24edf34abffbfbc0dbcebd1f11add84d90e6182b Mon Sep 17 00:00:00 2001 From: JKuijperM Date: Tue, 3 Jan 2023 17:28:24 +0100 Subject: [PATCH] WIP in the turn of the character --- .../BP_EndlessZombieGameMode.uasset | 4 +- Content/Blueprints/BP_PathGenerator.uasset | 4 +- Content/Blueprints/BP_ZombieCharacter.uasset | 4 +- .../Blueprints/Tiles/BP_TileStraight.uasset | 4 +- .../Blueprints/Tiles/BP_TileTurnRight.uasset | 3 + Content/Blueprints/Timelines/TurnCurve.uasset | 3 + Source/EndlessZombie/PathGenerator.cpp | 18 ++- Source/EndlessZombie/PathGenerator.h | 14 +- Source/EndlessZombie/Tile.cpp | 2 +- Source/EndlessZombie/Tile.h | 8 +- Source/EndlessZombie/TileCurve.cpp | 57 ++++++++ Source/EndlessZombie/TileCurve.h | 36 +++++ Source/EndlessZombie/ZombieCharacter.cpp | 127 +++++++++++++++++- Source/EndlessZombie/ZombieCharacter.h | 35 ++++- 14 files changed, 289 insertions(+), 30 deletions(-) create mode 100644 Content/Blueprints/Tiles/BP_TileTurnRight.uasset create mode 100644 Content/Blueprints/Timelines/TurnCurve.uasset create mode 100644 Source/EndlessZombie/TileCurve.cpp create mode 100644 Source/EndlessZombie/TileCurve.h diff --git a/Content/Blueprints/BP_EndlessZombieGameMode.uasset b/Content/Blueprints/BP_EndlessZombieGameMode.uasset index ccfdd3a..9aa832f 100644 --- a/Content/Blueprints/BP_EndlessZombieGameMode.uasset +++ b/Content/Blueprints/BP_EndlessZombieGameMode.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:428d9ef2344016cdbd889195ca121a99f3eae544a690837d2ef3a1418c0f4725 -size 20641 +oid sha256:d3dd1c59c26b87fd1cdc3103d29ce7833d5cbd841a008137c0c956a8af5251f9 +size 21542 diff --git a/Content/Blueprints/BP_PathGenerator.uasset b/Content/Blueprints/BP_PathGenerator.uasset index 5aa17df..83e345e 100644 --- a/Content/Blueprints/BP_PathGenerator.uasset +++ b/Content/Blueprints/BP_PathGenerator.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:dc4a19c1ae87566ebbdb231db20b810988e9022fd62025b1932346aed0c81174 -size 22366 +oid sha256:37e7daf1431d2c994ad4027c7f1a025693551f04212539b979a231e00c4f5925 +size 22666 diff --git a/Content/Blueprints/BP_ZombieCharacter.uasset b/Content/Blueprints/BP_ZombieCharacter.uasset index 620b2c3..62230a0 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:ac43ad7050cc6e5dc17293828fd264bbef8a47020fe80a0f389ced8086698c2c -size 34514 +oid sha256:a382241647580d65fcb11eaed49b5c9e28ca99e8efd50ddb5f8f7c27bce6d1ae +size 35058 diff --git a/Content/Blueprints/Tiles/BP_TileStraight.uasset b/Content/Blueprints/Tiles/BP_TileStraight.uasset index bc54b6e..a5029e0 100644 --- a/Content/Blueprints/Tiles/BP_TileStraight.uasset +++ b/Content/Blueprints/Tiles/BP_TileStraight.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:5b3317be7c9d4042271396c2952f0717b70fcfbf100aa15ff2d131554ae0de06 -size 30566 +oid sha256:b90f3c7238a1e632d67ece9f697a0f57b1550011561781496dca82807d5f03b2 +size 33905 diff --git a/Content/Blueprints/Tiles/BP_TileTurnRight.uasset b/Content/Blueprints/Tiles/BP_TileTurnRight.uasset new file mode 100644 index 0000000..21ed4ed --- /dev/null +++ b/Content/Blueprints/Tiles/BP_TileTurnRight.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:128870a6a999e250ae12fe9bf93bd5b80915245a672072c449950e09e49c5de4 +size 32357 diff --git a/Content/Blueprints/Timelines/TurnCurve.uasset b/Content/Blueprints/Timelines/TurnCurve.uasset new file mode 100644 index 0000000..bc46dfc --- /dev/null +++ b/Content/Blueprints/Timelines/TurnCurve.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:076327d6ae0300ac0495c29f30577940ef6738ba70587f3ef74d737ddf9aeb37 +size 1845 diff --git a/Source/EndlessZombie/PathGenerator.cpp b/Source/EndlessZombie/PathGenerator.cpp index d40c812..0c19eb3 100644 --- a/Source/EndlessZombie/PathGenerator.cpp +++ b/Source/EndlessZombie/PathGenerator.cpp @@ -16,10 +16,11 @@ void APathGenerator::BeginPlay() { Super::BeginPlay(); - for (int i = 0; i < 10; i++) + for (int i = 0; i < iInitialStraightTiles; i++) { - AddFloorTile(); + AddSelectedTile(Tiles[0]); } + AddSelectedTile(Tiles[1]); } @@ -32,10 +33,19 @@ void APathGenerator::Tick(float DeltaTime) void APathGenerator::AddFloorTile() { - if (Tile != nullptr) + if (Tiles[0] != nullptr) { - ATile* GeneratedTile = GetWorld()->SpawnActor(Tile, tNextSpawnPoint); + ATile* GeneratedTile = GetWorld()->SpawnActor(Tiles[0], tNextSpawnPoint); tNextSpawnPoint = GeneratedTile->GetAttachTransform(); } } +void APathGenerator::AddSelectedTile(TSubclassOf Tile) +{ + if (Tile != nullptr) + { + ATile* GeneratedTile = GetWorld()->SpawnActor(Tile, tNextSpawnPoint); + tNextSpawnPoint = GeneratedTile->GetAttachTransform(); + } +} + diff --git a/Source/EndlessZombie/PathGenerator.h b/Source/EndlessZombie/PathGenerator.h index daa8601..8015f28 100644 --- a/Source/EndlessZombie/PathGenerator.h +++ b/Source/EndlessZombie/PathGenerator.h @@ -11,8 +11,8 @@ UCLASS() class ENDLESSZOMBIE_API APathGenerator : public AActor { GENERATED_BODY() - -public: + +public: // Sets default values for this actor's properties APathGenerator(); @@ -20,15 +20,21 @@ protected: // Called when the game starts or when spawned virtual void BeginPlay() override; -public: +public: // Called every frame virtual void Tick(float DeltaTime) override; + /*UPROPERTY(EditAnywhere, Category = "Tiles") + TSubclassOf Tile;*/ UPROPERTY(EditAnywhere, Category = "Tiles") - TSubclassOf Tile; + TArray> Tiles; + UPROPERTY(EditAnywhere, Category = "Tiles") + int iInitialStraightTiles = 10; UFUNCTION(Category = "Tiles") void AddFloorTile(); + void AddSelectedTile(TSubclassOf Tile); + FTransform tNextSpawnPoint = FTransform(FVector(90.0f, 1110.0f, 0.0f)); }; diff --git a/Source/EndlessZombie/Tile.cpp b/Source/EndlessZombie/Tile.cpp index 7eab2f9..ed6a5e8 100644 --- a/Source/EndlessZombie/Tile.cpp +++ b/Source/EndlessZombie/Tile.cpp @@ -65,7 +65,7 @@ void ATile::OnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* Oth FTimerHandle TimerHandle; //GetWorldTimerManager().SetTimer(TimerHandle, this, &ATimeKollapsed_PlayerController::AddWidgetToViewPort, GameOverDelay, false); - GetWorldTimerManager().SetTimer(TimerHandle, this, &ATile::test, 2.f, false); + GetWorldTimerManager().SetTimer(TimerHandle, this, &ATile::DestroyTile, 2.f, false); } } diff --git a/Source/EndlessZombie/Tile.h b/Source/EndlessZombie/Tile.h index 006cbef..43487dc 100644 --- a/Source/EndlessZombie/Tile.h +++ b/Source/EndlessZombie/Tile.h @@ -12,8 +12,8 @@ UCLASS() class ENDLESSZOMBIE_API ATile : public AActor { GENERATED_BODY() - -public: + +public: // Sets default values for this actor's properties ATile(); @@ -21,7 +21,7 @@ protected: // Called when the game starts or when spawned virtual void BeginPlay() override; -public: +public: // Called every frame virtual void Tick(float DeltaTime) override; @@ -35,7 +35,7 @@ public: UFUNCTION(BlueprintCallable, Category = "AttachedPoint") FTransform GetAttachTransform(); UFUNCTION() - virtual void OnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult); + virtual void OnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult); private: void DestroyTile(); diff --git a/Source/EndlessZombie/TileCurve.cpp b/Source/EndlessZombie/TileCurve.cpp new file mode 100644 index 0000000..2d8a835 --- /dev/null +++ b/Source/EndlessZombie/TileCurve.cpp @@ -0,0 +1,57 @@ +// Copyright Jorge Kuijper. All Rights Reserved. + + +#include "TileCurve.h" +#include "ZombieCharacter.h" + +ATileCurve::ATileCurve() +{ + TurnTrigger = CreateDefaultSubobject(TEXT("TurnTrigger")); + TurnTrigger->SetupAttachment(SceneComponent); + TurnTrigger->SetCollisionProfileName(TEXT("OverlapOnlyPawn")); + //TurnTrigger->OnComponentBeginOverlap.AddDynamic(this, &ATileCurve::TurnBeginOverlap); +} + + +void ATileCurve::BeginPlay() +{ + Super::BeginPlay(); + + TurnTrigger->OnComponentBeginOverlap.AddDynamic(this, &ATileCurve::TurnBeginOverlap); + +} + + +void ATileCurve::TurnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) +{ + AZombieCharacter* ZombieCharacter = Cast(OtherActor); + + if (ZombieCharacter != nullptr) + { + if (!bWasTriggered) + { + // Yaw + float fYawValue; + + if (bTurnRight && !bTurnLeft) + { + UE_LOG(LogTemp, Warning, TEXT("[ATileCurve::OnBeginOverlapChild] Rotating right")); + fYawValue = 90.f; + FRotator NewRotation = FRotator(0.f, fYawValue, 0.f); + ZombieCharacter->bStraight = false; + ZombieCharacter->NewRotation = NewRotation; + //ZombieCharacter->RotateCharacter(NewRotation); + } + else if (!bTurnRight && bTurnLeft) + { + UE_LOG(LogTemp, Warning, TEXT("[ATileCurve::OnBeginOverlapChild] Rotating left")); + fYawValue = -90.f; + } + else + { + UE_LOG(LogTemp, Error, TEXT("[ATileCurve::OnBeginOverlapChild] You should not be here, check if bTurnRight and bTurnLeft have the same value.")) + } + bWasTriggered = true; + } + } +} \ No newline at end of file diff --git a/Source/EndlessZombie/TileCurve.h b/Source/EndlessZombie/TileCurve.h new file mode 100644 index 0000000..84ab84f --- /dev/null +++ b/Source/EndlessZombie/TileCurve.h @@ -0,0 +1,36 @@ +// Copyright Jorge Kuijper. All Rights Reserved. + +#pragma once + +#include "CoreMinimal.h" +#include "Tile.h" +#include "TileCurve.generated.h" + +/** + * + */ +UCLASS() +class ENDLESSZOMBIE_API ATileCurve : public ATile +{ + GENERATED_BODY() + +public: + ATileCurve(); + + UPROPERTY(EditAnywhere, BlueprintReadWrite) + UBoxComponent* TurnTrigger; + + UPROPERTY(EditAnywhere, Category = "Turn") + bool bTurnRight = true; + UPROPERTY(EditAnywhere, Category = "Turn") + bool bTurnLeft = false; + + UFUNCTION() + virtual void TurnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult); + +protected: + virtual void BeginPlay() override; + +private: + bool bWasTriggered = false; +}; diff --git a/Source/EndlessZombie/ZombieCharacter.cpp b/Source/EndlessZombie/ZombieCharacter.cpp index f89ff16..6895c5a 100644 --- a/Source/EndlessZombie/ZombieCharacter.cpp +++ b/Source/EndlessZombie/ZombieCharacter.cpp @@ -26,7 +26,7 @@ AZombieCharacter::AZombieCharacter() bUseControllerRotationRoll = false; // Configure character movement - GetCharacterMovement()->bOrientRotationToMovement = true; // Character moves in the direction of input... + GetCharacterMovement()->bOrientRotationToMovement = false; // Character moves in the direction of input... GetCharacterMovement()->RotationRate = FRotator(0.0f, 500.0f, 0.0f); // ...at this rotation rate // Note: For faster iteration times these variables, and many more, can be tweaked in the Character Blueprint @@ -37,17 +37,28 @@ AZombieCharacter::AZombieCharacter() GetCharacterMovement()->MinAnalogWalkSpeed = 20.f; GetCharacterMovement()->BrakingDecelerationWalking = 2000.f; - // Create a camera boom (pulls in towards the player if there is a collision) CameraBoom = CreateDefaultSubobject(TEXT("CameraBoom")); CameraBoom->SetupAttachment(RootComponent); CameraBoom->TargetArmLength = 400.f; // The camera follows at this distance behind the character CameraBoom->bUsePawnControlRotation = true; // Rotate the arm based on the controller + CameraBoom->SetWorldLocation(FVector(0.f, 0.f, 180.f)); // Create a follow camera FollowCamera = CreateDefaultSubobject(TEXT("FollowCamera")); FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName); // Attach the camera to the end of the boom and let the boom adjust to match the controller orientation FollowCamera->bUsePawnControlRotation = false; // Camera does not rotate relative to arm + FollowCamera->SetWorldRotation(FQuat(FRotator(-15.f, 0.f, 0.f))); + + // For timeline + bReadyState = true; + + mDirections.Add(0, FVector(1, 0, 0)); + mDirections.Add(1, FVector(0, 1, 0)); + mDirections.Add(2, FVector(-1, 0, 0)); + mDirections.Add(3, FVector(0, -1, 0)); + + vCurrentDirection = mDirections[0]; } // Called when the game starts or when spawned @@ -62,14 +73,35 @@ void AZombieCharacter::BeginPlay() Subsystem->AddMappingContext(DefaultMappingContext, 0); } } + + //CurrentRotation = GetActorRotation(); + + if (TurnCurve) + { + FOnTimelineFloat TimelineCallback; + FOnTimelineEventStatic TimelineFisihedCallback; + + TimelineCallback.BindUFunction(this, FName("ControlTurning")); + TimelineFisihedCallback.BindUFunction(this, FName("SetState")); + TurnTimeline.AddInterpFloat(TurnCurve, TimelineCallback); + TurnTimeline.SetTimelineFinishedFunc(TimelineFisihedCallback); + } } // Called every frame void AZombieCharacter::Tick(float DeltaTime) { Super::Tick(DeltaTime); - MoveForwardConstant(DeltaTime); + TurnTimeline.TickTimeline(DeltaTime); + + if (!bTurning) + { + MoveForwardConstant(DeltaTime); + } + + //NewRotation.Yaw += 5.f; + //RotateCharacter(a); } // Called to bind functionality to input @@ -122,8 +154,89 @@ void AZombieCharacter::Move(const FInputActionValue& Value) void AZombieCharacter::MoveForwardConstant(float DeltaTime) { - FVector vLocation = GetActorLocation(); - vLocation += GetActorForwardVector() * fBaseSpeed * DeltaTime; - SetActorLocation(vLocation); - + if (bStraight) + { + FVector vLocation = GetActorLocation(); + vLocation += GetActorForwardVector() * fBaseSpeed * DeltaTime; + SetActorLocation(vLocation); + } + else + { + + RotateCharacter(); + FVector vLocation = GetActorLocation(); + /*FVector forward = GetActorForwardVector(); + UE_LOG(LogTemp, Warning, TEXT("x: %.2f, y: %.2f, z: %.2f"), forward.X, forward.Y, forward.Z);*/ + vLocation += vCurrentDirection * 30.f * DeltaTime; + SetActorLocation(vLocation); + bStraight = true; + } + +} + +void AZombieCharacter::PlayTurn() +{ + bTurning = true; + if (fControlTurn < 90.f) + { + TurnTimeline.PlayFromStart(); + } + else + { + fControlTurn = 0.f; + } +} + +void AZombieCharacter::UpdateDirection(int i) +{ + iCurrentDirection += i; + if (iCurrentDirection > mDirections.Num()) + { + iCurrentDirection = 0; + } + else if (iCurrentDirection < 0) + { + iCurrentDirection = mDirections.Num(); + } + + vCurrentDirection = mDirections[iCurrentDirection]; +} + +void AZombieCharacter::RotateCharacter() +{ + FRotator CurrentRotation = GetActorRotation(); + PlayTurn(); + UE_LOG(LogTemp, Warning, TEXT("Girao")); + //Controller->SetControlRotation(NewRotation + CurrentRotation); +} + +void AZombieCharacter::ControlTurning() +{ + fTimelineValue = TurnTimeline.GetPlaybackPosition(); + float fTurnCurve = TurnCurve->GetFloatValue(fTimelineValue); + UE_LOG(LogTemp, Warning, TEXT("[AZombieCharacter::ControlTurning] TurnCurve: %f"), fTurnCurve); + + float fVal = fTurnCurve - fPrevCurvValue; + fPrevCurvValue = fTurnCurve; + + fControlTurn += fVal; + + if (fControlTurn <= 90.f) + { + FRotator CurrentRotation = Controller->GetControlRotation(); + CurrentRotation.Yaw += fVal; + Controller->SetControlRotation(CurrentRotation); + UE_LOG(LogTemp, Warning, TEXT("[AZombieCharacter::ControlTurning] Yaw: %f"), CurrentRotation.Yaw); + UE_LOG(LogTemp, Warning, TEXT("[AZombieCharacter::ControlTurning] Control Turn: %f"), fControlTurn); + + } + +} + +void AZombieCharacter::SetState() +{ + bReadyState = true; + bTurning = false; + bStraight = true; + UE_LOG(LogTemp, Warning, TEXT("[AZombieCharacter::SetState] Set state")); } \ No newline at end of file diff --git a/Source/EndlessZombie/ZombieCharacter.h b/Source/EndlessZombie/ZombieCharacter.h index 6d1955a..d97016d 100644 --- a/Source/EndlessZombie/ZombieCharacter.h +++ b/Source/EndlessZombie/ZombieCharacter.h @@ -5,6 +5,7 @@ #include "CoreMinimal.h" #include "GameFramework/Character.h" #include "InputActionValue.h" +#include "Components/TimelineComponent.h" #include "ZombieCharacter.generated.h" UCLASS() @@ -13,11 +14,11 @@ class ENDLESSZOMBIE_API AZombieCharacter : public ACharacter GENERATED_BODY() /** Camera boom positioning the camera behind the character */ - UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true")) + UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true")) class USpringArmComponent* CameraBoom; /** Follow camera */ - UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true")) + UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true")) class UCameraComponent* FollowCamera; /** MappingContext */ @@ -63,8 +64,38 @@ public: UPROPERTY(EditAnywhere, Category = "Movement") float fBaseSpeed = 100.f; + void RotateCharacter(); + bool bStraight = true; + FRotator NewRotation = FRotator(0.f, 0.f, 0.f); + + // Properties for the timeline to turn right + UFUNCTION() + void ControlTurning(); + UFUNCTION() + void SetState(); + UPROPERTY(EditAnywhere, Category = "Movement") + UCurveFloat* TurnCurve; + bool bReadyState; + float fCurveValue; + float fDistanceValue; + float fTimelineValue; + FTimeline TurnTimeline; + bool bTurning = false; + bool bRotate = false; + + TMap mDirections; + int iCurrentDirection = 0; + FVector vCurrentDirection; + + void UpdateDirection(int i); private: void MoveForwardConstant(float DeltaTime); + + void PlayTurn(); + + float fControlTurn = 0.f; + float fPrevCurvValue = 0.f; + };