Space Plunder
Loading...
Searching...
No Matches
CharacterComponent.h
Go to the documentation of this file.
1// Fill out your copyright notice in the Description page of Project Settings.
2
3#pragma once
4
5#include "CoreMinimal.h"
6#include "AIController.h"
8#include "Components/ActorComponent.h"
9#include "Data/BaseData.h"
10#include "CharacterComponent.generated.h"
11
12
15struct FGameplayTag;
16struct FRadialMenuData;
18struct FRadialWheelMenuData;
19class IHUDInterface;
20DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnRequestFriendlyActors, TArray<AActor*>&, Actors);
21DECLARE_LOG_CATEGORY_EXTERN(LogCharacterComponent, Display, All);
22
23//@TODO Check to make sure IsLocallyControlled isn't being incorrectly set on Server/Client
24USTRUCT(BlueprintType)
26{
27 GENERATED_BODY()
28
29 //@TODO Add in Controller etc. all needed for SetupComponent, so they can all input one Var
30 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Character")
31 bool bController = false;
32 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Character")
33 bool bPlayerController = false;
34 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Character")
35 bool bAIController = false;
36 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Character")
37 bool bSkeletalMesh = false;
38 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Character")
39 bool bAnimationInstance = false;
40 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Character")
41 bool bPawn = false;
42 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Character")
43 bool bHUDReference = false;
44 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Character")
45 bool bHasWidgets = false;
46
47 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Character")
48 bool bShouldDestroyOnDeath = false;
49
50 bool GetAnyRequirementsSet() const
51 {
52 if(bController || bPlayerController || bAIController || bSkeletalMesh || bAnimationInstance
53 || bPawn || bHUDReference)
54 {
55 return true;
56 }
57 return false;
58 }
59
61 {
62 if(Requirements.bController && bController == false)
63 {
64 if(Requirements.bPlayerController && bPlayerController == false)
65 {
66 return false;
67 }
68 if(Requirements.bAIController && bAIController == false)
69 {
70 return false;
71 }
72 return false;
73 }
74 if(Requirements.bPawn && bPawn == false)
75 {
76 return false;
77 }
78 if(Requirements.bAnimationInstance && bAnimationInstance == false)
79 {
80 return false;
81 }
82 if(Requirements.bSkeletalMesh && bSkeletalMesh == false)
83 {
84 return false;
85 }
86 if(Requirements.bHUDReference && bHUDReference == false)
87 {
88 return false;
89 }
90 return true;
91 }
92};
93
106UCLASS(Blueprintable, BlueprintType, ClassGroup=(BucciGames), meta=(BlueprintSpawnableComponent), Abstract)
107class BASEHELPERS_API UCharacterComponent : public UCommonActorComponent
108{
109 GENERATED_BODY()
110
111public:
113
114 virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
115
116
118 template <class T>
119 T* GetController() const
120 {
121 static_assert(TPointerIsConvertibleFromTo<T, AController>::Value, "'T' template parameter to GetController must be derived from AController");
122 return Cast<T>(GetPawnChecked<APawn>()->GetController());
123 }
124 template <class T>
125 T* GetControllerChecked() const
126 {
127 static_assert(TPointerIsConvertibleFromTo<T, AController>::Value, "'T' template parameter to GetControllerChecked must be derived from AController");
128 return CastChecked<T>(GetPawnChecked<APawn>()->GetController());
129 }
130 template <class T>
131 T* GetPawn() const
132 {
133 static_assert(TPointerIsConvertibleFromTo<T, APawn>::Value, "'T' template parameter to GetPawn must be derived from APawn");
134 return Cast<T>(GetOwner());
135 }
136 template <class T>
137 T* GetPawnChecked() const
138 {
139 static_assert(TPointerIsConvertibleFromTo<T, APawn>::Value, "'T' template parameter to GetPawnChecked must be derived from APawn");
140 return CastChecked<T>(GetOwner());
141 }
143 template <class T>
144 T* GetPlayerState() const
145 {
146 static_assert(TPointerIsConvertibleFromTo<T, APlayerState>::Value, "'T' template parameter to GetPlayerState must be derived from APlayerState");
147 return GetControllerChecked<AController>()->GetPlayerState<T>();
148 }
150 bool IsLocalController() const;
151
152 // /** Returns the point of view for either a player or controlled pawn */
153 // void GetPlayerViewPoint(FVector& Location, FRotator& Rotation) const;
154
156 // PlayerController accessors
157 // Only returns correct values for APlayerController owners
159
160 template <class T>
161 T* GetPlayer() const
162 {
163 static_assert(TPointerIsConvertibleFromTo<T, UPlayer>::Value, "'T' template parameter to GetPlayer must be derived from UPlayer");
164 APlayerController* PC = GetController<APlayerController>();
165 return PC ? Cast<T>(PC->Player) : nullptr;
166 }
167
168
169 UPROPERTY(BlueprintAssignable, Category="Character Component")
170 FOnRequestFriendlyActors OnRequestFriendlyActors;
171
172 // UFUNCTION(BlueprintCallable, Category="Character Component")
173 // virtual void SetupComponentAuto();
174
175 UFUNCTION(BlueprintCallable, Category="Character Component")
176 virtual void SetupComponentWidgets();
177
178 //- Override for using as Begin play //
179 virtual void ComponentSetupComplete() override;
180 //- Use instead of BeginPlay() in children //
181 //@ TODO use delegate instead
182 UFUNCTION(BlueprintImplementableEvent, Category="Character Component")
183 void OnComponentSetupComplete();
184
185 void SetIsCrouched(const bool bCrouching);
186 void SetIsDead(const bool bDead);
187 void SetIsKnockedOut(const bool bKnocked, const UDamageType* DamageType);
188 //@ TODO Change to set when component setup
189 // UFUNCTION(BlueprintCallable, Category="Character Component")
190 // void SetIsLocallyControlled(const bool bIsLocallyControlled){if(bIsLocallyControlled){bLocallyControlled = bIsLocallyControlled;};};
191
192
193 //- Returns all Perceived Friendly Actors //
194 UFUNCTION(BlueprintPure, BlueprintCallable, Category = "Character")
195 TArray<AActor*> GetFriendlyActors() const;
196 UFUNCTION(BlueprintPure, BlueprintCallable, Category = "Character")
197 float GetLastAnimationDuration() const {return LastAnimationDuration;}
198
199
200 //- Player Camera //
201 virtual FVector GetCameraTraceLocation() const;
202 /* The Camera Trace Location with the Distance to the player Added */
203 virtual FVector GetPlayerTraceLocation() const;
204 virtual FRotator GetCameraTraceRotation() const;
205 virtual FTransform GetCameraTraceTransform() const;
206
207
208 //@TODO Remove blueprint callable from most of these fucntions, Don't need to call from a bp, as it would call from 10 components
209
210 virtual void OpenRadialMenu(const FRadialMenuData& Data);
211 virtual int32 CloseRadialMenu();
212 bool GetIsRadialMenuOpen() const;
213
214 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Traits")
215 bool bUseCharacterTraits = true;
216
217protected:
218 virtual void BeginPlay() override;
219
220 virtual void AutoDetect() override;
221 virtual void Server_AutoDetect() override;
222
223
224 virtual void ComponentActivated(UActorComponent* Component, const bool bReset) override;
225 virtual void ComponentDeactivated(UActorComponent* Component) override;
226
227 UUserWidget* PushContentToLayer(const FGameplayTag& LayerName, const TSoftClassPtr<UUserWidget>& WidgetClass) const;
228 UUserWidget* AddWidgetToLayer(const FGameplayTag& LayerName, const TSoftClassPtr<UUserWidget>& WidgetClass) const;
229 UUserWidget* GetContentFromLayer(const FGameplayTag& LayerName, const TSoftClassPtr<UUserWidget>& WidgetClass) const;
230 UUserWidget* AddWidgetToLayerWithSlot(const FGameplayTag& LayerName, const FGameplayTag& SlotTag, const TSoftClassPtr<UUserWidget>& WidgetClass) const;
231
232
233
234 //- Getters //
235 UFUNCTION(BlueprintPure, BlueprintCallable, Category = "Character")
236 USceneComponent* GetThrowPoint() const;
237
238 UFUNCTION(BlueprintPure, BlueprintCallable, Category = "Character")
239 AController* GetOwnerController() const;
240 UFUNCTION(BlueprintPure, BlueprintCallable, Category = "Character")
241 APlayerController* GetOwnerPlayerController() const;
242 UFUNCTION(BlueprintPure,BlueprintCallable, Category = "Character")
243 USkeletalMeshComponent* GetOwnerMesh() const;
244 // UFUNCTION(BlueprintPure, BlueprintCallable, Category = "Character")
245 // UAnimInstance* GetOwnerAnimInstance() const;
246 UFUNCTION(BlueprintPure, BlueprintCallable, Category = "Character")
247 AAIController* GetOwnerAIController() const;
248 UFUNCTION(BlueprintPure, BlueprintCallable, Category = "Character")
249 APawn* GetOwnerPawn() const;
250 // UFUNCTION(BlueprintPure, BlueprintCallable, Category = "Character")
251 // AActor* GetOwnerHUDReference() const{return OwnerHUDReference;};
253 UFUNCTION(BlueprintPure, BlueprintCallable, Category = "Character")
254 FString GetOwnerName() const {return OwnerObjectName;};
256 // UFUNCTION(BlueprintPure, BlueprintCallable, Category = "Character")
257 // FString GetOwnerDisplayName() const {return OwnerDisplayName;};
258
259 //- Getters //
260 UFUNCTION(BlueprintPure, BlueprintCallable, Category = "Character")
261 bool GetIsDead() const {return bIsDead;};
262 UFUNCTION(BlueprintPure, Category = "Character")
263 bool GetIsOwnerKnockedOut() const {return bIsKnockedOut;};
264 UFUNCTION(BlueprintPure, BlueprintCallable, Category = "Character")
265 bool GetIsNPC() const {return bIsNPC;};
266 UFUNCTION(BlueprintPure, BlueprintCallable, Category = "Character")
267 bool GetIsCrouched() const {return bIsCrouching;};
268
269 //- Return if Succeeded //
270 UFUNCTION(BlueprintCallable, Category = "Character")
271 virtual bool GetOwnerCharacterSkills(FCharacterBaseSkills& Traits);
272
273
274
275
276 UFUNCTION(BlueprintCallable, Category="Character")
277 virtual void OwnerKnockedOut(const UDamageType* DamageType);
278 UFUNCTION(BlueprintCallable, Category="Character")
279 virtual void OwnerWakeUp();
280 UFUNCTION(BlueprintCallable, Category="Character")
281 virtual void OwnerDeath();
282 UFUNCTION(BlueprintImplementableEvent, Category="Character")
283 void OnDeathEvent();
284
285 void SetInputModeGameAndUI(const bool bGameAndUI, const bool bShowMouse, class UWidget* InWidgetToFocus = nullptr) const;
286
287 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Character|Setup")
288 FCharacterComponentRequirements ComponentRequirements;
289 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Character|Setup")
290 FCharacterComponentRequirements ComponentResults;
291
292 //@TODO do the same with SetupWidgets(), have bHasWidgets to setup?
293 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Character|Setup")
294 bool bHasWidgets = false;
295 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Character|Setup")
296 bool bUsesHUDWidgets = false;
297 bool bBindAnimationEvent = false;
298
299 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Character|Debugging")
300 bool bUsingEnhancedInput = true;
301 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Character|Debugging")
302 float MultiplayerStartDelay = 0.5f;
303 // UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Character|Debugging")
304 // bool bShowTraceLocations = true;
305
306
307
308 //- Play Animation //
309 UFUNCTION()
310 float PlayMontageAnimation(UAnimMontage* MontageToPlay, const float InPlayRate = 1.0f, const EMontagePlayReturnType ReturnValueType = EMontagePlayReturnType::Duration, const float InTimeToStartMontageAt = 0.0f, const bool bStopAllMontages = true);
311 UFUNCTION()
312 void StopMontageAnimation(const float InBlendOutTime, const UAnimMontage* Montage);
313
314 UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Animation")
315 float LastAnimationDuration = 0.0f;
316
317 UFUNCTION(Category="Character")
318 virtual void OnMontageNotifyBegin(FName NotifyName, const FBranchingPointNotifyPayload& BranchingPointPayload);
319 UFUNCTION(Category="Character")
320 virtual void OnMontageNotifyEnd(FName NotifyName, const FBranchingPointNotifyPayload& BranchingPointPayload);
321
322 // IControllerInterface* GetControllerInterface();
323 IHUDInterface* GetHUDInterface();
324 // IPlayerControllerInterface* GetPlayerControllerInterface();
325 ICharacterBaseInterface* GetCharacterBaseInterface();
326 IControllerInterface* GetControllerInterface() const;
327 // IHUDInterface* GetHUDInterface() const;
328 IPlayerControllerInterface* GetPlayerControllerInterface() const;
329 ICharacterBaseInterface* GetCharacterBaseInterface() const;
330
331private:
332
333 UFUNCTION(Server, Reliable, Category="Character Component")
334 void Server_SetOwnerMesh(USkeletalMeshComponent* SkeletalMesh);
335
336 // bool DetectOwnerPawn();
337 bool DetectOwnerMesh();
338 bool DetectOwnerAnimInst();
339 // bool DetectOwnerController();
340 bool DetectOwnerHUD();
341 bool BindAnimation();
342
343 void SetOwnerMesh(USkeletalMeshComponent* SkeletalMesh);
344 void SetAnimInstance(UAnimInstance* AnimationInstance);
345 // void SetController(AController* Controller);
346 void SetIsNPC(const bool bNPC){bIsNPC = bNPC;};
347 void SetHUDReference(AActor* HUDReference);
348
349 void TrySetupAgain();
350
351 // bool CheckComponentIsSetup(const FString& ComponentName) const;
352 bool bIsNPC = false;
353 bool bIsDead = false;
354 bool bIsKnockedOut = false;
355 bool bIsCrouching = false;
356 // UPROPERTY()
357 // TObjectPtr<AController> OwnerController = nullptr;
358 // UPROPERTY()
359 // TObjectPtr<APlayerController> OwnerPlayerController = nullptr;
360 // UPROPERTY()
361 // TObjectPtr<AAIController> OwnerAIController = nullptr;
362 UPROPERTY(Replicated)
363 TObjectPtr<USkeletalMeshComponent> OwnerMesh = nullptr;
364 UPROPERTY()
365 TObjectPtr<UAnimInstance> MainAnimInstance = nullptr;
366 // UPROPERTY()
367 // TObjectPtr<APawn> OwnerPawn = nullptr;
368 UPROPERTY()
369 TObjectPtr<AActor> OwnerHUDReference = nullptr;
370
371
372 //@ New Way
373 IHUDInterface* HUDInterface;
374 IPlayerControllerInterface* PlayerControllerInterface;
375 ICharacterBaseInterface* CharacterBaseInterface;
376 // UPROPERTY(Replicated)
377 IControllerInterface* ControllerInterface;
378
379 // TObjectPtr<AActor> ControllerActor = nullptr;
380
381 UPROPERTY(VisibleAnywhere, Category="Character")
382 FString OwnerObjectName = "Name";
383 // UPROPERTY(VisibleAnywhere, Category="Character")
384 // FString OwnerDisplayName = "DisplayName";
385
386 UPROPERTY(EditAnywhere, Category="Character|Setup")
387 int32 SetupAttempts = 5;
388 UPROPERTY(EditAnywhere, Category="Character|Setup")
389 float SetupAttemptDelay = 1.0f;
390
391 int32 CurrentSetupAttempt = 0;
392 FTimerHandle SetupCompleteDelayTimerHandle;
393 FTimerHandle UpdateCameraTimerHandle;
394 FTimerHandle SetupComponentDelayTimerHandle;
395
396};
397
398
DECLARE_LOG_CATEGORY_EXTERN(LogCharacterComponent, Display, All)
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnRequestFriendlyActors, TArray< AActor * > &, Actors)
UCLASS(Blueprintable, BlueprintType, ClassGroup=(BucciGames), meta=(BlueprintSpawnableComponent)) class CHATSYSTEM_API UChatSystemComponent
Definition ChatSystemComponent.h:13
Definition CustomCharacter.h:19
Definition ControllerInterface.h:20
Definition HUDInterface.h:23
Definition PlayerControllerInterface.h:21
Represents a character component that can be added to an actor.
Definition BaseData.h:815
Definition CharacterComponent.h:26
bool GetHasFoundRequirements(const FCharacterComponentRequirements &Requirements) const
Definition CharacterComponent.h:60
bool bController
Definition CharacterComponent.h:31
bool bPawn
Definition CharacterComponent.h:41
bool bSkeletalMesh
Definition CharacterComponent.h:37
bool bHUDReference
Definition CharacterComponent.h:43
bool bAIController
Definition CharacterComponent.h:35
bool bAnimationInstance
Definition CharacterComponent.h:39
bool bPlayerController
Definition CharacterComponent.h:33
Definition RadialWheelData.h:100