-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathXmsEntityBuilderComponent.cpp
More file actions
102 lines (80 loc) · 3.11 KB
/
XmsEntityBuilderComponent.cpp
File metadata and controls
102 lines (80 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright (c) 2025 Xist.GG LLC
#include "XmsEntityBuilderComponent.h"
#include "MassEntityBuilder.h"
#include "MassEntityManager.h"
#include "MassEntityUtils.h"
#include "XmsLog.h"
#include "EntityRegistry/XmsEntityRegistry.h"
#include "GameFramework/Actor.h"
#include "Common/XmsFragments.h"
#include "Representation/XmsRepSubsystem.h"
// Set Class Defaults
UXmsEntityBuilderComponent::UXmsEntityBuilderComponent(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
PrimaryComponentTick.bCanEverTick = true;
PrimaryComponentTick.bStartWithTickEnabled = true;
EntityMetaType = EXmsEntityMetaType::Wisp;
EntityLifespan = FXmsF_Lifespan {
.MaxAge = 4.,
};
bAutoBuildEnabled = true;
AutoBuildIntervalSeconds = 0.2;
}
void UXmsEntityBuilderComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
// If it's time to auto-build a new Entity, then do it now.
TimeToNextAutoBuild -= DeltaTime;
if (IsAutoBuildEnabled()
&& TimeToNextAutoBuild <= 0.)
{
BuildEntity();
TimeToNextAutoBuild = AutoBuildIntervalSeconds;
}
}
FMassEntityHandle UXmsEntityBuilderComponent::BuildEntity()
{
UE_VLOG_UELOG(this, LogXmsBuilder, Verbose, TEXT("%hs: %s: Building Entity"),
__FUNCTION__, *GetClass()->GetName());
const UWorld* World = GetWorld();
check(World);
FMassEntityManager& EntityManager = UE::Mass::Utils::GetEntityManagerChecked(*World);
UE::Mass::FEntityBuilder Builder = EntityManager.MakeEntityBuilder();
// Set up Entity debug identification so we know which child class created any given Entity
FMassArchetypeCreationParams ArchetypeCreationParams;
ArchetypeCreationParams.DebugName = GetClass()->GetFName();
Builder.ConfigureArchetypeCreation(ArchetypeCreationParams);
// Allow child classes to customize Builder setup
SetupEntityBuilder(OUT Builder);
const FMassEntityHandle Entity = Builder.Commit();
return Entity;
}
void UXmsEntityBuilderComponent::SetupEntityBuilder(UE::Mass::FEntityBuilder& Builder)
{
const AActor* OwnerActor = GetOwner();
check(OwnerActor);
// Set up Entity MetaData fragment
FXmsCSF_MetaData MetaData {
.MetaType = EntityMetaType,
};
// Warn if/when the MetaData is invalid
UE_CVLOG_UELOG(not MetaData.IsValid(), this, LogXmsBuilder, Warning,
TEXT("%hs: %s: Invalid MetaData for reserved Entity [%s]"),
__FUNCTION__, *GetClass()->GetName(), *Builder.GetEntityHandle().DebugGetDescription());
// Spawn the Entity with a copy of the owner actor's current transform
FXmsF_Transform Transform {
.Location = OwnerActor->GetActorLocation(),
.Rotation = OwnerActor->GetActorRotation(),
.Scale3D = OwnerActor->GetActorScale(),
};
// Configure Builder
Builder.Add<FXmsCSF_MetaData>(MetaData);
Builder.Add<FXmsF_Lifespan>(EntityLifespan);
Builder.Add<FXmsF_Transform>(Transform);
// For now, tag all Entities as being visible
Builder.Add<FXmsT_Represent>();
// Mass Observers apparently cannot observe Const Shared Fragments for created Entities,
// so we will use this Tag for Registry Observer compatibility.
Builder.Add<FXmsT_Registry>();
}