-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
6 changed files
with
121 additions
and
3 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |