diff --git a/Content/Blueprints/Obstacles/BP_BarrierA.uasset b/Content/Blueprints/Obstacles/BP_BarrierA.uasset index f14fc53..64b21c9 100644 --- a/Content/Blueprints/Obstacles/BP_BarrierA.uasset +++ b/Content/Blueprints/Obstacles/BP_BarrierA.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:d63d07eb7cbc3421c09180f90ffb586b7c76ecf9185899fef8da113b6ac6c968 -size 31063 +oid sha256:cdff9ca7fc6ed13c785f63aadd65073e5878fd07db67af49ff681d356e16e860 +size 31545 diff --git a/Content/Blueprints/Obstacles/BP_BarrierStrongA.uasset b/Content/Blueprints/Obstacles/BP_BarrierStrongA.uasset index 4d4fbd9..bdbf00d 100644 --- a/Content/Blueprints/Obstacles/BP_BarrierStrongA.uasset +++ b/Content/Blueprints/Obstacles/BP_BarrierStrongA.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:28f1f010a5cef02d5a56076a200e986eb7a8175ebfc744275218b4c111020da5 -size 32160 +oid sha256:8bc7c7ecdf84ccc779ee4590b84fe34d874eb59217a049a22114e2b3f0297fb1 +size 30795 diff --git a/Content/Blueprints/Obstacles/BP_BarrierStrongDamaged.uasset b/Content/Blueprints/Obstacles/BP_BarrierStrongDamaged.uasset index 48e805c..13a5cac 100644 --- a/Content/Blueprints/Obstacles/BP_BarrierStrongDamaged.uasset +++ b/Content/Blueprints/Obstacles/BP_BarrierStrongDamaged.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:48db69693a6fa9f8bcda922563e04ad5ab0cdef8e9d7ad5d243e6e6550690f7a -size 31808 +oid sha256:6c7b9af625d5628413081f447adee9bbb03d4a696125726cbaa2f5c8b2d419a6 +size 30517 diff --git a/Content/Blueprints/Tiles/BP_TileStraight.uasset b/Content/Blueprints/Tiles/BP_TileStraight.uasset index a5029e0..762dd96 100644 --- a/Content/Blueprints/Tiles/BP_TileStraight.uasset +++ b/Content/Blueprints/Tiles/BP_TileStraight.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:b90f3c7238a1e632d67ece9f697a0f57b1550011561781496dca82807d5f03b2 -size 33905 +oid sha256:7f4bff795095498f6bcaa231001e92c2cb7630f69270a02a90687773b4fb1242 +size 35003 diff --git a/Source/EndlessZombie/Obstacle.cpp b/Source/EndlessZombie/Obstacle.cpp index 635fef3..8158041 100644 --- a/Source/EndlessZombie/Obstacle.cpp +++ b/Source/EndlessZombie/Obstacle.cpp @@ -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. PrimaryActorTick.bCanEverTick = true; + SceneComponent = CreateDefaultSubobject(TEXT("SceneComponent")); + RootComponent = SceneComponent; + + StaticMesh = CreateDefaultSubobject(TEXT("StaticMesh")); + StaticMesh->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepRelativeTransform); + StaticMesh->SetRelativeRotation(FRotator(0.f, 90.0f, 0.f)); } // Called when the game starts or when spawned diff --git a/Source/EndlessZombie/Obstacle.h b/Source/EndlessZombie/Obstacle.h index 7b0343e..e7876e7 100644 --- a/Source/EndlessZombie/Obstacle.h +++ b/Source/EndlessZombie/Obstacle.h @@ -15,6 +15,11 @@ public: // Sets default values for this actor's properties AObstacle(); + UPROPERTY(EditAnywhere, BlueprintReadWrite) + USceneComponent* SceneComponent; + UPROPERTY(EditAnywhere, BlueprintReadWrite) + UStaticMeshComponent* StaticMesh; + protected: // Called when the game starts or when spawned virtual void BeginPlay() override; diff --git a/Source/EndlessZombie/PathGenerator.cpp b/Source/EndlessZombie/PathGenerator.cpp index d895e60..33eb1ad 100644 --- a/Source/EndlessZombie/PathGenerator.cpp +++ b/Source/EndlessZombie/PathGenerator.cpp @@ -2,6 +2,7 @@ #include "PathGenerator.h" +#include "TileCurve.h" // Sets default values APathGenerator::APathGenerator() @@ -34,11 +35,19 @@ void APathGenerator::Tick(float DeltaTime) 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(Tiles[iRandomIndex], tNextSpawnPoint); + if (Cast(GeneratedTile)) + bIsCurveLastTile = true; + else + bIsCurveLastTile = false; tNextSpawnPoint = GeneratedTile->GetAttachTransform(); } } diff --git a/Source/EndlessZombie/PathGenerator.h b/Source/EndlessZombie/PathGenerator.h index 8015f28..6bc2793 100644 --- a/Source/EndlessZombie/PathGenerator.h +++ b/Source/EndlessZombie/PathGenerator.h @@ -37,4 +37,7 @@ public: void AddSelectedTile(TSubclassOf Tile); FTransform tNextSpawnPoint = FTransform(FVector(90.0f, 1110.0f, 0.0f)); + +private: + bool bIsCurveLastTile = false; }; diff --git a/Source/EndlessZombie/Tile.cpp b/Source/EndlessZombie/Tile.cpp index ed6a5e8..8a694c7 100644 --- a/Source/EndlessZombie/Tile.cpp +++ b/Source/EndlessZombie/Tile.cpp @@ -6,6 +6,7 @@ #include "PathGenerator.h" #include "Kismet/GameplayStatics.h" #include "Kismet/KismetSystemLibrary.h" +#include "Kismet/KismetMathLibrary.h" // Sets default values ATile::ATile() @@ -23,6 +24,13 @@ ATile::ATile() EndTrigger = CreateDefaultSubobject(TEXT("EndPoint")); EndTrigger->SetupAttachment(SceneComponent); EndTrigger->SetCollisionProfileName(TEXT("OverlapOnlyPawn")); + + // Add the obstacle zone + ObstacleZone = CreateDefaultSubobject(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 @@ -31,6 +39,9 @@ void ATile::BeginPlay() Super::BeginPlay(); EndTrigger->OnComponentBeginOverlap.AddDynamic(this, &ATile::OnBeginOverlap); + + SpawnRandomLocation(ObstacleZone); + } // Called every frame @@ -63,16 +74,46 @@ void ATile::OnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* Oth FoundPathGenerator->AddFloorTile(); FTimerHandle TimerHandle; - + //GetWorldTimerManager().SetTimer(TimerHandle, this, &ATimeKollapsed_PlayerController::AddWidgetToViewPort, GameOverDelay, 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(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() { + TArray childActors; + GetAttachedActors(childActors); + for (auto childActor : childActors) + { + childActor->Destroy(); + } this->Destroy(); } diff --git a/Source/EndlessZombie/Tile.h b/Source/EndlessZombie/Tile.h index 43487dc..536d40c 100644 --- a/Source/EndlessZombie/Tile.h +++ b/Source/EndlessZombie/Tile.h @@ -6,6 +6,7 @@ #include "GameFramework/Actor.h" #include "Components/ArrowComponent.h" #include "Components/BoxComponent.h" +#include "Obstacle.h" #include "Tile.generated.h" UCLASS() @@ -31,12 +32,19 @@ public: UArrowComponent* ArrowComponent; UPROPERTY(EditAnywhere, BlueprintReadWrite) UBoxComponent* EndTrigger; + UPROPERTY(EditAnywhere, BlueprintReadWrite) + UBoxComponent* ObstacleZone; UFUNCTION(BlueprintCallable, Category = "AttachedPoint") FTransform GetAttachTransform(); UFUNCTION() 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> ObstacleArray; + private: void DestroyTile(); };