-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProjectile.cpp
More file actions
76 lines (63 loc) · 3.11 KB
/
Copy pathProjectile.cpp
File metadata and controls
76 lines (63 loc) · 3.11 KB
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
// Fill out your copyright notice in the Description page of Project Settings.
#include "Projectile.h"
#include "Components/StaticMeshComponent.h"
#include "GameFramework/ProjectileMovementComponent.h"
#include "DrawDebugHelpers.h"
#include "GameFramework/DamageType.h"
#include "Kismet/GameplayStatics.h"
#include "Particles/ParticleSystemComponent.h"
// Sets default values
AProjectile::AProjectile()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = false;//does not need to be true
ProjectileMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Projectile Mesh"));
RootComponent = ProjectileMesh;
ProjectileMovementComponent = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("Projectile Movement Component"));
ProjectileMovementComponent->MaxSpeed = 1500.f;
ProjectileMovementComponent->InitialSpeed = 1500.f;
TrailParticles = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("SmokeTrail"));
TrailParticles->SetupAttachment(RootComponent);
}
// Called when the game starts or when spawned
void AProjectile::BeginPlay()
{
Super::BeginPlay();
//we will perform hit event in begin play
ProjectileMesh->OnComponentHit.AddDynamic(this,&AProjectile::OnHit);
//to play the sound at any location lauch sound would be play at the starting
if (LaunchSound) {
UGameplayStatics::PlaySoundAtLocation(this, LaunchSound, GetActorLocation());
}
}
// Called every frame
void AProjectile::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void AProjectile::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpluse, const FHitResult& Hit) {
//UE_LOG(LogTemp,Warning,TEXT("OnHit"));
AActor* MyOwner = GetOwner();//we set the owner in the base pawn class that is the blueprint projectile that we drived from this call and we need the instigator controller means the player controller who shot the projectile
if (MyOwner == nullptr)
{
Destroy();
return;
}
AController* MyOwnerInstigator = MyOwner->GetInstigatorController();
UClass* DamageTypeClass=UDamageType::StaticClass();;
//checkinf that
if (OtherActor && OtherActor != this && OtherActor != MyOwner) {
UGameplayStatics::ApplyDamage(OtherActor, Damage, MyOwnerInstigator, this,DamageTypeClass );//this for the projectile itself the damage causer and in the end we will pass damage type class
if (HitParticles) {
UGameplayStatics::SpawnEmitterAtLocation(this, HitParticles, GetActorLocation(), GetActorRotation());//first parameter is world context so we will pass this , second is Hit particle ,this function is for showing the fire hit effect at any location and rotation
}
//to play the sound at any location
if (HitSound) {
UGameplayStatics::PlaySoundAtLocation(this, HitSound, GetActorLocation());
}//hit sound willl be played on hitting
if (HitCameraShakeClass) {
GetWorld()->GetFirstPlayerController()->ClientStartCameraShake(HitCameraShakeClass);//to implement function below we need player controller so
}
}
Destroy();
}