-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathXmsPlayerController.cpp
More file actions
206 lines (176 loc) · 7.85 KB
/
XmsPlayerController.cpp
File metadata and controls
206 lines (176 loc) · 7.85 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Copyright (c) 2025 Xist.GG LLC
#include "XmsPlayerController.h"
#include "GameFramework/Pawn.h"
#include "Blueprint/AIBlueprintHelperLibrary.h"
#include "NiagaraSystem.h"
#include "NiagaraFunctionLibrary.h"
#include "Engine/World.h"
#include "EnhancedInputComponent.h"
#include "EnhancedInputSubsystems.h"
#include "InputMappingContext.h"
#include "XmsCharacter.h"
#include "XmsLog.h"
#include "Engine/LocalPlayer.h"
#include "GameFramework/SpringArmComponent.h"
// Set class defaults
AXmsPlayerController::AXmsPlayerController(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
bShowMouseCursor = true;
DefaultMouseCursor = EMouseCursor::Default;
CachedDestination = FVector::ZeroVector;
ShortPressThreshold = 0.2f; // 200 ms
}
void AXmsPlayerController::PostInitProperties()
{
Super::PostInitProperties();
ApplyIniSettings();
}
void AXmsPlayerController::BeginPlay()
{
// Call the base class
Super::BeginPlay();
// Help the dev understand if they've misconfigured a required setting
ensureMsgf(FXCursor != nullptr, TEXT("%hs: You didn't configure a valid FXCursor"), __FUNCTION__);
ensureMsgf(DefaultMappingContext != nullptr, TEXT("%hs: You didn't configure a valid DefaultMappingContext"), __FUNCTION__);
ensureMsgf(CameraZoomAction != nullptr, TEXT("%hs: You didn't configure a valid CameraZoomAction"), __FUNCTION__);
ensureMsgf(SetDestinationClickAction != nullptr, TEXT("%hs: You didn't configure a valid SetDestinationClickAction"), __FUNCTION__);
ensureMsgf(SetDestinationTouchAction != nullptr, TEXT("%hs: You didn't configure a valid SetDestinationTouchAction"), __FUNCTION__);
}
void AXmsPlayerController::SetupInputComponent()
{
// set up gameplay key bindings
Super::SetupInputComponent();
// Add Input Mapping Context
if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(GetLocalPlayer()))
{
Subsystem->AddMappingContext(DefaultMappingContext, 0);
}
// Set up action bindings
if (UEnhancedInputComponent* EnhancedInputComponent = Cast<UEnhancedInputComponent>(InputComponent))
{
// Setup mouse input events
EnhancedInputComponent->BindAction(SetDestinationClickAction, ETriggerEvent::Started, this, &AXmsPlayerController::OnInputStarted);
EnhancedInputComponent->BindAction(SetDestinationClickAction, ETriggerEvent::Triggered, this, &AXmsPlayerController::OnSetDestinationTriggered);
EnhancedInputComponent->BindAction(SetDestinationClickAction, ETriggerEvent::Completed, this, &AXmsPlayerController::OnSetDestinationReleased);
EnhancedInputComponent->BindAction(SetDestinationClickAction, ETriggerEvent::Canceled, this, &AXmsPlayerController::OnSetDestinationReleased);
// Setup touch input events
EnhancedInputComponent->BindAction(SetDestinationTouchAction, ETriggerEvent::Started, this, &AXmsPlayerController::OnInputStarted);
EnhancedInputComponent->BindAction(SetDestinationTouchAction, ETriggerEvent::Triggered, this, &AXmsPlayerController::OnTouchTriggered);
EnhancedInputComponent->BindAction(SetDestinationTouchAction, ETriggerEvent::Completed, this, &AXmsPlayerController::OnTouchReleased);
EnhancedInputComponent->BindAction(SetDestinationTouchAction, ETriggerEvent::Canceled, this, &AXmsPlayerController::OnTouchReleased);
// Camera Zoom
EnhancedInputComponent->BindAction(CameraZoomAction, ETriggerEvent::Triggered, this, &AXmsPlayerController::OnCameraZoom);
}
else
{
UE_LOG(LogXms, Error, TEXT("'%s' Failed to find an Enhanced Input Component! This template is built to use the Enhanced Input system. If you intend to use the legacy system, then you will need to update this C++ file."), *GetNameSafe(this));
}
}
void AXmsPlayerController::OnCameraZoom(const FInputActionInstance& InputActionInstance)
{
const float RawInputValue = InputActionInstance.GetValue().Get<float>();
const float NormalizedInputValue = FMath::Clamp(RawInputValue, -1.f, 1.f);
AXmsCharacter* XmsCharacter = GetPawn<AXmsCharacter>();
check(XmsCharacter);
USpringArmComponent* SpringArm = XmsCharacter->GetCameraBoom();
check(SpringArm);
constexpr double ZoomStep = 500.;
constexpr double MinLength = 500.;
constexpr double MaxLength = 10000.;
const double NewValue = SpringArm->TargetArmLength + (NormalizedInputValue * ZoomStep);
SpringArm->TargetArmLength = FMath::Clamp(NewValue, MinLength, MaxLength);
}
void AXmsPlayerController::OnInputStarted()
{
StopMovement();
}
// Triggered every frame when the input is held down
void AXmsPlayerController::OnSetDestinationTriggered()
{
// We flag that the input is being pressed
FollowTime += GetWorld()->GetDeltaSeconds();
// We look for the location in the world where the player has pressed the input
FHitResult Hit;
bool bHitSuccessful = false;
if (bIsTouch)
{
bHitSuccessful = GetHitResultUnderFinger(ETouchIndex::Touch1, ECollisionChannel::ECC_Visibility, true, Hit);
}
else
{
bHitSuccessful = GetHitResultUnderCursor(ECollisionChannel::ECC_Visibility, true, Hit);
}
// If we hit a surface, cache the location
if (bHitSuccessful)
{
CachedDestination = Hit.Location;
}
// Move towards mouse pointer or touch
APawn* ControlledPawn = GetPawn();
if (ControlledPawn != nullptr)
{
FVector WorldDirection = (CachedDestination - ControlledPawn->GetActorLocation()).GetSafeNormal();
ControlledPawn->AddMovementInput(WorldDirection, 1.0, false);
}
}
void AXmsPlayerController::OnSetDestinationReleased()
{
// If it was a short press
if (FollowTime <= ShortPressThreshold)
{
// We move there and spawn some particles
UAIBlueprintHelperLibrary::SimpleMoveToLocation(this, CachedDestination);
UNiagaraFunctionLibrary::SpawnSystemAtLocation(this, FXCursor, CachedDestination, FRotator::ZeroRotator, FVector(1.f, 1.f, 1.f), true, true, ENCPoolMethod::None, true);
}
FollowTime = 0.f;
}
// Triggered every frame when the input is held down
void AXmsPlayerController::OnTouchTriggered()
{
bIsTouch = true;
OnSetDestinationTriggered();
}
void AXmsPlayerController::OnTouchReleased()
{
bIsTouch = false;
OnSetDestinationReleased();
}
void AXmsPlayerController::ApplyIniSettings()
{
if (FXCursor == nullptr
&& not FXCursorPath.IsEmpty())
{
FXCursor = Cast<UNiagaraSystem>(StaticLoadObject(UNiagaraSystem::StaticClass(), nullptr,
FXCursorPath, nullptr, LOAD_None, nullptr));
UE_CLOG(FXCursor == nullptr, LogXms, Error, TEXT("%hs: FXCursorPath [%s] is not valid"), __FUNCTION__, *FXCursorPath);
}
if (DefaultMappingContext == nullptr
&& not IMCPath.IsEmpty())
{
DefaultMappingContext = Cast<UInputMappingContext>(StaticLoadObject(UInputMappingContext::StaticClass(), nullptr,
IMCPath, nullptr, LOAD_None, nullptr));
UE_CLOG(DefaultMappingContext == nullptr, LogXms, Error, TEXT("%hs: IMCPath [%s] is not valid"), __FUNCTION__, *IMCPath);
}
if (CameraZoomAction == nullptr
&& not CameraZoomActionPath.IsEmpty())
{
CameraZoomAction = Cast<UInputAction>(StaticLoadObject(UInputAction::StaticClass(), nullptr,
CameraZoomActionPath, nullptr, LOAD_None, nullptr));
UE_CLOG(CameraZoomAction == nullptr, LogXms, Error, TEXT("%hs: CameraZoomActionPath [%s] is not valid"), __FUNCTION__, *CameraZoomActionPath);
}
if (SetDestinationClickAction == nullptr
&& not SetDestClickActionPath.IsEmpty())
{
SetDestinationClickAction = Cast<UInputAction>(StaticLoadObject(UInputAction::StaticClass(), nullptr,
SetDestClickActionPath, nullptr, LOAD_None, nullptr));
UE_CLOG(SetDestinationClickAction == nullptr, LogXms, Error, TEXT("%hs: SetDestClickActionPath [%s] is not valid"), __FUNCTION__, *SetDestClickActionPath);
}
if (SetDestinationTouchAction == nullptr
&& not SetDestTouchActionPath.IsEmpty())
{
SetDestinationTouchAction = Cast<UInputAction>(StaticLoadObject(UInputAction::StaticClass(), nullptr,
SetDestTouchActionPath, nullptr, LOAD_None, nullptr));
UE_CLOG(SetDestinationTouchAction == nullptr, LogXms, Error, TEXT("%hs: SetDestTouchActionPath [%s] is not valid"), __FUNCTION__, *SetDestTouchActionPath);
}
}