You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
140 lines
5.4 KiB
140 lines
5.4 KiB
2 years ago
|
// Fill out your copyright notice in the Description page of Project Settings.
|
||
|
|
||
|
|
||
2 years ago
|
#include "Projectile.h"
|
||
2 years ago
|
|
||
2 years ago
|
#include "Trooper.h"
|
||
2 years ago
|
#include "Net/UnrealNetwork.h"
|
||
|
|
||
2 years ago
|
AProjectile::AProjectile() {
|
||
2 years ago
|
// if (!RootComponent) {
|
||
|
// RootComponent = CreateDefaultSubobject<USceneComponent>(
|
||
|
// TEXT("ProjectileSceneComponent"));
|
||
|
// }
|
||
2 years ago
|
// if (!CollisionComponent) {
|
||
|
// CollisionComponent = CreateDefaultSubobject<USphereComponent>(
|
||
|
// TEXT("SphereComponent"));
|
||
2 years ago
|
// RootComponent = CollisionComponent;
|
||
2 years ago
|
// }
|
||
|
if (!ProjectileMeshComponent) {
|
||
|
ProjectileMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(
|
||
|
TEXT("ProjectileMeshComponent"));
|
||
|
static ConstructorHelpers::FObjectFinder<UStaticMesh> Mesh(
|
||
|
TEXT(
|
||
|
"StaticMesh'/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere'"));
|
||
|
if (Mesh.Succeeded()) {
|
||
|
ProjectileMeshComponent->SetStaticMesh(Mesh.Object);
|
||
|
RootComponent = ProjectileMeshComponent;
|
||
|
}
|
||
|
}
|
||
|
if (!ProjectileMovementComponent) {
|
||
|
ProjectileMovementComponent = CreateDefaultSubobject<
|
||
|
UProjectileMovementComponent>(TEXT("ProjectileMovementComponent"));
|
||
|
ProjectileMovementComponent->SetUpdatedComponent(
|
||
2 years ago
|
// CollisionComponent);
|
||
2 years ago
|
ProjectileMeshComponent);
|
||
2 years ago
|
ProjectileMovementComponent->bRotationFollowsVelocity = true;
|
||
2 years ago
|
ProjectileMovementComponent->InitialSpeed = 1000.0f;
|
||
|
ProjectileMovementComponent->MaxSpeed = 1000.0f;
|
||
|
ProjectileMovementComponent->ProjectileGravityScale = 0.0f;
|
||
|
}
|
||
|
InitialLifeSpan = 2.0f;
|
||
|
}
|
||
|
|
||
2 years ago
|
void AProjectile::Initialize(const UAbility *Ability,
|
||
2 years ago
|
uint8 playerIndex,
|
||
|
float PathLength) {
|
||
|
ProjectileMovementComponent->InitialSpeed =
|
||
|
ProjectileMovementComponent->MaxSpeed = Ability->Speed;
|
||
|
Damage = Ability->Damage;
|
||
2 years ago
|
SplashRadius = Ability->SplashRadius;
|
||
2 years ago
|
float Scale = Ability->LinearWidth / 100;
|
||
2 years ago
|
// CollisionComponent->SetSphereRadius(Ability->LinearWidth / 2);
|
||
2 years ago
|
ProjectileMeshComponent->SetWorldScale3D({Scale, Scale, Scale});
|
||
|
PlayerIndex = playerIndex;
|
||
|
SetLifeSpan(PathLength / Ability->Speed);
|
||
|
}
|
||
|
|
||
2 years ago
|
void AProjectile::Shoot(FVector From, FVector To) const {
|
||
2 years ago
|
ProjectileMovementComponent->Velocity =
|
||
|
(To - From).GetSafeNormal() * ProjectileMovementComponent->InitialSpeed;
|
||
|
}
|
||
|
|
||
2 years ago
|
void AProjectile::NotifyActorBeginOverlap(AActor *OtherActor) {
|
||
2 years ago
|
Super::NotifyActorBeginOverlap(OtherActor);
|
||
|
ATrooper *OtherTrooper = Cast<ATrooper>(OtherActor);
|
||
|
if (OtherTrooper) {
|
||
|
UE_LOG(LogTemp, Warning,
|
||
|
TEXT("Begin overlap: id: %d, index: %d, damage: %f, my index: %d"
|
||
|
),
|
||
|
OtherTrooper->GetId(), OtherTrooper->GetPlayerIndex(), Damage,
|
||
|
PlayerIndex);
|
||
|
if (PlayerIndex != -1 && PlayerIndex != OtherTrooper->
|
||
|
GetPlayerIndex()) {
|
||
2 years ago
|
OtherTrooper->TrooperTakeDamage(Damage);
|
||
2 years ago
|
}
|
||
|
} else {
|
||
|
UE_LOG(LogTemp, Warning, TEXT("Overlapped not a trooper"));
|
||
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
// void AMyProjectile::NotifyHit(UPrimitiveComponent *MyComp,
|
||
|
// AActor *Other,
|
||
|
// UPrimitiveComponent *OtherComp,
|
||
|
// bool bSelfMoved,
|
||
|
// FVector HitLocation,
|
||
|
// FVector HitNormal,
|
||
|
// FVector NormalImpulse,
|
||
|
// const FHitResult &Hit) {
|
||
|
// Super::NotifyHit(MyComp, Other, OtherComp, bSelfMoved, HitLocation,
|
||
|
// HitNormal,
|
||
|
// NormalImpulse, Hit);
|
||
|
// ATrooper *OtherTrooper = Cast<ATrooper>(Other);
|
||
|
// if (OtherTrooper) {
|
||
|
// UE_LOG(LogTemp, Warning,
|
||
|
// TEXT("On Hit: id: %d, index: %d, damage: %f, my index: %d"
|
||
|
// ),
|
||
|
// OtherTrooper->GetId(), OtherTrooper->GetPlayerIndex(), Damage,
|
||
|
// PlayerIndex);
|
||
|
// if (PlayerIndex != -1 && PlayerIndex != OtherTrooper->
|
||
|
// GetPlayerIndex()) {
|
||
|
// OtherTrooper->TakeDamage(Damage);
|
||
|
// }
|
||
|
// } else {
|
||
|
// UE_LOG(LogTemp, Warning, TEXT("Overlapped not a trooper"));
|
||
|
// }
|
||
|
// }
|
||
2 years ago
|
|
||
2 years ago
|
|
||
2 years ago
|
void AProjectile::BeginPlay() {
|
||
2 years ago
|
Super::BeginPlay();
|
||
|
}
|
||
|
|
||
2 years ago
|
void AProjectile::EndPlay(const EEndPlayReason::Type EndPlayReason) {
|
||
2 years ago
|
Super::EndPlay(EndPlayReason);
|
||
|
Explode();
|
||
|
}
|
||
|
|
||
2 years ago
|
void AProjectile::Explode_Implementation() const {
|
||
2 years ago
|
const FTransform SpawnTransform = GetTransform();
|
||
|
FActorSpawnParameters SpawnParameters;
|
||
|
SpawnParameters.Instigator = GetInstigator();
|
||
|
SpawnParameters.SpawnCollisionHandlingOverride =
|
||
|
ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
|
||
2 years ago
|
AExplosion *Explosion = GetWorld()->SpawnActor<AExplosion>(
|
||
2 years ago
|
ExplosionSubclass, SpawnTransform, SpawnParameters);
|
||
|
Explosion->Initialize(Damage, SplashRadius, PlayerIndex);
|
||
|
Explosion->SetActorLocation(GetActorLocation());
|
||
|
}
|
||
|
|
||
2 years ago
|
void AProjectile::GetLifetimeReplicatedProps(
|
||
2 years ago
|
TArray<FLifetimeProperty> &OutLifetimeProps) const {
|
||
|
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
|
||
2 years ago
|
DOREPLIFETIME(AProjectile, Damage);
|
||
|
DOREPLIFETIME(AProjectile, PlayerIndex);
|
||
2 years ago
|
// DOREPLIFETIME(AMyProjectile, CollisionComponent);
|
||
2 years ago
|
DOREPLIFETIME(AProjectile, ProjectileMeshComponent);
|
||
|
DOREPLIFETIME(AProjectile, ProjectileMovementComponent);
|
||
|
DOREPLIFETIME(AProjectile, SplashRadius);
|
||
2 years ago
|
}
|