Agrarsense
DeletionVolume.cpp
Go to the documentation of this file.
1// Copyright (c) 2024 FrostBit Software Lab at the Lapland University of Applied Sciences
2//
3// This work is licensed under the terms of the MIT license.
4// For a copy, see <https://opensource.org/licenses/MIT>.
5
6#include "DeletionVolume.h"
7
10
11#include "Kismet/GameplayStatics.h"
12
14{
15 PrimaryActorTick.bCanEverTick = false;
16
17 CollisionBox = CreateDefaultSubobject<UBoxComponent>(TEXT("CollisionBox"));
18 CollisionBox->InitBoxExtent(FVector(200.0f, 200.0f, 200.0f));
19 RootComponent = CollisionBox;
20
21 VisualizerComponent = CreateDefaultSubobject<UBoundsVisualizerComponent>(TEXT("BoundsVisualizerComponent"));
22 VisualizerComponent->SetCollisionEnabled(ECollisionEnabled::NoCollision);
23 VisualizerComponent->SetGenerateOverlapEvents(false);
24 VisualizerComponent->SetupAttachment(RootComponent);
25 VisualizerComponent->SetVisibility(true);
26}
27
29{
30 Super::BeginPlay();
31
33 {
34 VisualizerComponent->SetCubeMesh();
35 VisualizerComponent->SetVisibility(true);
36 }
37}
38
39void ADeletionVolume::EndPlay(const EEndPlayReason::Type EndPlayReason)
40{
41 Super::EndPlay(EndPlayReason);
42
43 if (CollisionBox)
44 {
45 CollisionBox->DestroyComponent();
46 CollisionBox = nullptr;
47 }
48
50 {
51 VisualizerComponent->DestroyComponent();
52 VisualizerComponent = nullptr;
53 }
54}
55
57{
58 if (IsValid(this))
59 {
60 if (CollisionBox)
61 {
62 CollisionBox->SetBoxExtent(Bounds);
63 }
64
66 {
67 VisualizerComponent->SetRelativeScale3D(Bounds);
68 }
69 }
70}
71
73{
74 if (!IsValid(this) || !CollisionBox)
75 {
76 return;
77 }
78
79 TArray<FString> Logs;
80
81 TArray<AActor*> OverlappingActors;
82 CollisionBox->GetOverlappingActors(OverlappingActors);
83
84 for (AActor* Actor : OverlappingActors)
85 {
86 // Only delete InstancedActor types
87 AInstancedActor* InstancedActor = Cast<AInstancedActor>(Actor);
88
89 if (InstancedActor && (!OnlyTrees || InstancedActor->IsTreeActor()))
90 {
91 InstancedActor->Destroy();
92
93 FVector ActorLocation = InstancedActor->GetActorLocation();
94 FString Msg = FString::Printf(TEXT("Removed actor from position: X=%f, Y=%f, Z=%f"), ActorLocation.X, ActorLocation.Y, ActorLocation.Z);
95 Logs.Add(Msg);
96 }
97 }
98
99 for (const FString& Msg : Logs)
100 {
102 }
103
104 // Destroy this actor after deleting overlapping actors inside the bounds.
105 this->Destroy();
106}
void DestroyOverlappingActors(bool OnlyTrees)
UBoxComponent * CollisionBox
UBoundsVisualizerComponent * VisualizerComponent
void ChangeOverlapBounds(FVector Bounds)
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override
virtual void BeginPlay() override
bool IsTreeActor() const
static void Log(const FString &Message, bool LogToTextFile=true, bool LogToROS=true)