blaster/Source/Blaster/Pickups/Pickup.cpp

117 lines
3.0 KiB
C++

// Fill out your copyright notice in the Description page of Project Settings.
#include "Pickup.h"
#include "NiagaraComponent.h"
#include "NiagaraFunctionLibrary.h"
#include "Blaster/Weapon/WeaponTypes.h"
#include "Components/SphereComponent.h"
#include "Kismet/GameplayStatics.h"
#include "Sound/SoundCue.h"
APickup::APickup()
{
PrimaryActorTick.bCanEverTick = true;
bReplicates = true;
RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("Root"));
OverlapSphere = CreateDefaultSubobject<USphereComponent>(TEXT("OverlapSphere"));
OverlapSphere->SetupAttachment(RootComponent);
OverlapSphere->SetSphereRadius(150.f);
OverlapSphere->SetCollisionEnabled(ECollisionEnabled::QueryOnly);
OverlapSphere->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Ignore);
OverlapSphere->SetCollisionResponseToChannel(ECollisionChannel::ECC_Pawn, ECollisionResponse::ECR_Overlap);
OverlapSphere->AddLocalOffset(FVector(0.f, 0.f, 85.f));
PickupMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("PickupMesh"));
PickupMesh->SetupAttachment(OverlapSphere);
PickupMesh->SetCollisionEnabled(ECollisionEnabled::NoCollision);
PickupMesh->SetRelativeScale3D(FVector(5.f, 5.f, 5.f));
PickupMesh->SetRenderCustomDepth(true);
PickupMesh->SetCustomDepthStencilValue(CUSTOM_DEPTH_PURPLE);
PickupEffectComponent = CreateDefaultSubobject<UNiagaraComponent>(TEXT("PickupEffectComponent"));
PickupEffectComponent->SetupAttachment(RootComponent);
}
void APickup::BeginPlay()
{
Super::BeginPlay();
if (HasAuthority())
{
GetWorldTimerManager().SetTimer(
BindOverlapTimer,
this,
&APickup::BindOverlapTimerFinished,
BindOverlapTime
);
}
}
void APickup::OnOverlap(ABlasterCharacter* BlasterCharacter)
{
}
void APickup::BindOverlapTimerFinished()
{
if (HasAuthority())
{
TArray<AActor*> OverlappingActors;
GetOverlappingActors(OverlappingActors, ABlasterCharacter::StaticClass());
for (const auto OverlappingActor : OverlappingActors)
{
if (ABlasterCharacter* BlasterCharacter = Cast<ABlasterCharacter>(OverlappingActor))
{
OnOverlap(BlasterCharacter);
}
}
OverlapSphere->OnComponentBeginOverlap.AddDynamic(this, &APickup::OnSphereOverlap);
}
}
void APickup::OnSphereOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex,
bool bFromSweep, const FHitResult& SweepResult)
{
if (ABlasterCharacter* BlasterCharacter = Cast<ABlasterCharacter>(OtherActor))
{
OnOverlap(BlasterCharacter);
}
}
void APickup::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
if (PickupMesh)
{
PickupMesh->AddWorldRotation(FRotator(0.f, BaseTurnRate * DeltaTime, 0.f));
}
}
void APickup::Destroyed()
{
Super::Destroyed();
if (PickupSound)
{
UGameplayStatics::PlaySoundAtLocation(
this,
PickupSound,
GetActorLocation()
);
}
if (PickupEffect)
{
UNiagaraFunctionLibrary::SpawnSystemAtLocation(
this,
PickupEffect,
GetActorLocation(),
GetActorRotation()
);
}
}