-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBabyCharacter_C.cpp
523 lines (454 loc) · 14 KB
/
BabyCharacter_C.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
#include "BabyCharacter_C.h"
#include "Engine/Engine.h"
#include "NiagaraSystem.h"
#include "NiagaraComponent.h"
#include "NiagaraFunctionLibrary.h"
#include "ConstructorHelpers.h"
#include "Components/CapsuleComponent.h"
#include "Components/SkeletalMeshComponent.h"
#include "Components/StaticMeshComponent.h"
#include "Components/SkinnedMeshComponent.h"
#include "Components/BoxComponent.h"
#include "Containers/UnrealString.h"
#include "Kismet/KismetMathLibrary.h"
#include "Kismet/GameplayStatics.h"
#include "Materials/MaterialInstanceDynamic.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "Engine/SkeletalMeshSocket.h"
#include "BaseGameMode_C.h"
#include "EffectsManager_C.h"
#include "BabyController_C.h"
#include "Kismet/GameplayStatics.h"
#include "Components/AudioComponent.h"
#include <algorithm>
//*****
// CORE
ABabyCharacter_C::ABabyCharacter_C()
{
PrimaryActorTick.bCanEverTick = true;
// Set the class of the bottle that needs to be picked up
static ConstructorHelpers::FObjectFinder<UClass> sBottleClassFinder(TEXT("/Game/Blueprints/BP_Bottle.BP_Bottle_C"));
m_BottleClass = sBottleClassFinder.Object;
static ConstructorHelpers::FObjectFinder<UStaticMesh> sBottleMeshFinder(TEXT("StaticMesh'/Game/Objects/MilkBottle/SM_MilkBottle.SM_MilkBottle'"));
m_pBottleMesh = sBottleMeshFinder.Object;
auto bottle = CreateDefaultSubobject<ABottle_C>("Bottle");
//Mesh and trigger volume
m_pTagTrigger = CreateDefaultSubobject<UBoxComponent>("TagTrigger");
m_pTagTrigger->SetupAttachment(RootComponent);
m_pTouchTrigger = CreateDefaultSubobject<UBoxComponent>("TouchTrigger");
m_pTouchTrigger->SetupAttachment(RootComponent);
m_pBottle = CreateDefaultSubobject<UStaticMeshComponent>("BottleMesh");
m_pBottle->SetStaticMesh(m_pBottleMesh);
m_pBottle->SetupAttachment(RootComponent);
m_pBottle->RelativeLocation = FVector(-40.f, 0.f, 0.f);
m_pBottle->SetVisibility(false);
//Timer
m_FartTimer = fmod(m_MaxFartTimer, float(rand() % (int)m_MaxFartTimer));
}
void ABabyCharacter_C::BeginPlay()
{
Super::BeginPlay();
ABaseGameMode_C* pGameMode = Cast<ABaseGameMode_C>(GetWorld()->GetAuthGameMode());
if (pGameMode != nullptr)
{
GetMesh()->SetSkeletalMesh(pGameMode->GetPlayerModel());
}
// Get all materials from the mesh, create dynamic instances from them, to use later in invisibility
for (int i = 0; i < GetMesh()->GetMaterials().Num(); ++i)
{
m_DynamicMaterialArray.Add(GetMesh()->CreateAndSetMaterialInstanceDynamic(i));
}
m_pTagTrigger->OnComponentBeginOverlap.AddDynamic(this, &ABabyCharacter_C::OnOverlapBegin);
m_pTouchTrigger->OnComponentBeginOverlap.AddDynamic(this, &ABabyCharacter_C::OnOverlapBegin);
//Get the effects manager
TArray<AActor*> temp{};
UGameplayStatics::GetAllActorsOfClass(GetWorld(), AEffectsManager_C::StaticClass(), temp);
if (temp.Num()) //Safety
m_pEffectsManager = Cast<AEffectsManager_C>(temp[0]);
//AttachFartEffect();
AttachSleepEffect();
}
void ABabyCharacter_C::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
//Double tag
if (m_DoubletagPrevention)
{
m_DoubleTagTimer += DeltaTime;
if (m_DoubleTagTimer >= m_MaxDoubleTagTimer)
{
m_DoubleTagTimer = 0.f;
m_DoubletagPrevention = false;
}
}
#pragma region Effect
//Fart trigger
if (TaggingState::Taggee == m_TaggingState && m_TaggingState != TaggingState::Caught)
{
//Increment the normal timer
m_FartTimer += DeltaTime;
//Activate effect
if (m_FartTimer >= m_MaxFartTimer)
{
m_FartTimer = 0.f;
AttachFartEffect();
PlayFart();
}
}
#pragma endregion Effect
//Bottle reset
if (m_BottleRefilTimer >= 0.0f)
{
m_BottleRefilTimer += DeltaTime;
}
if (m_BottleRefilTimer >= m_MaxBottleRefilTimer)
{
m_BottleRefilTimer = -1.f;
}
//Tagging
if (m_TouchState == TouchState::Tag)
{
m_ExtendTagTimer += DeltaTime;
if (m_ExtendTagTimer >= m_MaxExtendTagTimer)
{
m_TouchState = TouchState::Touch;
if (!m_JustAttacked)
{
m_JustAttacked = false;
}
PlayAttack(m_JustAttacked);
m_JustAttacked = false;
}
}
else
{
if (m_ExtendCoolDown < m_MaxExtendCoolDown)
{
m_ExtendCoolDown += DeltaTime;
}
}
//Opacity
#pragma region opacity
// Fix player invisbility glitch
float movementSpeed = GetVelocity().Size();
m_Invisibility = FMath::Lerp(m_Invisibility, 1 - movementSpeed / m_MaxCrawlingSpeed, m_CurrentTimeDelta);
m_CurrentTimeDelta += DeltaTime;
if (m_CurrentTimeDelta > 1.f)
{
m_CurrentTimeDelta = 0;
}
if (m_PositionHistory.Num() == m_MaxPositionHistory)
{
m_PositionHistory.RemoveAt(0);
}
m_CurrentPosition = GetTransform().GetLocation();
m_PositionHistory.Add(m_CurrentPosition);
if (FVector::Dist2D(UKismetMathLibrary::GetVectorArrayAverage(m_PositionHistory), m_CurrentPosition) < m_PositionDelta)
{
m_Invisibility = std::max(m_Invisibility, 0.7f);
}
if (m_TaggingState == TaggingState::Taggee)
{
// Set the oppacity of the materials of the mesh
for (UMaterialInstanceDynamic* pMaterial : m_DynamicMaterialArray)
{
if (!pMaterial)
continue;
if (m_MovementState == MovementState::Idle)
{
pMaterial->SetScalarParameterValue("Opacity", 1);
}
else
{
pMaterial->SetScalarParameterValue("Opacity", m_Invisibility);
}
}
}
// Update the splatter, if one is present
if (m_CurrentSplatterPercentage > 0)
{
for (UMaterialInstanceDynamic* pMaterial : m_DynamicMaterialArray)
{
if (!pMaterial)
continue;
pMaterial->SetScalarParameterValue("AddedOpacity", m_CurrentSplatterPercentage);
}
m_SplatterTimeRemaining -= DeltaTime;
m_CurrentSplatterPercentage = m_SplatterTimeRemaining / m_MaxSplatterDuration;
}
#pragma endregion opacity
//Update movement
#pragma region movement
if (std::abs(m_AxisVelMult.X) > 0.6f || std::abs(m_AxisVelMult.Y) > 0.6f)
{
SetStance(MovementState::Running);
PlayWalk();
}
else if (std::abs(m_AxisVelMult.X) > 0.01f || std::abs(m_AxisVelMult.Y) > 0.01f)
{
SetStance(MovementState::Walking);
PlayCrawl();
}
else
SetStance(MovementState::Idle);
AddMovement(FVector(m_AxisVelMult, 0.f));
//Rotation setup
if (std::abs(m_TempMovementDir.X > 0.f) || std::abs(m_TempMovementDir.Y) > 0.f)
{
m_LatestMovementDir = m_TempMovementDir;
}
else if (std::abs(m_AxisVelMult.X) > 0.f || std::abs(m_AxisVelMult.Y) > 0.f)
{
m_LatestMovementDir = FVector(m_AxisVelMult, 0.f);
}
this->SetActorRotation(m_LatestMovementDir.ToOrientationQuat());
#pragma endregion movement
}
void ABabyCharacter_C::AddMovement(const FVector& movement)
{
AddMovementInput(FVector(1.f, 0.f, 0.f), movement.X);
AddMovementInput(FVector(0.f, 1.f, 0.f), movement.Y);
}
void ABabyCharacter_C::TeleportToPos(const FVector& pos)
{
TeleportTo(pos, FRotator(0.f, 0.f, 0.f), false, true);
}
void ABabyCharacter_C::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
}
//*******
// EVENTS
void ABabyCharacter_C::OnOverlapBegin(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
if (m_TaggingState != TaggingState::Tagger)
return;
// Other Actor is the actor that triggered the event. Check that is not ourself or nullptr.
if (OtherActor && (OtherActor != this) && OtherComp)
break;
if (OtherComp->ComponentHasTag("Trigger"))
return;
if (OverlappedComp == m_pTagTrigger && m_TouchState == TouchState::Tag)
{
ABabyCharacter_C* pBabyChar = Cast<ABabyCharacter_C>(OtherActor);
if (pBabyChar)
{
if (pBabyChar->GetDoubleTagPrevention())
{
return;
}
m_TagScore++;
FRotator direction = FRotator(180.f, 0.f, 0.f);
FVector offset = FVector(-30,0,-50);
AttachHitEffect(offset, direction);
pBabyChar->SetCaught(true);
Cast<ABabyController_C>(pBabyChar->GetController())->GiveHapticFeedBack();
Cast<ABabyController_C>(this->GetController())->GiveHapticFeedBack();
m_JustAttacked = true;
m_DoubletagPrevention = true;
}
}
else if (OverlappedComp == m_pTouchTrigger && m_TouchState == TouchState::Touch)
{
ABabyCharacter_C* pBabyChar = Cast<ABabyCharacter_C>(OtherActor);
if (pBabyChar)
{
if (pBabyChar->GetDoubleTagPrevention())
{
return;
}
m_TagScore++;
pBabyChar->SetCaught(true);
Cast<ABabyController_C>(pBabyChar->GetController())->GiveHapticFeedBack();
Cast<ABabyController_C>(this->GetController())->GiveHapticFeedBack();
m_JustAttacked = true;
m_DoubletagPrevention = true;
}
}
}
//*******
// STATES
void ABabyCharacter_C::SetStance(const MovementState preferedState)
{
m_MovementState = preferedState;
if (m_MovementState == MovementState::Walking || m_MovementState == MovementState::Idle)
GetCharacterMovement()->MaxWalkSpeed = m_MaxWalkingSpeed;
else
GetCharacterMovement()->MaxWalkSpeed = m_MaxCrawlingSpeed;
}
void ABabyCharacter_C::SetCaught(const bool isCaught)
{
ABaseGameMode_C* pGameModeCast = Cast<ABaseGameMode_C>(GetWorld()->GetAuthGameMode());
//Safety to prevent tagger from getting caught
if (m_TaggingState == TaggingState::Taggee && isCaught)
{
m_TaggingState = TaggingState::Caught;
// Make sure the baby is visible when it's caught
for (UMaterialInstanceDynamic* pMaterial : m_DynamicMaterialArray)
{
if (!pMaterial)
continue;
pMaterial->SetScalarParameterValue("Opacity", 1);
}
pGameModeCast->HandleTag(this, true);
if (pGameModeCast->GetGameType() == GameType::Cap)
{
//Activate the sleeping effect
SetSleepEffectVis(true);
PlaySleeping();
}
}
else if (!isCaught)
{
m_TaggingState = TaggingState::Taggee;
if (pGameModeCast->GetGameType() == GameType::Cap)
{
//Deactivate the sleeping effect
SetSleepEffectVis(false);
}
}
}
void ABabyCharacter_C::SetTagger(const bool isTagger)
{
m_GotTagged = true;
if (isTagger)
{
m_TaggingState = TaggingState::Tagger;
Cast<UStaticMeshComponent>(GetDefaultSubobjectByName("PillowMesh"))->SetVisibility(true);
Cast<UStaticMeshComponent>(GetDefaultSubobjectByName("Pacifier"))->SetVisibility(true);
}
else
{
m_TaggingState = TaggingState::Taggee;
Cast<UStaticMeshComponent>(GetDefaultSubobjectByName("PillowMesh"))->SetVisibility(false);
Cast<UStaticMeshComponent>(GetDefaultSubobjectByName("Pacifier"))->SetVisibility(false);
}
}
bool ABabyCharacter_C::IsPlayerTagger() const
{
if (m_TaggingState == TaggingState::Tagger)
{
return true;
}
return false;
}
bool ABabyCharacter_C::IsPlayerCaught() const
{
if (m_TaggingState == TaggingState::Caught)
{
return true;
}
return false;
}
TouchState ABabyCharacter_C::GetTouchState() const
{
return m_TouchState;
}
float ABabyCharacter_C::GetCurrentSpeed() const
{
return GetVelocity().Size();
}
float ABabyCharacter_C::GetMaxWalkingSpeed() const
{
return m_MaxWalkingSpeed;
}
float ABabyCharacter_C::GetMaxCrawlingSpeed() const
{
return m_MaxCrawlingSpeed;
}
bool ABabyCharacter_C::GetDoubleTagPrevention() const
{
return m_DoubletagPrevention;
}
//*******
// INPUTS
void ABabyCharacter_C::MoveForward(float axis)
{
m_AxisVelMult.X = axis;
}
void ABabyCharacter_C::MoveRight(float axis)
{
m_AxisVelMult.Y = axis;
}
void ABabyCharacter_C::LookForward(float axis)
{
m_TempMovementDir.X = axis;
}
void ABabyCharacter_C::LookRight(float axis)
{
m_TempMovementDir.Y = axis;
}
void ABabyCharacter_C::Tag()
{
if (m_ExtendCoolDown < m_MaxExtendCoolDown && m_TaggingState != TaggingState::Tagger)
return;
if (m_TouchState == TouchState::Touch)
{
m_TouchState = TouchState::Tag;
m_ExtendTagTimer = { 0.f };
m_ExtendCoolDown = { 0.f };
m_JustAttacked = true;
}
}
void ABabyCharacter_C::Throw()
{
if (m_HasBottle && m_TaggingState == TaggingState::Tagger)
{
FActorSpawnParameters spawnParams;
spawnParams.bNoFail = true;
auto bottle = GetWorld()->SpawnActor<ABottle_C>(m_BottleClass, GetTransform().GetLocation() - FVector{ 100.f, 0.f, 0.f }, FRotator::ZeroRotator, spawnParams);
bottle->Launch(m_LatestMovementDir);
m_pBottle->SetVisibility(false);
m_HasBottle = false;
}
}
void ABabyCharacter_C::PickupBottle(ABottle_C* bottle)
{
if (!m_HasBottle)
{
m_pBottle->SetVisibility(true);
m_HasBottle = true;
}
}
//********
// EFFECTS
UNiagaraComponent* ABabyCharacter_C::AttachEffect(const int id, const FName& socketName, const bool autoDel)
{
USkeletalMeshComponent* pMesh{ this->GetMesh() };
UNiagaraSystem* pNiagaraSys{ m_pEffectsManager->GetParticleByID(id) };
const USkeletalMeshSocket* pSocket{ this->GetMesh()->GetSocketByName(socketName) };
return UNiagaraFunctionLibrary::SpawnSystemAttached(
pNiagaraSys, pMesh, socketName,
pSocket->GetSocketLocation(this->GetMesh()),
FRotator::ZeroRotator,
EAttachLocation::Type::SnapToTargetIncludingScale,
autoDel);
}
void ABabyCharacter_C::AttachFartEffect(const FVector& offset, const FRotator& dir)
{
//Farting ID = 0, text = Fart
m_FartActor = AttachEffect(0, "Body_Fart_Effect_Socket", true);
m_FartActor->SetRelativeLocationAndRotation(offset, dir);
}
void ABabyCharacter_C::AttachSleepEffect(const FVector& offset, const FRotator& dir)
{
//Farting ID = 1, text = Sleeping
m_SleepActor = AttachEffect(1, "Head_Sleep_Effect_Socket", false);
m_SleepActor->SetRelativeLocationAndRotation(offset, dir);
SetSleepEffectVis(false);
}
void ABabyCharacter_C::AttachHitEffect(const FVector& offset, const FRotator& dir)
{
//Farting ID = 6, text = Puff
m_SleepActor = AttachEffect(6, "Socket_Palm", true);
m_SleepActor->SetRelativeLocationAndRotation(offset, dir);
}
void ABabyCharacter_C::SetSleepEffectVis(bool active)
{
m_SleepActor->SetVisibility(active);
}
void ABabyCharacter_C::SetSplatter(float percentage)
{
m_CurrentSplatterPercentage = percentage;
m_SplatterTimeRemaining = m_MaxSplatterDuration;
}