Added the funcionality to spawn obstacles

This commit is contained in:
JKuijperM 2023-02-05 18:46:39 +01:00
parent 737c316700
commit e903f1733f
10 changed files with 84 additions and 12 deletions

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1 version https://git-lfs.github.com/spec/v1
oid sha256:d63d07eb7cbc3421c09180f90ffb586b7c76ecf9185899fef8da113b6ac6c968 oid sha256:cdff9ca7fc6ed13c785f63aadd65073e5878fd07db67af49ff681d356e16e860
size 31063 size 31545

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1 version https://git-lfs.github.com/spec/v1
oid sha256:28f1f010a5cef02d5a56076a200e986eb7a8175ebfc744275218b4c111020da5 oid sha256:8bc7c7ecdf84ccc779ee4590b84fe34d874eb59217a049a22114e2b3f0297fb1
size 32160 size 30795

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1 version https://git-lfs.github.com/spec/v1
oid sha256:48db69693a6fa9f8bcda922563e04ad5ab0cdef8e9d7ad5d243e6e6550690f7a oid sha256:6c7b9af625d5628413081f447adee9bbb03d4a696125726cbaa2f5c8b2d419a6
size 31808 size 30517

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1 version https://git-lfs.github.com/spec/v1
oid sha256:b90f3c7238a1e632d67ece9f697a0f57b1550011561781496dca82807d5f03b2 oid sha256:7f4bff795095498f6bcaa231001e92c2cb7630f69270a02a90687773b4fb1242
size 33905 size 35003

View File

@ -9,6 +9,12 @@ AObstacle::AObstacle()
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it. // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true; PrimaryActorTick.bCanEverTick = true;
SceneComponent = CreateDefaultSubobject<USceneComponent>(TEXT("SceneComponent"));
RootComponent = SceneComponent;
StaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMesh"));
StaticMesh->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepRelativeTransform);
StaticMesh->SetRelativeRotation(FRotator(0.f, 90.0f, 0.f));
} }
// Called when the game starts or when spawned // Called when the game starts or when spawned

View File

@ -15,6 +15,11 @@ public:
// Sets default values for this actor's properties // Sets default values for this actor's properties
AObstacle(); AObstacle();
UPROPERTY(EditAnywhere, BlueprintReadWrite)
USceneComponent* SceneComponent;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
UStaticMeshComponent* StaticMesh;
protected: protected:
// Called when the game starts or when spawned // Called when the game starts or when spawned
virtual void BeginPlay() override; virtual void BeginPlay() override;

View File

@ -2,6 +2,7 @@
#include "PathGenerator.h" #include "PathGenerator.h"
#include "TileCurve.h"
// Sets default values // Sets default values
APathGenerator::APathGenerator() APathGenerator::APathGenerator()
@ -34,11 +35,19 @@ void APathGenerator::Tick(float DeltaTime)
void APathGenerator::AddFloorTile() void APathGenerator::AddFloorTile()
{ {
if (Tiles[0] != nullptr) if (Tiles.Num() > 0)
{ {
int iRandomIndex;
if (!bIsCurveLastTile)
iRandomIndex = FMath::RandRange(0, Tiles.Num() - 1);
else
iRandomIndex = 0;
int iRandomIndex = FMath::RandRange(0, Tiles.Num() - 1);
ATile* GeneratedTile = GetWorld()->SpawnActor<ATile>(Tiles[iRandomIndex], tNextSpawnPoint); ATile* GeneratedTile = GetWorld()->SpawnActor<ATile>(Tiles[iRandomIndex], tNextSpawnPoint);
if (Cast<ATileCurve>(GeneratedTile))
bIsCurveLastTile = true;
else
bIsCurveLastTile = false;
tNextSpawnPoint = GeneratedTile->GetAttachTransform(); tNextSpawnPoint = GeneratedTile->GetAttachTransform();
} }
} }

View File

@ -37,4 +37,7 @@ public:
void AddSelectedTile(TSubclassOf<class ATile> Tile); void AddSelectedTile(TSubclassOf<class ATile> Tile);
FTransform tNextSpawnPoint = FTransform(FVector(90.0f, 1110.0f, 0.0f)); FTransform tNextSpawnPoint = FTransform(FVector(90.0f, 1110.0f, 0.0f));
private:
bool bIsCurveLastTile = false;
}; };

View File

