AzureAbyss/Source/the_twilight_abyss/TurnBasedCombatV2/HoldToInitCombat.cpp

120 lines
4.2 KiB
C++

// Fill out your copyright notice in the Description page of Project Settings.
#include "HoldToInitCombat.h"
#include "AIController.h"
#include "TurnBaseCombatV2.h"
#include "BehaviorTree/BlackboardComponent.h"
#include "Blueprint/UserWidget.h"
#include "Camera/CameraComponent.h"
#include "Misc/OutputDeviceNull.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);
InitCombatWidget->AddToViewport();
InitCombatWidget->SetVisibility(ESlateVisibility::Hidden);
ReloadSlider = Cast<URadialSlider>(InitCombatWidget->GetWidgetFromName("ReloadSlider"));
CombatSystem = Cast<ATurnBaseCombatV2>(GetWorld()->GetGameState());
}
// Called every frame
void UHoldToInitCombat::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
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);
}
CombatSystem->RedVignetteMaterialInstance->GetScalarParameterValue(FName("BlendWeight"), RedVignetteIntensity);
if (RedVignetteIntensity > 0.0f)
{
CombatSystem->RedVignetteMaterialInstance->SetScalarParameterValue(FName("BlendWeight"), RedVignetteIntensity - DeltaTime * 2.0f);
}
}
void UHoldToInitCombat::OnClickDown()
{
if (!bHasBookAndGun) return;
if (Cast<ATurnBaseCombatV2>(GetWorld()->GetGameState())->bIsInCombat) return;
if (ReloadTimer > 0.0f) return;
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").GetLocation();
UNiagaraFunctionLibrary::SpawnSystemAtLocation(GetWorld(), GunEffect, GunLocationOffset, PlayerActor->GetActorRotation());
FOutputDeviceNull AR;
const FString Command = FString::Printf(TEXT("PlayCameraShakeShoot"));
this->GetOwner()->CallFunctionByNameWithArguments(*Command, AR, nullptr, true);
}
if (AActor* ClickHit = LookingAtEnemy(); ClickHit != nullptr)
{
TargetEnemy = ClickHit;
EnemyBlackboard = Cast<AAIController>(TargetEnemy->GetInstigatorController())->GetBlackboardComponent();
if (!EnemyBlackboard->GetValueAsBool("WasInCombat"))
{
Cast<ATurnBaseCombatV2>(GetWorld()->GetGameState())->StartCombat(TargetEnemy, true);
return;
}
InitCombatWidget->SetVisibility(ESlateVisibility::Hidden);
return;
}
ReloadTimer = ReloadTime;
InitCombatWidget->SetVisibility(ESlateVisibility::Visible);
}
AActor* UHoldToInitCombat::LookingAtEnemy() const
{
FVector Start = Cast<UCameraComponent>(GetOwner()->FindComponentByClass<UCameraComponent>())->GetComponentLocation();
FVector End = GetOwner()->GetActorForwardVector() * 1000.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;
}