/
BaseProjectile.cpp
97 lines (79 loc) · 3.13 KB
/
BaseProjectile.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
// Copyright 1998-2015 Epic Games, Inc. All Rights Reserve
#include "TemporalFluxArena.h"
#include "BaseProjectile.h"
#include "Ship.h"
#include "GameFramework/ProjectileMovementComponent.h"
ABaseProjectile::ABaseProjectile()
{
speed = 3000.0f;
// Static reference to the mesh to use for the projectile
static ConstructorHelpers::FObjectFinder<UStaticMesh> ProjectileMeshAsset(TEXT("/Game/TwinStick/Meshes/TwinStickProjectile.TwinStickProjectile"));
// Create mesh component for the projectile sphere
ProjectileMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("ProjectileMesh0"));
ProjectileMesh->SetStaticMesh(ProjectileMeshAsset.Object);
ProjectileMesh->AttachTo(RootComponent);
ProjectileMesh->BodyInstance.SetCollisionProfileName("Projectile");
ProjectileMesh->OnComponentHit.AddDynamic(this, &ABaseProjectile::OnHit); // set up a notification for when this component hits something
RootComponent = ProjectileMesh;
// Use a ProjectileMovementComponent to govern this projectile's movement
ProjectileMovement = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("ProjectileMovement0"));
ProjectileMovement->UpdatedComponent = ProjectileMesh;
ProjectileMovement->InitialSpeed = 3000.f;
ProjectileMovement->MaxSpeed = 3000.f;
ProjectileMovement->bRotationFollowsVelocity = true;
ProjectileMovement->bShouldBounce = false;
ProjectileMovement->ProjectileGravityScale = 0.f; // No gravity
// Die after 3 seconds by default
InitialLifeSpan = 3.0f;
damage = 10;
}
// Called when the game starts or when spawned
void ABaseProjectile::BeginPlay()
{
Super::BeginPlay();
}
/**
* Function to handle the projectile hitting something
*/
void ABaseProjectile::OnHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComponent, FVector NormalImpulse, const FHitResult& Hit)
{
// Only add impulse and destroy projectile if we hit a physics
if ((OtherActor != NULL) && (OtherActor != this))
{
UClass* otherClass = OtherActor->GetClass();
/*if (OtherComp->IsSimulatingPhysics()) {
OtherComp->AddImpulseAtLocation(GetVelocity() * 20.0f, GetActorLocation());
}*/
if (otherClass->IsChildOf(AShip::StaticClass())){
AShip* otherShip = Cast<AShip>(OtherActor);
otherShip->HitByProjectile(this);
Destroy();
}
else if (!otherClass->IsChildOf(ABaseProjectile::StaticClass())
&& !otherClass->IsChildOf(USphereComponent::StaticClass())
&& !otherClass->IsChildOf(UStaticMeshComponent::StaticClass())){
Destroy();
}
}
}
ABaseProjectile* ABaseProjectile::CreateProjectile(AActor* other, float damage, float speed, int size){
ABaseProjectile* newProjectile = other->GetWorld()->SpawnActor<ABaseProjectile>(other->GetActorLocation(), other->GetActorRotation());
newProjectile->SetDamage(damage);
newProjectile->size = size;
newProjectile->speed = speed;
return newProjectile;
}
float ABaseProjectile::GetDamage(){
return damage;
}
void ABaseProjectile::SetDamage(float newDamage){
damage = newDamage;
}
float ABaseProjectile::GetSpeed(){
return speed;
}
void ABaseProjectile::SetSpeed(float newVal){
speed = newVal;
ProjectileMovement->InitialSpeed = speed;
ProjectileMovement->MaxSpeed = speed;
}