// 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 HUDWidgetClass(TEXT("/Game/Blueprints/Combat_UI/CombatInit")); InitCombatWidgetClass = HUDWidgetClass.Class; } } // Called when the game starts void UHoldToInitCombat::BeginPlay() { Super::BeginPlay(); InitCombatWidget = CreateWidget(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(GetWorld()->GetGameState())->StartCombat(TargetEnemy); //UBlackboardComponent* TargetEnemyBlackboard = Cast(TargetEnemy->GetComponentByClass(UBlackboardComponent::StaticClass())); //TargetEnemyBlackboard->SetValueAsBool("IsInCombat", true); 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(); } 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; }