Skip to content

Commit

Permalink
Merge pull request #91 from MyEpitech/pauseMenu/victor
Browse files Browse the repository at this point in the history
feat(pause): added pause menu, need to set UIand to add callbacks to …
  • Loading branch information
victorpalle authored Jun 10, 2022
2 parents 3703497 + 49c37f6 commit a328d01
Show file tree
Hide file tree
Showing 10 changed files with 286 additions and 12 deletions.
104 changes: 104 additions & 0 deletions project/Conf/Scenes/PauseScene/button.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
{
"1-first": {
"type": "button",
"nbFrame": 3,
"position": [848, 300, 0],
"texture": "Ressources/buttons/little_button.png",
"audio": "Ressources/buttons/click_sound.ogg",
"text": {
"font": "Ressources/fonts/squarefont/Square.ttf",
"text": "Resume",
"color": [255, 255, 255, 255],
"fontSize": 40,
"position": [220, 40, 0]
},
"image": {
"texture": "Ressources/icon/vs_ico.png",
"scale": 1,
"position": [15, 10, 0]
}
},
"2-second": {
"type": "button",
"nbFrame": 3,
"position": [848, 500, 0],
"texture": "Ressources/buttons/little_button.png",
"audio": "Ressources/buttons/click_sound.ogg",
"text": {
"font": "Ressources/fonts/squarefont/Square.ttf",
"text": "Settings",
"color": [255, 255, 255, 255],
"fontSize": 40,
"position": [220, 40, 0]
},
"image": {
"texture": "Ressources/icon/settings_ico.png",
"scale": 1,
"position": [15, 10, 0]
}
},
"3-third": {
"type": "button",
"nbFrame": 3,
"position": [848, 700, 0],
"texture": "Ressources/buttons/little_button.png",
"audio": "Ressources/buttons/click_sound.ogg",
"text": {
"font": "Ressources/fonts/squarefont/Square.ttf",
"text": "Exit",
"color": [255, 255, 255, 255],
"fontSize": 40,
"position": [220, 40, 0]
},
"image": {
"texture": "Ressources/icon/exit_ico.png",
"scale": 1,
"position": [15, 10, 0]
}
},
"4-fourth": {
"type": "button",
"nbFrame": 3,
"position": [848, 300, 0],
"texture": "Ressources/buttons/little_button.png",
"audio": "Ressources/buttons/click_sound.ogg",
"text": {
"font": "Ressources/fonts/squarefont/Square.ttf",
"text": "Oui",
"color": [255, 255, 255, 255],
"fontSize": 40,
"position": [220, 40, 0]
},
"image": {
"texture": "Ressources/icon/exit_ico.png",
"scale": 1,
"position": [15, 10, 0]
}
},
"5-fifth": {
"type": "button",
"nbFrame": 3,
"position": [848, 700, 0],
"texture": "Ressources/buttons/little_button.png",
"audio": "Ressources/buttons/click_sound.ogg",
"text": {
"font": "Ressources/fonts/squarefont/Square.ttf",
"text": "Non",
"color": [255, 255, 255, 255],
"fontSize": 40,
"position": [220, 40, 0]
},
"image": {
"texture": "Ressources/icon/exit_ico.png",
"scale": 1,
"position": [15, 10, 0]
}
},
"6-sixth": {
"type": "button",
"nbFrame": 3,
"position": [480, 700, 0],
"texture": "Ressources/buttons/back_button.png",
"audio": "Ressources/buttons/click_sound.ogg"
}
}
7 changes: 7 additions & 0 deletions project/Conf/Scenes/PauseScene/image.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"0-background": {
"texture": "Ressources/sprites/pop_up_background.png",
"scale": 1,
"position": [472, 259, 0]
}
}
9 changes: 9 additions & 0 deletions project/Conf/Scenes/PauseScene/text.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"title": {
"font": "Ressources/fonts/primetime/PRIMETIME.ttf",
"text": "Voulez-vous sauvegarder ?",
"color": [255, 255, 255, 255],
"fontSize": 40,
"position": [500, 260, 0]
}
}
Binary file added project/Ressources/sprites/pop_up_background.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion project/Source/Core/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void Core::loop()

