82 lines
2.5 KiB
C++
82 lines
2.5 KiB
C++
// Fill out your copyright notice in the Description page of Project Settings.
|
|
|
|
|
|
#include "Weapon.h"
|
|
|
|
#include "Blaster/Character/BlasterCharacter.h"
|
|
#include "Components/SphereComponent.h"
|
|
#include "Components/WidgetComponent.h"
|
|
|
|
AWeapon::AWeapon()
|
|
{
|
|
PrimaryActorTick.bCanEverTick = false;
|
|
bReplicates = true;
|
|
|
|
WeaponMesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("WeaponMesh"));
|
|
WeaponMesh->SetupAttachment(WeaponMesh); // WeaponMesh instead?
|
|
|
|
SetRootComponent(WeaponMesh);
|
|
|
|
WeaponMesh->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Block);
|
|
WeaponMesh->SetCollisionResponseToChannel(ECollisionChannel::ECC_Pawn, ECollisionResponse::ECR_Ignore);
|
|
WeaponMesh->SetCollisionEnabled(ECollisionEnabled::NoCollision);
|
|
|
|
AreaSphere = CreateDefaultSubobject<USphereComponent>(TEXT("AreaSphere"));
|
|
AreaSphere->SetupAttachment(RootComponent);
|
|
AreaSphere->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Ignore);
|
|
AreaSphere->SetCollisionEnabled(ECollisionEnabled::NoCollision);
|
|
|
|
PickupWidget = CreateDefaultSubobject<UWidgetComponent>(TEXT("PickupWidget"));
|
|
PickupWidget->SetupAttachment(RootComponent);
|
|
}
|
|
|
|
void AWeapon::BeginPlay()
|
|
{
|
|
Super::BeginPlay();
|
|
|
|
if (HasAuthority())
|
|
{
|
|
AreaSphere->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
|
|
AreaSphere->SetCollisionResponseToChannel(ECollisionChannel::ECC_Pawn, ECollisionResponse::ECR_Overlap);
|
|
AreaSphere->OnComponentBeginOverlap.AddDynamic(this, &AWeapon::OnSphereOverlap);
|
|
AreaSphere->OnComponentEndOverlap.AddDynamic(this, &AWeapon::OnSphereEndOverlap);
|
|
}
|
|
|
|
if (PickupWidget)
|
|
{
|
|
PickupWidget->SetVisibility(false);
|
|
}
|
|
}
|
|
|
|
void AWeapon::OnSphereOverlap(UPrimitiveComponent* OverlappedComponent,
|
|
AActor* OtherActor,
|
|
UPrimitiveComponent* OtherComp,
|
|
int32 OtherBodyIndex,
|
|
bool bFromSweep,
|
|
const FHitResult& SweepResult)
|
|
{
|
|
ABlasterCharacter* BlasterCharacter = Cast<ABlasterCharacter>(OtherActor);
|
|
if (BlasterCharacter)
|
|
{
|
|
BlasterCharacter->SetOverlappingWeapon(this);
|
|
}
|
|
}
|
|
|
|
void AWeapon::OnSphereEndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
|
|
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
|
|
{
|
|
ABlasterCharacter* BlasterCharacter = Cast<ABlasterCharacter>(OtherActor);
|
|
if (BlasterCharacter)
|
|
{
|
|
BlasterCharacter->SetOverlappingWeapon(nullptr);
|
|
}
|
|
}
|
|
|
|
void AWeapon::ShowPickupWidget(bool bShowWidget)
|
|
{
|
|
if (PickupWidget)
|
|
{
|
|
PickupWidget->SetVisibility(bShowWidget);
|
|
}
|
|
}
|