/
SBombActor.cpp
139 lines (105 loc) · 3.2 KB
/
SBombActor.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
// Copyright 1998-2015 Epic Games, Inc. All Rights Reserved.
#include "Items/SBombActor.h"
#include "TimerManager.h"
#include "Particles/ParticleSystemComponent.h"
#include "Components/AudioComponent.h"
#include "GameFramework/Actor.h"
#include "Kismet/GameplayStatics.h"
#include "Sound/SoundCue.h"
ASBombActor::ASBombActor()
{
FuzePCS = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("Fuze"));
FuzePCS->bAutoActivate = false;
FuzePCS->bAutoDestroy = false;
FuzePCS->SetupAttachment(RootComponent);
ExplosionPCS = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("Explosion"));
ExplosionPCS->bAutoActivate = false;
ExplosionPCS->bAutoDestroy = false;
ExplosionPCS->SetupAttachment(RootComponent);
AudioComp = CreateDefaultSubobject<UAudioComponent>(TEXT("AudioComp"));
AudioComp->bAutoActivate = false;
AudioComp->bAutoDestroy = false;
AudioComp->SetupAttachment(RootComponent);
// Let the bomb be thrown and roll around
MeshComp->SetSimulatePhysics(true);
MaxFuzeTime = 3.0f;
ExplosionDamage = 200;
ExplosionRadius = 600;
SetReplicates(true);
SetReplicatingMovement(true);
}
void ASBombActor::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
Super::EndPlay(EndPlayReason);
// Ensure the fuze timer is cleared
GetWorld()->GetTimerManager().ClearTimer(FuzeTimerHandle);
// Alternatively you can clear ALL timers.
/*GetWorld()->GetTimerManager().ClearAllTimersForObject(this);*/
}
void ASBombActor::OnUsed(APawn* InstigatorPawn)
{
if (bIsFuzeActive)
{
return;
}
Super::OnUsed(InstigatorPawn);
bIsFuzeActive = true;
// Runs on all clients (NetMulticast)
SimulateFuzeFX();
// Activate the fuze to explode the bomb after several seconds
GetWorldTimerManager().SetTimer(FuzeTimerHandle, this, &ASBombActor::Explode, MaxFuzeTime, false);
}
void ASBombActor::Explode()
{
if (bExploded)
{
return;
}
bExploded = true;
// Runs on all clients (NetMulticast)
SimulateExplosion();
// Apply damage to player, enemies and environmental objects
TArray<AActor*> IgnoreActors;
UGameplayStatics::ApplyRadialDamage(this, ExplosionDamage, GetActorLocation(), ExplosionRadius, DamageType, IgnoreActors, this, nullptr);
//DrawDebugSphere(GetWorld(), GetActorLocation(), ExplosionRadius, 32, FColor::Red, false, 1.5f);
/* Allow clients to show FX before deleting */
SetLifeSpan(2.0f);
}
void ASBombActor::SimulateFuzeFX_Implementation()
{
if (FuzeSound)
{
AudioComp->SetSound(FuzeSound);
AudioComp->FadeIn(0.25f);
}
if (FuzeFX)
{
FuzePCS->SetTemplate(FuzeFX);
FuzePCS->ActivateSystem();
}
}
void ASBombActor::SimulateExplosion_Implementation()
{
// First deactivate all running fuze effects
FuzePCS->DeactivateSystem();
AudioComp->Stop();
MeshComp->SetVisibility(false, false);
// Activate all explosion effects
if (ExplosionSound)
{
AudioComp->SetSound(ExplosionSound);
AudioComp->Play();
}
if (ExplosionFX)
{
ExplosionPCS->SetTemplate(ExplosionFX);
ExplosionPCS->ActivateSystem();
}
// For Blueprint
K2_SimulateExplosion();
}
float ASBombActor::TakeDamage(float Damage, struct FDamageEvent const& DamageEvent, class AController* EventInstigator, class AActor* DamageCauser)
{
Explode();
return Super::TakeDamage(Damage, DamageEvent, EventInstigator, DamageCauser);
}