Initial HUD implementation and timer + game over

This commit is contained in:
2025-04-16 12:54:22 +05:30
parent c292da805d
commit f732cdef59
8 changed files with 298 additions and 8 deletions

View File

@@ -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);
}