D1/Source/D1/AI/GruntlingEnemy.cpp

108 lines
3.7 KiB
C++

// Fill out your copyright notice in the Description page of Project Settings.
#include "AI/GruntlingEnemy.h"
#include "Components/CollisionComponent.h"
#include "Components/CombatComponent.h"
#include "Components/WidgetComponent.h"
#include "DamageType/AttackDamageType.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "Kismet/GameplayStatics.h"
#include "UI/UI_HealthBar.h"
AGruntlingEnemy::AGruntlingEnemy()
{
TargetingWidgetComponent->SetRelativeLocation(FVector(0.f, 0.f, 132.f));
//Setting SocketName
AttachSocketName = TEXT("WeaponSocket");
WeaponHandSocketName = TEXT("WeaponSocket");
//Setting UWidgetComponent
HealthBarComponent = CreateDefaultSubobject<UWidgetComponent>(TEXT("HealthBar"));
static ConstructorHelpers::FClassFinder<UUserWidget> WidgetRef(TEXT("/Game/CombatSystem/UI/WBP_HealthBar.WBP_HealthBar_C"));
if(WidgetRef.Class)
{
HealthBarComponent->SetWidgetClass(WidgetRef.Class);
HealthBarComponent->SetWidgetSpace(EWidgetSpace::Screen);
HealthBarComponent->SetDrawSize({150.f, 10.f});
HealthBarComponent->SetupAttachment(GetMesh());
HealthBarComponent->SetRelativeLocation(FVector(0.f, 0.f, 200.f));
HealthBarComponent->SetVisibility(false);
}
//Setting WeaponMeshComponent
WeaponMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("WeaponMeshComponent"));
WeaponMeshComponent->SetRelativeLocationAndRotation(FVector(0.f, 0.f, 0.f), FRotator(0.f, 0.f, 0.f));
WeaponMeshComponent->SetupAttachment(GetMesh(), AttachSocketName);
WeaponMeshComponent->SetCollisionEnabled(ECollisionEnabled::NoCollision);
//Setting MainWeaponCollisionComponent
//Setting others is Parents
MainWeaponCollisionComponent->OnHitDelegate.BindUObject(this, &AGruntlingEnemy::OnHit);
//Setting CombatComponent
//Setting others is Parents
CombatComponent->OnCombatToggled.AddUObject(this, &AGruntlingEnemy::OnCombatToggled);
}
void AGruntlingEnemy::BeginPlay()
{
Super::BeginPlay();
MainWeaponCollisionComponent->SetCollisionMeshComponent(WeaponMeshComponent);
MainWeaponCollisionComponent->AddActorToIgnore(this);
if(APlayerController* playerController = UGameplayStatics::GetPlayerController(GetWorld(), 0))
{
UUI_HealthBar* HealthBarRef = Cast<UUI_HealthBar>(HealthBarComponent->GetWidget());
if(HealthBarRef) //ExposeOnSpawn 대용으로 사용
{
HealthBarRef->InitializeHealthBar(this->StatsComponent, EStats::Health);
HealthBarComponent->SetWidget(HealthBarRef);
}
}
}
void AGruntlingEnemy::OnTargeted(bool bIsTargeted)
{
Super::OnTargeted(bIsTargeted);
HealthBarComponent->SetVisibility(bIsTargeted);
}
void AGruntlingEnemy::OnHit(FHitResult hitResult)
{
ICombatInterface* pActor = Cast<ICombatInterface>(hitResult.GetActor());
if (pActor)
{
if (pActor->Execute_CanReceiveDamage(hitResult.GetActor()))
UGameplayStatics::ApplyPointDamage(hitResult.GetActor(), StatsComponent->GetCurrentStatValue(EStats::Damage), this->GetActorForwardVector(), hitResult, GetController(), this, UAttackDamageType::StaticClass());
}
}
void AGruntlingEnemy::OnCombatToggled(bool IsCombatEnabled)
{
FName SocketName;
if(IsCombatEnabled)
SocketName = WeaponHandSocketName;
else
SocketName = AttachSocketName;
FAttachmentTransformRules rules(EAttachmentRule::SnapToTarget, EAttachmentRule::SnapToTarget, EAttachmentRule::SnapToTarget, true);
WeaponMeshComponent->AttachToComponent(GetMesh(), rules, SocketName);
}
void AGruntlingEnemy::PerformDeath()
{
//TODO : 죽었을 때 무기에 Collision이 남아있는 버그 있음
Super::PerformDeath();
SimulateWeaponPhysics();
GetCharacterMovement()->SetMovementMode(MOVE_None);
}
void AGruntlingEnemy::SimulateWeaponPhysics()
{
WeaponMeshComponent->SetCollisionProfileName(TEXT("PhysicsActor"), true);
WeaponMeshComponent->SetSimulatePhysics(true);
}