From 2e59bb6e0e2c118c8ac4a07dc79117a453e5413a Mon Sep 17 00:00:00 2001 From: baz Date: Wed, 26 Jun 2024 00:17:14 +0100 Subject: [PATCH] Add enemy spawning --- Content/Gamemode/BP_DefaultGamemode.uasset | 4 +- Source/vampires/VampireGameMode.cpp | 59 ++++++++++++++++++++++ Source/vampires/VampireGameMode.h | 21 +++++++- 3 files changed, 81 insertions(+), 3 deletions(-) diff --git a/Content/Gamemode/BP_DefaultGamemode.uasset b/Content/Gamemode/BP_DefaultGamemode.uasset index ac85e62..82d5e9b 100644 --- a/Content/Gamemode/BP_DefaultGamemode.uasset +++ b/Content/Gamemode/BP_DefaultGamemode.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:88e3e3207b0f1b3c1444a2d232dbf2c320a94fa3c97733c3eec6fd9712229814 -size 21126 +oid sha256:94e59ead95ffb4f9a230fa05df1e608852c96e459d1caea091b031156997e65d +size 21323 diff --git a/Source/vampires/VampireGameMode.cpp b/Source/vampires/VampireGameMode.cpp index 7f36029..9b7f1fa 100644 --- a/Source/vampires/VampireGameMode.cpp +++ b/Source/vampires/VampireGameMode.cpp @@ -3,3 +3,62 @@ #include "VampireGameMode.h" +#include "Kismet/GameplayStatics.h" + +void AVampireGameMode::BeginPlay() +{ + Super::BeginPlay(); + + PlayerCharacter = Cast(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0)); + PlayerController = Cast(UGameplayStatics::GetPlayerController(PlayerCharacter, 0)); + + GetWorldTimerManager().SetTimer(SpawnEnemyTimerDelegate, this, &AVampireGameMode::SpawnEnemy, 1.0f, true); +} + +void AVampireGameMode::SpawnEnemy() +{ + FVector TopLeft, TopLeftDir; + FVector TopRight, TopRightDir; + FVector BottomLeft, BottomLeftDir; + FVector BottomRight, BottomRightDir; + FVector CenterPos, CenterDir; + + FVector2d ViewportSize; + GEngine->GameViewport->GetViewportSize(ViewportSize); + + PlayerController->DeprojectScreenPositionToWorld(0, 0, TopLeft, TopLeftDir); + PlayerController->DeprojectScreenPositionToWorld(ViewportSize.X, 0, TopRight, TopRightDir); + PlayerController->DeprojectScreenPositionToWorld(0, ViewportSize.Y, BottomLeft, BottomLeftDir); + PlayerController->DeprojectScreenPositionToWorld(ViewportSize.X, ViewportSize.Y, BottomRight, BottomRightDir); + PlayerController->DeprojectScreenPositionToWorld(ViewportSize.X / 2, ViewportSize.Y / 2, CenterPos, CenterDir); + + FVector SpawnLocation; + switch (FMath::RandRange(0, 3)) + { + case 0: + SpawnLocation = FMath::Lerp(TopLeft, TopRight, FMath::RandRange(0.0f, 1.0f)); + break; + case 1: + SpawnLocation = FMath::Lerp(TopRight, BottomRight, FMath::RandRange(0.0f, 1.0f)); + break; + case 2: + SpawnLocation = FMath::Lerp(BottomRight, BottomLeft, FMath::RandRange(0.0f, 1.0f)); + break; + case 3: + SpawnLocation = FMath::Lerp(BottomLeft, TopLeft, FMath::RandRange(0.0f, 1.0f)); + break; + default: + UE_LOG(LogTemp, Warning, TEXT("Something broke")); + break; + } + + SpawnLocation.Z = PlayerCharacter->GetActorLocation().Z; + FTransform Transform; + Transform.SetLocation(SpawnLocation); + + FActorSpawnParameters SpawnParameters; + SpawnParameters.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn; + + AEnemyCharacter* actor = GetWorld()->SpawnActor(EnemyTemplate, Transform, SpawnParameters); + actor->SpawnDefaultController(); +} diff --git a/Source/vampires/VampireGameMode.h b/Source/vampires/VampireGameMode.h index b869eb9..c2313f1 100644 --- a/Source/vampires/VampireGameMode.h +++ b/Source/vampires/VampireGameMode.h @@ -3,6 +3,9 @@ #pragma once #include "CoreMinimal.h" +#include "EnemyCharacter.h" +#include "PlayerCharacter.h" +#include "VampirePlayerController.h" #include "GameFramework/GameMode.h" #include "VampireGameMode.generated.h" @@ -13,5 +16,21 @@ UCLASS() class VAMPIRES_API AVampireGameMode : public AGameMode { GENERATED_BODY() - + +public: + UPROPERTY(EditDefaultsOnly) + TSubclassOf EnemyTemplate; + +private: + APlayerCharacter* PlayerCharacter; + + AVampirePlayerController* PlayerController; + + FTimerHandle SpawnEnemyTimerDelegate; + +protected: + virtual void BeginPlay() override; + + UFUNCTION() + void SpawnEnemy(); };