-
Notifications
You must be signed in to change notification settings - Fork 0
/
SAction_BossStunProjectile.cpp
99 lines (74 loc) · 3.48 KB
/
SAction_BossStunProjectile.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
#include "SAction_BossStunProjectile.h"
#include "Kismet/GameplayStatics.h"
#include "Sound/SoundBase.h"
#include "GameFramework/Character.h"
#include "SActionComponent.h"
#include "SActionEffect.h"
USAction_BossStunProjectile::USAction_BossStunProjectile()
{
HandSocketName = "Muzzle_01";
AttackAnimDelay = 0.2f;
SweepRadius = 20.0f;
SweepDistanceFallback = 5000;
}
void USAction_BossStunProjectile::StartAction_Implementation(AActor* Instigator)
{
Super::StartAction_Implementation(Instigator);
USActionComponent* ActionComponent = Cast<USActionComponent>(Instigator->GetComponentByClass(USActionComponent::StaticClass()));
if (ActionComp && CoolDownActionClass)
{
ActionComponent->AddAction(Instigator, CoolDownActionClass);
}
ACharacter* Character = Cast<ACharacter>(Instigator);
if (Character)
{
Character->PlayAnimMontage(AttackAnim);
UGameplayStatics::SpawnEmitterAttached(CastingEffect, Character->GetMesh(), HandSocketName, FVector::ZeroVector, FRotator::ZeroRotator, EAttachLocation::SnapToTarget);
UGameplayStatics::SpawnSoundAttached(CastingSound, Character->GetMesh());
if (Character->HasAuthority())
{
FTimerHandle TimerHandle_AttackDelay;
FTimerDelegate Delegate;
Delegate.BindUFunction(this, "AttackDelay_Elapsed", Character);
GetWorld()->GetTimerManager().SetTimer(TimerHandle_AttackDelay, Delegate, AttackAnimDelay, false);
}
}
}
void USAction_BossStunProjectile::AttackDelay_Elapsed(ACharacter* InstigatorCharacter)
{
if (ensureAlways(ProjectileClass))
{
FVector HandLocation = InstigatorCharacter->GetMesh()->GetSocketLocation(HandSocketName);
//Increase spawn height to avoid exploding on floor on spawn
HandLocation.Z += 100.0f;
// We trace against the environment first to find whats under the player crosshair.
// We use the hit location to adjust the projectile launch direction so it will hit what is under the crosshair rather than shoot straight forward from the player hands.
FActorSpawnParameters SpawnParams;
SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
SpawnParams.Instigator = InstigatorCharacter;
FCollisionShape Shape;
Shape.SetSphere(SweepRadius);
// Ignore Player
FCollisionQueryParams Params;
Params.AddIgnoredActor(InstigatorCharacter);
FVector TraceDirection = InstigatorCharacter->GetControlRotation().Vector();
// Add sweep radius onto start to avoid the sphere clipping into floor/walls the camera is directly against.
FVector TraceStart = InstigatorCharacter->GetPawnViewLocation() + (TraceDirection * SweepRadius);
// endpoint far into the look-at distance (not too far, still adjust somewhat towards crosshair on a miss)
FVector TraceEnd = TraceStart + (TraceDirection * SweepDistanceFallback);
FHitResult Hit;
// returns true if we got to a blocking hit (Channel1="Projectile" defined in DefaultGame.ini)
if (GetWorld()->SweepSingleByChannel(Hit, TraceStart, TraceEnd, FQuat::Identity, ECC_GameTraceChannel1, Shape, Params))
{
// Overwrite trace end with impact point in world
TraceEnd = Hit.ImpactPoint;
}
// find new direction/rotation from Hand pointing to impact point in world.
FRotator ProjRotation = (TraceEnd - HandLocation).Rotation();
const FRotator BaseRotation = (TraceEnd - HandLocation).Rotation();
ProjRotation = FRotator(0, BaseRotation.Yaw, 0);
FTransform SpawnTM = FTransform(ProjRotation, HandLocation);
GetWorld()->SpawnActor<AActor>(ProjectileClass, SpawnTM, SpawnParams);
}
StopAction(InstigatorCharacter);
}