AzureAbyss/Source/the_twilight_abyss/TurnBasedCombatV2/HoldToInitCombat.cpp

117 lines
4.0 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;
}
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);
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 < 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)
{
Cast<ATurnBaseCombatV2>(GetWorld()->GetGameState())->StartCombat(TargetEnemy);
OnRightClickUp();
}
}
void UHoldToInitCombat::OnRightClickDown()
{
if (AActor* RightClickHit = LookingAtEnemy(); RightClickHit != nullptr)
{
TargetEnemy = RightClickHit;
bRightClickDown = true;
//InitCombatWidget->AddToViewport();
}
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());
}
}
void UHoldToInitCombat::OnRightClickUp()
{
bRightClickDown = false;
RightClickDownTime = 0.0f;
if (InitCombatWidget->IsInViewport())
{
InitCombatWidget->RemoveFromParent();
}
}
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))
{
2023-01-31 00:56:53 +00:00
//DrawDebugLine(GetWorld(), Start, End, FColor::Green, false, 5.0f, 0, 10.0f);
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-17 08:46:31 +00:00
HitResult.GetActor()->Destroy();
return nullptr;
}
}
2023-01-31 00:56:53 +00:00
//DrawDebugLine(GetWorld(), Start, End, FColor::Red, false, 5.0f, 0, 10.0f);
return nullptr;
}