From 64d66c498201e2152294aadcc72aec5d87f0a400 Mon Sep 17 00:00:00 2001 From: JKuijperM Date: Sun, 12 Mar 2023 12:51:43 +0100 Subject: [PATCH] Modification in turn functionality Removed the turn functionality that uses the timeline, now in the turning tiles you are free to do a movement to left or right and the animation is faster than the previous version --- Content/Blueprints/BP_ZombieCharacter.uasset | 4 +- .../Blueprints/Tiles/BP_TileTurnLeft.uasset | 4 +- .../Blueprints/Tiles/BP_TileTurnRight.uasset | 4 +- Content/Input/InputMapping.uasset | 2 +- Source/EndlessZombie/TileCurve.cpp | 19 +-- Source/EndlessZombie/ZombieCharacter.cpp | 110 +++++------------- Source/EndlessZombie/ZombieCharacter.h | 32 +---- 7 files changed, 41 insertions(+), 134 deletions(-) diff --git a/Content/Blueprints/BP_ZombieCharacter.uasset b/Content/Blueprints/BP_ZombieCharacter.uasset index ff2164e..1cd6b08 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:96b498c17899f18649d1af2a84fb121464381a57d2c720e1b67754a4c459cd5a -size 35291 +oid sha256:51c2a5da66d7a584e0b591a3262e7c49d15cd71dfe53e23254247a3e78962698 +size 35097 diff --git a/Content/Blueprints/Tiles/BP_TileTurnLeft.uasset b/Content/Blueprints/Tiles/BP_TileTurnLeft.uasset index 66ed331..08116ce 100644 --- a/Content/Blueprints/Tiles/BP_TileTurnLeft.uasset +++ b/Content/Blueprints/Tiles/BP_TileTurnLeft.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:5808943abab611606f9d0a28be192c9e5053fd47d01dc7468b6b313c098d71ca -size 36884 +oid sha256:368e355640209d925f14b1aa209150fae69e0fdc8cc6ea88769dceed6c6766ef +size 36928 diff --git a/Content/Blueprints/Tiles/BP_TileTurnRight.uasset b/Content/Blueprints/Tiles/BP_TileTurnRight.uasset index cacb830..41cb58d 100644 --- a/Content/Blueprints/Tiles/BP_TileTurnRight.uasset +++ b/Content/Blueprints/Tiles/BP_TileTurnRight.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:53b5e0b7bc251e49906f5431f804f40c17b8757bd776e8c2643c2a8e2ca29b2d -size 37056 +oid sha256:302d46c4efd912d483f97d9e2e469566dd5eca88883381779e45b2924ec3e0f0 +size 37105 diff --git a/Content/Input/InputMapping.uasset b/Content/Input/InputMapping.uasset index 0c46972..c845818 100644 --- a/Content/Input/InputMapping.uasset +++ b/Content/Input/InputMapping.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:da11530fbc1a44bde5eed884dd45e343c6a6bc6e85e5d8ef8dd4f2989067ed62 +oid sha256:1e1083088494b22be0ef59e2488e878366780e1ad135ab336512208a4fd3a114 size 5608 diff --git a/Source/EndlessZombie/TileCurve.cpp b/Source/EndlessZombie/TileCurve.cpp index ff7ff15..feb9e7b 100644 --- a/Source/EndlessZombie/TileCurve.cpp +++ b/Source/EndlessZombie/TileCurve.cpp @@ -27,23 +27,6 @@ void ATileCurve::TurnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AAct if (ZombieCharacter != nullptr) { - if (!bWasTriggered) - { - if (bTurnRight && !bTurnLeft) - { - ZombieCharacter->bStraight = false; - ZombieCharacter->iSideTurn = 1; - } - else if (!bTurnRight && bTurnLeft) - { - ZombieCharacter->bStraight = false; - ZombieCharacter->iSideTurn = -1; - } - else - { - UE_LOG(LogTemp, Error, TEXT("[ATileCurve::OnBeginOverlapChild] You should not be here, check if bTurnRight and bTurnLeft have the same value.")) - } - bWasTriggered = true; - } + ZombieCharacter->bCanTurn = true; } } \ No newline at end of file diff --git a/Source/EndlessZombie/ZombieCharacter.cpp b/Source/EndlessZombie/ZombieCharacter.cpp index c16f011..eeeb598 100644 --- a/Source/EndlessZombie/ZombieCharacter.cpp +++ b/Source/EndlessZombie/ZombieCharacter.cpp @@ -52,15 +52,6 @@ AZombieCharacter::AZombieCharacter() 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 @@ -75,19 +66,6 @@ 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 @@ -97,13 +75,7 @@ void AZombieCharacter::Tick(float DeltaTime) if (!bDied) { - - TurnTimeline.TickTimeline(DeltaTime); - - if (!bTurning) - { - MoveForwardConstant(DeltaTime); - } + MoveForwardConstant(DeltaTime); } } @@ -120,6 +92,8 @@ void AZombieCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputCom // Moving EnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Triggered, this, &AZombieCharacter::Move); + EnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Started, this, &AZombieCharacter::CanTurn); + // Crouching //EnhancedInputComponent->BindAction(CrouchAction, ETriggerEvent::Triggered, this, &AZombieCharacter::Crouch); @@ -141,17 +115,22 @@ void AZombieCharacter::Move(const FInputActionValue& Value) const FRotator Rotation = Controller->GetControlRotation(); const FRotator YawRotation(0, Rotation.Yaw, 0); - // get forward vector - const FVector ForwardDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X); - // get right vector const FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y); - // add movement - AddMovementInput(ForwardDirection, MovementVector.Y); + // add movement AddMovementInput(RightDirection, MovementVector.X); } } +void AZombieCharacter::CanTurn(const FInputActionValue& Value) +{ + float fTurnSide = Value.Get().X; + if (bCanTurn) + { + rDesireRotation = rDesireRotation + FRotator(0.f, fTurnSide * 90.f, 0.f); + bCanTurn = false; + } +} void AZombieCharacter::RestartLevel() { @@ -191,64 +170,29 @@ void AZombieCharacter::Die() void AZombieCharacter::MoveForwardConstant(float DeltaTime) { - if (bStraight) + if (Controller) { + TurnCorner(DeltaTime); + // find out which way is forward + const FRotator Rotation = Controller->GetControlRotation(); + const FRotator YawRotation(0, Rotation.Yaw, 0); + // get forward vector + const FVector ForwardDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X); + + //UE_LOG(LogTemp, Warning, TEXT("%f"), fBaseSpeed); FVector vLocation = GetActorLocation(); fBaseSpeed += fAcceleration * DeltaTime; vLocation += GetActorForwardVector() * fBaseSpeed; SetActorLocation(vLocation); - } - else - { - RotateCharacter(); - bStraight = true; - } - -} - -void AZombieCharacter::PlayTurn() -{ - bTurning = true; - if (fControlTurn < 90.f) - { - TurnTimeline.PlayFromStart(); - } - else - { - fControlTurn = 0.f; + //AddMovementInput(ForwardDirection, .5f, true); } } -void AZombieCharacter::RotateCharacter() +void AZombieCharacter::TurnCorner(float DeltaTime) { - FRotator CurrentRotation = GetActorRotation(); - fControlTurn = 0.f; - PlayTurn(); -} - -void AZombieCharacter::ControlTurning() -{ - fTimelineValue = TurnTimeline.GetPlaybackPosition(); - float fTurnCurve = TurnCurve->GetFloatValue(fTimelineValue); - - float fVal = fTurnCurve - fPrevCurvValue; - fPrevCurvValue = fTurnCurve; - - fControlTurn += fVal; - - if (fControlTurn <= 90.f) + if (GetControlRotation() != rDesireRotation) { - FRotator CurrentRotation = Controller->GetControlRotation(); - CurrentRotation.Yaw += fVal * iSideTurn; - Controller->SetControlRotation(CurrentRotation); + FRotator rNewRotation = FMath::RInterpTo(GetControlRotation(), rDesireRotation, DeltaTime, fTurnSpeed); + Controller->SetControlRotation(rNewRotation); } - -} - -void AZombieCharacter::SetState() -{ - bReadyState = true; - bTurning = false; - bStraight = true; - fPrevCurvValue = 0.f; } \ No newline at end of file diff --git a/Source/EndlessZombie/ZombieCharacter.h b/Source/EndlessZombie/ZombieCharacter.h index 81ca22c..d5b6a51 100644 --- a/Source/EndlessZombie/ZombieCharacter.h +++ b/Source/EndlessZombie/ZombieCharacter.h @@ -52,6 +52,7 @@ protected: virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override; void Move(const FInputActionValue& Value); + void CanTurn(const FInputActionValue& Value); //void Crouch(const FInputActionValue& Value); void RestartLevel(); @@ -70,28 +71,10 @@ public: UPROPERTY(EditAnywhere, Category = "Movement") float fAcceleration = .001f; - void RotateCharacter(); - bool bStraight = true; - int iSideTurn = 1; - - // Properties for the timeline to turn right - UFUNCTION() - void ControlTurning(); - UFUNCTION() - void SetState(); + bool bCanTurn = false; + FRotator rDesireRotation = FRotator(0.f, 0.f, 0.f); 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; + float fTurnSpeed = 10.f; void Die(); @@ -99,11 +82,8 @@ private: void MoveForwardConstant(float DeltaTime); - void PlayTurn(); - - float fControlTurn = 0.f; - float fPrevCurvValue = 0.f; - bool bDied = false; + void TurnCorner(float DeltaTime); + };