Implement pause menu

This commit is contained in:
2025-04-17 01:08:07 +05:30
parent 44aee655de
commit 0fed8de306
8 changed files with 164 additions and 7 deletions

Binary file not shown.

Binary file not shown.

View File

@@ -7,7 +7,10 @@
{ {
"Name": "MyProject3", "Name": "MyProject3",
"Type": "Runtime", "Type": "Runtime",
"LoadingPhase": "Default" "LoadingPhase": "Default",
"AdditionalDependencies": [
"Engine"
]
} }
], ],
"Plugins": [ "Plugins": [

View File

@@ -84,11 +84,18 @@ void ASpaceShooterHUD::DrawHUD()
ASpaceShooterGameMode* GameMode = GetGameMode(); ASpaceShooterGameMode* GameMode = GetGameMode();
if (!GameMode) return; if (!GameMode) return;
if (!GameMode->IsGameOver()) APlayerController* PC = GetWorld()->GetFirstPlayerController();
bool bIsPaused = PC && PC->IsPaused();
if (!GameMode->IsGameOver() && bIsPaused)
{
DrawPauseScreen();
}
if (!GameMode->IsGameOver() && !bIsPaused)
{ {
DrawGameplayHUD(); DrawGameplayHUD();
} }
else if (GameMode->IsGameOver())
{ {
DrawGameOverScreen(); DrawGameOverScreen();
} }
@@ -113,6 +120,51 @@ void ASpaceShooterHUD::DrawGameplayHUD()
DrawStatusBars(); DrawStatusBars();
} }
void ASpaceShooterHUD::DrawPauseScreen()
{
if (!Canvas) return;
// Draw semi-transparent dark overlay (this creates a "blur-like" effect)
FCanvasBoxItem BoxItem(FVector2D(0, 0), FVector2D(Canvas->SizeX, Canvas->SizeY));
BoxItem.SetColor(FLinearColor(0.0f, 0.0f, 0.0f, 0.75f)); // More opaque for better readability
Canvas->DrawItem(BoxItem);
// Draw "GAME PAUSED" text
FString PauseText = TEXT("GAME PAUSED");
float TextWidth, TextHeight;
GetTextSize(PauseText, TextWidth, TextHeight, TextFont, PauseTextScale);
// Center the text
float PosX = (Canvas->SizeX - TextWidth) * 0.5f;
float PosY = (Canvas->SizeY - TextHeight) * 0.5f;
// Draw text shadow for better visibility
DrawText(PauseText,
FLinearColor(0.0f, 0.0f, 0.0f, 1.0f),
PosX + 2.0f,
PosY + 2.0f,
TextFont,
PauseTextScale);
// Draw main text
DrawText(PauseText,
FLinearColor(1.0f, 1.0f, 1.0f, 1.0f),
PosX,
PosY,
TextFont,
PauseTextScale);
// Draw "Press ESC to Resume" text
FString ResumeText = TEXT("Press ESC to Resume");
GetTextSize(ResumeText, TextWidth, TextHeight, TextFont);
DrawText(ResumeText,
FLinearColor(1.0f, 1.0f, 1.0f, 0.8f),
(Canvas->SizeX - TextWidth) * 0.5f,
PosY + TextHeight * 3,
TextFont);
}
void ASpaceShooterHUD::DrawGameOverBackground() void ASpaceShooterHUD::DrawGameOverBackground()
{ {
if (!Canvas) return; if (!Canvas) return;

View File

@@ -58,6 +58,9 @@ protected:
UPROPERTY(EditAnywhere, Category = "HUD|GameOver") UPROPERTY(EditAnywhere, Category = "HUD|GameOver")
float PulsateMaxAlpha = 1.0f; float PulsateMaxAlpha = 1.0f;
UPROPERTY(EditAnywhere, Category = "HUD|Pause")
float PauseTextScale = 2.0f;
private: private:
void DrawGameplayHUD(); void DrawGameplayHUD();
void DrawGameOverScreen(); void DrawGameOverScreen();
@@ -65,6 +68,8 @@ private:
class ASpaceShooterGameMode* GetGameMode() const; class ASpaceShooterGameMode* GetGameMode() const;
class ASpaceshipPawn* GetPlayerPawn() const; class ASpaceshipPawn* GetPlayerPawn() const;
void DrawPauseScreen();
float GameOverStartTime; float GameOverStartTime;
void DrawGameOverBackground(); void DrawGameOverBackground();
void DrawGameOverText(const FString& Text, const FVector2D& Position, const FLinearColor& Color, float Scale = 1.0f); void DrawGameOverText(const FString& Text, const FVector2D& Position, const FLinearColor& Color, float Scale = 1.0f);

View File

@@ -0,0 +1,61 @@
#include "SpaceshipPlayerController.h"
#include "EnhancedInputComponent.h"
#include "EnhancedInputSubsystems.h"
#include "Components/PostProcessComponent.h"
#include "Materials/MaterialInstanceDynamic.h"
#include "SpaceShooterGameMode.h"
ASpaceshipPlayerController::ASpaceshipPlayerController()
{
// Don't automatically show mouse cursor
bShowMouseCursor = false;
}
void ASpaceshipPlayerController::BeginPlay()
{
Super::BeginPlay();
// Add Input Mapping Context
if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(GetLocalPlayer()))
{
if (DefaultMappingContext)
{
Subsystem->AddMappingContext(DefaultMappingContext, 0);
}
}
}
void ASpaceshipPlayerController::SetupInputComponent()
{
Super::SetupInputComponent();
if (UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(InputComponent))
{
// Bind the pause action
if (PauseAction)
{
EnhancedInputComponent->BindAction(PauseAction, ETriggerEvent::Started,
this, &ASpaceshipPlayerController::HandlePauseAction);
}
}
}
void ASpaceshipPlayerController::HandlePauseAction()
{
// Don't allow pausing if game is over
if (ASpaceShooterGameMode* GameMode = Cast<ASpaceShooterGameMode>(GetWorld()->GetAuthGameMode()))
{
if (GameMode->IsGameOver())
{
return;
}
}
bool bIsPaused = IsPaused();
SetPause(!bIsPaused);
// Show cursor only when paused, hide when resuming
bShowMouseCursor = !bIsPaused;
// To fix resume not working, don't change input mode from game only to UI only since in UI mode the key mapping for pause won't work
}

View File

@@ -0,0 +1,33 @@
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/PlayerController.h"
#include "SpaceshipPlayerController.generated.h"
UCLASS()
class MYPROJECT3_API ASpaceshipPlayerController : public APlayerController
{
GENERATED_BODY()
public:
ASpaceshipPlayerController();
protected:
virtual void BeginPlay() override;
virtual void SetupInputComponent() override;
// Input Action for pausing
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Input")
class UInputAction* PauseAction;
// Input Mapping Context
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Input")
class UInputMappingContext* DefaultMappingContext;
private:
// Post process component for blur effect
UPROPERTY()
class UPostProcessComponent* BlurPostProcess;
void HandlePauseAction();
};