void Core::getEvent()
{
std::map<Action, bool> actionPressed = _keyboard.getKeysPressed<Action>(_actionPressed);
std::map<Action, bool> actionPressed = _keyboard.getKeysHasBeenPressed<Action>(_actionPressed);
std::map<PlayerAction, bool> playerAction;
std::vector<std::map<PlayerAction, bool>> playerActions;
std::size_t index = 0;
Expand Down
9 changes: 9 additions & 0 deletions project/Source/Inputs/Keyboard/Keyboard.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ class Keyboard {
actionPressed.emplace(action, isBeingPressed(key));
return actionPressed;
};
template<typename ENUM>
std::map<ENUM, bool> getKeysHasBeenPressed(std::map<ENUM, int> map) const
{
std::map<ENUM, bool> actionPressed;
for (auto &[action, key] : map)
actionPressed.emplace(action, hasbeenReleased(key));
return actionPressed;
};

protected:
private:
};
Expand Down
49 changes: 39 additions & 10 deletions project/Source/Scenes/GameScenes/GameScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ Scene::GameScene::GameScene(std::shared_ptr<Settings> settings, std::shared_ptr<
_players.emplace(static_cast<char>(Object::PLAYER_ORDER::PLAYER2), std::make_unique<Object::Player>(_models.at(1), _textures.at(2), _animations.at(0), 1, _playerPositions.at(static_cast<char>(Object::PLAYER_ORDER::PLAYER2)), Object::MAP_OBJECTS::PLAYER));
_players.emplace(static_cast<char>(Object::PLAYER_ORDER::PLAYER3), std::make_unique<Object::Player>(_models.at(2), _textures.at(3), _animations.at(0), 1, _playerPositions.at(static_cast<char>(Object::PLAYER_ORDER::PLAYER3)), Object::MAP_OBJECTS::PLAYER));
_players.emplace(static_cast<char>(Object::PLAYER_ORDER::PLAYER4), std::make_unique<Object::Player>(_models.at(3), _textures.at(4), _animations.at(0), 1, _playerPositions.at(static_cast<char>(Object::PLAYER_ORDER::PLAYER4)), Object::MAP_OBJECTS::PLAYER));
_pauseScene = std::make_unique<Scene::PauseScene>(settings, gameSettings, std::bind(&Scene::GameScene::resumeGame, this));
_defaultAttributes = {{"bombRange", {1, 3}},
{"explosionRange", {1, 6}},
{"speed", {0.4, 0.8}},
Expand Down Expand Up @@ -116,6 +117,11 @@ void Scene::GameScene::loadSceneAssets()
/* BONUSES */
}

void Scene::GameScene::resumeGame()
{
_isPaused = false;
}

void Scene::GameScene::AwardBonus(Object::PLAYER_ORDER playerNb, Object::BONUS_OBJECTS bonus)
{
switch (bonus) {
Expand Down Expand Up @@ -172,26 +178,25 @@ bool Scene::GameScene::isCollidingObject(Position const &direction, Position con

void Scene::GameScene::handleBombs()
{
if (!_bombs.empty()) {
for (std::size_t bombPos = 0; bombPos < _bombs.size(); bombPos++) {
for (std::size_t bombPos = 0; bombPos < _bombs.size(); bombPos++) {
if (_bombs.at(bombPos)->checkIfShouldExplode()) {
_players.at(static_cast<int>(_bombs.at(bombPos)->getPlayer()))->setAlreadyPlacedBombs(false);
exploseBomb(_bombs.at(bombPos)->getPosition(), _bombs.at(bombPos)->getRange());
_bombs.erase(_bombs.begin() + bombPos);
}
}
}
}

Scene::Scenes Scene::GameScene::handleEvent()
void Scene::GameScene::handleButtons()
{
bool moving = false;
int index = 0;

_nextScene = Scene::Scenes::GAME;
for (auto &button : _buttons)
button->checkHover(GetMousePosition());
}

void Scene::GameScene::handlePlayers()
{
bool moving = false;
int index = 0;
printTimer();

_settings->getPlayerActionsPressed().at(index);
Expand All @@ -212,11 +217,31 @@ Scene::Scenes Scene::GameScene::handleEvent()
player->animation(1);
index++;
}
handleWin();
handleBombs();
}

Scene::Scenes Scene::GameScene::handleEvent()
{
_nextScene = Scene::Scenes::GAME;
if (!_isPaused) {
handleWin();
handlePlayers();
handleBombs();
} else {
_pauseScene->handleEvent();
}
handlePause();
handleButtons();
return _nextScene;
}

void Scene::GameScene::handlePause()
{
std::map<Action, bool> tmp = _settings->getActionPressed();
if (tmp[Action::Previous] == true) {
_isPaused = !_isPaused;
}
}

void Scene::GameScene::placeBomb(Position pos, float lifetime, std::size_t range, Object::PLAYER_ORDER playerNb)
{
bool blockTooked = false;
Expand Down Expand Up @@ -359,6 +384,10 @@ void Scene::GameScene::draw()
bomb->draw();

_settings->getCamera()->endMode3D();

if (_isPaused)
_pauseScene->draw();

for (auto &image : _images)
image->draw();
for (auto &text : _texts)
Expand Down
13 changes: 12 additions & 1 deletion project/Source/Scenes/GameScenes/GameScene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "Bomb.hpp"
#include "IRenderding.hpp"
#include "Rendering/Texture.hpp"
#include "PauseScene.hpp"
#include "tools.hpp"
#include "Settings.hpp"

Expand All @@ -47,7 +48,7 @@ namespace Scene {
void settingsScene();
void newGameScene();
void mainMenuScene();
void draw();
void draw() override;

int getMovingKeys();

Expand All @@ -57,6 +58,12 @@ namespace Scene {
void placeBomb(Position pos, float lifetime, std::size_t range, Object::PLAYER_ORDER playerNb);
void exploseBomb(Position const &position, int radius);

void handlePlayers();

void handleButtons();

void handlePause();

void placeBonus(std::pair<int, int> position, std::size_t percentageDrop);
void AwardBonus(Object::PLAYER_ORDER playerNb, Object::BONUS_OBJECTS bonus);

Expand All @@ -68,8 +75,11 @@ namespace Scene {

void handleWin();

void resumeGame();

void save();


protected:
private:
std::shared_ptr<GameSettings> _gameSettings;
Expand Down Expand Up @@ -103,6 +113,7 @@ namespace Scene {
std::map<PlayerAction, Position> _collisionCondition;
const std::map<PlayerAction, std::pair<Position, Position>> _actionMap;

std::unique_ptr<Scene::PauseScene> _pauseScene;
bool _isPaused;
};
}
Expand Down
68 changes: 68 additions & 0 deletions project/Source/Scenes/GameScenes/PauseScene.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
** EPITECH PROJECT, 2022
** project
** File description:
** PauseScene
*/

#include "PauseScene.hpp"

Scene::PauseScene::PauseScene(std::shared_ptr<Settings> settings, std::shared_ptr<GameSettings> gameSettings, std::function<void(void)> callBack) : AScene(settings)
{
std::vector<std::function<void(void)>> callBacks = {callBack, callBack, std::bind(&Scene::PauseScene::printExitPopUp, this), std::bind(&Scene::PauseScene::printExitPopUp, this), callBack, std::bind(&Scene::PauseScene::unPrintExitPopUp, this)};

_buttons = loadObjects<Object::Button>("Conf/Scenes/PauseScene/button.json");
_images = loadObjects<Object::Image>("Conf/Scenes/PauseScene/image.json");
_texts = loadObjects<Object::Text>("Conf/Scenes/PauseScene/text.json");

for (std::size_t index = 0; index !=_buttons.size(); index++) {
_buttons.at(index)->setCallBack(callBacks.at(index));
}
}

Scene::PauseScene::~PauseScene()
{
}

Scene::Scenes Scene::PauseScene::handleEvent()
{
if (!_shouldPrintExitPopUp) {
for (int index = 0; index < 3; index++)
_buttons.at(index)->checkHover(GetMousePosition());
} else {
for (int cpt = 3; cpt < 6; cpt++)
_buttons.at(cpt)->checkHover(GetMousePosition());
}
_buttons.at(5)->checkHover(GetMousePosition());
return _nextScene;
}

void Scene::PauseScene::printExitPopUp()
{
_shouldPrintExitPopUp = true;
}

void Scene::PauseScene::unPrintExitPopUp()
{
_shouldPrintExitPopUp = false;
}

void Scene::PauseScene::exitGame()
{
std::cout << "exit game" << std::endl;
}

void Scene::PauseScene::draw()
{
for (auto &image : _images)
image->draw();
if (!_shouldPrintExitPopUp) {
for (int index = 0; index < 3; index++)
_buttons.at(index)->draw();
} else {
for (int cpt = 3; cpt < 6; cpt++)
_buttons.at(cpt)->draw();
for (auto &text : _texts)
text->draw();
}
}
Loading

0 comments on commit a328d01

Please sign in to comment.