Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Fill out your copyright notice in the Description page of Project Settings.
- #include "AandSVR.h"
- #include "ClockworkAnimations.h"
- float UClockworkAnimations::timeRatio = 1.0;
- // Sets default values for this component's properties
- UClockworkAnimations::UClockworkAnimations()
- {
- // 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;
- }
- // Called when the game starts
- void UClockworkAnimations::BeginPlay()
- {
- Super::BeginPlay();
- Init();
- // ...
- }
- // Called every frame
- void UClockworkAnimations::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
- {
- Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
- // ...
- }
- void UClockworkAnimations::SetFrequency(float frequency) {
- this->frequency = frequency;
- }
- void UClockworkAnimations::Init() {
- const UDataTable* data = this->ClockworkAnimationData.DataTable;
- TArray<TArray<FString>>rows = data->GetTableData();
- for (int32 i = 1; i != rows.Num(); ++i) {
- //-----------------------parse data
- TArray<FString> row = rows[i];
- float period = FCString::Atof(*row[3]);
- float opening = FCString::Atof(*row[4]);
- const TCHAR* Delims[] = { TEXT("="), TEXT(","), TEXT(")") };
- TArray<FString> Parsed;
- row[5].ParseIntoArray(Parsed, Delims, 3);
- FVector axis(FCString::Atof(*Parsed[1]), FCString::Atof(*Parsed[3]), FCString::Atof(*Parsed[5]));
- row[6].ParseIntoArray(Parsed, Delims, 3);
- FVector deformationAxis(FCString::Atof(*Parsed[1]), FCString::Atof(*Parsed[3]), FCString::Atof(*Parsed[5]));
- row[7].ParseIntoArray(Parsed, Delims, 3);
- FVector deformFactor(FCString::Atof(*Parsed[1]), FCString::Atof(*Parsed[3]), FCString::Atof(*Parsed[5]));
- //------------------------find reference in scene
- UStaticMeshComponent* staticComponent = nullptr;
- USceneComponent* sceneComponent = nullptr;
- //iterate through all StaticMeshComponents in scene
- for (TObjectIterator<UStaticMeshComponent> Itr; Itr; ++Itr)
- {
- UStaticMeshComponent *Component = *Itr;
- if (row[0].Equals(Itr->GetName())) {
- UE_LOG(LogTemp, Warning, TEXT("Actor name: %s"), *Itr->GetName());
- staticComponent = *Itr;
- }
- }
- if (staticComponent == nullptr) {
- //iterate through all SceneComponents in scene
- for (TObjectIterator<USceneComponent> Itr; Itr; ++Itr)
- {
- USceneComponent *Component = *Itr;
- if (row[0].Equals(Itr->GetName())) {
- UE_LOG(LogTemp, Warning, TEXT("Actor name: %s"), *Itr->GetName());
- sceneComponent = *Itr;
- }
- }
- }
- else {
- sceneComponent = staticComponent;
- }
- //--------------------------populate local array of mobvable pieces
- MovablePiece piece(row[0], row[1], row[2], period, opening, axis, deformationAxis, deformFactor, sceneComponent);
- piece.initStep(this->frequency);
- this->pieces.Add(piece);
- }
- UE_LOG(LogTemp, Warning, TEXT("Number of MovablePieces: %d"), pieces.Num());
- }
- void UClockworkAnimations::AnimateParts() {
- for (MovablePiece piece : this->pieces) {
- if (piece.type.Equals("OC")) {
- OscillatingRotation(piece.sceneComponent, piece.axis, piece.opening, 0);
- }
- else if (piece.type.Equals("RS")) {
- //UE_LOG(LogTemp, Warning, TEXT("This is RS"));
- JerkedRotation(piece);
- }
- else if (piece.type.Equals("AC")) {
- //UE_LOG(LogTemp, Warning, TEXT("This is AC"));
- }
- }
- }
- void UClockworkAnimations::PubOscillatingRotation(USceneComponent* target, FVector axis, float amplitude, float phase) {
- double time = UClockworkAnimations::simulationTime();
- float angle = amplitude*FMath::Sin(2 * PI*this->frequency*time + phase);
- FQuat outQuat(axis, FMath::DegreesToRadians(angle));
- FRotator outRot(outQuat);
- target->SetRelativeRotation(outRot);
- UE_LOG(LogTemp, Warning, TEXT("This is OC"));
- }
- void UClockworkAnimations::OscillatingRotation(USceneComponent* target, FVector axis, float amplitude, float phase) {
- double time = UClockworkAnimations::simulationTime();
- float angle = amplitude*FMath::Sin(2 * PI*frequency*time + phase);
- FQuat outQuat(axis, FMath::DegreesToRadians(angle));
- FRotator outRot(outQuat);
- target->SetRelativeRotation(outRot);
- }
- void UClockworkAnimations::AnchorMovement(USceneComponent* target, FVector axis, float amplitude, float phase) {
- double time = UClockworkAnimations::simulationTime();
- float angle = FMath::Sin(2 * PI*frequency*time + phase);
- if (FMath::IsNearlyEqual(angle, 1.0f, 0.1f)) {
- FQuat outQuat(axis, FMath::DegreesToRadians(amplitude));
- FRotator outRot(outQuat);
- target->SetRelativeRotation(FMath::Lerp(target->RelativeRotation, outRot, amplitude*0.1));
- }
- else if (FMath::IsNearlyEqual(angle, -1.0f, 0.1f)) {
- FQuat outQuat(axis, FMath::DegreesToRadians(-amplitude));
- FRotator outRot(outQuat);
- target->SetRelativeRotation(FMath::Lerp(target->RelativeRotation, outRot, amplitude*0.1));
- }
- }
- void UClockworkAnimations::JerkedRotation(MovablePiece& piece) {
- double time = UClockworkAnimations::simulationTime();
- float impulse = FMath::Sin(time*PI*frequency);
- if (FMath::IsNearlyEqual(impulse, 1.0f, 0.2f) || FMath::IsNearlyEqual(impulse, -1.0f, 0.2f)) {
- FQuat outQuat(piece.axis, FMath::DegreesToRadians(piece.currentStep));
- piece.incrementStep();
- FRotator outRot(outQuat);
- piece.sceneComponent->SetRelativeRotation(FMath::InterpEaseOut(piece.sceneComponent->RelativeRotation, outRot, (FApp::GetDeltaTime() / (1 / frequency)) * UClockworkAnimations::timeRatio, 3.0));
- }
- }
- double UClockworkAnimations::simulationTime()
- {
- FDateTime dtTime = FDateTime::Now();
- double dtCurrent = dtTime.GetHour() * 3600 + dtTime.GetMinute() * 60 + dtTime.GetSecond() + dtTime.GetMillisecond() * 0.001;
- return dtCurrent *UClockworkAnimations::timeRatio;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement