diff --git a/Source/Nakatomi/Throwable.cpp b/Source/Nakatomi/Throwable.cpp new file mode 100644 index 0000000..81c44eb --- /dev/null +++ b/Source/Nakatomi/Throwable.cpp @@ -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(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(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()) + { + healthComponent->TakeDamage(this, healthComponent->GetMaxHealth(), nullptr, + nullptr, this); + } + } +} diff --git a/Source/Nakatomi/Throwable.h b/Source/Nakatomi/Throwable.h new file mode 100644 index 0000000..d1776f3 --- /dev/null +++ b/Source/Nakatomi/Throwable.h @@ -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); + +};