Add basic logic to `EnemyCharacter` `OnFire` functions

This commit is contained in:
Louis Hobbs 2023-03-13 22:58:21 +00:00
parent e77b6cce35
commit 8a7ee5faee
1 changed files with 59 additions and 1 deletions

View File

@ -4,6 +4,8 @@
#include "GameFramework/CharacterMovementComponent.h" #include "GameFramework/CharacterMovementComponent.h"
#include "EnemyAIController.h" #include "EnemyAIController.h"
#define COLLISION_WEAPON ECC_GameTraceChannel1
AEnemyCharacter::AEnemyCharacter() AEnemyCharacter::AEnemyCharacter()
{ {
PerceptionComponent = CreateDefaultSubobject<UAIPerceptionComponent>(TEXT("Perception Component")); PerceptionComponent = CreateDefaultSubobject<UAIPerceptionComponent>(TEXT("Perception Component"));
@ -38,11 +40,17 @@ UAIPerceptionComponent* AEnemyCharacter::GetPerceptionComponent()
void AEnemyCharacter::OnFire() void AEnemyCharacter::OnFire()
{ {
CurrentWeapon->SetCurrentWeaponStatus(WeaponState::Firing);
TArray<FHitResult> Hits = TArray<FHitResult>(); TArray<FHitResult> Hits = TArray<FHitResult>();
CalculateHits(&Hits); CalculateHits(&Hits);
ProcessHits(Hits); ProcessHits(Hits);
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("BANG")); CurrentWeapon->PlayFireSoundAtLocation(GetActorLocation());
// TODO: Play some animation here
CurrentWeapon->SetCurrentWeaponStatus(WeaponState::Cooldown);
} }
void AEnemyCharacter::BeginPlay() void AEnemyCharacter::BeginPlay()
@ -52,8 +60,58 @@ void AEnemyCharacter::BeginPlay()
void AEnemyCharacter::CalculateHits(TArray<FHitResult>* hits) void AEnemyCharacter::CalculateHits(TArray<FHitResult>* hits)
{ {
// Set up randomness
const int32 RandomSeed = FMath::Rand();
FRandomStream WeaponRandomStream(RandomSeed);
const float Spread = CurrentWeapon->GetWeaponProperties()->WeaponSpread;
const float Range = CurrentWeapon->GetWeaponProperties()->ProjectileRange;
// Calculate starting position and direction
FVector TraceStart = this->GetTransform().GetLocation();
FRotator PlayerRot = this->GetTransform().GetRotation().Rotator();
TraceStart = GetRootComponent()->GetComponentLocation();
FVector AimDir = PlayerRot.Vector();
AimDir.Z = 0.0;
TraceStart = TraceStart + AimDir * ((GetInstigator()->GetActorLocation() - TraceStart) | AimDir);
// Calculate the hit results from the trace
TArray<FHitResult> HitResults;
// Set up the collision query params, use the Weapon trace settings, Ignore the actor firing this trace
FCollisionQueryParams TraceParams(SCENE_QUERY_STAT(WeaponTrace), true, GetInstigator());
TraceParams.bReturnPhysicalMaterial = true;
for (size_t i = 0; i < CurrentWeapon->GetWeaponProperties()->ProjectilesPerShot; i++)
{
// Calculate the maximum distance the weapon can fire
FVector ShootDir = WeaponRandomStream.VRandCone(AimDir, FMath::DegreesToRadians(Spread), FMath::DegreesToRadians(Spread));
FVector MaxHitLoc = TraceStart + (ShootDir * Range);
GetWorld()->LineTraceMultiByChannel(HitResults, TraceStart, MaxHitLoc, COLLISION_WEAPON, TraceParams);
for (FHitResult Result : HitResults)
{
hits->Add(Result);
DrawDebugLine(GetWorld(), TraceStart, Result.ImpactPoint, FColor::Red, true, 500, 0U, 0);
}
}
} }
void AEnemyCharacter::ProcessHits(TArray<FHitResult> hits) void AEnemyCharacter::ProcessHits(TArray<FHitResult> hits)
{ {
for (FHitResult Hit : hits)
{
// TODO: Handle hits in a meaningful way
FActorSpawnParameters SpawnParameters;
SpawnParameters.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
// Spawn field actor
FTransform transform;
transform.SetLocation(Hit.ImpactPoint);
auto field = GetWorld()->SpawnActor<AFieldSystemActor>(CurrentWeapon->GetFieldSystemActor(), transform, SpawnParameters);
if (Hit.GetActor())
{
}
}
} }