-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLyraEquipmentInstance.cpp
More file actions
150 lines (128 loc) · 4.16 KB
/
LyraEquipmentInstance.cpp
File metadata and controls
150 lines (128 loc) · 4.16 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright Epic Games, Inc. All Rights Reserved.
#include "LyraEquipmentInstance.h"
#include "Character/LyraCharacter.h" // @TangoAlphaDev - Include the LyraCharacter header
#include "Components/SkeletalMeshComponent.h"
#include "GameFramework/Character.h"
#include "LyraEquipmentDefinition.h"
#include "Net/UnrealNetwork.h"
#if UE_WITH_IRIS
#include "Iris/ReplicationSystem/ReplicationFragmentUtil.h"
#endif // UE_WITH_IRIS
#include UE_INLINE_GENERATED_CPP_BY_NAME(LyraEquipmentInstance)
class FLifetimeProperty;
class UClass;
class USceneComponent;
ULyraEquipmentInstance::ULyraEquipmentInstance(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
}
UWorld* ULyraEquipmentInstance::GetWorld() const
{
if (APawn* OwningPawn = GetPawn())
{
return OwningPawn->GetWorld();
}
else
{
return nullptr;
}
}
void ULyraEquipmentInstance::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(ThisClass, Instigator);
DOREPLIFETIME(ThisClass, SpawnedActors);
}
#if UE_WITH_IRIS
void ULyraEquipmentInstance::RegisterReplicationFragments(UE::Net::FFragmentRegistrationContext& Context, UE::Net::EFragmentRegistrationFlags RegistrationFlags)
{
using namespace UE::Net;
Super::RegisterReplicationFragments(Context, RegistrationFlags);
// Build descriptors and allocate PropertyReplicationFragments for this object
FReplicationFragmentUtil::CreateAndRegisterFragmentsForObject(this, Context, RegistrationFlags);
}
#endif // UE_WITH_IRIS
APawn* ULyraEquipmentInstance::GetPawn() const
{
return Cast<APawn>(GetOuter());
}
APawn* ULyraEquipmentInstance::GetTypedPawn(TSubclassOf<APawn> PawnType) const
{
APawn* Result = nullptr;
if (UClass* ActualPawnType = PawnType)
{
if (GetOuter()->IsA(ActualPawnType))
{
Result = Cast<APawn>(GetOuter());
}
}
return Result;
}
void ULyraEquipmentInstance::SpawnEquipmentActors(const TArray<FLyraEquipmentActorToSpawn>& ActorsToSpawn)
{
if (APawn* OwningPawn = GetPawn())
{
USceneComponent* AttachTarget = OwningPawn->GetRootComponent();
if (ACharacter* Char = Cast<ACharacter>(OwningPawn))
{
AttachTarget = Char->GetMesh();
}
for (const FLyraEquipmentActorToSpawn& SpawnInfo : ActorsToSpawn)
{
AActor* NewActor = GetWorld()->SpawnActorDeferred<AActor>(SpawnInfo.ActorToSpawn, FTransform::Identity, OwningPawn);
NewActor->FinishSpawning(FTransform::Identity, /*bIsDefaultTransform=*/ true);
// @TangoAlphaDev
if (ACharacter* Char = Cast<ACharacter>(OwningPawn))
{
if (ALyraCharacter* LyraChar = Cast<ALyraCharacter>(Char))
{
if (LyraChar->isLeftCamera) // Choose the socket based on isLeftCamera bool
{
NewActor->SetActorRelativeTransform(SpawnInfo.LeftAttachTransform);
NewActor->AttachToComponent(AttachTarget, FAttachmentTransformRules::KeepRelativeTransform, SpawnInfo.LeftAttachSocket);
}
else
{
NewActor->SetActorRelativeTransform(SpawnInfo.AttachTransform);
NewActor->AttachToComponent(AttachTarget, FAttachmentTransformRules::KeepRelativeTransform, SpawnInfo.AttachSocket);
}
}
else
{
// Default attachment in case LyraCharacter cast fails
NewActor->SetActorRelativeTransform(SpawnInfo.AttachTransform);
NewActor->AttachToComponent(AttachTarget, FAttachmentTransformRules::KeepRelativeTransform, SpawnInfo.AttachSocket);
}
}
else
{
// Default attachment in case ACharacter cast fails
NewActor->SetActorRelativeTransform(SpawnInfo.AttachTransform);
NewActor->AttachToComponent(AttachTarget, FAttachmentTransformRules::KeepRelativeTransform, SpawnInfo.AttachSocket);
}
// @TangoAlphaDev
SpawnedActors.Add(NewActor);
}
}
}
void ULyraEquipmentInstance::DestroyEquipmentActors()
{
for (AActor* Actor : SpawnedActors)
{
if (Actor)
{
Actor->Destroy();
}
}
}
void ULyraEquipmentInstance::OnEquipped()
{
K2_OnEquipped();
}
void ULyraEquipmentInstance::OnUnequipped()
{
K2_OnUnequipped();
}
void ULyraEquipmentInstance::OnRep_Instigator()
{
}