Skip to content

Commit

Permalink
Add pause
Browse files Browse the repository at this point in the history
Adds a pause state that can be activated by clicking ESC.
The game state then does not play and the pause text is displayed.
  • Loading branch information
Notiooo committed Jan 13, 2024
1 parent 0cac5b0 commit 1043b7a
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 3 deletions.
Binary file added AimGL/resources/Textures/pause.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions AimGL/src/CMakeLists_Sources.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ set(PROJECT_SOURCES
States/CustomStates/LogoState.cpp
States/CustomStates/ExitGameState.cpp
States/CustomStates/GameState.cpp
States/CustomStates/PauseState.cpp
Player/Player.cpp
World/Camera.cpp
World/InfiniteGridFloor.cpp
Expand Down
2 changes: 2 additions & 0 deletions AimGL/src/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "States/CustomStates/ExitGameState.h"
#include "States/CustomStates/GameState.h"
#include "States/CustomStates/LogoState.h"
#include "States/CustomStates/PauseState.h"
#include "Utils/Mouse.h"
#include "constants.h"
#include "pch.h"
Expand Down Expand Up @@ -70,6 +71,7 @@ Game::Game()
mAppStack.saveState<LogoState>(State_ID::LogoState, *mGameWindow);
mAppStack.saveState<ExitGameState>(State_ID::ExitGameState);
mAppStack.saveState<GameState>(State_ID::GameState, *mGameWindow);
mAppStack.saveState<PauseState>(State_ID::PauseState, *mGameWindow);

// Initial state of the statestack is TitleState
mAppStack.push(State_ID::LogoState);
Expand Down
11 changes: 8 additions & 3 deletions AimGL/src/States/CustomStates/GameState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ bool GameState::update(const float& deltaTime)
bool GameState::handleEvent(const sf::Event& event)
{
MTR_SCOPE("GameState", "GameState::handleEvent");
Mouse::handleFirstPersonBehaviour(event, mWindow);
if (not Mouse::isMouseLocked())
{
return true;
}

mPlayer.handleEvent(event);
mShootingRange.handleEvent(event);
mSidewayMovingTargetsRange.handleEvent(event);
Expand All @@ -72,13 +78,13 @@ bool GameState::handleEvent(const sf::Event& event)
{
switch (event.key.code)
{
case sf::Keyboard::Escape: Mouse::unlockMouse(mWindow); break;
case sf::Keyboard::Escape: requestPush(State_ID::PauseState); break;
case sf::Keyboard::F1: switchWireframe(); break;
case sf::Keyboard::F2: switchDrawingColliders(); break;
case sf::Keyboard::F3: switchDrawingImgui(); break;
case sf::Keyboard::F4: Mouse::unlockMouse(mWindow); break;
}
}
Mouse::handleFirstPersonBehaviour(event, mWindow);
return true;
}

Expand Down Expand Up @@ -153,7 +159,6 @@ bool GameState::updateImGui(const float& deltaTime)
updateImguiDebugMenu();
updateImGuiDebugInstructionText();
}
mInfiniteGridFloor.showDebugImGui();
}
return true;
}
60 changes: 60 additions & 0 deletions AimGL/src/States/CustomStates/PauseState.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include "PauseState.h"
#include "Utils/Mouse.h"
#include "pch.h"

PauseState::PauseState(StateStack& stack, WindowToRender& window)
: State(stack)
, mWindowToRender(window)
, mRenderer(window)
, mPauseTextTexture("resources/Textures/pause.png")
, mPauseText(mPauseTextTexture)
, mRectangle({window.getSize().x, window.getSize().y}, {0.f, 0.f, 0.f, 1.f})
, mWasMouseLocked(Mouse::isMouseLocked())

{
mPauseText.setPosition(
glm::vec2(mWindowToRender.getSize().x / 2.f, mWindowToRender.getSize().y / 2.f),
Sprite2D::Origin::Center);
mPauseText.setHeight(mWindowToRender.getSize().y / 3.f);
mRectangle.setPosition({0, 0});
mRectangle.setOpacity(0.7f);
Mouse::unlockMouse(window);
}

void PauseState::draw(sf::Window& target) const
{
MTR_SCOPE("PauseState", "PauseState::draw");
mRectangle.draw(mRenderer);
mPauseText.draw(mRenderer);
}

bool PauseState::update(const float& deltaTime)
{
MTR_SCOPE("PauseState", "PauseState::update");
return false;
}

bool PauseState::fixedUpdate(const float& deltaTime)
{
MTR_SCOPE("PauseState", "PauseState::fixedUpdate");
return false;
}

bool PauseState::handleEvent(const sf::Event& event)
{
MTR_SCOPE("PauseState", "PauseState::handleEvent");
if (event.type == sf::Event::KeyPressed)
{
switch (event.key.code)
{
case sf::Keyboard::Escape:
if (mWasMouseLocked)
{
Mouse::lockMouseAtCenter(mWindowToRender);
}
requestPop();
break;
}
}
return false;
}
50 changes: 50 additions & 0 deletions AimGL/src/States/CustomStates/PauseState.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#pragma once
#include "States/State.h"

#include "Game.h"
#include "Renderer/Graphics/2D/Rectangle2D.h"
#include "Renderer/Graphics/2D/Sprite2D.h"
#include "Renderer/Graphics/Texture.h"

class StateStack;

/**
* @brief The state in which the player pauses the game
*/
class PauseState : public State
{
public:
PauseState(StateStack& stack, WindowToRender& window);

/**
* \brief Draws only this state to the passed target
* \param target where it should be drawn to
*/
void draw(sf::Window& target) const override;

/**
* \brief Updates the state logic at equal intervals independent of the frame rate.
* \param deltaTime Time interval
*/
bool fixedUpdate(const float& deltaTime) override;

/**
* \brief Updates the game logic dependent, or independent of time, every rendered frame.
* \param deltaTime the time that has passed since the game was last updated.
*/
bool update(const float& deltaTime) override;

/**
* \brief It takes input (event) from the user and interprets it
* \param event user input
*/
bool handleEvent(const sf::Event& event) override;

private:
WindowToRender& mWindowToRender;
Renderer mRenderer;
Texture mPauseTextTexture;
Sprite2D mPauseText;
Rectangle2D mRectangle;
bool mWasMouseLocked;
};

0 comments on commit 1043b7a

Please sign in to comment.