From 0fed8de306ec0633a62a8e3f276061f9a3b7b596 Mon Sep 17 00:00:00 2001 From: aicorr Date: Thu, 17 Apr 2025 01:08:07 +0530 Subject: [PATCH] Implement pause menu --- .../Blueprints/BP_SpaceShooterGameMode.uasset | 4 +- .../BP_SpaceshipPlayerController.uasset | 3 + Content/Input/IA_Pause.uasset | 4 +- MyProject3.uproject | 5 +- Source/MyProject3/SpaceShooterHUD.cpp | 56 ++++++++++++++++- Source/MyProject3/SpaceShooterHUD.h | 5 ++ .../MyProject3/SpaceshipPlayerController.cpp | 61 +++++++++++++++++++ Source/MyProject3/SpaceshipPlayerController.h | 33 ++++++++++ 8 files changed, 164 insertions(+), 7 deletions(-) create mode 100644 Content/Blueprints/BP_SpaceshipPlayerController.uasset create mode 100644 Source/MyProject3/SpaceshipPlayerController.cpp create mode 100644 Source/MyProject3/SpaceshipPlayerController.h diff --git a/Content/Blueprints/BP_SpaceShooterGameMode.uasset b/Content/Blueprints/BP_SpaceShooterGameMode.uasset index 6d16140..d9af9a8 100644 --- a/Content/Blueprints/BP_SpaceShooterGameMode.uasset +++ b/Content/Blueprints/BP_SpaceShooterGameMode.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:e2a6d951c7d7409fdd49634062524570b4b4e3b2428740a65e5b9348bd0f995c -size 20403 +oid sha256:04dcb148f67d5823d7168afb82ffb4caca7a75c742043e78f6ac16d86e2114a8 +size 20635 diff --git a/Content/Blueprints/BP_SpaceshipPlayerController.uasset b/Content/Blueprints/BP_SpaceshipPlayerController.uasset new file mode 100644 index 0000000..4e5beb5 --- /dev/null +++ b/Content/Blueprints/BP_SpaceshipPlayerController.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7d45b6ef2fa3576bb42df3fbce9af12a8d78ee827007ef07d6ead7e6eebed91f +size 19853 diff --git a/Content/Input/IA_Pause.uasset b/Content/Input/IA_Pause.uasset index 9385a86..9d2a38d 100644 --- a/Content/Input/IA_Pause.uasset +++ b/Content/Input/IA_Pause.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:eba7de52786a7093098b6d02689acdc20467d52c3ac32e1aebd5ccc3487a522d -size 1893 +oid sha256:201c9a076a6afb27906a6ef670ad3e84688027c7c2a22354f45be4cdd08ad55f +size 1964 diff --git a/MyProject3.uproject b/MyProject3.uproject index 9ee4b2e..f1f3e62 100644 --- a/MyProject3.uproject +++ b/MyProject3.uproject @@ -7,7 +7,10 @@ { "Name": "MyProject3", "Type": "Runtime", - "LoadingPhase": "Default" + "LoadingPhase": "Default", + "AdditionalDependencies": [ + "Engine" + ] } ], "Plugins": [ diff --git a/Source/MyProject3/SpaceShooterHUD.cpp b/Source/MyProject3/SpaceShooterHUD.cpp index 5f0c83c..3404585 100644 --- a/Source/MyProject3/SpaceShooterHUD.cpp +++ b/Source/MyProject3/SpaceShooterHUD.cpp @@ -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; diff --git a/Source/MyProject3/SpaceShooterHUD.h b/Source/MyProject3/SpaceShooterHUD.h index 34f1fa7..5bf97b6 100644 --- a/Source/MyProject3/SpaceShooterHUD.h +++ b/Source/MyProject3/SpaceShooterHUD.h @@ -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); diff --git a/Source/MyProject3/SpaceshipPlayerController.cpp b/Source/MyProject3/SpaceshipPlayerController.cpp new file mode 100644 index 0000000..7c3715d --- /dev/null +++ b/Source/MyProject3/SpaceshipPlayerController.cpp @@ -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(GetLocalPlayer())) + { + if (DefaultMappingContext) + { + Subsystem->AddMappingContext(DefaultMappingContext, 0); + } + } +} + +void ASpaceshipPlayerController::SetupInputComponent() +{ + Super::SetupInputComponent(); + + if (UEnhancedInputComponent* EnhancedInputComponent = CastChecked(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(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 +} \ No newline at end of file diff --git a/Source/MyProject3/SpaceshipPlayerController.h b/Source/MyProject3/SpaceshipPlayerController.h new file mode 100644 index 0000000..91cd0d9 --- /dev/null +++ b/Source/MyProject3/SpaceshipPlayerController.h @@ -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(); +}; \ No newline at end of file