Add initial Throwable Actor implementation

This commit is contained in:
Louis Hobbs 2023-09-06 17:34:15 +01:00
parent cb46d98936
commit 7569f2d792
2 changed files with 81 additions and 0 deletions

View File

@ -0,0 +1,47 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "Throwable.h"
#include "PlayerCharacter.h"
#include "Kismet/GameplayStatics.h"
// Sets default values
AThrowable::AThrowable()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = false;
StaticMeshComponent->SetCollisionProfileName(FName("NoCollision"));
SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("SphereComponent"));
SphereComponent->SetSphereRadius(25.0f, true);
SphereComponent->SetCollisionEnabled(ECollisionEnabled::QueryAndProbe);
SphereComponent->SetCollisionProfileName(FName("IgnoreOnlyPawn"));
SphereComponent->SetupAttachment(RootComponent);
}
// Called when the game starts or when spawned
void AThrowable::BeginPlay()
{
Super::BeginPlay();
SphereComponent->OnComponentBeginOverlap.AddDynamic(this, &AThrowable::OnOverlapBegin);
auto playerCharacter = Cast<APlayerCharacter>(UGameplayStatics::GetPlayerCharacter(GetWorld(),0));
auto playerForwardVector = playerCharacter->GetActorForwardVector();
StaticMeshComponent->AddImpulse(playerForwardVector);
}
void AThrowable::OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep,
const FHitResult& SweepResult)
{
if (OtherActor)
{
if (auto healthComponent = GetComponentByClass<UHealthComponent>())
{
healthComponent->TakeDamage(this, healthComponent->GetMaxHealth(), nullptr,
nullptr, this);
}
}
}

View File

@ -0,0 +1,34 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "ExplosiveActor.h"
#include "Throwable.generated.h"
UCLASS()
class NAKATOMI_API AThrowable : public AExplosiveActor
{
GENERATED_BODY()
private:
UPROPERTY()
USphereComponent* SphereComponent;
public:
// Sets default values for this actor's properties
AThrowable();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
UFUNCTION()
virtual void OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
UPrimitiveComponent* OtherComp,
int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
};