LEAP Documentation 40220
Documentation for the LEAP project
SquadComponent.h
Go to the documentation of this file.
1// Copyright Blue Isle Studios Inc 2018. All Rights Reserved.
2
3#pragma once
4
5#include "CoreMinimal.h"
6#include "Components/ActorComponent.h"
7#include "SquadComponent.generated.h"
8
9class APlayerController;
11class AProjectXPlayerStateTeam;
12
13USTRUCT(BlueprintType)
15{
16 GENERATED_BODY()
17
19 {
20 SquadID = INDEX_NONE;
21 }
22
23 FSquadInstance(uint8 InTeam)
24 {
25 Team = InTeam;
26 SquadID = SquadIDCounter;
27 SquadIDCounter++;
28 }
29
30 bool operator==(const FSquadInstance& Other) const
31 {
32 return SquadID != INDEX_NONE && SquadID == Other.SquadID;
33 }
34
35 bool operator!() const
36 {
37 return SquadID == INDEX_NONE;
38 }
39
40 operator bool()
41 {
42 return SquadID != INDEX_NONE;
43 }
44
45 //Returns true if this squad is now empty.
46 bool Cleanup();
47 bool HasAlivePlayers();
48 bool IsInvalid();
49 bool CanJoin(AProjectXPlayerState* Player);
50 void ForceOnRepUpdate();
51 void AddPlayer(AProjectXPlayerState* NewPlayer);
52 void RemovePlayer(AProjectXPlayerState* Player);
53 bool IsEmpty();
54
55public:
56 UPROPERTY()
57 int32 SquadID = INDEX_NONE;
58
59 UPROPERTY()
60 uint8 Team = MAX_uint8;
61
62 UPROPERTY()
63 TArray<TWeakObjectPtr<AProjectXPlayerState>> PlayerList = TArray<TWeakObjectPtr<AProjectXPlayerState>>();
64
65 static int32 SquadIDCounter;
66
67 UPROPERTY()
68 uint8 UpdateCounter = 0;
69};
70
71DECLARE_DYNAMIC_MULTICAST_DELEGATE(FSquadListUpdateSignature);
72DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FSquadUpdateSignature, int32, SquadID);
73DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FSquadMemberUpdateSignature, int32, SquadID, AProjectXPlayerState*, PlayerState);
74
75UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
76class PROJECTX_API USquadComponent : public UActorComponent
77{
78 GENERATED_UCLASS_BODY()
79
80//~ Begin UActorComponent Interface
81protected:
82 virtual void BeginPlay() override;
83//~ End UActorComponent Interface
84
85public:
86 UFUNCTION(BlueprintCallable, Category = SquadComponent)
87 FORCEINLINE int32 GetSquadCount() const { return SquadCount; }
88 UFUNCTION(BlueprintCallable, Category = SquadComponent)
89 FORCEINLINE int32 GetMaxSquadSize() const { return MaxSquadSize; }
90 UFUNCTION(BlueprintCallable, Category = SquadComponent)
91 void GetSquadList(TArray<FSquadInstance>& OutSquadList) const;
92 UFUNCTION(BlueprintCallable, Category = SquadComponent)
93 void GetSquadListForTeam(TArray<FSquadInstance>& OutSquadList, uint8 Team) const;
94 UFUNCTION(BlueprintPure, Category = SquadComponent, meta = (DisplayName = "Get Squad By ID", ScriptName = "GetSquadByID"))
95 void K2_GetSquadByID(int32 SquadID, FSquadInstance& Squad);
96 UFUNCTION(BlueprintPure, Category = SquadComponent, meta = (DisplayName = "Get Squad By Player", ScriptName = "GetSquadByPlayer"))
97 bool K2_GetSquadByPlayer(AProjectXPlayerState* Player, FSquadInstance& Squad);
98 UFUNCTION(BlueprintPure, Category = SquadComponent)
99 virtual bool IsInSameSquad(const AProjectXPlayerState* PlayerStateA, const AProjectXPlayerState* PlayerStateB);
100
101 virtual void AutoAssignSquad(AProjectXPlayerState* Player);
102
103 UFUNCTION(BlueprintCallable, Category = SquadComponent)
104 virtual bool JoinSquad(AProjectXPlayerState* Player, int32 SquadID);
105 UFUNCTION(BlueprintCallable, Category = SquadComponent, meta = (DisplayName = "Can Join Squad", ScriptName = "CanJoinSquad"))
106 bool K2_CanJoinSquad(AProjectXPlayerState* Player, int32 SquadID);
107
108 virtual bool CanJoinSquad(AProjectXPlayerState* Player, FSquadInstance* Squad) const;
109
110 virtual void LeaveSquad(AProjectXPlayerState* Player);
111
112 virtual FSquadInstance* GetSquadByID(int32 SquadID);
113 virtual FSquadInstance* GetSquadByPlayer(const AProjectXPlayerState* Player);
114
115 UFUNCTION(BlueprintCallable, Category = SquadComponent)
116 virtual AProjectXPlayerState* GetSquadLeaderByID(int32 SquadID);
117
118 virtual void CleanupSquads();
119
120 virtual void TryJoinSquadVoiceChannel(TWeakObjectPtr<AProjectXPlayerState> NewSquadMember, int32 TeamID, int32 SquadID);
121
122public:
123 //Called when the size of the squad list changes.
124 UPROPERTY(BlueprintAssignable)
125 FSquadListUpdateSignature OnSquadListChanged;
126 //Called each time a squad's member list changes.
127 UPROPERTY(BlueprintAssignable)
128 FSquadUpdateSignature OnSquadChanged;
129 //Called each time a user joins or leaves a squad.
130 UPROPERTY(BlueprintAssignable)
131 FSquadMemberUpdateSignature OnSquadMemberAdded;
132 UPROPERTY(BlueprintAssignable)
133 FSquadMemberUpdateSignature OnSquadMemberRemoved;
134
135protected:
136 UFUNCTION()
137 virtual void OnRep_SquadList();
138 UFUNCTION()
139 virtual void PlayerJoinGame(AProjectXPlayerState* Player, const FString& JoinTarget);
140 UFUNCTION()
141 virtual void OnPlayerStateRemoved(APlayerState* PlayerState);
142
143protected:
144 UPROPERTY(ReplicatedUsing = OnRep_SquadList)
145 TArray<FSquadInstance> SquadList;
146
147 TArray<FSquadInstance> CachedSquadList;
148 int32 VoiceChatSalt = 0;
149 FString NetAddress = "";
150
151 TMap<int32, int32> CachedSquadIDMap;
152
153 UPROPERTY(EditDefaultsOnly, BlueprintReadOnly)
154 uint8 MaxSquadSize = 5;
155 UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Replicated)
156 uint8 SquadCount = 6;
157
158public:
159 UFUNCTION(BlueprintPure, Category = SquadComponent, meta = (WorldContext = "WorldContextObject", CallableWithoutWorldContext))
160 static USquadComponent* GetSquadComponent(const UObject* WorldContextObject);
161 UFUNCTION(BlueprintPure, Category = SquadComponent)
162 static bool GetSquadInfo(const FSquadInstance& Squad, uint8& Team, int32& ID, TArray<AProjectXPlayerState*>& PlayerList);
163 UFUNCTION(BlueprintPure, Category = SquadComponent)
164 static bool AreSquadsEqual(const FSquadInstance& OldSquad, const FSquadInstance& NewSquad);
165};
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FSquadMemberUpdateSignature, int32, SquadID, AProjectXPlayerState *, PlayerState)
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FSquadListUpdateSignature)
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FSquadUpdateSignature, int32, SquadID)
Definition: ProjectXPlayerController.h:83
Definition: ProjectXPlayerState.h:238
Definition: SquadComponent.h:15
bool operator!() const
Definition: SquadComponent.h:35
FSquadInstance(uint8 InTeam)
Definition: SquadComponent.h:23
int32 SquadID
Definition: SquadComponent.h:57
bool operator==(const FSquadInstance &Other) const
Definition: SquadComponent.h:30