LEAP Documentation 40220
Documentation for the LEAP project
InteractionComponent.h
Go to the documentation of this file.
1// Copyright 2018 Blue Isle Studios, Inc. All Rights Reserved.
2#pragma once
3
4#include "CoreMinimal.h"
5#include "Components/ActorComponent.h"
6#include "InteractionComponent.generated.h"
7
8class UInteractableComponent;
9class APlayerController;
10
11UENUM(BlueprintType)
12enum class EInteractionError : uint8
13{
15 PlayerTooFar = 1
16};
17
19DECLARE_DYNAMIC_MULTICAST_DELEGATE_FourParams(FHighlightTextSignature, FText, HighlightText, UInteractableComponent*, Interactable, UPrimitiveComponent*, Component, APlayerController*, Sender);
20DECLARE_DYNAMIC_MULTICAST_DELEGATE_FourParams(FHoldSignature, UInteractableComponent*, Interactable, UPrimitiveComponent*, Component, float, HoldTime, APlayerController*, Sender);
21DECLARE_DYNAMIC_MULTICAST_DELEGATE_ThreeParams(FHoldStopSignature, UInteractableComponent*, Interactable, UPrimitiveComponent*, Component, APlayerController*, Sender);
22DECLARE_DYNAMIC_MULTICAST_DELEGATE_ThreeParams(FHighlightNoTextSignature, UInteractableComponent*, Interactable, UPrimitiveComponent*, Component, APlayerController*, Sender);
23
24UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
25class PROJECTX_API UInteractionComponent : public UActorComponent
26{
27 GENERATED_BODY()
28
29public:
30 /* How far can the player interact? */
31 UPROPERTY(EditDefaultsOnly)
32 float InteractionDistance = 500.0f;
33 /* How far can the player be from the interactable on the server before the request is discarded? */
34 UPROPERTY(EditDefaultsOnly)
35 float ServerInteractionDistanceAllowableError = 1000.0f;
36
37 UPROPERTY(BlueprintAssignable, Category = "Interaction Events")
38 FHighlightTextSignature OnHighlightEnter;
39 UPROPERTY(BlueprintAssignable, Category = "Interaction Events")
40 FHighlightNoTextSignature OnHighlightExit;
41 UPROPERTY(BlueprintAssignable, Category = "Interaction Events")
42 FInteractionErrorSignature OnInteractionError;
43 UPROPERTY(BlueprintAssignable, Category = "Interaction Events")
44 FHoldSignature OnHoldInteraction;
45 UPROPERTY(BlueprintAssignable, Category = "Interaction Events")
46 FHoldStopSignature OnHoldInteractionStop;
47
48 void UpdateCanInteract(bool bNewCanInteract);
49
50 bool HasValidInteractionTarget() const;
51
52private:
53 friend class AProjectXCharacter;
54
55 FTimerHandle HoldTimerHandle;
56 UInteractableComponent* TargetCache;
57 mutable APlayerController* PlayerController;
58
59 UInteractionComponent();
60
61 APlayerController* GetPlayerController() const;
62 FHitResult FindTargetInteractable() const;
63 void GetTarget(UInteractableComponent*& Target, UPrimitiveComponent*& TargetComponent) const;
64
65 virtual void TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
66
67 UFUNCTION()
68 void Interact();
69 UFUNCTION()
70 void StopInteract();
71 UFUNCTION()
72 void FinishHoldInteract(UInteractableComponent* HitInteractable, UPrimitiveComponent* HitComponent);
73 UFUNCTION()
74 void ClearHoldInteract();
75
76 void HoldInteract(UInteractableComponent* Target,UPrimitiveComponent* HitComponent);
77 bool IsValidInteraction(const UInteractableComponent* HitInteractable, UPrimitiveComponent* HitComponent, const bool bDisplayErrors = true) const;
78 UFUNCTION(Reliable, Server, WithValidation)
79 void Server_Select(UInteractableComponent* HitInteractable, UPrimitiveComponent* HitComponent);
80 UFUNCTION(Reliable, Server, WithValidation)
81 void Server_Hold(UInteractableComponent* HitInteractable, UPrimitiveComponent* HitComponent);
82 UFUNCTION(Reliable, Server, WithValidation)
83 void Server_StopHold();
84
85 bool bCanInteract = true;
86};
DECLARE_DYNAMIC_MULTICAST_DELEGATE_FourParams(FHighlightTextSignature, FText, HighlightText, UInteractableComponent *, Interactable, UPrimitiveComponent *, Component, APlayerController *, Sender)
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FInteractionErrorSignature, EInteractionError, Error)
EInteractionError
Definition: InteractionComponent.h:13
DECLARE_DYNAMIC_MULTICAST_DELEGATE_ThreeParams(FHoldStopSignature, UInteractableComponent *, Interactable, UPrimitiveComponent *, Component, APlayerController *, Sender)
Definition: ProjectXCharacter.h:128