// 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 "Kismet/GameplayStatics.h" #include "Misc/OutputDeviceNull.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; } if (GunEffect == nullptr) { static ConstructorHelpers::FObjectFinder GunEffectAsset(TEXT("/Game/Blueprints/Combat_UI/NS_GunEffect")); GunEffect = GunEffectAsset.Object; } } // Called when the game starts void UHoldToInitCombat::BeginPlay() { Super::BeginPlay(); InitCombatWidget = CreateWidget(GetWorld(), InitCombatWidgetClass); InitCombatWidget->AddToViewport(); InitCombatWidget->SetVisibility(ESlateVisibility::Hidden); ReloadSlider = Cast(InitCombatWidget->GetWidgetFromName("ReloadSlider")); CombatSystem = Cast(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(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); } if (bClickDown && TargetEnemy != nullptr) { Cast(GetWorld()->GetGameState())->StartCombat(TargetEnemy); } } void UHoldToInitCombat::OnClickDown() { if (Cast(GetWorld()->GetGameState())->bIsInCombat) return; if (ReloadTimer > 0.0f) return; if (GunEffect) { const AActor* PlayerActor = GetWorld()->GetFirstPlayerController()->GetPawn(); const UStaticMeshComponent* GunComponent = Cast(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()); 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(TargetEnemy->GetInstigatorController())->GetBlackboardComponent(); if (!EnemyBlackboard->GetValueAsBool("WasInCombat")) { Cast(GetWorld()->GetGameState())->StartCombat(TargetEnemy, true); return; } InitCombatWidget->SetVisibility(ESlateVisibility::Hidden); return; } ReloadTimer = ReloadTime; InitCombatWidget->SetVisibility(ESlateVisibility::Visible); } 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)) { //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; }