Advertisement
BorrowTheProgrammer

FloatingActor_cpp

Jun 12th, 2022
1,102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.08 KB | None | 0 0
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2.  
  3.  
  4. #include "FloatingActor.h"
  5.  
  6. #include "Components/LineBatchComponent.h"
  7. #define MAX_LABEL 100000
  8.  
  9. // Sets default values
  10. AFloatingActor::AFloatingActor()
  11. {
  12.     // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
  13.     PrimaryActorTick.bCanEverTick = true;
  14.     ActorCanBeMoved = false;
  15.     CurrentVertex = 0;
  16.     VisualMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
  17.     VisualMesh->SetupAttachment(RootComponent);
  18.     LineBatch = CreateDefaultSubobject<ULineBatchComponent>(TEXT("ActorLine"));
  19.     LineBatch->SetupAttachment(RootComponent);
  20.    
  21.     static ConstructorHelpers::FObjectFinder<UStaticMesh> CubeVisualAsset(TEXT("StaticMesh'/Engine/BasicShapes/Sphere.Sphere'"));
  22.  
  23.     if (CubeVisualAsset.Succeeded())
  24.     {
  25.         VisualMesh->SetStaticMesh(CubeVisualAsset.Object);
  26.         VisualMesh->SetRelativeLocation(FVector(0.0f, 0.0f, 0.0f));
  27.     }
  28.  
  29. }
  30.  
  31. // Called when the game starts or when spawned
  32. void AFloatingActor::BeginPlay()
  33. {
  34.     Super::BeginPlay();
  35.    
  36. }
  37.  
  38. // Called every frame
  39. void AFloatingActor::Tick(float DeltaTime)
  40. {
  41.     Super::Tick(DeltaTime);
  42.     if(!ActorCanBeMoved && CurrentVertex == 0)
  43.     {
  44.         CreateActorPath();
  45.     } else
  46.     {
  47.         FVector Direction = NextActorPosition - CurrentActorPosition;
  48.         Direction.Normalize();
  49.         FVector NewActorPosition = GetActorLocation() + Direction * ActorSpeed * DeltaTime;
  50.         SetActorLocation(NewActorPosition);
  51.        
  52.         if (FVector::DistSquared(NewActorPosition, CurrentActorPosition) > FVector::DistSquared(NextActorPosition,
  53.             CurrentActorPosition))
  54.         {
  55.             SetActorLocation(NextActorPosition);
  56.        
  57.             if(CurrentVertex == ActorShortPath.size() - 2)
  58.             {
  59.                 ActorCanBeMoved = false;
  60.                 LineBatch->Flush();
  61.                 CurrentVertex = 0;
  62.                 ActorShortPath.clear();
  63.                 StartPoint = FMath::RandRange(0, ActorPoints.size() - 1);
  64.                 do
  65.                 {
  66.                     EndPoint = FMath::RandRange(0, ActorPoints.size() - 1);
  67.                 } while (StartPoint == EndPoint);
  68.                 ActorShortPath = getShortPathWhenFinished(StartPoint, EndPoint);
  69.             }
  70.             else
  71.             {
  72.                 CurrentVertex++;
  73.                 CurrentActorPosition = ActorPoints[ActorShortPath[CurrentVertex]];
  74.                 NextActorPosition = ActorPoints[ActorShortPath[CurrentVertex + 1]];
  75.             }
  76.            
  77.         }
  78.     }
  79. }
  80.  
  81. void AFloatingActor::SetStartPoint(const int PointIndex)
  82. {
  83.     this->StartPoint = PointIndex;
  84. }
  85.  
  86. void AFloatingActor::SetEndPoint(const int PointIndex)
  87. {
  88.     this->EndPoint = PointIndex;
  89. }
  90.  
  91. void AFloatingActor::SetActorShortPath(const std::vector<int> ShortPath)
  92. {
  93.     this->ActorShortPath = ShortPath;
  94. }
  95.  
  96. void AFloatingActor::SetActorSpeed(const float Speed)
  97. {
  98.     this->ActorSpeed = Speed;
  99. }
  100.  
  101. void AFloatingActor::CreateActorPath()
  102. {
  103.     ActorCanBeMoved = true;
  104.     CurrentActorPosition = ActorPoints[ActorShortPath[CurrentVertex]];
  105.     NextActorPosition = ActorPoints[ActorShortPath[CurrentVertex + 1]];
  106.     //NextActorPosition = FVector(0.0f);
  107.    
  108.     for(int i = 0; i < ActorShortPath.size() - 1; ++i)
  109.     {
  110.         LineBatch->DrawLine(ActorPoints[ActorShortPath[i]], ActorPoints[ActorShortPath[i + 1]],
  111.         ActorPathColor, 10, 10);
  112.     }
  113. }
  114.  
  115. void AFloatingActor::SetPoints(std::vector<FVector> GraphPoints)
  116. {
  117.     this->ActorPoints = GraphPoints;
  118. }
  119.  
  120. std::vector<int> AFloatingActor::getShortPathWhenFinished(int start, int end)
  121. {
  122.     int N = CopiedMatrix.size();
  123.  
  124.     std::vector<float> vertex_labels;
  125.     std::vector<bool> vertex_visited;
  126.     std::vector<int> found_path;
  127.     for (int i = 0; i < N; ++i) {
  128.         vertex_labels.push_back(MAX_LABEL);
  129.         vertex_visited.push_back(false);
  130.         found_path.push_back(start);
  131.     }
  132.  
  133.     vertex_labels[start] = 0;
  134.  
  135.     int current_vertex = start;
  136.     do {
  137.         // поиск соседа-вершины с минимальным расстоянием
  138.         for (int i = 0; i < N; ++i) {
  139.             // расстояние от текущей вершины до вершины i
  140.             float h = (CopiedMatrix[current_vertex])[i];
  141.             // если расстояние = 0, то пути нет
  142.             if (h == 0) continue;
  143.             // если расстояние не 0, то проверка на текущую метку вершины5
  144.             if (h + vertex_labels[current_vertex] < vertex_labels[i]) {
  145.                 vertex_labels[i] = h + vertex_labels[current_vertex];
  146.                 found_path[i] = current_vertex;
  147.             }
  148.         }
  149.         vertex_visited[current_vertex] = true;  // отмечаем вершину как посещенную
  150.  
  151.         // ищем соседа с минимальным расстоянием и заменяем индекс
  152.         float minimal_pathsum;
  153.         float min_index = vertex_labels.size() + 1;
  154.  
  155.         for (int i = 0; i < (int)vertex_labels.size(); ++i) {
  156.             if ((!vertex_visited[i]) && (vertex_labels[i] < MAX_LABEL)) {
  157.                 minimal_pathsum = vertex_labels[i];
  158.                 min_index = i;
  159.             }
  160.         }
  161.         current_vertex = min_index;
  162.     } while (current_vertex != vertex_labels.size() + 1);
  163.  
  164.     // кратчайший путь
  165.     std::vector<int> shortPath;
  166.     // индексация от конца к началу
  167.     int index = end;
  168.     while (index != start) {
  169.         shortPath.push_back(index);
  170.         index = found_path[index];
  171.     }
  172.     shortPath.push_back(start);
  173.  
  174.     return shortPath;
  175. }
  176.  
  177.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement