Initial HUD implementation and timer + game over
This commit is contained in:
@@ -41,6 +41,10 @@ ASpaceShooterGameMode::ASpaceShooterGameMode()
|
||||
{
|
||||
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("GameMode Constructor"));
|
||||
}
|
||||
|
||||
KillCount = 0;
|
||||
bIsGameOver = false;
|
||||
RemainingTime = GameDuration;
|
||||
}
|
||||
|
||||
void ASpaceShooterGameMode::StartPlay()
|
||||
@@ -60,6 +64,9 @@ void ASpaceShooterGameMode::StartPlay()
|
||||
{
|
||||
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, TEXT("GameMode StartPlay"));
|
||||
}
|
||||
|
||||
RemainingTime = GameDuration;
|
||||
GetWorldTimerManager().SetTimer(GameTimerHandle, this, &ASpaceShooterGameMode::UpdateGameTimer, 1.0f, true);
|
||||
}
|
||||
|
||||
void ASpaceShooterGameMode::Tick(float DeltaTime)
|
||||
@@ -567,4 +574,48 @@ FVector ASpaceShooterGameMode::EnsureMinimumSpawnDistance(const FVector& Propose
|
||||
// Otherwise, extend the vector to meet the minimum distance
|
||||
Direction.Normalize();
|
||||
return PlayerLocation + Direction * MinimumSpawnDistance;
|
||||
}
|
||||
|
||||
void ASpaceShooterGameMode::UpdateGameTimer()
|
||||
{
|
||||
if (bIsGameOver) return;
|
||||
|
||||
RemainingTime -= 1.0f;
|
||||
|
||||
if (RemainingTime <= 0.0f)
|
||||
{
|
||||
EndGame();
|
||||
}
|
||||
}
|
||||
|
||||
void ASpaceShooterGameMode::EndGame()
|
||||
{
|
||||
bIsGameOver = true;
|
||||
|
||||
// Stop enemy spawning
|
||||
GetWorldTimerManager().ClearTimer(EnemySpawnTimer);
|
||||
|
||||
// Clear existing enemies
|
||||
TArray<AActor*> FoundEnemies;
|
||||
UGameplayStatics::GetAllActorsOfClass(GetWorld(), AEnemySpaceship::StaticClass(), FoundEnemies);
|
||||
for (AActor* Enemy : FoundEnemies)
|
||||
{
|
||||
Enemy->Destroy();
|
||||
}
|
||||
|
||||
// Pause the game
|
||||
UGameplayStatics::SetGamePaused(GetWorld(), true);
|
||||
}
|
||||
|
||||
void ASpaceShooterGameMode::IncrementKillCount()
|
||||
{
|
||||
if (!bIsGameOver)
|
||||
{
|
||||
KillCount++;
|
||||
}
|
||||
}
|
||||
|
||||
void ASpaceShooterGameMode::RestartGame()
|
||||
{
|
||||
UGameplayStatics::OpenLevel(this, FName(*GetWorld()->GetName()), false);
|
||||
}
|
||||
Reference in New Issue
Block a user