-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright (c) 2024 Alexei Gladkikh | ||
|
||
#include "Cloud9ShootingRange.h" | ||
|
||
#include "Cloud9/Tools/Extensions/FVector.h" | ||
#include "Cloud9/Tools/Extensions/TContainer.h" | ||
#include "Components/BoxComponent.h" | ||
|
||
FName ACloud9ShootingRange::ZoneComponentName = "ZoneComponentName"; | ||
|
||
ACloud9ShootingRange::ACloud9ShootingRange() | ||
{ | ||
PrimaryActorTick.bCanEverTick = true; | ||
ZoneComponent = CreateDefaultSubobject<UBoxComponent>(ZoneComponentName); | ||
RootComponent = ZoneComponent; | ||
ZoneSize = {100.0f, 100.0f, 100.0f}; | ||
Count = 1; | ||
GridSize = FVector::OneVector; | ||
} | ||
|
||
void ACloud9ShootingRange::OnConstruction(const FTransform& Transform) | ||
{ | ||
Super::OnConstruction(Transform); | ||
ZoneComponent->SetBoxExtent(ZoneSize); | ||
} | ||
|
||
void ACloud9ShootingRange::BeginPlay() | ||
{ | ||
Super::BeginPlay(); | ||
} | ||
|
||
void ACloud9ShootingRange::Tick(float DeltaTime) | ||
{ | ||
Super::Tick(DeltaTime); | ||
SpawnShootingActors(); | ||
} | ||
|
||
bool ACloud9ShootingRange::SpawnShootingActors() | ||
{ | ||
int Retries = 0; | ||
FVector Origin; | ||
FVector BoxExtent; | ||
GetActorBounds(false, Origin, BoxExtent); | ||
while (Actors.Num() != Count) | ||
{ | ||
// GridSize items can be zero (GridSnap should handle it) | ||
let Location = EFVector::Random(BoxExtent - Origin, BoxExtent + Origin, GridSize); | ||
let Actor = GetWorld()->SpawnActor(Class.Get(), &Location); | ||
if (Actors | ETContainer::AnyByPredicate{[Actor](let It) { return It->IsOverlappingActor(Actor); }}) | ||
{ | ||
if (constexpr int MaxRetries = 10; Retries++ == MaxRetries) | ||
{ | ||
log( | ||
Error, | ||
"[Range=%s] parameters seems to be invalid can't spawn specified count of Actors = %d", | ||
*GetName(), Count | ||
); | ||
SetActorTickEnabled(false); | ||
return false; | ||
} | ||
|
||
continue; | ||
} | ||
|
||
Actors.Add(Actor); | ||
} | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright (c) 2024 Alexei Gladkikh | ||
|
||
#pragma once | ||
|
||
#include "CoreMinimal.h" | ||
#include "GameFramework/Actor.h" | ||
#include "Cloud9ShootingRange.generated.h" | ||
|
||
class UBoxComponent; | ||
|
||
UCLASS() | ||
class CLOUD9_API ACloud9ShootingRange : public AActor | ||
{ | ||
GENERATED_BODY() | ||
|
||
public: | ||
static FName ZoneComponentName; | ||
|
||
ACloud9ShootingRange(); | ||
|
||
protected: | ||
virtual void OnConstruction(const FTransform& Transform) override; | ||
|
||
virtual void BeginPlay() override; | ||
|
||
virtual void Tick(float DeltaTime) override; | ||
|
||
UPROPERTY(BlueprintReadOnly, Category=Implementation) | ||
UBoxComponent* ZoneComponent; | ||
|
||
UPROPERTY(BlueprintReadOnly, Category=Implementation) | ||
TArray<AActor*> Actors; | ||
|
||
UPROPERTY(EditAnywhere, Category=Config) | ||
int Count; | ||
|
||
UPROPERTY(EditAnywhere, Category=Config) | ||
TSoftClassPtr<AActor> Class; | ||
|
||
UPROPERTY(EditAnywhere, Category=Config) | ||
FVector ZoneSize; | ||
|
||
UPROPERTY(EditAnywhere, Category=Config) | ||
FVector GridSize; | ||
|
||
private: | ||
bool SpawnShootingActors(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters