Brain spawn

Added the functionality to spawn randomly one brain per tile
This commit is contained in:
JKuijperM 2023-06-05 18:41:05 +02:00
parent 800b10d65f
commit 52c3dff6d0
7 changed files with 52 additions and 18 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:947a238f28a5c5d94fece83194baf572e1ba3e0f9c3c94cb264947887b5e42c8 oid sha256:98a9077418a30a25184b951cd22f4627fc4f37b8bf0f71ce8908015b33c302dd
size 35071 size 35401

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1 version https://git-lfs.github.com/spec/v1
oid sha256:368e355640209d925f14b1aa209150fae69e0fdc8cc6ea88769dceed6c6766ef oid sha256:ceb7baf8d39f681cb829006fc9dac54aaf2dc0ce00457bff7a336af811c690f6
size 36928 size 37357

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1 version https://git-lfs.github.com/spec/v1
oid sha256:302d46c4efd912d483f97d9e2e469566dd5eca88883381779e45b2924ec3e0f0 oid sha256:9b103009453641f601ab59fcf1237bc369000b37128a2d006f252b900265deac
size 37105 size 37544

View File

@ -2,6 +2,7 @@
#include "Brain.h" #include "Brain.h"
#include "ZombieCharacter.h"
#include "EndlessZombieGameMode.h" #include "EndlessZombieGameMode.h"
#include "Kismet/GameplayStatics.h" #include "Kismet/GameplayStatics.h"
#include "Components/BoxComponent.h" #include "Components/BoxComponent.h"
@ -22,7 +23,11 @@ void ABrain::OnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* Ot
{ {
Super::OnBeginOverlap(OverlappedComponent, OtherActor, OtherComp, OtherBodyIndex, bFromSweep, SweepResult); Super::OnBeginOverlap(OverlappedComponent, OtherActor, OtherComp, OtherBodyIndex, bFromSweep, SweepResult);
AZombieCharacter* ZombieCharacter = Cast<AZombieCharacter>(OtherActor);
if (ZombieCharacter)
{
AEndlessZombieGameMode* CurrentGameMode = Cast<AEndlessZombieGameMode>(UGameplayStatics::GetGameMode(GetWorld())); AEndlessZombieGameMode* CurrentGameMode = Cast<AEndlessZombieGameMode>(UGameplayStatics::GetGameMode(GetWorld()));
if (CurrentGameMode) if (CurrentGameMode)
CurrentGameMode->UpdateScore(iPunctuation); CurrentGameMode->UpdateScore(iPunctuation);
}
} }

View File

@ -2,6 +2,7 @@
#include "PickupItem.h" #include "PickupItem.h"
#include "ZombieCharacter.h"
#include "Components/BoxComponent.h" #include "Components/BoxComponent.h"
// Sets default values // Sets default values
@ -28,6 +29,8 @@ void APickupItem::BeginPlay()
void APickupItem::OnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) void APickupItem::OnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{ {
AZombieCharacter* ZombieCharacter = Cast<AZombieCharacter>(OtherActor);
if (ZombieCharacter)
this->Destroy(); this->Destroy();
} }

View File

@ -2,8 +2,9 @@
#include "Tile.h" #include "Tile.h"
#include "ZombieCharacter.h" #include "Brain.h"
#include "PathGenerator.h" #include "PathGenerator.h"
#include "ZombieCharacter.h"
#include "Kismet/GameplayStatics.h" #include "Kismet/GameplayStatics.h"
#include "Kismet/KismetSystemLibrary.h" #include "Kismet/KismetSystemLibrary.h"
#include "Kismet/KismetMathLibrary.h" #include "Kismet/KismetMathLibrary.h"
@ -32,6 +33,11 @@ ATile::ATile()
ObstacleZone->SetRelativeLocation(FVector(511.0f, 0.f, 26.0f)); ObstacleZone->SetRelativeLocation(FVector(511.0f, 0.f, 26.0f));
ObstacleZone->SetRelativeScale3D(FVector(12.50f, 14.5f, 1.0f)); ObstacleZone->SetRelativeScale3D(FVector(12.50f, 14.5f, 1.0f));
// Add the brain zone
BrainZone = CreateDefaultSubobject<UBoxComponent>(TEXT("BrainZone"));
BrainZone->AttachToComponent(SceneComponent, FAttachmentTransformRules::KeepRelativeTransform);
BrainZone->SetRelativeLocation(FVector(511.0f, 0.f, 133.0f));
BrainZone->SetRelativeScale3D(FVector(12.50f, 11.16f, 1.0f));
} }
// Called when the game starts or when spawned // Called when the game starts or when spawned
@ -41,8 +47,8 @@ void ATile::BeginPlay()
EndTrigger->OnComponentBeginOverlap.AddDynamic(this, &ATile::OnBeginOverlap); EndTrigger->OnComponentBeginOverlap.AddDynamic(this, &ATile::OnBeginOverlap);
SpawnRandomLocation(ObstacleZone); SpawnObstacleAtRandomLocation(ObstacleZone);
SpawnBrainAtRandomLocation(BrainZone);
} }
// Called every frame // Called every frame
@ -83,7 +89,7 @@ void ATile::OnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* Oth
} }
} }
void ATile::SpawnRandomLocation(UBoxComponent* spawnArea) void ATile::SpawnObstacleAtRandomLocation(UBoxComponent* spawnArea)
{ {
// TODO: Check if I have to spawn something // TODO: Check if I have to spawn something
if (ObstacleArray.Num() > 0) if (ObstacleArray.Num() > 0)
@ -107,6 +113,20 @@ void ATile::SpawnRandomLocation(UBoxComponent* spawnArea)
} }
} }
void ATile::SpawnBrainAtRandomLocation(UBoxComponent* spawnArea)
{
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));
ABrain* aBrain = GetWorld()->SpawnActor<ABrain>(vRandomPoint, FRotator::ZeroRotator);
//aBrain->AttachToActor(this, FAttachmentTransformRules::KeepRelativeTransform);
}
void ATile::DestroyTile() void ATile::DestroyTile()
{ {
TArray<AActor*> childActors; TArray<AActor*> childActors;

View File

@ -34,17 +34,23 @@ public:
UBoxComponent* EndTrigger; UBoxComponent* EndTrigger;
UPROPERTY(EditAnywhere, BlueprintReadWrite) UPROPERTY(EditAnywhere, BlueprintReadWrite)
UBoxComponent* ObstacleZone; UBoxComponent* ObstacleZone;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
UBoxComponent* BrainZone;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Pickup Item")
int iMaxBrains = 3;
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); void SpawnObstacleAtRandomLocation(UBoxComponent* spawnArea);
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Obstacles") UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Obstacles")
TArray<TSubclassOf<class AObstacle>> ObstacleArray; TArray<TSubclassOf<class AObstacle>> ObstacleArray;
void SpawnBrainAtRandomLocation(UBoxComponent* spawnArea);
private: private:
void DestroyTile(); void DestroyTile();
}; };