-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHollowKnight.cpp
394 lines (331 loc) · 12.1 KB
/
HollowKnight.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// Fill out your copyright notice in the Description page of Project Settings.
#include "HollowKnight.h"
#include "Components/SphereComponent.h"
#include "GameFramework/FloatingPawnMovement.h"
#include "Camera/CameraComponent.h"
#include "Kismet/KismetSystemLibrary.h"
#include "Runtime/Engine/Classes/Engine/EngineTypes.h"
// Sets default values
AHollowKnight::AHollowKnight()
{
// Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
capsule = CreateDefaultSubobject<USphereComponent>(TEXT("Knight_SphereComponent"));
pawnMovement = CreateDefaultSubobject<UFloatingPawnMovement>(TEXT("Knight_PawnMovement"));
camera = CreateDefaultSubobject<UCameraComponent>(TEXT("Knight_CameraComponent"));
}
// Called when the game starts or when spawned
void AHollowKnight::BeginPlay()
{
Super::BeginPlay();
APlayerController* controller = GetWorld()->GetFirstPlayerController();
controller->Possess(this);
controller->SetViewTarget(this);
camera->SetRelativeLocation(FVector(-1000, 0, 0));
maxWalkSpeed = 800.f; // Original values: 800.f
maxFallSpeed = -1000.f; // Original values: -1000.f
wallFallSpeed = -300.f; // Original values: -300.f
jumpAcceleration = 9000.f; // Original values: 7000.f
jumpMaxDuration = .233333f; // Original values: .233333f
jumpSpeedCap = 900.f; // Original values: 700.f
doubleJump = false;
dashDuration = .133333f; // Original values: ???
dashAcceleration = 10000.f; // Original values: ???
dashMaxSpeed = 2000.f; // Original values: ???
dashCounter = 0;
gravityAcceleration = 3000.f; // Original values: 3000.f
risingGravityAcceleration = 2100.f; // Original values: 2100.f
wallJumpInitial = false;
ChangeMovement(EMotionStates::FALLING);
capsule->SetSphereRadius(50.f);
capsule->SetHiddenInGame(false);
capsule->SetSimulatePhysics(true);
capsule->SetEnableGravity(false);
capsule->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Block);
capsule->OnComponentHit.AddDynamic(this, &AHollowKnight::OnHit);
}
// Called every frame
void AHollowKnight::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
AddGravity();
Jumping(DeltaTime);
Dashing(DeltaTime);
CheckWallCollision();
ResolveMovement(DeltaTime);
}
// Called to bind functionality to input
void AHollowKnight::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
APlayerController* controller = GetWorld()->GetFirstPlayerController();
PlayerInputComponent->BindAxis(FName("MoveHorizontal"), this, &AHollowKnight::MoveHorizontal);
PlayerInputComponent->BindAction(FName("Jump"), IE_Pressed, this, &AHollowKnight::StartJump);
PlayerInputComponent->BindAction(FName("Jump"), IE_Released, this, &AHollowKnight::InterruptJump);
PlayerInputComponent->BindAction(FName("Dash"), IE_Pressed, this, &AHollowKnight::StartDash);
}
void AHollowKnight::AddVelocity(FVector direction, float scale)
{
this->addedVelocity += direction * scale;
}
void AHollowKnight::ResolveMovement(float deltaTime)
{
if (motionState == EMotionStates::JUMPING || motionState == EMotionStates::RISING) {
if (this->velocity.Z <= 0.01f) {
ChangeMovement(EMotionStates::FALLING);
}
}
FVector initialLocation = this->GetActorLocation();
FVector totalVelocity = (FMath::Sign(this->addedVelocity.Y) == FMath::Sign(this->velocity.Y)) ? this->addedVelocity + this->velocity : this->addedVelocity;
totalVelocity = FMath::Sign(this->velocity.Y) == 0 ? this->addedVelocity + this->velocity : totalVelocity;
float horizontalClampMin = (motionState == EMotionStates::DASHING) ? -1 * this->dashMaxSpeed : -1 * this->maxWalkSpeed;
float horizontalClampMax = (motionState == EMotionStates::DASHING) ? this->dashMaxSpeed : this->maxWalkSpeed;
float verticalClampMin = (motionState == EMotionStates::GROUNDED || motionState == EMotionStates::FALLING) ? this->maxFallSpeed : 0.f;
verticalClampMin = (motionState == EMotionStates::WALLSLIDE) ? this->wallFallSpeed : verticalClampMin;
verticalClampMin = (motionState == EMotionStates::WALLSLIDE && this->landedOnWall) ? 0 : verticalClampMin;
verticalClampMin = (motionState == EMotionStates::DASHING) ? 0 : verticalClampMin;
float verticalClampMax = (motionState == EMotionStates::DASHING) ? 0 : this->jumpSpeedCap;
totalVelocity = FVector(
totalVelocity.X,
FMath::Clamp(totalVelocity.Y, horizontalClampMin, horizontalClampMax),
FMath::Clamp(totalVelocity.Z, verticalClampMin, verticalClampMax));
FVector newLocation = deltaTime * totalVelocity + initialLocation;
FHitResult hitResult;
SetActorLocation(newLocation, true, &hitResult, ETeleportType::None);
while(hitResult.bBlockingHit) {
FVector displacement = hitResult.TraceEnd - hitResult.TraceStart;
FVector displacementAdjustment = displacement - FVector::DotProduct(displacement, hitResult.ImpactNormal) * hitResult.ImpactNormal + hitResult.ImpactNormal * 0.0125;
FVector displacedActor = this->GetActorLocation() + displacementAdjustment;
displacedActor = FVector(0.f, displacedActor.Y, displacedActor.Z);
SetActorLocation(displacedActor, true, &hitResult, ETeleportType::None);
}
this->velocity = (this->GetActorLocation() - initialLocation) / deltaTime;
this->addedVelocity = FVector::ZeroVector;
}
bool AHollowKnight::ChangeMovement(EMotionStates::Type newMotionState)
{
EMotionStates::Type oldMotionState = motionState;
switch (newMotionState)
{
case EMotionStates::GROUNDED:
if (oldMotionState != EMotionStates::DASHING) {
motionState = newMotionState;
}
break;
case EMotionStates::JUMPING:
if (oldMotionState != EMotionStates::DASHING) {
motionState = newMotionState;
}
break;
case EMotionStates::RISING:
if (oldMotionState != EMotionStates::DASHING) {
motionState = newMotionState;
}
break;
case EMotionStates::FALLING:
if (oldMotionState != EMotionStates::JUMPING) {
motionState = newMotionState;
}
break;
case EMotionStates::DASHING:
motionState = newMotionState;
break;
case EMotionStates::WALLSLIDE:
if (oldMotionState == EMotionStates::FALLING) {
motionState = newMotionState;
}
break;
}
if (oldMotionState != newMotionState) {
if (oldMotionState == EMotionStates::WALLSLIDE) {
this->landedOnWall = false;
}
if (newMotionState == EMotionStates::WALLSLIDE) {
this->landedOnWall = true;
UKismetSystemLibrary::K2_SetTimer(this, TEXT("CompleteWallLanding"), .1f, false);
}
}
return oldMotionState != newMotionState;
}
void AHollowKnight::AddGravity()
{
float addedGravity = gravityAcceleration;
switch (motionState)
{
case EMotionStates::GROUNDED:
break;
case EMotionStates::JUMPING:
addedGravity = 0.f;
break;
case EMotionStates::RISING:
addedGravity = risingGravityAcceleration;
break;
case EMotionStates::FALLING:
break;
case EMotionStates::WALLSLIDE:
AddVelocity(FVector::RightVector, FMath::Sign(this->wallNormal.Y) * -1.f);
break;
}
AddVelocity(FVector::DownVector, addedGravity);
}
void AHollowKnight::MoveHorizontal(float amount)
{
if (!this->wallJumpInitial && motionState != EMotionStates::DASHING) {
if (FMath::Abs(amount) != 0) {
this->lookDirection = amount > 0 ? FVector::RightVector : FVector::LeftVector;
}
AddVelocity(amount * FVector::RightVector, maxWalkSpeed);
}
}
void AHollowKnight::CheckWallCollision()
{
FVector rightSphereCenter = this->GetActorLocation() + 50 * FVector::RightVector;
FVector leftSphereCenter = this->GetActorLocation() + 50 * FVector::LeftVector;
FCollisionShape collisionSphere = FCollisionShape::MakeSphere(5.0f);
TArray<TEnumAsByte<EObjectTypeQuery>> collisionParams;
collisionParams.Add(UEngineTypes::ConvertToObjectType(ECollisionChannel::ECC_WorldStatic));
collisionParams.Add(UEngineTypes::ConvertToObjectType(ECollisionChannel::ECC_WorldDynamic));
TArray<AActor*> ignoredActors;
FHitResult rightOutHit;
FHitResult leftOutHit;
bool rightHit = UKismetSystemLibrary::SphereTraceSingleForObjects(this, rightSphereCenter, rightSphereCenter, collisionSphere.GetSphereRadius(),
collisionParams, false, ignoredActors, EDrawDebugTrace::None, rightOutHit, true);
bool leftHit = UKismetSystemLibrary::SphereTraceSingleForObjects(this, leftSphereCenter, leftSphereCenter, collisionSphere.GetSphereRadius(),
collisionParams, false, ignoredActors, EDrawDebugTrace::None, leftOutHit, true);
if (rightOutHit.bBlockingHit) {
if (FMath::Abs(FVector::DotProduct(rightOutHit.ImpactNormal, FVector::RightVector)) >= 0.71f){
this->wallDirection = FVector::RightVector * FMath::Sign(rightOutHit.ImpactNormal.Y);
}
}
if (leftOutHit.bBlockingHit) {
if (FMath::Abs(FVector::DotProduct(leftOutHit.ImpactNormal, FVector::LeftVector)) >= 0.71f) {
this->wallDirection = FVector::RightVector * FMath::Sign(leftOutHit.ImpactNormal.Y);
}
}
this->touchingWall = rightHit || leftHit;
}
void AHollowKnight::CompleteWallLanding()
{
this->landedOnWall = false;
}
void AHollowKnight::StartJump()
{
this->jumpTimer = 0;
if (motionState == EMotionStates::GROUNDED) {
ChangeMovement(EMotionStates::JUMPING);
}
else {
if (this->touchingWall) {
ChangeMovement(EMotionStates::JUMPING);
this->wallJumpInitial = true;
this->lookDirection = -1 * this->wallDirection;
this->velocity = FVector::ZeroVector;
}
else {
if (IsInAir() && this->doubleJump) {
this->doubleJump = false;
ChangeMovement(EMotionStates::JUMPING);
}
}
}
}
void AHollowKnight::Jumping(float deltaTime)
{
if (motionState == EMotionStates::JUMPING) {
if (this->wallJumpInitial) {
FVector wallJumpDirection = FVector::UpVector + this->wallDirection;
AddVelocity(wallJumpDirection, this->jumpAcceleration);
}
else {
AddVelocity(FVector::UpVector, this->jumpAcceleration);
}
this->jumpTimer += deltaTime;
if (this->jumpTimer >= this->jumpMaxDuration) {
this->jumpTimer = 0;
StopJump(false);
}
if (this->jumpTimer >= this->jumpMaxDuration / 2) {
this->wallJumpInitial = false;
}
}
}
void AHollowKnight::StopJump(bool sharp)
{
if (motionState == EMotionStates::JUMPING) {
this->wallJumpInitial = false;
ChangeMovement(EMotionStates::RISING);
if (sharp) {
AddVelocity(FVector::DownVector, FMath::Abs(this->velocity.Z / (GetWorld()->GetDeltaSeconds() * 1.5)));
}
}
}
void AHollowKnight::Jump()
{
StartJump();
}
void AHollowKnight::InterruptJump()
{
StopJump(true);
}
void AHollowKnight::StartDash()
{
if (this->dashCounter < 1) {
this->dashCounter++;
this->dashDirection = this->lookDirection;
ChangeMovement(EMotionStates::DASHING);
UKismetSystemLibrary::K2_ClearTimer(this, TEXT("Ungrounded"));
}
}
void AHollowKnight::Dashing(float deltaSeconds)
{
if (motionState == EMotionStates::DASHING) {
AddVelocity(this->dashDirection, this->dashAcceleration);
this->dashTimer += deltaSeconds;
if (this->dashTimer >= this->dashDuration) {
this->dashTimer = 0;
StopDash();
}
}
}
void AHollowKnight::StopDash()
{
//if (motionState == EMotionStates::DASHING) {
// ChangeMovement(EMotionStates::FALLING);
//}
ChangeMovement(EMotionStates::FALLING);
}
void AHollowKnight::Ungrounded()
{
if (!IsInAir()) {
ChangeMovement(EMotionStates::FALLING);
}
}
bool AHollowKnight::IsInAir()
{
return (motionState == EMotionStates::JUMPING || motionState == EMotionStates::FALLING || motionState == EMotionStates::RISING);
}
void AHollowKnight::Grounded()
{
ChangeMovement(EMotionStates::GROUNDED);
this->dashCounter = 0;
this->doubleJump = true;
UKismetSystemLibrary::K2_SetTimer(this, TEXT("Ungrounded"), .000001f, false); // timer resets each frame until not on ground
}
void AHollowKnight::OnWall(FVector hitNormal)
{
this->dashCounter = 0;
ChangeMovement(EMotionStates::WALLSLIDE);
this->wallNormal = hitNormal;
this->doubleJump = true;
UKismetSystemLibrary::K2_SetTimer(this, TEXT("Ungrounded"), .000001f, false); // timer resets each frame until not on wall
}
void AHollowKnight::OnHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComponent, FVector NormalImpulse, const FHitResult& Hit)
{
//Check if dot is sqrt(2)/2 (45 degrees)
if (FVector::DotProduct(Hit.ImpactNormal, FVector::UpVector) >= 0.71f) {
Grounded();
}
else if(FMath::Abs(FVector::DotProduct(Hit.ImpactNormal, FVector::RightVector)) >= 0.71f) {
OnWall(Hit.ImpactNormal);
}
}