Last active
February 10, 2018 21:44
-
-
Save luca1337/c9c281f4e4f3ae6aae4ab4ce147de682 to your computer and use it in GitHub Desktop.
CurveManager C++ Handle object given a curve in a 3D space
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Fill out your copyright notice in the Description page of Project Settings. | |
#include "CurveManager.h" | |
#include "Runtime/Engine/Public/DrawDebugHelpers.h" | |
#include "CurveTaskCharacter.h" | |
#include "Runtime/Engine/Classes/Components/TextRenderComponent.h" | |
#include "Runtime/Engine/Classes/Kismet/GameplayStatics.h" | |
#define DRAW | |
UCurveManager::UCurveManager() | |
{ | |
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features | |
// off to improve performance if you don't need them. | |
PrimaryComponentTick.bCanEverTick = true; | |
this->TextRenderer = CreateDefaultSubobject<UTextRenderComponent>(TEXT("RendererText")); | |
} | |
void UCurveManager::BeginPlay() | |
{ | |
Super::BeginPlay(); | |
this->RootComponent = this->GetAttachmentRoot(); | |
this->TextRenderer->AttachToComponent(this, FAttachmentTransformRules::KeepRelativeTransform); | |
this->TextRenderer->SetRelativeLocation(this->RelativeLocation + | |
FVector(this->TextRenderer->RelativeScale3D.X | |
- this->RootComponent->RelativeScale3D.X / 2, | |
this->TextRenderer->RelativeScale3D.Y | |
- this->RootComponent->RelativeScale3D.Y / 2 + 280, | |
this->TextRenderer->RelativeScale3D.Z | |
- this->RootComponent->RelativeScale3D.Z / 2 + 100)); | |
//retrieve the minimum and maximum time in the range of the given curve | |
this->VectorCurve->GetTimeRange(this->Min, this->Max); | |
//UE_LOG(LogTemp, Warning, TEXT("min: %f max: %f"), Min, Max); | |
} | |
void UCurveManager::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) | |
{ | |
Super::TickComponent(DeltaTime, TickType, ThisTickFunction); | |
//turn this on to rotate all components with text toward main camera | |
//this->RotateTextTowards(); | |
if (this->VectorCurve) | |
{ | |
if (!this->PlayBack) | |
this->Time += this->Speed * DeltaTime; | |
if (this->Time >= this->Max) | |
this->PlayBack = true; | |
if (this->PlayBack) | |
{ | |
this->Time -= this->Speed * DeltaTime; | |
if (this->Time <= this->Min) | |
this->PlayBack = !this->PlayBack; | |
} | |
FVector PositionCurrent = this->VectorCurve->GetVectorValue(this->Time); | |
this->SetRelativeLocation(PositionCurrent, false, false, ETeleportType::None); | |
this->TextRenderer->SetTextRenderColor(this->DebugColor); | |
const FString VectorValue = FString::Printf(TEXT("Current Vector Value: X: %f Y: %f Z: %f\nMax Value: %f"), PositionCurrent.X, | |
PositionCurrent.Y, PositionCurrent.Z, this->Time); | |
this->TextRenderer->SetText(FText::FromString(VectorValue)); | |
#ifdef DRAW | |
DrawDebugPoint(GetWorld(), this->RootComponent->RelativeLocation + FVector(VectorCurve->GetVectorValue(this->Time).X, VectorCurve->GetVectorValue(this->Time).Y, VectorCurve->GetVectorValue(this->Time).Z), 3.0f, this->DebugColor, true); | |
#endif | |
} | |
} | |
void UCurveManager::RotateTextTowards() | |
{ | |
APlayerCameraManager* CameraManager = Cast<APlayerCameraManager>(UGameplayStatics::GetPlayerCameraManager(this, 0)); | |
FRotator NewRotation = CameraManager->GetCameraRotation(); | |
NewRotation.Yaw += 180.0f; | |
this->TextRenderer->SetWorldRotation(NewRotation); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Fill out your copyright notice in the Description page of Project Settings. | |
#pragma once | |
#include "CoreMinimal.h" | |
#include "Components/SceneComponent.h" | |
#include "Runtime/Engine/Classes/Curves/CurveVector.h" | |
#include "EngineUtils.h" | |
#include "CurveManager.generated.h" | |
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) ) | |
class CURVETASK_API UCurveManager : public USceneComponent | |
{ | |
GENERATED_BODY() | |
public: | |
// Sets default values for this component's properties | |
UCurveManager(); | |
protected: | |
// Called when the game starts | |
virtual void BeginPlay() override; | |
AActor* Owner; | |
float Min; | |
float Max; | |
void RotateTextTowards(); | |
TArray<AActor*> ActorsOnWorld; | |
inline void FindAllActors(UWorld* World, TArray<AActor*>& Out) | |
{ | |
for (TActorIterator<AActor> ActorItr(GetWorld()); ActorItr; ++ActorItr) | |
{ | |
AActor* Actor = Cast<AActor>(*ActorItr); | |
if (Actor && !Actor->IsPendingKill()) | |
{ | |
Out.Add(Actor); | |
} | |
} | |
} | |
public: | |
// Called every frame | |
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override; | |
UPROPERTY(EditAnywhere) | |
UCurveVector* VectorCurve; | |
UPROPERTY(EditAnywhere) | |
TArray<USceneComponent*> ChildArray; | |
UPROPERTY(EditAnywhere) | |
float Speed = 10; | |
bool PlayBack; | |
UPROPERTY(EditAnywhere) | |
FColor DebugColor = FColor::Red; | |
UFUNCTION(BlueprintCallable) | |
USceneComponent* AddComponent(USceneComponent* Component) | |
{ | |
ChildArray.Add(Component); | |
return Component; | |
} | |
private: | |
float Time; | |
USceneComponent* RootComponent; | |
class UTextRenderComponent* TextRenderer; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment