-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGiantMissile.cpp
397 lines (336 loc) · 12.1 KB
/
GiantMissile.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
// Fill out your copyright notice in the Description page of Project Settings.
#include "Leviathan.h"
#include "Kismet/KismetMathLibrary.h"
#include "GiantMissile.h"
AGiantMissile::AGiantMissile()
{
PrimaryActorTick.bCanEverTick = true;
SetActorEnableCollision(true);
StaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMesh"));
SetRootComponent(StaticMesh);
JetParticleComp = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("ParticleComp"));
JetParticleComp->SetupAttachment(RootComponent);
EMPParticleComp = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("EMPParticleComp"));
EMPParticleComp->SetupAttachment(RootComponent);
EMPParticleComp->bAutoActivate = false;
JetLight = CreateDefaultSubobject<UPointLightComponent>(TEXT("JetLight"));
JetLight->SetupAttachment(RootComponent);
JetLight->Intensity = 500.0f;
JetLight->AttenuationRadius = 10000.0f;
JetLight->SetRelativeLocation(FVector(-1350, 0, 0));
WarheadCollider = CreateDefaultSubobject<USphereComponent>(TEXT("WarheadCollider"));
//WarheadCollider->SetSphereRadius(10.0f);
WarheadCollider->AttachToComponent(StaticMesh, FAttachmentTransformRules::KeepRelativeTransform);
WarheadCollider->OnComponentBeginOverlap.AddDynamic(this, &AGiantMissile::OnWarheadBeginOverlap);
ObjectiveMarker = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("ObjectiveMarker"));
ObjectiveMarker->SetupAttachment(RootComponent);
ObjectiveMarker->SetVisibility(true);
AudioComp = CreateDefaultSubobject<UAudioComponent>(TEXT("AudioComp"));
AudioComp->SetupAttachment(RootComponent);
}
void AGiantMissile::BeginPlay()
{
Super::BeginPlay();
TurningSpeed = FMath::RandRange(TurningSpeed * 0.9f, TurningSpeed * 1.9f);
Dest = (GetActorForwardVector() * 20000) - GetActorLocation();
if (FlySoundbase)
{
AudioComp->SetSound(FlySoundbase);
if (AudioComp->Sound)
AudioComp->Play();
}
UMyGameInstance* GameInstance = Cast<UMyGameInstance>(GetGameInstance());
AudioComp->SetVolumeMultiplier(GameInstance->Volume);
}
void AGiantMissile::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
SparkTimer += DeltaTime;
SightTimer += DeltaTime;
TimeSinceFired += DeltaTime;
PlayerActual = Cast<ALeviathanCharacter>(GetWorld()->GetFirstPlayerController()->GetCharacter());
//handles draw of objective marker over missile
ShouldMissileObjMarkerBeVisible();
if (!bAlive)
{
HandleEMP(DeltaTime);
}
else
{
// Missile is born with propulsion, then tracks after delay
if (ActivationDelay >= TimeSinceFired)
{
StaticMesh->AddForce(StaticMesh->GetForwardVector() * (BaseSpeed + SpeedDynamic));
}
else if (PlayerActual)
{
// Active Missile -> Check LOS, set Dest for FlightDynamics below
if (PlayerActual)
{
if (!PlayerActual->IsAlive())
{
Dest = (GetActorLocation() + FVector::UpVector * 9000) - GetActorLocation();
}
else if (!bDomesticated && SightTimer >= (1 / ReactionTime))
{
if (LineOfSight())
{
Dest = PlayerActual->GetActorLocation();
}
else
{
Dest = Redirection; /// set in LineOfSight() below
}
SightTimer = 0.0f;
}
// Get domesticated by player (done by proximity)
Domesticate(DeltaTime);
// Fly to either goal
FlightDynamics(Dest, DeltaTime);
}
}
}
}
void AGiantMissile::Domesticate(float DeltaTime)
{
// Process is done by distance to Player
float DistToPlayer = FVector::Dist(GetActorLocation(), PlayerActual->GetActorLocation());
// Be a good missile
if (bDomesticated || (DistToPlayer <= 850 && bAlive))
{
DomesticationTimer += DeltaTime;
if (DomesticationTimer >= 1.1f && PlayerActual->bHooked)
{
bDomesticated = true;
StaticMesh->SetEnableGravity(false);
Dest = GetActorLocation() + (8000 * PlayerActual->GetCamera()->GetForwardVector());
}
}
else if (DomesticationTimer > 0.0f)
{
DomesticationTimer -= DeltaTime;
}
// Un-Domesticate
if (DistToPlayer >= 3333.0f)
{
bDomesticated = false;
StaticMesh->SetEnableGravity(true);
}
}
void AGiantMissile::FlightDynamics(FVector goalTarget, float deltaT)
{
// Get flyable vector
FVector DestPosition = goalTarget;
FVector ToDest = goalTarget - GetActorLocation();
float DistanceToGoal = ToDest.Size();
if (DistanceToGoal >= DamageOuterRadius)
{
// Create 'pilot feel' by counteracting our velocity
FVector CurrentVelocity = StaticMesh->GetComponentVelocity() - ToDest;
CurrentVelocity.X *= 0.0f; /// prevents missile turning away at the moment of le kill
CurrentVelocity.Z *= 1.333f;
DestPosition -= (CurrentVelocity * (DistanceToGoal / 2000.0f));
// Regard gravity
if (DistanceToGoal >= (DamageOuterRadius * 3.0f))
DestPosition += (FVector::UpVector * CounterGravityEffort);
}
// Rotate smooth to DestPosition
FRotator RoteToGoal = UKismetMathLibrary::FindLookAtRotation(GetActorLocation(), DestPosition);
RoteToGoal = FMath::RInterpTo(GetActorRotation(), RoteToGoal, deltaT, TurningSpeed);
RootComponent->SetWorldRotation(RoteToGoal);
// Prepare dynamic speed based on nose-angle to Player
FVector VecToGoal = (PlayerActual->GetActorLocation() - GetActorLocation()).GetSafeNormal();
FVector LocalFord = StaticMesh->GetForwardVector();
float tempAngle = FMath::RadiansToDegrees(FMath::Acos(FVector::DotProduct(VecToGoal, LocalFord)));
SpeedDynamic = (BaseSpeed / (tempAngle + 0.1f)) * KillVectorAcceleration;
//GEngine->AddOnScreenDebugMessage(-1, 0.01f, FColor::Green, FString::Printf(TEXT("Speed: %f"), SpeedDynamic / 100));
// Propulse forward
StaticMesh->AddForce(StaticMesh->GetForwardVector() * (BaseSpeed + SpeedDynamic));
}
// LOS -- handles Redirection
bool AGiantMissile::LineOfSight()
{
// Prepare variables
bool result = false;
FVector ToPlayer = PlayerActual->GetActorLocation() - GetActorLocation();
float DistanceToPlayer = FVector::Dist(GetActorLocation(), PlayerActual->GetActorLocation());
// do LOS by raycast
if (DistanceToPlayer <= VisionRange)
{
FHitResult Hit;
FCollisionQueryParams SelflessQuery;
SelflessQuery.AddIgnoredActor(this);
FVector TraceStart = GetActorLocation();
FVector TraceEnd = PlayerActual->GetActorLocation();
// Cast that trace
GetWorld()->LineTraceSingleByChannel(Hit, TraceStart, TraceEnd, ECC_Pawn,
SelflessQuery, FCollisionResponseParams::DefaultResponseParam);
///DrawDebugLine(GetWorld(), TraceStart, TraceEnd, FColor::White, false, 0.2f, 0, 5.0f);
if (Hit.Actor == PlayerActual)
{ result = true; }
else
{
FVector Detour = Hit.Normal * 10000.0f + ToPlayer + (FVector::UpVector * 10000.0f); /// create detour around obstruction
Redirection = Detour;
//DrawDebugLine(GetWorld(), GetActorLocation(), Detour, FColor::Green, false, 0.1f, 0, 10.f);
}
}
return result;
}
void AGiantMissile::EMPShock()
{
bAlive = false;
bPreviouslyHit = true;
JetLight->Intensity = 100.0f;
JetParticleComp->Deactivate();
EMPParticleComp->SetActive(true);
StaticMesh->SetLinearDamping(0.2f);
StaticMesh->SetAngularDamping(0.1f);
StaticMesh->AddAngularImpulse(FVector(2.0f, 2.0f, 0.5f) * BaseSpeed);
AudioComp->Stop();
if (EMPSoundbase)
{
AudioComp->SetSound(EMPSoundbase);
AudioComp->Play();
}
}
void AGiantMissile::ShouldMissileObjMarkerBeVisible()
{
ObjectiveMarker->SetVisibility(PlayerActual->bMissileObjMarkerShouldShow);
}
void AGiantMissile::EMPRecover()
{
bAlive = true;
JetLight->Intensity = 500.0f;
EMPParticleComp->SetActive(false);
JetParticleComp->SetActive(true);
StaticMesh->SetLinearDamping(2.0f);
StaticMesh->SetAngularDamping(1.3f);
AudioComp->Stop();
if (FlySoundbase)
{
AudioComp->SetSound(FlySoundbase);
AudioComp->Play();
}
EMPTimer = 0.0f;
}
void AGiantMissile::HandleEMP(float deltaT)
{
EMPTimer += deltaT;
if (EMPTimer >= EMPDownTime)
EMPRecover();
/// extra gravity for effect
StaticMesh->AddForce(FVector::UpVector * -BaseSpeed * 0.279f);
}
// Make sparks!!
void AGiantMissile::ReceiveHit(UPrimitiveComponent* MyComp, AActor* Other, UPrimitiveComponent* OtherComp, bool bSelfMoved, FVector HitLocation, FVector HitNormal, FVector NormalImpulse, const FHitResult& Hit)
{
if (OtherComp->ComponentHasTag(FName("EMP")) && bAlive)
{
EMPShock();
}
else
{
/// collision sound
if (SparkTimer >= (0.2f / SparksPerSecond))
{
int arraySize = CollideSounds.Num();
int choice = FMath::FloorToInt(FMath::FRandRange(0, arraySize - 1));
AudioComp->SetSound(CollideSounds[choice]);
AudioComp->Play();
}
/// SPARKS
if ((StaticMesh->GetComponentVelocity().Size() > 100.0f)
&& (SparkTimer >= (1 / SparksPerSecond)))
{
FActorSpawnParameters SpawnPars;
AParticleSource* Sparks =
Cast<AParticleSource>(GetWorld()->SpawnActor<AParticleSource>
(ParticleSparks, HitLocation, (StaticMesh->ComponentVelocity.GetSafeNormal() + HitNormal).Rotation(), SpawnPars)
);
if (Sparks)
SparkTimer = 0.0f;
}
}
}
void AGiantMissile::OnWarheadBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
if (OtherComp->ComponentHasTag("Bridge"))
{
///GEngine->AddOnScreenDebugMessage(-1, 1.0f, FColor::Red, TEXT("oh look a bridge"));
}
if (OtherComp->ComponentHasTag(FName("EMP")) && bAlive)
{
EMPShock();
bAlive = false;
}
// Detonation
else if (bAlive && (!OtherComp->ComponentHasTag(FName("Camera"))) && OtherActor != NULL && OtherComp->GetOwner() != this) /// bAlive represents active EMP effects
{
// DAMAGE
if (PlayerActual && LineOfSight())
{
TArray<AActor*> IgnoredActors;
IgnoredActors.Add(this);
IgnoredActors.Add(Instigator);
UGameplayStatics::ApplyRadialDamageWithFalloff(
GetWorld(),
BaseDamage,
MinimumDamage,
GetActorLocation(),
DamageInnerRadius,
DamageOuterRadius,
DamageFalloff,
UDamageType::StaticClass(),
IgnoredActors,
this,
Instigator != NULL ? Instigator->GetController() : NULL,
ECC_Visibility);
// PHYSICS FORCE
TArray<TEnumAsByte<EObjectTypeQuery>> TraceObjects;
TraceObjects.Add(UEngineTypes::ConvertToObjectType(ECC_PhysicsBody));
TArray<AActor*> SphereHits;
TArray<AActor*> ActorsIgnored;
ActorsIgnored.Add(this);
if (UKismetSystemLibrary::SphereOverlapActors_NEW(
GetWorld(), RootComponent->GetComponentLocation(), DamageOuterRadius,
TraceObjects, AActor::StaticClass(), ActorsIgnored, SphereHits))
{
for (int i = 0; i < SphereHits.Num(); i++)
{
UStaticMeshComponent* SM = Cast<UStaticMeshComponent>((SphereHits[i])->GetRootComponent());
SM->AddRadialImpulse(GetActorLocation(), DamageOuterRadius, ExplosionForce * 1.5f, ERadialImpulseFalloff::RIF_Linear, true);
}
}
// PLAYER FORCE
float DistToPlayer = FVector::Dist(PlayerActual->GetActorLocation(), GetActorLocation());
if (PlayerActual && DistToPlayer <= DamageOuterRadius)
PlayerActual->GetMovementComponent()->AddRadialImpulse(GetActorLocation(), DamageOuterRadius, ExplosionForce, ERadialImpulseFalloff::RIF_Linear, true);
}
AudioComp->Stop();
if (DeathSoundbase)
{
AudioComp->SetSound(DeathSoundbase);
AudioComp->Play();
}
// PARTICLES
if (UWorld* ThisWorld = GetWorld())
{
FActorSpawnParameters SpawnParameters;
Explosion = Cast<AParticleSource>(ThisWorld->SpawnActor<AParticleSource>(ParticleExplosion,
GetActorLocation(), SweepResult.Normal.Rotation(), SpawnParameters));
class AAmbientSound* ExplosionSoundRef = Cast<AAmbientSound>(ThisWorld->SpawnActor<AAmbientSound>(ExplosionSound, GetActorLocation(), SweepResult.Normal.Rotation(), SpawnParameters));
if (ExplosionSoundRef)
{
UMyGameInstance* GameInstance = Cast<UMyGameInstance>(GetGameInstance());
ExplosionSoundRef->GetAudioComponent()->SetVolumeMultiplier(GameInstance->Volume);
ExplosionSoundRef->GetAudioComponent()->SetSound(DeathSoundbase);
ExplosionSoundRef->GetAudioComponent()->Play();
}
if (Explosion)
{
Destroy();
}
}
}
}