-
Notifications
You must be signed in to change notification settings - Fork 761
/
GDHeroCharacter.cpp
319 lines (254 loc) · 10.2 KB
/
GDHeroCharacter.cpp
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// Copyright 2020 Dan Kestranek.
#include "Characters/Heroes/GDHeroCharacter.h"
#include "AI/GDHeroAIController.h"
#include "Camera/CameraComponent.h"
#include "Characters/Abilities/AttributeSets/GDAttributeSetBase.h"
#include "Characters/Abilities/GDAbilitySystemComponent.h"
#include "Components/CapsuleComponent.h"
#include "Components/DecalComponent.h"
#include "Components/WidgetComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "GASDocumentation/GASDocumentationGameMode.h"
#include "Kismet/GameplayStatics.h"
#include "Kismet/KismetMathLibrary.h"
#include "Player/GDPlayerController.h"
#include "Player/GDPlayerState.h"
#include "UI/GDFloatingStatusBarWidget.h"
#include "UObject/ConstructorHelpers.h"
AGDHeroCharacter::AGDHeroCharacter(const class FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
CameraBoom = CreateDefaultSubobject<USpringArmComponent>(FName("CameraBoom"));
CameraBoom->SetupAttachment(RootComponent);
CameraBoom->bUsePawnControlRotation = true;
CameraBoom->SetRelativeLocation(FVector(0, 0, 68.492264));
FollowCamera = CreateDefaultSubobject<UCameraComponent>(FName("FollowCamera"));
FollowCamera->SetupAttachment(CameraBoom);
FollowCamera->FieldOfView = 80.0f;
GunComponent = CreateDefaultSubobject<USkeletalMeshComponent>(FName("Gun"));
GetCapsuleComponent()->SetCollisionResponseToChannel(ECollisionChannel::ECC_Camera, ECollisionResponse::ECR_Ignore);
// Makes sure that the animations play on the Server so that we can use bone and socket transforms
// to do things like spawning projectiles and other FX.
GetMesh()->VisibilityBasedAnimTickOption = EVisibilityBasedAnimTickOption::AlwaysTickPoseAndRefreshBones;
GetMesh()->SetCollisionEnabled(ECollisionEnabled::NoCollision);
GetMesh()->SetCollisionProfileName(FName("NoCollision"));
UIFloatingStatusBarComponent = CreateDefaultSubobject<UWidgetComponent>(FName("UIFloatingStatusBarComponent"));
UIFloatingStatusBarComponent->SetupAttachment(RootComponent);
UIFloatingStatusBarComponent->SetRelativeLocation(FVector(0, 0, 120));
UIFloatingStatusBarComponent->SetWidgetSpace(EWidgetSpace::Screen);
UIFloatingStatusBarComponent->SetDrawSize(FVector2D(500, 500));
UIFloatingStatusBarClass = StaticLoadClass(UObject::StaticClass(), nullptr, TEXT("/Game/GASDocumentation/UI/UI_FloatingStatusBar_Hero.UI_FloatingStatusBar_Hero_C"));
if (!UIFloatingStatusBarClass)
{
UE_LOG(LogTemp, Error, TEXT("%s() Failed to find UIFloatingStatusBarClass. If it was moved, please update the reference location in C++."), *FString(__FUNCTION__));
}
AIControllerClass = AGDHeroAIController::StaticClass();
DeadTag = FGameplayTag::RequestGameplayTag(FName("State.Dead"));
}
// Called to bind functionality to input
void AGDHeroCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAxis("MoveForward", this, &AGDHeroCharacter::MoveForward);
PlayerInputComponent->BindAxis("MoveRight", this, &AGDHeroCharacter::MoveRight);
PlayerInputComponent->BindAxis("LookUp", this, &AGDHeroCharacter::LookUp);
PlayerInputComponent->BindAxis("LookUpRate", this, &AGDHeroCharacter::LookUpRate);
PlayerInputComponent->BindAxis("Turn", this, &AGDHeroCharacter::Turn);
PlayerInputComponent->BindAxis("TurnRate", this, &AGDHeroCharacter::TurnRate);
// Bind player input to the AbilitySystemComponent. Also called in OnRep_PlayerState because of a potential race condition.
BindASCInput();
}
// Server only
void AGDHeroCharacter::PossessedBy(AController * NewController)
{
Super::PossessedBy(NewController);
AGDPlayerState* PS = GetPlayerState<AGDPlayerState>();
if (PS)
{
// Set the ASC on the Server. Clients do this in OnRep_PlayerState()
AbilitySystemComponent = Cast<UGDAbilitySystemComponent>(PS->GetAbilitySystemComponent());
// AI won't have PlayerControllers so we can init again here just to be sure. No harm in initing twice for heroes that have PlayerControllers.
PS->GetAbilitySystemComponent()->InitAbilityActorInfo(PS, this);
// Set the AttributeSetBase for convenience attribute functions
AttributeSetBase = PS->GetAttributeSetBase();
// If we handle players disconnecting and rejoining in the future, we'll have to change this so that possession from rejoining doesn't reset attributes.
// For now assume possession = spawn/respawn.
InitializeAttributes();
AddStartupEffects();
AddCharacterAbilities();
AGDPlayerController* PC = Cast<AGDPlayerController>(GetController());
if (PC)
{
PC->CreateHUD();
}
InitializeFloatingStatusBar();
// Respawn specific things that won't affect first possession.
// Forcibly set the DeadTag count to 0
AbilitySystemComponent->SetTagMapCount(DeadTag, 0);
// Set Health/Mana/Stamina to their max. This is only necessary for *Respawn*.
SetHealth(GetMaxHealth());
SetMana(GetMaxMana());
SetStamina(GetMaxStamina());
}
}
USpringArmComponent * AGDHeroCharacter::GetCameraBoom()
{
return CameraBoom;
}
UCameraComponent * AGDHeroCharacter::GetFollowCamera()
{
return FollowCamera;
}
float AGDHeroCharacter::GetStartingCameraBoomArmLength()
{
return StartingCameraBoomArmLength;
}
FVector AGDHeroCharacter::GetStartingCameraBoomLocation()
{
return StartingCameraBoomLocation;
}
UGDFloatingStatusBarWidget * AGDHeroCharacter::GetFloatingStatusBar()
{
return UIFloatingStatusBar;
}
USkeletalMeshComponent * AGDHeroCharacter::GetGunComponent() const
{
return GunComponent;
}
void AGDHeroCharacter::FinishDying()
{
if (GetLocalRole() == ROLE_Authority)
{
AGASDocumentationGameMode* GM = Cast<AGASDocumentationGameMode>(GetWorld()->GetAuthGameMode());
if (GM)
{
GM->HeroDied(GetController());
}
}
Super::FinishDying();
}
/**
* On the Server, Possession happens before BeginPlay.
* On the Client, BeginPlay happens before Possession.
* So we can't use BeginPlay to do anything with the AbilitySystemComponent because we don't have it until the PlayerState replicates from possession.
*/
void AGDHeroCharacter::BeginPlay()
{
Super::BeginPlay();
// Only needed for Heroes placed in world and when the player is the Server.
// On respawn, they are set up in PossessedBy.
// When the player a client, the floating status bars are all set up in OnRep_PlayerState.
InitializeFloatingStatusBar();
StartingCameraBoomArmLength = CameraBoom->TargetArmLength;
StartingCameraBoomLocation = CameraBoom->GetRelativeLocation();
}
void AGDHeroCharacter::PostInitializeComponents()
{
Super::PostInitializeComponents();
if (GunComponent && GetMesh())
{
GunComponent->AttachToComponent(GetMesh(), FAttachmentTransformRules::SnapToTargetNotIncludingScale, FName("GunSocket"));
}
}
void AGDHeroCharacter::LookUp(float Value)
{
if (IsAlive())
{
AddControllerPitchInput(Value);
}
}
void AGDHeroCharacter::LookUpRate(float Value)
{
if (IsAlive())
{
AddControllerPitchInput(Value * BaseLookUpRate * GetWorld()->DeltaTimeSeconds);
}
}
void AGDHeroCharacter::Turn(float Value)
{
if (IsAlive())
{
AddControllerYawInput(Value);
}
}
void AGDHeroCharacter::TurnRate(float Value)
{
if (IsAlive())
{
AddControllerYawInput(Value * BaseTurnRate * GetWorld()->DeltaTimeSeconds);
}
}
void AGDHeroCharacter::MoveForward(float Value)
{
AddMovementInput(UKismetMathLibrary::GetForwardVector(FRotator(0, GetControlRotation().Yaw, 0)), Value);
}
void AGDHeroCharacter::MoveRight(float Value)
{
AddMovementInput(UKismetMathLibrary::GetRightVector(FRotator(0, GetControlRotation().Yaw, 0)), Value);
}
void AGDHeroCharacter::InitializeFloatingStatusBar()
{
// Only create once
if (UIFloatingStatusBar || !AbilitySystemComponent.IsValid())
{
return;
}
// Setup UI for Locally Owned Players only, not AI or the server's copy of the PlayerControllers
AGDPlayerController* PC = Cast<AGDPlayerController>(UGameplayStatics::GetPlayerController(GetWorld(), 0));
if (PC && PC->IsLocalPlayerController())
{
if (UIFloatingStatusBarClass)
{
UIFloatingStatusBar = CreateWidget<UGDFloatingStatusBarWidget>(PC, UIFloatingStatusBarClass);
if (UIFloatingStatusBar && UIFloatingStatusBarComponent)
{
UIFloatingStatusBarComponent->SetWidget(UIFloatingStatusBar);
// Setup the floating status bar
UIFloatingStatusBar->SetHealthPercentage(GetHealth() / GetMaxHealth());
UIFloatingStatusBar->SetManaPercentage(GetMana() / GetMaxMana());
}
}
}
}
// Client only
void AGDHeroCharacter::OnRep_PlayerState()
{
Super::OnRep_PlayerState();
AGDPlayerState* PS = GetPlayerState<AGDPlayerState>();
if (PS)
{
// Set the ASC for clients. Server does this in PossessedBy.
AbilitySystemComponent = Cast<UGDAbilitySystemComponent>(PS->GetAbilitySystemComponent());
// Init ASC Actor Info for clients. Server will init its ASC when it possesses a new Actor.
AbilitySystemComponent->InitAbilityActorInfo(PS, this);
// Bind player input to the AbilitySystemComponent. Also called in SetupPlayerInputComponent because of a potential race condition.
BindASCInput();
// Set the AttributeSetBase for convenience attribute functions
AttributeSetBase = PS->GetAttributeSetBase();
// If we handle players disconnecting and rejoining in the future, we'll have to change this so that posession from rejoining doesn't reset attributes.
// For now assume possession = spawn/respawn.
InitializeAttributes();
AGDPlayerController* PC = Cast<AGDPlayerController>(GetController());
if (PC)
{
PC->CreateHUD();
}
// Simulated on proxies don't have their PlayerStates yet when BeginPlay is called so we call it again here
InitializeFloatingStatusBar();
// Respawn specific things that won't affect first possession.
// Forcibly set the DeadTag count to 0
AbilitySystemComponent->SetTagMapCount(DeadTag, 0);
// Set Health/Mana/Stamina to their max. This is only necessary for *Respawn*.
SetHealth(GetMaxHealth());
SetMana(GetMaxMana());
SetStamina(GetMaxStamina());
}
}
void AGDHeroCharacter::BindASCInput()
{
if (!ASCInputBound && AbilitySystemComponent.IsValid() && IsValid(InputComponent))
{
AbilitySystemComponent->BindAbilityActivationToInputComponent(InputComponent, FGameplayAbilityInputBinds(FString("ConfirmTarget"),
FString("CancelTarget"), FString("EGDAbilityInputID"), static_cast<int32>(EGDAbilityInputID::Confirm), static_cast<int32>(EGDAbilityInputID::Cancel)));
ASCInputBound = true;
}
}