Create shooting game mode
This commit is contained in:
71
Source/MyProject3/SpaceShooterGameMode.cpp
Normal file
71
Source/MyProject3/SpaceShooterGameMode.cpp
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
#include "SpaceShooterGameMode.h"
|
||||||
|
#include "SpaceshipPawn.h"
|
||||||
|
#include "EnemySpaceship.h"
|
||||||
|
#include "Kismet/GameplayStatics.h"
|
||||||
|
|
||||||
|
ASpaceShooterGameMode::ASpaceShooterGameMode()
|
||||||
|
{
|
||||||
|
PrimaryActorTick.bCanEverTick = true;
|
||||||
|
|
||||||
|
// Set default pawn class to our spaceship
|
||||||
|
DefaultPawnClass = ASpaceshipPawn::StaticClass();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ASpaceShooterGameMode::StartPlay()
|
||||||
|
{
|
||||||
|
Super::StartPlay();
|
||||||
|
|
||||||
|
// Start spawning enemies
|
||||||
|
GetWorldTimerManager().SetTimer(EnemySpawnTimer, this, &ASpaceShooterGameMode::SpawnEnemy,
|
||||||
|
EnemySpawnInterval, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ASpaceShooterGameMode::Tick(float DeltaTime)
|
||||||
|
{
|
||||||
|
Super::Tick(DeltaTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ASpaceShooterGameMode::SpawnEnemy()
|
||||||
|
{
|
||||||
|
// Count current enemies
|
||||||
|
TArray<AActor*> FoundEnemies;
|
||||||
|
UGameplayStatics::GetAllActorsOfClass(GetWorld(), AEnemySpaceship::StaticClass(), FoundEnemies);
|
||||||
|
|
||||||
|
// Only spawn if we haven't reached the maximum
|
||||||
|
if (FoundEnemies.Num() < MaxEnemies)
|
||||||
|
{
|
||||||
|
UWorld* World = GetWorld();
|
||||||
|
if (World)
|
||||||
|
{
|
||||||
|
FVector SpawnLocation = GetRandomSpawnLocation();
|
||||||
|
FRotator SpawnRotation = FRotator::ZeroRotator;
|
||||||
|
FActorSpawnParameters SpawnParams;
|
||||||
|
SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButAlwaysSpawn;
|
||||||
|
|
||||||
|
World->SpawnActor<AEnemySpaceship>(AEnemySpaceship::StaticClass(), SpawnLocation,
|
||||||
|
SpawnRotation, SpawnParams);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
FVector ASpaceShooterGameMode::GetRandomSpawnLocation()
|
||||||
|
{
|
||||||
|
APlayerController* PlayerController = UGameplayStatics::GetPlayerController(GetWorld(), 0);
|
||||||
|
if (PlayerController && PlayerController->GetPawn())
|
||||||
|
{
|
||||||
|
FVector PlayerLocation = PlayerController->GetPawn()->GetActorLocation();
|
||||||
|
|
||||||
|
// Generate random angle
|
||||||
|
float Angle = FMath::RandRange(0.0f, 2.0f * PI);
|
||||||
|
|
||||||
|
// Calculate spawn position in a circle around the player
|
||||||
|
FVector SpawnLocation;
|
||||||
|
SpawnLocation.X = PlayerLocation.X + SpawnRadius * FMath::Cos(Angle);
|
||||||
|
SpawnLocation.Y = PlayerLocation.Y + SpawnRadius * FMath::Sin(Angle);
|
||||||
|
SpawnLocation.Z = PlayerLocation.Z;
|
||||||
|
|
||||||
|
return SpawnLocation;
|
||||||
|
}
|
||||||
|
|
||||||
|
return FVector::ZeroVector;
|
||||||
|
}
|
||||||
32
Source/MyProject3/SpaceShooterGameMode.h
Normal file
32
Source/MyProject3/SpaceShooterGameMode.h
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "CoreMinimal.h"
|
||||||
|
#include "GameFramework/GameModeBase.h"
|
||||||
|
#include "SpaceShooterGameMode.generated.h"
|
||||||
|
|
||||||
|
UCLASS()
|
||||||
|
class MYPROJECT3_API ASpaceShooterGameMode : public AGameModeBase
|
||||||
|
{
|
||||||
|
GENERATED_BODY()
|
||||||
|
|
||||||
|
public:
|
||||||
|
ASpaceShooterGameMode();
|
||||||
|
|
||||||
|
virtual void StartPlay() override;
|
||||||
|
virtual void Tick(float DeltaTime) override;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spawning")
|
||||||
|
float EnemySpawnInterval = 3.0f;
|
||||||
|
|
||||||
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spawning")
|
||||||
|
float SpawnRadius = 2000.0f;
|
||||||
|
|
||||||
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spawning")
|
||||||
|
int32 MaxEnemies = 5;
|
||||||
|
|
||||||
|
private:
|
||||||
|
FTimerHandle EnemySpawnTimer;
|
||||||
|
void SpawnEnemy();
|
||||||
|
FVector GetRandomSpawnLocation();
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user