Nakatomi/Source/Nakatomi/Throwable.cpp

50 lines
1.6 KiB
C++
Raw Normal View History

// 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()
{
2023-09-08 23:17:05 +02:00
// 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;
2023-09-21 22:07:54 +02:00
StaticMeshComponent->SetCollisionProfileName(FName("BlockAll"));
StaticMeshComponent->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
2023-09-21 22:07:54 +02:00
StaticMeshComponent->SetCollisionObjectType(ECC_WorldDynamic);
StaticMeshComponent->SetSimulatePhysics(true);
2023-09-21 22:07:54 +02:00
StaticMeshComponent->SetGenerateOverlapEvents(true);
StaticMeshComponent->SetNotifyRigidBodyCollision(true);
SetRootComponent(StaticMeshComponent);
}
// Called when the game starts or when spawned
void AThrowable::BeginPlay()
{
Super::BeginPlay();
2023-09-21 22:07:54 +02:00
StaticMeshComponent->OnComponentHit.AddDynamic(this, &AThrowable::OnOverlapBegin);
auto playerCharacter = UGameplayStatics::GetPlayerCharacter(GetWorld(), 0);
auto playerForwardVector = playerCharacter->GetActorForwardVector();
playerForwardVector.Z = ImpulseAngle;
2023-09-21 22:07:54 +02:00
StaticMeshComponent->AddImpulse(playerForwardVector * ImpulseForce);
}
2023-09-21 22:07:54 +02:00
void AThrowable::OnOverlapBegin(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp,
FVector NormalImpulse, const FHitResult& Hit)
{
2023-09-21 22:07:54 +02:00
if (!OtherActor->ActorHasTag(FName("Player")) && OtherActor != this)
{
if (HealthComponent)
{
HealthComponent->TakeDamage(this, HealthComponent->GetMaxHealth(), nullptr,
nullptr, this);
}
}
}