104 lines
3.4 KiB
C++
104 lines
3.4 KiB
C++
// Fill out your copyright notice in the Description page of Project Settings.
|
|
|
|
|
|
#include "HoldToInitCombat.h"
|
|
#include "TurnBaseCombatV2.h"
|
|
#include "Blueprint/UserWidget.h"
|
|
|
|
|
|
// Sets default values for this component's properties
|
|
UHoldToInitCombat::UHoldToInitCombat()
|
|
{
|
|
PrimaryComponentTick.bCanEverTick = true;
|
|
if (InitCombatWidgetClass == nullptr)
|
|
{
|
|
static ConstructorHelpers::FClassFinder<UUserWidget> HUDWidgetClass(TEXT("/Game/Blueprints/Combat_UI/CombatInit"));
|
|
InitCombatWidgetClass = HUDWidgetClass.Class;
|
|
}
|
|
if (GunEffect == nullptr)
|
|
{
|
|
static ConstructorHelpers::FObjectFinder<UNiagaraSystem> GunEffectAsset(TEXT("/Game/Blueprints/Combat_UI/NS_GunEffect"));
|
|
GunEffect = GunEffectAsset.Object;
|
|
}
|
|
}
|
|
|
|
|
|
// Called when the game starts
|
|
void UHoldToInitCombat::BeginPlay()
|
|
{
|
|
Super::BeginPlay();
|
|
InitCombatWidget = CreateWidget<UUserWidget>(GetWorld(), InitCombatWidgetClass);
|
|
InitCombatWidget->AddToViewport();
|
|
InitCombatWidget->SetVisibility(ESlateVisibility::Hidden);
|
|
|
|
ReloadSlider = Cast<URadialSlider>(InitCombatWidget->GetWidgetFromName("ReloadSlider"));
|
|
}
|
|
|
|
|
|
// Called every frame
|
|
void UHoldToInitCombat::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
|
|
{
|
|
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
|
|
if (ReloadTimer > 0.0f)
|
|
{
|
|
ReloadTimer -= DeltaTime;
|
|
ReloadSlider->SetValue(FMath::Clamp<float>(ReloadTime - ReloadTimer, 0.f, ReloadTime) / ReloadTime);
|
|
}
|
|
else if (InitCombatWidget->GetVisibility() == ESlateVisibility::Visible)
|
|
{
|
|
InitCombatWidget->SetVisibility(ESlateVisibility::Hidden);
|
|
}
|
|
|
|
if (bClickDown && TargetEnemy != nullptr)
|
|
{
|
|
Cast<ATurnBaseCombatV2>(GetWorld()->GetGameState())->StartCombat(TargetEnemy);
|
|
}
|
|
}
|
|
|
|
void UHoldToInitCombat::OnClickDown()
|
|
{
|
|
if (Cast<ATurnBaseCombatV2>(GetWorld()->GetGameState())->bIsInCombat) return;
|
|
if (ReloadTimer > 0.0f) return;
|
|
if (GunEffect)
|
|
{
|
|
const AActor* PlayerActor = GetWorld()->GetFirstPlayerController()->GetPawn();
|
|
const UStaticMeshComponent* GunComponent = Cast<UStaticMeshComponent>(PlayerActor->GetComponentsByTag(UPrimitiveComponent::StaticClass(), FName("Gun"))[0]);
|
|
const FVector GunLocationOffset = GunComponent->GetSocketTransform("Muzzle").TransformPosition(FVector(-100, 0, 0));
|
|
UNiagaraFunctionLibrary::SpawnSystemAtLocation(GetWorld(), GunEffect, GunLocationOffset, PlayerActor->GetActorRotation());
|
|
}
|
|
|
|
if (AActor* ClickHit = LookingAtEnemy(); ClickHit != nullptr)
|
|
{
|
|
TargetEnemy = ClickHit;
|
|
Cast<ATurnBaseCombatV2>(GetWorld()->GetGameState())->StartCombat(TargetEnemy);
|
|
InitCombatWidget->SetVisibility(ESlateVisibility::Hidden);
|
|
return;
|
|
}
|
|
|
|
ReloadTimer = ReloadTime;
|
|
InitCombatWidget->SetVisibility(ESlateVisibility::Visible);
|
|
}
|
|
|
|
AActor* UHoldToInitCombat::LookingAtEnemy() const
|
|
{
|
|
FVector Start = GetOwner()->GetActorLocation();
|
|
FVector End = GetOwner()->GetActorForwardVector() * 1000.0f + Start;
|
|
FCollisionQueryParams CollisionParams;
|
|
CollisionParams.AddIgnoredActor(GetOwner());
|
|
if (FHitResult HitResult; GetWorld()->LineTraceSingleByChannel(HitResult, Start, End, ECC_Pawn, CollisionParams))
|
|
{
|
|
//DrawDebugLine(GetWorld(), Start, End, FColor::Green, false, 5.0f, 0, 10.0f);
|
|
if (HitResult.GetActor()->Tags.Contains("Enemy"))
|
|
{
|
|
return HitResult.GetActor();
|
|
}
|
|
if (HitResult.GetActor()->Tags.Contains("Break"))
|
|
{
|
|
HitResult.GetActor()->Destroy();
|
|
return nullptr;
|
|
}
|
|
}
|
|
//DrawDebugLine(GetWorld(), Start, End, FColor::Red, false, 5.0f, 0, 10.0f);
|
|
return nullptr;
|
|
}
|