-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSprintComponent.cpp
More file actions
109 lines (85 loc) · 3.6 KB
/
SprintComponent.cpp
File metadata and controls
109 lines (85 loc) · 3.6 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
// ©2023 JDSherbert. All Rights Reserved.
#include "Project/Public/Components/SprintComponent.h"
#include <Runtime/Engine/Classes/Components/InputComponent.h>
#include <Runtime/Engine/Classes/GameFramework/Character.h>
#include "EnhancedInput/Public/EnhancedInputComponent.h"
#include "EnhancedInput/Public/EnhancedInputSubsystems.h"
/* --------------------------------- Namespace ----------------------------------- */
namespace SprintComponentDefs
{
constexpr float DefaultSprintSpeed = 1000.0f;
const FRotator DefaultSprintRotationSpeed = FRotator(0.0f, 50.0f, 0.0f);
}
/* ---------------------------- Method Definitions ------------------------------- */
USprintComponent::USprintComponent(const FObjectInitializer& ObjectInitializer)
: SprintInputAction(nullptr)
, DefaultWalkSpeed(0.0f)
, DefaultRotationRate(FRotator(0.0f, 0.0f, 0.0f))
, bSprinting(false)
, SprintKeyHoldTime(0.0f)
, SprintSpeed(SprintComponentDefs::DefaultSprintSpeed)
, SprintRotationRate(SprintComponentDefs::DefaultSprintRotationSpeed)
{
PrimaryComponentTick.bCanEverTick = true;
SetIsReplicatedByDefault(true);
}
/* ------------------------------------------------------------------------------- */
void USprintComponent::BeginPlay()
{
Super::BeginPlay();
Init();
}
/* ------------------------------------------------------------------------------- */
void USprintComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
}
/* ------------------------------------------------------------------------------- */
void USprintComponent::Init()
{
if (AActor* Owner = GetOwner())
{
/**
* This should happen automatically -
* AActor will handle the initialization of its own subcomponents!
*/
// RegisterComponent();
if (ACharacter* Character = CastChecked<ACharacter>(Owner))
{
if (UEnhancedInputComponent* TempInput = Character->EnhancedInputComponent)
{
TempInput->BindAction(SprintInputAction, ETriggerEvent::Triggered, this, &USprintComponent::Sprint, true); //! Not working
TempInput->BindAction(SprintInputAction, ETriggerEvent::Completed, this, &USprintComponent::Sprint, false);
}
}
if (UCharacterMovementComponent* TempMovementComponent = CastChecked<UCharacterMovementComponent>(Owner->GetComponentByClass(UCharacterMovementComponent::StaticClass())))
{
MovementComponent = TempMovementComponent;
DefaultWalkSpeed = MovementComponent->MaxWalkSpeed;
DefaultRotationRate = MovementComponent->RotationRate;
}
}
}
/* ------------------------------------------------------------------------------- */
void USprintComponent::Sprint(const bool bActive /*= false*/)
{
Server_Sprint(bActive);
}
/* ------------------------------------------------------------------------------- */
void USprintComponent::Server_Sprint_Implementation(const bool bActive /* = false*/)
{
SetSprinting(bActive);
if (IsSprinting()) SprintKeyHoldTime += FApp::GetDeltaTime();
else if (SprintKeyHoldTime != 0.0f) SprintKeyHoldTime = 0.0f;
if (UCharacterMovementComponent* CharacterMovementComponent = GetMovementComponent())
{
CharacterMovementComponent->MaxWalkSpeed = (IsSprinting()) ? SprintSpeed : DefaultWalkSpeed;
CharacterMovementComponent->RotationRate = (IsSprinting()) ? SprintRotationRate : DefaultRotationRate;
}
}
/* ------------------------------------------------------------------------------- */
void USprintComponent::OnRep_bSprinting()
{
(IsSprinting()) ? Event_OnSprintStart() : Event_OnSprintStop();
}
/* ------------------------------------------------------------------------------- */