Skip to content

Commit

Permalink
#175 Added balls grid
Browse files Browse the repository at this point in the history
  • Loading branch information
xthebat committed Feb 16, 2024
1 parent 1f17a7b commit 0e40cc5
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 0 deletions.
Binary file modified Content/Maps/warmup.umap
Binary file not shown.
69 changes: 69 additions & 0 deletions Source/Cloud9/Environment/Cloud9ShootingRange.cpp
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;
}
48 changes: 48 additions & 0 deletions Source/Cloud9/Environment/Cloud9ShootingRange.h
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();
};
20 changes: 20 additions & 0 deletions Source/Cloud9/Tools/Extensions/FVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#pragma once

#include "Cloud9/Tools/Macro/Operator.h"
#include "Cloud9/Tools/Macro/Common.h"
#include "Cloud9/Tools/Concepts.h"

namespace EFVector
Expand All @@ -40,4 +41,23 @@ namespace EFVector

OPERATOR_BODY(Normalize)
};

inline FVector Random(FVector Min, FVector Max)
{
return {
FMath::RandRange(Min.X, Max.X),
FMath::RandRange(Min.Y, Max.Y),
FMath::RandRange(Min.Z, Max.Z),
};
}

inline FVector Random(FVector Min, FVector Max, FVector Grid)
{
let Vector = Random(Min, Max);
return {
FMath::GridSnap(Vector.X, Grid.X),
FMath::GridSnap(Vector.Y, Grid.Y),
FMath::GridSnap(Vector.Z, Grid.Z),
};
}
}

0 comments on commit 0e40cc5

Please sign in to comment.