Advertisement
Guest User

UpdateWorldResourceNodes

a guest
Nov 15th, 2024
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.35 KB | None | 0 0
  1. /// For now, this destroys any meshes that aren't explicity marked as our randomized meshes
  2. /// It also updates the locations of resource nodes based on raycasting if they haven't
  3. /// been raycast before
  4. /// Destroying any vanilla meshes on udpate may not be necessary, but requires more playtesting
  5. /// @param World
  6. void UResourceRouletteManager::UpdateWorldResourceNodes(const UWorld* World) const
  7. {
  8.     if (!World)
  9.     {
  10.         FResourceRouletteUtilityLog::Get().LogMessage("UpdateWorldResourceNodes aborted: World is invalid.",ELogLevel::Error);
  11.         return;
  12.     }
  13.  
  14.     if (!(bIsResourcesScanned && bIsResourcesRandomized && bIsResourcesSpawned))
  15.     {
  16.         FResourceRouletteUtilityLog::Get().LogMessage("Updating Skipped.", ELogLevel::Debug);
  17.         return;
  18.     }
  19.  
  20.     FVector PlayerLocation;
  21.     APlayerController* PlayerController = UGameplayStatics::GetPlayerController(World, 0);
  22.     if (PlayerController && PlayerController->GetPawn())
  23.     {
  24.         PlayerLocation = PlayerController->GetPawn()->GetActorLocation();
  25.     }
  26.     else
  27.     {
  28.         return;
  29.     }
  30.  
  31.     const float UpdateRadius = 150000.0f;
  32.  
  33.     AResourceRouletteSubsystem* ResourceRouletteSubsystem = AResourceRouletteSubsystem::Get(World);
  34.     if (!ResourceRouletteSubsystem)
  35.     {
  36.         FResourceRouletteUtilityLog::Get().LogMessage("ResourceRouletteSubsystem is invalid.", ELogLevel::Error);
  37.         return;
  38.     }
  39.    
  40.     TArray<FResourceNodeData> ProcessedNodes = ResourceRouletteSubsystem->GetSessionRandomziedResourceNodes();
  41.  
  42.     bool bNodeUpdated = false;
  43.     for (FResourceNodeData& NodeData : ProcessedNodes)
  44.     {
  45.         if (!NodeData.IsRayCasted && FVector::Dist(NodeData.Location, PlayerLocation) <= UpdateRadius)
  46.         {
  47.             UResourceRouletteUtility::CalculateLocationAndRotationForNode(NodeData, World);
  48.             bNodeUpdated = true;
  49.            
  50.             if (AFGResourceNode** ResourceNodePtr = ResourceNodeSpawner->GetSpawnedResourceNodes().Find(NodeData.NodeGUID))
  51.             {
  52.                 AFGResourceNode* ResourceNode = *ResourceNodePtr;
  53.                 ResourceNode->SetActorLocation(NodeData.Location, false, nullptr, ETeleportType::TeleportPhysics);
  54.                 ResourceNode->SetActorRotation(NodeData.Rotation, ETeleportType::TeleportPhysics);
  55.                
  56.                 if (UStaticMeshComponent* MeshComponent = ResourceNode->FindComponentByClass<UStaticMeshComponent>())
  57.                 {
  58.                     FResourceRouletteUtilityLog::Get().LogMessage(FString::Printf(TEXT("Updating MeshComponent Location to: %s, Rotation to: %s"),
  59.                         *NodeData.Location.ToString(), *NodeData.Rotation.ToString()),  ELogLevel::Debug);
  60.                     MeshComponent->SetWorldLocation(NodeData.Location, false, nullptr, ETeleportType::TeleportPhysics);
  61.                     MeshComponent->SetWorldRotation(NodeData.Rotation, false, nullptr, ETeleportType::TeleportPhysics);
  62.                 }
  63.             }
  64.         }
  65.     }
  66.     if (bNodeUpdated)
  67.     {
  68.         ResourceRouletteSubsystem->SetSessionRandomizedResourceNodes(ProcessedNodes);
  69.     }
  70.    
  71.     for (TObjectIterator<UStaticMeshComponent> It; It; ++It)
  72.     {
  73.         UStaticMeshComponent* StaticMeshComponent = *It;
  74.         if (StaticMeshComponent && StaticMeshComponent->GetWorld() == World && !StaticMeshComponent->ComponentTags.
  75.             Contains(ResourceRouletteTag))
  76.         {
  77.             if (const UStaticMesh* StaticMesh = StaticMeshComponent->GetStaticMesh())
  78.             {
  79.                 FName MeshPath = FName(*StaticMesh->GetPathName());
  80.  
  81.                 if (MeshesToDestroy.Contains(MeshPath))
  82.                 {
  83.                     StaticMeshComponent->SetActive(false);
  84.                     StaticMeshComponent->SetVisibility(false);
  85.                     StaticMeshComponent->DestroyComponent();
  86.                 }
  87.             }
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement