2022-11-29 00:23:15 +00:00
|
|
|
|
// Fill out your copyright notice in the Description page of Project Settings.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "HoldToInitCombat.h"
|
2023-04-26 04:51:07 +00:00
|
|
|
|
|
|
|
|
|
#include "AIController.h"
|
2022-11-29 00:23:15 +00:00
|
|
|
|
#include "TurnBaseCombatV2.h"
|
2023-04-26 04:51:07 +00:00
|
|
|
|
#include "BehaviorTree/BlackboardComponent.h"
|
2022-11-29 00:23:15 +00:00
|
|
|
|
#include "Blueprint/UserWidget.h"
|
2023-04-27 05:59:52 +00:00
|
|
|
|
#include "Kismet/GameplayStatics.h"
|
2023-04-27 04:13:41 +00:00
|
|
|
|
#include "Misc/OutputDeviceNull.h"
|
2022-11-29 00:23:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
}
|
2023-01-31 03:13:17 +00:00
|
|
|
|
if (GunEffect == nullptr)
|
|
|
|
|
{
|
|
|
|
|
static ConstructorHelpers::FObjectFinder<UNiagaraSystem> GunEffectAsset(TEXT("/Game/Blueprints/Combat_UI/NS_GunEffect"));
|
|
|
|
|
GunEffect = GunEffectAsset.Object;
|
|
|
|
|
}
|
2022-11-29 00:23:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Called when the game starts
|
|
|
|
|
void UHoldToInitCombat::BeginPlay()
|
|
|
|
|
{
|
|
|
|
|
Super::BeginPlay();
|
|
|
|
|
InitCombatWidget = CreateWidget<UUserWidget>(GetWorld(), InitCombatWidgetClass);
|
2023-04-26 01:10:01 +00:00
|
|
|
|
InitCombatWidget->AddToViewport();
|
|
|
|
|
InitCombatWidget->SetVisibility(ESlateVisibility::Hidden);
|
|
|
|
|
|
|
|
|
|
ReloadSlider = Cast<URadialSlider>(InitCombatWidget->GetWidgetFromName("ReloadSlider"));
|
2023-04-27 05:59:52 +00:00
|
|
|
|
CombatSystem = Cast<ATurnBaseCombatV2>(GetWorld()->GetGameState());
|
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-04-26 01:10:01 +00:00
|
|
|
|
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);
|
|
|
|
|
}
|
2022-11-29 00:23:15 +00:00
|
|
|
|
|
2023-04-27 05:59:52 +00:00
|
|
|
|
CombatSystem->RedVignetteMaterialInstance->GetScalarParameterValue(FName("BlendWeight"), RedVignetteIntensity);
|
|
|
|
|
if (RedVignetteIntensity > 0.0f)
|
|
|
|
|
{
|
|
|
|
|
CombatSystem->RedVignetteMaterialInstance->SetScalarParameterValue(FName("BlendWeight"), RedVignetteIntensity - DeltaTime * 2.0f);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-26 01:10:01 +00:00
|
|
|
|
if (bClickDown && TargetEnemy != nullptr)
|
2022-11-29 00:23:15 +00:00
|
|
|
|
{
|
|
|
|
|
Cast<ATurnBaseCombatV2>(GetWorld()->GetGameState())->StartCombat(TargetEnemy);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-26 01:10:01 +00:00
|
|
|
|
void UHoldToInitCombat::OnClickDown()
|
2022-11-29 00:23:15 +00:00
|
|
|
|
{
|
2023-04-26 02:22:13 +00:00
|
|
|
|
if (Cast<ATurnBaseCombatV2>(GetWorld()->GetGameState())->bIsInCombat) return;
|
2023-04-26 01:10:01 +00:00
|
|
|
|
if (ReloadTimer > 0.0f) return;
|
2023-01-31 01:48:35 +00:00
|
|
|
|
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());
|
2023-04-27 04:13:41 +00:00
|
|
|
|
FOutputDeviceNull AR;
|
|
|
|
|
const FString Command = FString::Printf(TEXT("PlayCameraShakeShoot"));
|
|
|
|
|
this->GetOwner()->CallFunctionByNameWithArguments(*Command, AR, nullptr, true);
|
2023-01-31 01:48:35 +00:00
|
|
|
|
}
|
2022-11-29 00:23:15 +00:00
|
|
|
|
|
2023-04-26 01:10:01 +00:00
|
|
|
|
if (AActor* ClickHit = LookingAtEnemy(); ClickHit != nullptr)
|
2022-11-29 00:23:15 +00:00
|
|
|
|
{
|
2023-04-26 01:10:01 +00:00
|
|
|
|
TargetEnemy = ClickHit;
|
2023-04-26 04:51:07 +00:00
|
|
|
|
EnemyBlackboard = Cast<AAIController>(TargetEnemy->GetInstigatorController())->GetBlackboardComponent();
|
|
|
|
|
if (!EnemyBlackboard->GetValueAsBool("WasInCombat"))
|
|
|
|
|
{
|
2023-04-26 05:21:08 +00:00
|
|
|
|
Cast<ATurnBaseCombatV2>(GetWorld()->GetGameState())->StartCombat(TargetEnemy, true);
|
2023-04-26 04:51:07 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2023-04-26 01:10:01 +00:00
|
|
|
|
InitCombatWidget->SetVisibility(ESlateVisibility::Hidden);
|
|
|
|
|
return;
|
2022-11-29 00:23:15 +00:00
|
|
|
|
}
|
2023-04-26 01:10:01 +00:00
|
|
|
|
|
|
|
|
|
ReloadTimer = ReloadTime;
|
|
|
|
|
InitCombatWidget->SetVisibility(ESlateVisibility::Visible);
|
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;
|
|
|
|
|
}
|