139 lines
3.7 KiB
C++
139 lines
3.7 KiB
C++
// Fill out your copyright notice in the Description page of Project Settings.
|
|
|
|
|
|
#include "Weapon.h"
|
|
|
|
#include "Casing.h"
|
|
#include "Blaster/Character/BlasterCharacter.h"
|
|
#include "Components/SphereComponent.h"
|
|
#include "Components/WidgetComponent.h"
|
|
#include "Engine/SkeletalMeshSocket.h"
|
|
#include "Net/UnrealNetwork.h"
|
|
|
|
AWeapon::AWeapon()
|
|
{
|
|
PrimaryActorTick.bCanEverTick = false;
|
|
bReplicates = true;
|
|
|
|
WeaponMesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("WeaponMesh"));
|
|
|
|
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::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
|
|
{
|
|
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
|
|
|
|
DOREPLIFETIME(AWeapon, WeaponState);
|
|
}
|
|
|
|
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::SetWeaponState(EWeaponState State)
|
|
{
|
|
WeaponState = State;
|
|
switch (WeaponState)
|
|
{
|
|
case EWeaponState::EWS_Equipped:
|
|
ShowPickupWidget(false);
|
|
GetAreaSphere()->SetCollisionEnabled(ECollisionEnabled::NoCollision);
|
|
break;
|
|
}
|
|
}
|
|
|
|
void AWeapon::OnRep_WeaponState()
|
|
{
|
|
switch (WeaponState)
|
|
{
|
|
case EWeaponState::EWS_Equipped:
|
|
ShowPickupWidget(false);
|
|
break;
|
|
}
|
|
}
|
|
|
|
void AWeapon::ShowPickupWidget(bool bShowWidget)
|
|
{
|
|
if (PickupWidget)
|
|
{
|
|
PickupWidget->SetVisibility(bShowWidget);
|
|
}
|
|
}
|
|
|
|
void AWeapon::Fire(const FVector& HitTarget)
|
|
{
|
|
if (FireAnimation)
|
|
{
|
|
WeaponMesh->PlayAnimation(FireAnimation, false);
|
|
}
|
|
if (CasingClass)
|
|
{
|
|
const USkeletalMeshSocket* AmmoEjectSocket = WeaponMesh->GetSocketByName(FName("AmmoEject"));
|
|
if (AmmoEjectSocket)
|
|
{
|
|
FTransform SocketTransform = AmmoEjectSocket->GetSocketTransform(WeaponMesh);
|
|
|
|
UWorld* World = GetWorld();
|
|
if (World)
|
|
{
|
|
World->SpawnActor<ACasing>(
|
|
CasingClass,
|
|
SocketTransform.GetLocation(),
|
|
SocketTransform.GetRotation().Rotator()
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|