@ -6,6 +6,7 @@
#include "PathGenerator.h" #include "PathGenerator.h"
#include "Kismet/GameplayStatics.h" #include "Kismet/GameplayStatics.h"
#include "Kismet/KismetSystemLibrary.h" #include "Kismet/KismetSystemLibrary.h"
#include "Kismet/KismetMathLibrary.h"
// Sets default values // Sets default values
ATile::ATile() ATile::ATile()
@ -23,6 +24,13 @@ ATile::ATile()
EndTrigger = CreateDefaultSubobject<UBoxComponent>(TEXT("EndPoint")); EndTrigger = CreateDefaultSubobject<UBoxComponent>(TEXT("EndPoint"));
EndTrigger->SetupAttachment(SceneComponent); EndTrigger->SetupAttachment(SceneComponent);
EndTrigger->SetCollisionProfileName(TEXT("OverlapOnlyPawn")); EndTrigger->SetCollisionProfileName(TEXT("OverlapOnlyPawn"));
// Add the obstacle zone
ObstacleZone = CreateDefaultSubobject<UBoxComponent>(TEXT("ObstacleZone"));
ObstacleZone->AttachToComponent(SceneComponent, FAttachmentTransformRules::KeepRelativeTransform);
ObstacleZone->SetRelativeLocation(FVector(511.0f, 0.f, 26.0f));
ObstacleZone->SetRelativeScale3D(FVector(12.50f, 14.5f, 1.0f));
} }
// Called when the game starts or when spawned // Called when the game starts or when spawned
@ -31,6 +39,9 @@ void ATile::BeginPlay()
Super::BeginPlay(); Super::BeginPlay();
EndTrigger->OnComponentBeginOverlap.AddDynamic(this, &ATile::OnBeginOverlap); EndTrigger->OnComponentBeginOverlap.AddDynamic(this, &ATile::OnBeginOverlap);
SpawnRandomLocation(ObstacleZone);
} }
// Called every frame // Called every frame
@ -63,16 +74,46 @@ void ATile::OnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* Oth
FoundPathGenerator->AddFloorTile(); FoundPathGenerator->AddFloorTile();
FTimerHandle TimerHandle; FTimerHandle TimerHandle;
//GetWorldTimerManager().SetTimer(TimerHandle, this, &ATimeKollapsed_PlayerController::AddWidgetToViewPort, GameOverDelay, false); //GetWorldTimerManager().SetTimer(TimerHandle, this, &ATimeKollapsed_PlayerController::AddWidgetToViewPort, GameOverDelay, false);
GetWorldTimerManager().SetTimer(TimerHandle, this, &ATile::DestroyTile, 2.f, false); GetWorldTimerManager().SetTimer(TimerHandle, this, &ATile::DestroyTile, 2.f, false);
} }
} }
} }
void ATile::SpawnRandomLocation(UBoxComponent* spawnArea)
{
// TODO: Check if I have to spawn something
if (ObstacleArray.Num() > 0)
{
FVector vOrigin = spawnArea->Bounds.Origin;
FVector vBoxExtent = spawnArea->Bounds.BoxExtent;
//FVector vRandomPoint = RandomPointInBoundingBox(vOrigin, vBoxExtent);
const FVector BoxMin = vOrigin - vBoxExtent;
const FVector BoxMax = vOrigin + vBoxExtent;
FVector vRandomPoint = FMath::RandPointInBox(FBox(BoxMin, BoxMax));
int iRandomIndex = FMath::RandRange(0, ObstacleArray.Num() - 1);
AActor* obstacle = GetWorld()->SpawnActor<AActor>(ObstacleArray[iRandomIndex]/*, vRandomPoint, FRotator::ZeroRotator*/);
//obstacle->AttachToComponent(SceneComponent, FAttachmentTransformRules::KeepRelativeTransform);
obstacle->AttachToActor(this, FAttachmentTransformRules::KeepRelativeTransform);
vRandomPoint.Z = GetActorLocation().Z - 12;
obstacle->SetActorLocation(vRandomPoint);
obstacle->SetActorRotation(GetActorRotation());
}
}
void ATile::DestroyTile() void ATile::DestroyTile()
{ {
TArray<AActor*> childActors;
GetAttachedActors(childActors);
for (auto childActor : childActors)
{
childActor->Destroy();
}
this->Destroy(); this->Destroy();
} }

View File

@ -6,6 +6,7 @@
#include "GameFramework/Actor.h" #include "GameFramework/Actor.h"
#include "Components/ArrowComponent.h" #include "Components/ArrowComponent.h"
#include "Components/BoxComponent.h" #include "Components/BoxComponent.h"
#include "Obstacle.h"
#include "Tile.generated.h" #include "Tile.generated.h"
UCLASS() UCLASS()
@ -31,12 +32,19 @@ public:
UArrowComponent* ArrowComponent; UArrowComponent* ArrowComponent;
UPROPERTY(EditAnywhere, BlueprintReadWrite) UPROPERTY(EditAnywhere, BlueprintReadWrite)
UBoxComponent* EndTrigger; UBoxComponent* EndTrigger;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
UBoxComponent* ObstacleZone;
UFUNCTION(BlueprintCallable, Category = "AttachedPoint") UFUNCTION(BlueprintCallable, Category = "AttachedPoint")
FTransform GetAttachTransform(); FTransform GetAttachTransform();
UFUNCTION() UFUNCTION()
virtual void OnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult); virtual void OnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
void SpawnRandomLocation(UBoxComponent* boxTrigger);
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Obstacles")
TArray<TSubclassOf<class AObstacle>> ObstacleArray;
private: private:
void DestroyTile(); void DestroyTile();
}; };