From c08cfc2255d0b9acad75d3266ea7e52cbbd67f79 Mon Sep 17 00:00:00 2001 From: zulianc Date: Mon, 24 Mar 2025 13:28:38 +0100 Subject: [PATCH] ingame moins moche (mais pas fini) --- src/Core/Game.cpp | 16 ++- src/Core/Game.h | 20 +++- .../AppMenus/GamePlayingAppMenu.cpp | 101 +++++++++++++++--- src/GraphicalUI/AppMenus/GamePlayingAppMenu.h | 12 ++- .../AppMenus/GameSettingsAppMenu.h | 6 +- src/GraphicalUI/AppMenus/MainAppMenu.h | 4 +- src/GraphicalUI/main.cpp | 21 +++- src/TextUI/TextApp.cpp | 2 +- 8 files changed, 146 insertions(+), 36 deletions(-) diff --git a/src/Core/Game.cpp b/src/Core/Game.cpp index 4c7993c..3131ee9 100644 --- a/src/Core/Game.cpp +++ b/src/Core/Game.cpp @@ -346,12 +346,16 @@ bool Game::isOnB2BChain() const { return this->B2BChain; } -bool Game::areBlocksBones() const { - return this->parameters.getBoneBlocks(); +float Game::getLockDelayProgression() const { + return (float) this->totalLockDelay / this->parameters.getLockDelay(); } -Position Game::ghostPiecePosition() const { - return this->board.lowestPosition(); +float Game::getForcedLockDelayProgression() const { + return (float) this->totalForcedLockDelay / this->parameters.getForcedLockDelay(); +} + +bool Game::areBlocksBones() const { + return this->parameters.getBoneBlocks(); } const Board& Game::getBoard() const { @@ -366,6 +370,10 @@ const Position& Game::getActivePiecePosition() const { return this->board.getActivePiecePosition(); } +Position Game::getGhostPiecePosition() const { + return this->board.lowestPosition(); +} + const std::shared_ptr& Game::getHeldPiece() const { return this->board.getHeldPiece(); } diff --git a/src/Core/Game.h b/src/Core/Game.h index 9585d42..f07f5d8 100644 --- a/src/Core/Game.h +++ b/src/Core/Game.h @@ -116,16 +116,21 @@ class Game { */ bool isOnB2BChain() const; + /** + * @return How close the active piece's lock delay is to the maximum allowed, betwwen 0 and 1 + */ + float getLockDelayProgression() const; + + /** + * @return How close the active piece's forced lock delay is to the maximum allowed, betwwen 0 and 1 + */ + float getForcedLockDelayProgression() const; + /** * @return If all blocks are currently bone blocks */ bool areBlocksBones() const; - /** - * @return The position of the ghost piece - */ - Position ghostPiecePosition() const; - /** * @return The board */ @@ -141,6 +146,11 @@ class Game { */ const Position& getActivePiecePosition() const; + /** + * @return The position of the ghost piece + */ + Position getGhostPiecePosition() const; + /** * @return A pointer to the held piece, can be null */ diff --git a/src/GraphicalUI/AppMenus/GamePlayingAppMenu.cpp b/src/GraphicalUI/AppMenus/GamePlayingAppMenu.cpp index ff15acb..4d7ed4d 100644 --- a/src/GraphicalUI/AppMenus/GamePlayingAppMenu.cpp +++ b/src/GraphicalUI/AppMenus/GamePlayingAppMenu.cpp @@ -4,16 +4,28 @@ #include #include +#include +#include #include GamePlayingAppMenu::GamePlayingAppMenu(std::shared_ptr menuStack, std::shared_ptr settings, std::shared_ptr renderWindow) : AppMenu(menuStack, settings, renderWindow), - game(this->settings->getMenu().startGame(this->settings->getGamemode())) - { + game(this->settings->getMenu().startGame(this->settings->getGamemode())) { this->game.start(); this->paused = false; + this->pausePressed = false; + this->retryPressed = false; + + int maxWidthMultiplier = 40 / (this->game.getBoard().getWidth()); + int maxHeightMultiplier = 50 / (this->game.getBoard().getBaseHeight() + 10); + this->cellSizeZoom = std::min(maxWidthMultiplier, maxHeightMultiplier); + + float boardWidth = this->game.getBoard().getWidth() * this->cellSizeZoom; + float boardHeight = (this->game.getBoard().getBaseHeight() + 10) * this->cellSizeZoom; + this->boardPosition = sf::Rect(sf::Vector2f(40.f - (boardWidth / 2), 25.f - (boardHeight / 2)) * (float) this->settings->getWindowSizeMultiplier(), + sf::Vector2f(boardWidth, boardHeight)); } void GamePlayingAppMenu::computeFrame() { @@ -33,11 +45,24 @@ void GamePlayingAppMenu::computeFrame() { } if (actions.contains(RETRY)) { - this->game.reset(); - this->game.start(); + this->retryPressed = true; } + else { + if (this->retryPressed) { + this->game.reset(); + this->game.start(); + } + this->retryPressed = false; + } + if (actions.contains(PAUSE)) { - this->paused = (!this->paused); + this->pausePressed = true; + } + else { + if (this->pausePressed) { + this->paused = (!this->paused); + } + this->pausePressed = false; } if (!paused) { @@ -47,24 +72,58 @@ void GamePlayingAppMenu::computeFrame() { } void GamePlayingAppMenu::drawFrame() const { - this->renderWindow->clear(sf::Color::Black); - - int sizeMultiplier = this->settings->getWindowSizeMultiplier(); - sf::Vector2f cellSize((float) sizeMultiplier, (float) sizeMultiplier); + this->renderWindow->clear(sf::Color(200, 200, 200)); + + float cellSizeMultiplier = this->settings->getWindowSizeMultiplier() * this->cellSizeZoom; + sf::Vector2f cellSize(cellSizeMultiplier, cellSizeMultiplier); for (int y = this->game.getBoard().getBaseHeight() + 10; y >= 0; y--) { for (int x = 0; x < this->game.getBoard().getWidth(); x++) { - bool isActivePieceHere = (this->game.getActivePiece() != nullptr) && (this->game.getActivePiece()->getPositions().contains(Position{x, y} - this->game.getActivePiecePosition())); - bool isGhostPieceHere = (this->game.getActivePiece() != nullptr) && (this->game.getActivePiece()->getPositions().contains(Position{x, y} - this->game.ghostPiecePosition())); - Block block = (isActivePieceHere || isGhostPieceHere) ? this->game.getActivePiece()->getBlockType() : this->game.getBoard().getBlock(Position{x, y}); + Block block = this->game.getBoard().getBlock(Position{x, y}); sf::RectangleShape cell(cellSize); - cell.setFillColor(sf::Color(BLOCKS_COLOR[block].red, BLOCKS_COLOR[block].green, BLOCKS_COLOR[block].blue, (isGhostPieceHere && !isActivePieceHere) ? 150 : 255)); - cell.setPosition(sf::Vector2f(x * sizeMultiplier, (this->game.getBoard().getBaseHeight() + 10 - y) * sizeMultiplier)); + cell.setFillColor(this->getColorOfBlock(block, (block == NOTHING) ? 0 : -50)); + cell.setPosition(this->getBlockPosition(x, y)); this->renderWindow->draw(cell); } } + if (this->game.getActivePiece() != nullptr) { + sf::Color ghostColor = this->getColorOfBlock(this->game.getActivePiece()->getBlockType(), 100); + float pieceOutlineSize = std::roundf(cellSizeMultiplier / 4); + sf::Color pieceOultlineColor = sf::Color(255, 255 - (255 * this->game.getForcedLockDelayProgression()), 255 - (255 * this->game.getForcedLockDelayProgression())); + sf::Color pieceColor = this->getColorOfBlock(this->game.getActivePiece()->getBlockType(), -200 * (this->game.getLockDelayProgression())); + + for (const Position& position : this->game.getActivePiece()->getPositions()) { + Position cellPosition = (this->game.getGhostPiecePosition() + position); + + sf::RectangleShape cell(cellSize); + cell.setFillColor(ghostColor); + cell.setPosition(this->getBlockPosition(cellPosition.x, cellPosition.y)); + this->renderWindow->draw(cell); + } + + for (const Position& position : this->game.getActivePiece()->getPositions()) { + Position cellPosition = (this->game.getActivePiecePosition() + position); + + sf::RectangleShape cell(cellSize); + cell.setOutlineThickness(pieceOutlineSize); + cell.setOutlineColor(pieceOultlineColor); + cell.setPosition(this->getBlockPosition(cellPosition.x, cellPosition.y)); + this->renderWindow->draw(cell); + } + + for (const Position& position : this->game.getActivePiece()->getPositions()) { + Position cellPosition = (this->game.getActivePiecePosition() + position); + + sf::RectangleShape cell(cellSize); + cell.setFillColor(pieceColor); + cell.setPosition(this->getBlockPosition(cellPosition.x, cellPosition.y)); + this->renderWindow->draw(cell); + } + } + + /* if (this->game.getNextPieces().size() > 0) { for (int y = 10; y >= 0; y--) { for (int x = 0; x <= 10; x++) { @@ -98,6 +157,20 @@ void GamePlayingAppMenu::drawFrame() const { } } } + */ this->renderWindow->display(); } + +sf::Color GamePlayingAppMenu::getColorOfBlock(Block block, int luminosityShift) const { + Color rgbColor = BLOCKS_COLOR[block]; + return sf::Color(std::clamp(rgbColor.red + luminosityShift, 0, 255), + std::clamp(rgbColor.green + luminosityShift, 0, 255), + std::clamp(rgbColor.blue + luminosityShift, 0, 255)); +} + +sf::Vector2f GamePlayingAppMenu::getBlockPosition(int x, int y) const { + float cellSizeMultiplier = this->settings->getWindowSizeMultiplier() * this->cellSizeZoom; + return sf::Vector2f(this->boardPosition.position.x + (x * cellSizeMultiplier), + this->boardPosition.position.y + ((this->game.getBoard().getBaseHeight() + 10 - y) * cellSizeMultiplier)); +} diff --git a/src/GraphicalUI/AppMenus/GamePlayingAppMenu.h b/src/GraphicalUI/AppMenus/GamePlayingAppMenu.h index e903cc1..3b4d6a7 100644 --- a/src/GraphicalUI/AppMenus/GamePlayingAppMenu.h +++ b/src/GraphicalUI/AppMenus/GamePlayingAppMenu.h @@ -11,11 +11,19 @@ class GamePlayingAppMenu : public AppMenu { private: Game game; bool paused; + bool pausePressed; + bool retryPressed; + float cellSizeZoom; + sf::Rect boardPosition; public: GamePlayingAppMenu(std::shared_ptr menuStack, std::shared_ptr settings, std::shared_ptr renderWindow); - void computeFrame(); + void computeFrame() override; - void drawFrame() const; + void drawFrame() const override; + + sf::Color getColorOfBlock(Block block, int luminosityShift) const; + + sf::Vector2f getBlockPosition(int x, int y) const; }; diff --git a/src/GraphicalUI/AppMenus/GameSettingsAppMenu.h b/src/GraphicalUI/AppMenus/GameSettingsAppMenu.h index 1682809..f67002b 100644 --- a/src/GraphicalUI/AppMenus/GameSettingsAppMenu.h +++ b/src/GraphicalUI/AppMenus/GameSettingsAppMenu.h @@ -15,9 +15,9 @@ class GameSettingsAppMenu : public AppMenu { public: GameSettingsAppMenu(std::shared_ptr menuStack, std::shared_ptr settings, std::shared_ptr renderWindow); - void computeFrame(); + void computeFrame() override; - void drawFrame() const; + void drawFrame() const override; - void placeText(sf::Text& text, const sf::String& string, float xPos, float yPos, unsigned int xCursorOutline, unsigned int yCursorOutline) const ; + void placeText(sf::Text& text, const sf::String& string, float xPos, float yPos, unsigned int xCursorOutline, unsigned int yCursorOutline) const; }; diff --git a/src/GraphicalUI/AppMenus/MainAppMenu.h b/src/GraphicalUI/AppMenus/MainAppMenu.h index 12407e1..6d2f925 100644 --- a/src/GraphicalUI/AppMenus/MainAppMenu.h +++ b/src/GraphicalUI/AppMenus/MainAppMenu.h @@ -15,7 +15,7 @@ class MainAppMenu : public AppMenu { public: MainAppMenu(std::shared_ptr menuStack, std::shared_ptr settings, std::shared_ptr renderWindow); - void computeFrame(); + void computeFrame() override; - void drawFrame() const; + void drawFrame() const override; }; diff --git a/src/GraphicalUI/main.cpp b/src/GraphicalUI/main.cpp index 0a1945c..0d7def8 100644 --- a/src/GraphicalUI/main.cpp +++ b/src/GraphicalUI/main.cpp @@ -4,6 +4,19 @@ #include +#ifdef __JMINOS_RELEASE__ + +int main() { + std::srand(std::time(NULL)); + + GraphApp UI; + UI.run(); + + return 0; +} + +#else + void resetConfigFiles(); void resetSettingsFile(); void resetKeybindFile(int layout); @@ -12,8 +25,6 @@ void resetKeybindFile(int layout); int main() { std::srand(std::time(NULL)); -#ifndef __JMINOS_RELEASE__ - PiecesFiles pf; for (int i = 1; i <= MAXIMUM_PIECES_SIZE; i++) { if (!std::filesystem::exists("data/pieces/" + std::to_string(i) + "minos.bin")) { @@ -30,11 +41,9 @@ int main() { } } - // uncomment before compiling release version + // do this before compiling release version //resetConfigFiles(); -#endif - GraphApp UI; UI.run(); @@ -154,3 +163,5 @@ void resetKeybindFile(int layout) { layoutFile.write(&byte, 1); } } + +#endif diff --git a/src/TextUI/TextApp.cpp b/src/TextUI/TextApp.cpp index 542a887..f7b8796 100644 --- a/src/TextUI/TextApp.cpp +++ b/src/TextUI/TextApp.cpp @@ -251,7 +251,7 @@ void TextApp::printGame(const Game& game) const { for (int x = 0; x < game.getBoard().getWidth(); x++) { /* BOARD PRINTING */ bool isActivePieceHere = (game.getActivePiece() != nullptr) && (game.getActivePiece()->getPositions().contains(Position{x, y} - game.getActivePiecePosition())); - bool isGhostPieceHere = (game.getActivePiece() != nullptr) && (game.getActivePiece()->getPositions().contains(Position{x, y} - game.ghostPiecePosition())); + bool isGhostPieceHere = (game.getActivePiece() != nullptr) && (game.getActivePiece()->getPositions().contains(Position{x, y} - game.getGhostPiecePosition())); Block block = (isActivePieceHere || isGhostPieceHere) ? game.getActivePiece()->getBlockType() : game.getBoard().getBlock(Position{x, y}); if (isActivePieceHere || isGhostPieceHere) {