From 0f5c983e755e400596719db30446f8a01c621f3e Mon Sep 17 00:00:00 2001 From: Rafal Swierczek <34179rs@gmail.com> Date: Fri, 6 Oct 2023 18:27:33 +0100 Subject: [PATCH] Implemented AOverloadModule and its Activation Functions --- .../OverloadModule/CG_OverloadModule.uasset | 3 + .../Content/Levels/GadgetSystemTestLevel.umap | 4 +- .../Architecture/Floor_400x400.uasset | 4 +- .../Props/MaterialSphere.uasset | 4 +- .../Combat/OverloadModule/OverloadModule.cpp | 122 ++++++++++++++++++ .../Combat/OverloadModule/OverloadModule.h | 64 +++++++++ 6 files changed, 195 insertions(+), 6 deletions(-) create mode 100644 EndlessVendetta/Content/Gadgets/CombatGadgets/OverloadModule/CG_OverloadModule.uasset create mode 100644 EndlessVendetta/Source/EndlessVendetta/GadgetSystem/GadgetClasses/Combat/OverloadModule/OverloadModule.cpp create mode 100644 EndlessVendetta/Source/EndlessVendetta/GadgetSystem/GadgetClasses/Combat/OverloadModule/OverloadModule.h diff --git a/EndlessVendetta/Content/Gadgets/CombatGadgets/OverloadModule/CG_OverloadModule.uasset b/EndlessVendetta/Content/Gadgets/CombatGadgets/OverloadModule/CG_OverloadModule.uasset new file mode 100644 index 00000000..4d8790c5 --- /dev/null +++ b/EndlessVendetta/Content/Gadgets/CombatGadgets/OverloadModule/CG_OverloadModule.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7d61bb4d0a3f7c25b8901699a8047059fb83a255d89990a5b366410f51d23792 +size 23259 diff --git a/EndlessVendetta/Content/Levels/GadgetSystemTestLevel.umap b/EndlessVendetta/Content/Levels/GadgetSystemTestLevel.umap index 80f122b6..62d689b2 100644 --- a/EndlessVendetta/Content/Levels/GadgetSystemTestLevel.umap +++ b/EndlessVendetta/Content/Levels/GadgetSystemTestLevel.umap @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:d2231f4d2a4b46f8a7daa258e14487b5ee0c0d6ed602fa256de0ef9cdd04f188 -size 106160 +oid sha256:f27623f7de4cd3ffafc332f52d32f9d58031dad0ba48c3954472553188926618 +size 106253 diff --git a/EndlessVendetta/Content/StarterContent/Architecture/Floor_400x400.uasset b/EndlessVendetta/Content/StarterContent/Architecture/Floor_400x400.uasset index 699c604a..c2a34afc 100644 --- a/EndlessVendetta/Content/StarterContent/Architecture/Floor_400x400.uasset +++ b/EndlessVendetta/Content/StarterContent/Architecture/Floor_400x400.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:c4ecdd1c9e06062da33fac4c12ca5f4011e86800bee678d985f6c95066e281d1 -size 14831 +oid sha256:4c0ecf0ac0702cc2bd355ec050232a61c49e10f102448efbeb735a51824adef6 +size 14948 diff --git a/EndlessVendetta/Content/StarterContent/Props/MaterialSphere.uasset b/EndlessVendetta/Content/StarterContent/Props/MaterialSphere.uasset index 56bddfc9..7759cc15 100644 --- a/EndlessVendetta/Content/StarterContent/Props/MaterialSphere.uasset +++ b/EndlessVendetta/Content/StarterContent/Props/MaterialSphere.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:3a8777c01491888bba9f40eda9d2bed76a611f0e80f75917501ce69c1a321342 -size 47333 +oid sha256:a34dc965cab9faa793895f1015563f0440389135257bb3e9682863c87e3cdae1 +size 47710 diff --git a/EndlessVendetta/Source/EndlessVendetta/GadgetSystem/GadgetClasses/Combat/OverloadModule/OverloadModule.cpp b/EndlessVendetta/Source/EndlessVendetta/GadgetSystem/GadgetClasses/Combat/OverloadModule/OverloadModule.cpp new file mode 100644 index 00000000..1ad9fad0 --- /dev/null +++ b/EndlessVendetta/Source/EndlessVendetta/GadgetSystem/GadgetClasses/Combat/OverloadModule/OverloadModule.cpp @@ -0,0 +1,122 @@ +// Fill out your copyright notice in the Description page of Project Settings. + + +#include "OverloadModule.h" + +#include "BehaviorTree/BTCompositeNode.h" +#include "Components/SphereComponent.h" +#include "GameFramework/Character.h" + +void AOverloadModule::BeginPlay() +{ + Super::BeginPlay(); + + this->SetActorTickInterval(0.2); + PlayerChar = GetWorld()->GetFirstPlayerController()->GetCharacter(); + PlayerCam = Cast(PlayerChar->GetComponentByClass(UCameraComponent::StaticClass())); +} + + +void AOverloadModule::Tick(float DeltaSeconds) +{ + Super::Tick(DeltaSeconds); + + CheckForPotentialHackTarget(); +} + +void AOverloadModule::CheckForPotentialHackTarget() +{ + FHitResult OutHit; + FVector LT_Start = PlayerCam->GetComponentLocation(); + FVector LT_End = PlayerCam->GetComponentLocation() + (PlayerCam->GetForwardVector() * DeadBodyCheckDistance); + + DrawDebugLine(GetWorld(), LT_Start, LT_End, FColor::Red, false, 0.2, 0, 1); + + if (!GetWorld()->LineTraceSingleByChannel(OutHit, LT_Start, LT_End, ECC_Camera) || !(OutHit.GetActor()->ActorHasTag(FName("Enemy")) && OutHit.GetActor()->ActorHasTag(FName("Dead")))) + { + PotentialHackTarget = nullptr; + DisplayNeedADeadEnemy(); + return; + } + + if (HackedEnemies.Contains(OutHit.GetActor()->GetUniqueID())) + { + PotentialHackTarget = nullptr; + DisplayAlreadyHacked(); + } + + DrawDebugLine(GetWorld(), LT_Start, OutHit.ImpactPoint, FColor::Green, false, 0.2, 0, 1); + PotentialHackTarget = OutHit.GetActor(); + DisplayFoundPotentialTarget(); +} + + +void AOverloadModule::Activate() +{ + if (GadgetCantBeUsed() || !IsValid(PotentialHackTarget)) return; + + Super::Activate(); + HackedEnemies.Add(PotentialHackTarget->GetUniqueID()); + ExplodeClosestAliveEnemy(); +} + +void AOverloadModule::FinishedUsing() +{ + Super::FinishedUsing(); +} + +void AOverloadModule::ExplodeClosestAliveEnemy() +{ + FActorSpawnParameters SpawnParams; + SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn; + AActor* SphereCollisionActor = GetWorld()->SpawnActor(USphereComponent::StaticClass(), PlayerChar->GetActorLocation(), PlayerChar->GetActorRotation(), SpawnParams); + USphereComponent* SphereCollision = Cast(SphereCollisionActor); + + SphereCollision->SetHiddenInGame(false); + SphereCollision->SetSphereRadius(AreaOfEffectInMeters * 100, true); + + TArray OverlappingActors; + TArray EnemiesInRadius; + + // Find all living enemies within the radius + SphereCollision->GetOverlappingActors(OverlappingActors, ACharacter::StaticClass()); + for (AActor* OverlappingActor : OverlappingActors) + { + if (OverlappingActor->ActorHasTag(FName("Enemy")) && !OverlappingActor->ActorHasTag(FName("Dead"))) EnemiesInRadius.Add(OverlappingActor); + } + + if (EnemiesInRadius.IsEmpty()) + { + DisplayFailedToFindTarget(); + return; + } + + // Find the closest Enemy in radius + AActor* ClosestEnemy = EnemiesInRadius[0]; + float Dist = FVector::Distance(PlayerChar->GetActorLocation(), ClosestEnemy->GetActorLocation()); + UE_LOG(LogTemp, Warning, TEXT("Distacne To first Considered Enemy = %f"), Dist); + if (EnemiesInRadius.Num() < 2) + { + SpawnExplosiveOnActor(ClosestEnemy, SpawnParams); + return; + } + + for (int i = 1; i < EnemiesInRadius.Num(); i++) + { + float ConsideredDist = FVector::Distance(PlayerChar->GetActorLocation(), EnemiesInRadius[i]->GetActorLocation()); + if (ConsideredDist <= Dist) + { + ClosestEnemy = EnemiesInRadius[i]; + Dist = ConsideredDist; + } + } + + SpawnExplosiveOnActor(ClosestEnemy, SpawnParams); + +} + +void AOverloadModule::SpawnExplosiveOnActor(AActor* ActortoExplode, FActorSpawnParameters SpawnParams) +{ + GetWorld()->SpawnActor(Explosive, ActortoExplode->GetActorLocation(), ActortoExplode->GetActorRotation(), SpawnParams); +} + diff --git a/EndlessVendetta/Source/EndlessVendetta/GadgetSystem/GadgetClasses/Combat/OverloadModule/OverloadModule.h b/EndlessVendetta/Source/EndlessVendetta/GadgetSystem/GadgetClasses/Combat/OverloadModule/OverloadModule.h new file mode 100644 index 00000000..7dbfcb41 --- /dev/null +++ b/EndlessVendetta/Source/EndlessVendetta/GadgetSystem/GadgetClasses/Combat/OverloadModule/OverloadModule.h @@ -0,0 +1,64 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#pragma once + +#include "CoreMinimal.h" +#include "Camera/CameraComponent.h" +#include "EndlessVendetta/GadgetSystem/CombatGadget.h" +#include "EndlessVendetta/GadgetSystem/GadgetClasses/Combat/Explosive.h" +#include "OverloadModule.generated.h" + +/** + * + */ +UCLASS() +class ENDLESSVENDETTA_API AOverloadModule : public ACombatGadget +{ + GENERATED_BODY() + + ACharacter* PlayerChar; + + UCameraComponent* PlayerCam; + + AActor* PotentialHackTarget; + + // An Array of previously hacked enemies, stops player from hacking the same Dead Enemy + TArray HackedEnemies; + + virtual void BeginPlay() override; + + virtual void Tick(float DeltaSeconds) override; + + virtual void Activate() override; + + virtual void FinishedUsing() override; + + void CheckForPotentialHackTarget(); + + void ExplodeClosestAliveEnemy(); + + void SpawnExplosiveOnActor(AActor* ActortoExplode, FActorSpawnParameters SpawnParams); + +protected: + UPROPERTY(EditDefaultsOnly, Category = "Overload Module") + float AreaOfEffectInMeters = 15; + + UPROPERTY(EditDefaultsOnly, Category = "Overload Module") + float DeadBodyCheckDistance = 300; + + UPROPERTY(EditDefaultsOnly, Category = "Overload Module") + TSubclassOf Explosive; + +// --------------- Display Functions --------------- + UFUNCTION(BlueprintImplementableEvent, Category = "Overload Module") + void DisplayAlreadyHacked(); + + UFUNCTION(BlueprintImplementableEvent, Category = "Overload Module") + void DisplayNeedADeadEnemy(); + + UFUNCTION(BlueprintImplementableEvent, Category = "Overload Module") + void DisplayFoundPotentialTarget(); + + UFUNCTION(BlueprintImplementableEvent, Category = "Overload Module") + void DisplayFailedToFindTarget(); +};