Agrarsense
InstancedActor.h
Go to the documentation of this file.
1// Copyright (c) 2023 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#pragma once
7
8#include "CoreMinimal.h"
9#include "GameFramework/Actor.h"
10#include "Engine/EngineTypes.h"
11#include "Engine/StaticMesh.h"
12#include "Components/StaticMeshComponent.h"
13#include "Internationalization/Text.h"
14
18
19#include "InstancedActor.generated.h"
20
36UCLASS()
37class AGRARSENSE_API AInstancedActor : public AActor, public IInteractable, public IDestroyable, public IActorInformation
38{
39 GENERATED_BODY()
40
41public:
42
43 AInstancedActor(const FObjectInitializer& ObjectInitializer);
44
45 virtual void BeginPlay() override;
46
47 virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
48
53 UFUNCTION(BlueprintCallable)
54 bool AddToInstancedRendering();
55
60 UFUNCTION(BlueprintCallable)
61 void RemoveFromInstancedRendering(bool EnableStaticMeshComponent = true);
62
66 UFUNCTION(BlueprintCallable)
67 void UpdateTransformPosition();
68
73 void InstanceAdded(int32 CompIndex, int32 InstanceNum);
74
79 void UpdateIndex(int32 UpdatedIndex);
80
85 UFUNCTION(BlueprintCallable)
86 void SetTypeAndModel(const FString& Type, const FString& Model)
87 {
88 TypeID = Type;
89 ModelID = Model;
90 }
91
92 /*
93 * Get whether this actor is a tree.
94 */
95 UFUNCTION(BlueprintCallable)
96 bool IsTreeActor() const;
97
102 void UpdatePreviousTransform(const FTransform& NewTransform)
103 {
104 PreviousTransform = NewTransform;
105 }
106
111 UFUNCTION(BlueprintPure)
112 bool IsAddedToInstancedRendering() const
113 {
114 return AddedToInstancedRenderer;
115 }
116
117 UFUNCTION(BlueprintPure)
118 UStaticMeshComponent* GetStaticMeshComponent() const
119 {
120 return StaticMeshComponent;
121 }
122
123 UFUNCTION(BlueprintPure)
124 int GetInstanceStartCullDistance() const
125 {
126 return InstanceStartCullDistanceInCm;
127 }
128
129 UFUNCTION(BlueprintPure)
130 int GetInstanceEndCullDistance() const
131 {
132 return InstanceEndCullDistanceInCm;
133 }
134
135 UFUNCTION(BlueprintPure)
136 bool UpdateTransformAutomatically() const
137 {
138 return ShouldUpdateTransformAutomatically;
139 }
140
141 UFUNCTION(BlueprintCallable)
142 FString GetTypeIDString() const
143 {
144 return TypeID;
145 }
146
147 UFUNCTION(BlueprintCallable)
148 FString GetModelIDString() const
149 {
150 return ModelID;
151 }
152
154 {
155 return ComponentIndex;
156 }
157
159 {
160 return InstanceNumber;
161 }
162
164 {
165 return AlternativeMaterial;
166 }
167
169 {
170 return CustomDepthSetup;
171 }
172
174 {
175 return AllowWorldPositionOffsetDisable;
176 }
177
178 FTransform GetPreviousTransform() const
179 {
180 return PreviousTransform;
181 }
182
183 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "AInstancedActor")
184 UStaticMeshComponent* StaticMeshComponent = nullptr;
185
186 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "AInstancedActor")
187 bool AllowWorldPositionOffsetDisable = false;
188
189 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "AInstancedActor")
190 bool AddToInstancedRenderer = true;
191
192 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "AInstancedActor")
193 int InstanceStartCullDistanceInCm = 45000;
194
195 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "AInstancedActor")
196 int InstanceEndCullDistanceInCm = 46500;
197
198 // Should this actor transform be automatically updated (more expensive)
199 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "AInstancedActor")
200 bool ShouldUpdateTransformAutomatically = false;
201
202 // If we have multiple actors with same mesh but with different material(s), this should be set to true!
203 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "AInstancedActor")
204 bool AlternativeMaterial = false;
205
206 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "AInstancedActor")
207 FText InteractableName;
208
209 virtual FText GetInteractableName_Implementation() const override
210 {
211 return InteractableName;
212 }
213
214 virtual FTransform GetInteractableWorldTransform_Implementation() const override
215 {
216 return GetActorTransform();
217 }
218
219 virtual FString GetActorID_Implementation() const override
220 {
221 return ActorID;
222 }
223
224 virtual FString GetActorName_Implementation() const override
225 {
226 return ActorName;
227 }
228
229 virtual void SetActorName_Implementation(const FString& NewActorName) override
230 {
231 ActorName = NewActorName;
232 }
233
234 virtual void SetActorIDAndName_Implementation(const FString& NewActorName, const FString& NewID) override
235 {
236 ActorName = NewActorName;
237 ActorID = NewID;
238 SetAndValidateActorIDAndName(ActorName, ActorID, TWeakObjectPtr<AActor>(this));
239 }
240
241private:
242
243 UPROPERTY(VisibleAnywhere)
244 FString ActorName;
245
246 UPROPERTY(VisibleAnywhere)
247 FString ActorID;
248
249 FTransform PreviousTransform;
250
251 int32 ComponentIndex = 9999999;
252
253 int32 InstanceNumber = 9999999;
254
255 bool AddedToInstancedRenderer = false;
256
257 FString TypeID = "none";
258
259 FString ModelID = "none";
260
261 bool CustomDepthSetup = false;
262
263};
int GetInstanceIndex() const
virtual void SetActorIDAndName_Implementation(const FString &NewActorName, const FString &NewID) override
FTransform GetPreviousTransform() const
bool HasAlternativeMaterial() const
int GetComponentIndex() const
virtual void SetActorName_Implementation(const FString &NewActorName) override
virtual FString GetActorID_Implementation() const override
virtual FTransform GetInteractableWorldTransform_Implementation() const override
void UpdatePreviousTransform(const FTransform &NewTransform)
bool IsCustomDepthSetup() const
bool GetAllowWorldPositionOffsetDisable() const
virtual FString GetActorName_Implementation() const override