Implement pause menu
This commit is contained in:
@@ -84,11 +84,18 @@ void ASpaceShooterHUD::DrawHUD()
|
||||
ASpaceShooterGameMode* GameMode = GetGameMode();
|
||||
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();
|
||||
}
|
||||
else
|
||||
if (GameMode->IsGameOver())
|
||||
{
|
||||
DrawGameOverScreen();
|
||||
}
|
||||
@@ -113,6 +120,51 @@ void ASpaceShooterHUD::DrawGameplayHUD()
|
||||
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()
|
||||
{
|
||||
if (!Canvas) return;
|
||||
|
||||
@@ -58,6 +58,9 @@ protected:
|
||||
UPROPERTY(EditAnywhere, Category = "HUD|GameOver")
|
||||
float PulsateMaxAlpha = 1.0f;
|
||||
|
||||
UPROPERTY(EditAnywhere, Category = "HUD|Pause")
|
||||
float PauseTextScale = 2.0f;
|
||||
|
||||
private:
|
||||
void DrawGameplayHUD();
|
||||
void DrawGameOverScreen();
|
||||
@@ -65,6 +68,8 @@ private:
|
||||
class ASpaceShooterGameMode* GetGameMode() const;
|
||||
class ASpaceshipPawn* GetPlayerPawn() const;
|
||||
|
||||
void DrawPauseScreen();
|
||||
|
||||
float GameOverStartTime;
|
||||
void DrawGameOverBackground();
|
||||
void DrawGameOverText(const FString& Text, const FVector2D& Position, const FLinearColor& Color, float Scale = 1.0f);
|
||||
|
||||
61
Source/MyProject3/SpaceshipPlayerController.cpp
Normal file
61
Source/MyProject3/SpaceshipPlayerController.cpp
Normal 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
|
||||
}
|
||||
33
Source/MyProject3/SpaceshipPlayerController.h
Normal file
33
Source/MyProject3/SpaceshipPlayerController.h
Normal 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();
|
||||
};
|
||||
Reference in New Issue
Block a user