Karma Engine
Loading...
Searching...
No Matches
Actor.h
1#pragma once
2
3#include "krpch.h"
4#include "Object.h"
5
6#include "GameFramework/SceneComponent.h"
7
8namespace Karma
9{
10 class UActorComponent;
11 class ULevel;
12 class APawn;
13 class FTransform;
14 class UWorld;
15 class UChildActorComponent;
16
21 class KARMA_API AActor : public UObject
22 {
23 DECLARE_KARMA_CLASS(AActor, UObject)
24
25 public:
27 AActor();
28
29 private:
35
41 APawn* m_Instigator;
42
46 uint8_t m_bHasFinishedSpawning : 1;
47
52 uint8_t m_bActorInitialized : 1;
53
57 static uint32_t m_BeginPlayCallDepth;
58
59
61 // TWeakObjectPtr in UE
62 std::shared_ptr<UChildActorComponent> m_ParentComponent;
63
65 // TArray<TObjectPtr<UActorComponent>>
67
69 enum class EActorBeginPlayState : uint8_t
70 {
71 HasNotBegunPlay,
72 BeginningPlay,
73 HasBegunPlay,
74 };
75
80 EActorBeginPlayState m_ActorHasBegunPlay : 2;
81
87 uint8_t m_bActorBeginningPlayFromLevelStreaming : 1;
88
92 uint8_t m_bActorWantsDestroyDuringBeginPlay : 1;
93
97 uint8_t m_bAutoDestroyWhenFinished : 1;
98
99 public:
101 ULevel* GetLevel() const;
102
104 void PostSpawnInitialize(FTransform const& SpawnTransform, AActor* InOwner, APawn* InInstigator, bool bRemoteOwned, bool bNoFail, bool bDeferConstruction);
105
107 virtual UWorld* GetWorld() const override final;
108
110 bool HasActorBegunPlay() const { return m_ActorHasBegunPlay == EActorBeginPlayState::HasBegunPlay; }
111
113 bool IsActorBeginningPlay() const { return m_ActorHasBegunPlay == EActorBeginPlayState::BeginningPlay; }
114
116 void DispatchBeginPlay(bool bFromLevelStreaming = false);
117
122 virtual void SetOwner(AActor* NewOwner);
123
125 void FinishSpawning(const FTransform& Transform, bool bIsDefaultTransform = false/*, const FComponentInstanceDataCache* InstanceDataCache = nullptr*/);
126
131 inline bool IsOwnedBy(const AActor* TestOwner) const
132 {
133 for (const AActor* Arg = this; Arg; Arg = Arg->m_Owner)
134 {
135 if (Arg == TestOwner)
136 {
137 return true;
138 }
139 }
140 return false;
141 }
142
144 void PostActorConstruction();
145
152 void GetComponents(KarmaVector<USceneComponent*>& OutComponents) const // make use of smartpointer ?
153 {
154 // We should consider removing this function. It's not really hurting anything by existing but the one above it was added so that
155 // we weren't assuming T*, preventing TObjectPtrs from working for this function. The only downside is all the people who force the
156 // template argument with GetComponents's code suddenly not compiling with no clear error message.
157
158 OutComponents.SmartReset();
159
160 // Our own implementation, different from UE, maybe sync in future
161 typename std::vector<std::shared_ptr<UActorComponent>>::const_iterator iterator = m_OwnedComponents.GetElements().begin();
162
163 USceneComponent* tempSceneComponent;
164
165 while (iterator != m_OwnedComponents.GetElements().end())
166 {
167 tempSceneComponent = static_cast<USceneComponent*>((*iterator).get());
168 if (tempSceneComponent != nullptr)
169 {
170 OutComponents.Add(tempSceneComponent);
171 }
172 }
173
174 /*
175 ForEachComponent_Internal<T>(T::StaticClass(), bIncludeFromChildActors, [&](T* InComp)
176 {
177 OutComponents.Add(InComp);
178 });
179 */
180 }
181
182 void GetComponents(KarmaVector<UActorComponent*>& OutComponents) const // make use of smartpointer ?
183 {
184 // We should consider removing this function. It's not really hurting anything by existing but the one above it was added so that
185 // we weren't assuming T*, preventing TObjectPtrs from working for this function. The only downside is all the people who force the
186 // template argument with GetComponents's code suddenly not compiling with no clear error message.
187
188 OutComponents.SmartReset();
189
190 // Our own implementation, different from UE, maybe sync in future
191 typename std::vector<std::shared_ptr<UActorComponent>>::const_iterator iterator = m_OwnedComponents.GetElements().begin();
192
193 USceneComponent* tempSceneComponent;
194
195 while (iterator != m_OwnedComponents.GetElements().end())
196 {
197 tempSceneComponent = static_cast<USceneComponent*>((*iterator).get());
198 if (tempSceneComponent != nullptr)
199 {
200 OutComponents.Add(tempSceneComponent);
201 }
202 }
203 }
204
206 void SetInstigator(APawn* InInstigator);
207
209 FORCEINLINE USceneComponent* GetRootComponent() const { return m_RootComponent; }
210
215 bool SetRootComponent(USceneComponent* NewRootComponent);
216
218 static void DispatchOnComponentsCreated(AActor* NewActor);
219
225 {
226 return ActorToWorld();
227 }
228
230 inline const FTransform& ActorToWorld() const
231 {
232 return (m_RootComponent ? m_RootComponent->GetComponentTransform() : FTransform::m_Identity);
233 }
234
240 void InitializeComponents();
241
248 virtual void PostInitializeComponents();
249
255 AActor* GetParentActor() const;
256
258 UChildActorComponent* GetParentComponent() const;
259
264 void RemoveOwnedComponent(std::shared_ptr<UActorComponent> Component);
265
266 bool GetAutoDestroyWhenFinished() const { return m_bAutoDestroyWhenFinished; }
267
268 static USceneComponent* FixupNativeActorComponents(AActor* Actor);
269
270 bool CanTick() const { return m_bCanEverTick; }
271 void DisableTick(bool bDisable) { m_bCanEverTick = !bDisable; }
272
279 virtual void Tick(float DeltaSeconds);
280
281 public:
287
292 AActor* m_Owner;// UE uses smart pointer
293
299
300 protected:
304 virtual void BeginPlay();
305
310 //void ReceiveBeginPlay();
311
312 protected:
320
321 bool m_bCanEverTick;
322 };
323}
This file contains the class UObject along with helper functions.
Karma's std::vector wrapper.
Definition KarmaTypes.h:128
Definition Actor.h:22
USceneComponent * m_RootComponent
Definition Actor.h:319
const FTransform & GetTransform() const
Definition Actor.h:224
KarmaVector< AActor * > m_Children
Definition Actor.h:298
bool IsOwnedBy(const AActor *TestOwner) const
Definition Actor.h:131
AActor * m_Owner
Definition Actor.h:292
bool HasActorBegunPlay() const
Definition Actor.h:110
const FTransform & ActorToWorld() const
Definition Actor.h:230
void GetComponents(KarmaVector< USceneComponent * > &OutComponents) const
Definition Actor.h:152
float m_CreationTime
Definition Actor.h:286
FORCEINLINE USceneComponent * GetRootComponent() const
Definition Actor.h:209
bool IsActorBeginningPlay() const
Definition Actor.h:113
Definition Transform.h:107
Definition ChildActorComponent.h:14
Definition Level.h:21
The base class of all the game code relevant objects.
Definition Object.h:106
Definition SceneComponent.h:34
Definition World.h:108