AzureAbyss/Source/the_twilight_abyss/TurnBasedCombatV2/HoldToInitCombat.cpp

90 lines
2.7 KiB
C++
Raw Normal View History

// 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;
}
}
// Called when the game starts
void UHoldToInitCombat::BeginPlay()
{
Super::BeginPlay();
InitCombatWidget = CreateWidget<UUserWidget>(GetWorld(), InitCombatWidgetClass);
UInputComponent* playerInputComponent = GetWorld()->GetFirstPlayerController()->InputComponent;
//Bind Right Mouse Button to the function OnRightClickDown
playerInputComponent->BindAction("RightClick", IE_Pressed, this, &UHoldToInitCombat::OnRightClickDown);
//Bind Right Mouse Button to the function OnRightClickUp
playerInputComponent->BindAction("RightClick", IE_Released, this, &UHoldToInitCombat::OnRightClickUp);
}
// Called every frame
void UHoldToInitCombat::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
//If the player is holding down the right mouse button for 3 seconds, then the player will enter combat mode
if (bRightClickDown && RightClickDownTime < 3.0f)
{
RightClickDownTime += DeltaTime;
}
else if (bRightClickDown && RightClickDownTime >= 3.0f)
{
//Enter Combat Mode
Cast<ATurnBaseCombatV2>(GetWorld()->GetGameState())->StartCombat(TargetEnemy);
OnRightClickUp();
}
}
void UHoldToInitCombat::OnRightClickDown()
{
if (AActor* RightClickHit = LookingAtEnemy(); RightClickHit != nullptr)
{
TargetEnemy = RightClickHit;
bRightClickDown = true;
InitCombatWidget->AddToViewport();
}
}
void UHoldToInitCombat::OnRightClickUp()
{
bRightClickDown = false;
RightClickDownTime = 0.0f;
if (InitCombatWidget->IsInViewport())
{
InitCombatWidget->RemoveFromViewport();
}
}
AActor* UHoldToInitCombat::LookingAtEnemy() const
{
FVector Start = GetOwner()->GetActorLocation();
FVector End = GetOwner()->GetActorForwardVector() * 1500.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();
}
}
DrawDebugLine(GetWorld(), Start, End, FColor::Red, false, 5.0f, 0, 10.0f);
return nullptr;
}