2022-11-29 00:23:15 +00:00
|
|
|
|
// 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);
|
|
|
|
|
|
2023-01-30 23:03:50 +00:00
|
|
|
|
UInputComponent* PlayerInputComponent = GetWorld()->GetFirstPlayerController()->InputComponent;
|
2022-11-29 00:23:15 +00:00
|
|
|
|
//Bind Right Mouse Button to the function OnRightClickDown
|
2023-01-30 23:03:50 +00:00
|
|
|
|
PlayerInputComponent->BindAction("RightClick", IE_Pressed, this, &UHoldToInitCombat::OnRightClickDown);
|
2022-11-29 00:23:15 +00:00
|
|
|
|
//Bind Right Mouse Button to the function OnRightClickUp
|
2023-01-30 23:03:50 +00:00
|
|
|
|
PlayerInputComponent->BindAction("RightClick", IE_Released, this, &UHoldToInitCombat::OnRightClickUp);
|
2022-11-29 00:23:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Called every frame
|
|
|
|
|
void UHoldToInitCombat::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
|
|
|
|
|
{
|
|
|
|
|
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
|
|
|
|
|
|
2023-01-31 00:30:45 +00:00
|
|
|
|
// //If the player is holding down the right mouse button for 3 seconds, then the player will enter combat mode
|
|
|
|
|
// if (bRightClickDown && RightClickDownTime < 0.1f)
|
|
|
|
|
// {
|
|
|
|
|
// RightClickDownTime += DeltaTime;
|
|
|
|
|
// }
|
|
|
|
|
// else if (bRightClickDown && RightClickDownTime >= 0.1f)
|
|
|
|
|
// {
|
|
|
|
|
// //Enter Combat Mode
|
|
|
|
|
// Cast<ATurnBaseCombatV2>(GetWorld()->GetGameState())->StartCombat(TargetEnemy);
|
|
|
|
|
// //UBlackboardComponent* TargetEnemyBlackboard = Cast<UBlackboardComponent>(TargetEnemy->GetComponentByClass(UBlackboardComponent::StaticClass()));
|
|
|
|
|
// //TargetEnemyBlackboard->SetValueAsBool("IsInCombat", true);
|
|
|
|
|
// OnRightClickUp();
|
|
|
|
|
// }
|
|
|
|
|
if (bRightClickDown)
|
2022-11-29 00:23:15 +00:00
|
|
|
|
{
|
|
|
|
|
Cast<ATurnBaseCombatV2>(GetWorld()->GetGameState())->StartCombat(TargetEnemy);
|
|
|
|
|
OnRightClickUp();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UHoldToInitCombat::OnRightClickDown()
|
|
|
|
|
{
|
|
|
|
|
if (AActor* RightClickHit = LookingAtEnemy(); RightClickHit != nullptr)
|
|
|
|
|
{
|
|
|
|
|
TargetEnemy = RightClickHit;
|
|
|
|
|
bRightClickDown = true;
|
2023-01-31 00:30:45 +00:00
|
|
|
|
//InitCombatWidget->AddToViewport();
|
2022-11-29 00:23:15 +00:00
|
|
|
|
}
|
2023-01-31 01:48:35 +00:00
|
|
|
|
|
|
|
|
|
if (GunEffect)
|
|
|
|
|
{
|
|
|
|
|
//Get Player Actor
|
|
|
|
|
const AActor* PlayerActor = GetWorld()->GetFirstPlayerController()->GetPawn();
|
|
|
|
|
//Get Static Mesh Location on the player actor
|
|
|
|
|
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());
|
|
|
|
|
}
|
2022-11-29 00:23:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UHoldToInitCombat::OnRightClickUp()
|
|
|
|
|
{
|
|
|
|
|
bRightClickDown = false;
|
|
|
|
|
RightClickDownTime = 0.0f;
|
|
|
|
|
if (InitCombatWidget->IsInViewport())
|
|
|
|
|
{
|
2023-01-31 00:30:45 +00:00
|
|
|
|
InitCombatWidget->RemoveFromParent();
|
2022-11-29 00:23:15 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AActor* UHoldToInitCombat::LookingAtEnemy() const
|
|
|
|
|
{
|
|
|
|
|
FVector Start = GetOwner()->GetActorLocation();
|
2023-01-31 00:32:03 +00:00
|
|
|
|
FVector End = GetOwner()->GetActorForwardVector() * 1000.0f + Start;
|
2022-11-29 00:23:15 +00:00
|
|
|
|
FCollisionQueryParams CollisionParams;
|
|
|
|
|
CollisionParams.AddIgnoredActor(GetOwner());
|
|
|
|
|
if (FHitResult HitResult; GetWorld()->LineTraceSingleByChannel(HitResult, Start, End, ECC_Pawn, CollisionParams))
|
|
|
|
|
{
|
2023-01-31 00:56:53 +00:00
|
|
|
|
//DrawDebugLine(GetWorld(), Start, End, FColor::Green, false, 5.0f, 0, 10.0f);
|
2022-11-29 00:23:15 +00:00
|
|
|
|
if (HitResult.GetActor()->Tags.Contains("Enemy"))
|
|
|
|
|
{
|
|
|
|
|
return HitResult.GetActor();
|
|
|
|
|
}
|
2023-01-17 08:46:31 +00:00
|
|
|
|
if (HitResult.GetActor()->Tags.Contains("Break"))
|
2023-01-31 00:30:45 +00:00
|
|
|
|
{
|
2023-01-17 08:46:31 +00:00
|
|
|
|
HitResult.GetActor()->Destroy();
|
2023-01-31 00:30:45 +00:00
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2022-11-29 00:23:15 +00:00
|
|
|
|
}
|
2023-01-31 00:56:53 +00:00
|
|
|
|
//DrawDebugLine(GetWorld(), Start, End, FColor::Red, false, 5.0f, 0, 10.0f);
|
2022-11-29 00:23:15 +00:00
|
|
|
|
return nullptr;
|
|
|
|
|
}
|