stats in-game

This commit is contained in:
2025-03-28 21:00:46 +01:00
parent be071bd606
commit 0f026635f6
11 changed files with 164 additions and 39 deletions

View File

@@ -64,9 +64,6 @@ class AppMenu {
if (playerCursor.has_value() && cursorPos.has_value()) {
text.setOutlineThickness((playerCursor.value().getPosition() == cursorPos.value()) ? (sizeMultiplier / 2) : 0);
}
else {
text.setOutlineThickness(0);
}
text.setOrigin(sf::Vector2f({0, text.getLocalBounds().size.y / 2}));
text.setPosition(sf::Vector2f({sizeMultiplier * xPos, sizeMultiplier * yPos}));
this->renderWindow->draw(text);
@@ -79,9 +76,6 @@ class AppMenu {
if (playerCursor.has_value() && cursorPos.has_value()) {
text.setOutlineThickness((playerCursor.value().getPosition() == cursorPos.value()) ? (sizeMultiplier / 2) : 0);
}
else {
text.setOutlineThickness(0);
}
text.setOrigin({text.getLocalBounds().getCenter().x, text.getLocalBounds().size.y / 2});
text.setPosition(sf::Vector2f({sizeMultiplier * 40.f, sizeMultiplier * yPos}));
this->renderWindow->draw(text);

View File

@@ -6,14 +6,17 @@
#include <memory>
#include <algorithm>
#include <cmath>
#include <string>
#include <SFML/Graphics.hpp>
static const int TIME_BEFORE_STARTING = 60;
GamePlayingAppMenu::GamePlayingAppMenu(std::shared_ptr<MenuStack> menuStack, std::shared_ptr<Settings> settings, std::shared_ptr<sf::RenderWindow> renderWindow) :
AppMenu(menuStack, settings, renderWindow),
game(this->settings->getMenu().startGame(this->settings->getGamemode())) {
this->game.start();
this->startTimer = TIME_BEFORE_STARTING;
this->paused = false;
this->pausePressed = false;
this->retryPressed = false;
@@ -24,14 +27,14 @@ GamePlayingAppMenu::GamePlayingAppMenu(std::shared_ptr<MenuStack> menuStack, std
float boardWidth = this->game.getBoard().getWidth() * this->cellSizeZoom;
float boardHeight = (this->game.getBoard().getBaseHeight() + 10) * this->cellSizeZoom;
this->boardPosition = sf::Rect<float>(sf::Vector2f((this->settings->getWindowSizeMultiplier() * 40) - (boardWidth / 2),
(this->settings->getWindowSizeMultiplier() * 25) - (boardHeight / 2)),
sf::Vector2f(boardWidth, boardHeight));
this->boardPosition = sf::FloatRect(sf::Vector2f((this->settings->getWindowSizeMultiplier() * 40) - (boardWidth / 2),
(this->settings->getWindowSizeMultiplier() * 25) - (boardHeight / 2)),
sf::Vector2f(boardWidth, boardHeight));
for (int i = 0; i < 5; i++) {
this->nextQueuePosition[i] = sf::Rect<float>(sf::Vector2f(this->boardPosition.position.x + boardWidth + (5.f * this->settings->getWindowSizeMultiplier()), (1.f + (10.f * i)) * this->settings->getWindowSizeMultiplier()),
sf::Vector2f(8.f * this->settings->getWindowSizeMultiplier(), 8.f * this->settings->getWindowSizeMultiplier()));
this->nextQueuePosition[i] = sf::FloatRect(sf::Vector2f(this->boardPosition.position.x + boardWidth + (5.f * this->settings->getWindowSizeMultiplier()), (10.f + (10.f * i)) * this->settings->getWindowSizeMultiplier()),
sf::Vector2f(8.f * this->settings->getWindowSizeMultiplier(), 8.f * this->settings->getWindowSizeMultiplier()));
}
this->nextCellSizeZoom = this->nextQueuePosition[0].size.y;
@@ -40,14 +43,21 @@ GamePlayingAppMenu::GamePlayingAppMenu(std::shared_ptr<MenuStack> menuStack, std
this->nextCellSizeZoom = std::min(this->nextCellSizeZoom, nextPieceCellSizeZoom);
}
this->holdBoxPosition = sf::Rect<float>(sf::Vector2f(this->boardPosition.position.x - ((8.f + 5.f) * this->settings->getWindowSizeMultiplier()), (11.f) * this->settings->getWindowSizeMultiplier()),
sf::Vector2f(8.f * this->settings->getWindowSizeMultiplier(), 8.f * this->settings->getWindowSizeMultiplier()));
this->holdBoxPosition = sf::FloatRect(sf::Vector2f(this->boardPosition.position.x - ((8.f + 5.f) * this->settings->getWindowSizeMultiplier()), (10.f) * this->settings->getWindowSizeMultiplier()),
sf::Vector2f(8.f * this->settings->getWindowSizeMultiplier(), 8.f * this->settings->getWindowSizeMultiplier()));
this->holdCellSizeZoom = this->nextCellSizeZoom;
}
void GamePlayingAppMenu::computeFrame() {
this->updateMetaBinds();
if (this->startTimer > 0) {
this->startTimer--;
if (this->startTimer == 0) {
this->game.start();
}
}
if (this->escReleased) {
this->menuStack->pop();
}
@@ -67,7 +77,7 @@ void GamePlayingAppMenu::computeFrame() {
else {
if (this->retryPressed) {
this->game.reset();
this->game.start();
this->startTimer = TIME_BEFORE_STARTING;
}
this->retryPressed = false;
}
@@ -153,32 +163,40 @@ void GamePlayingAppMenu::drawFrame() const {
}
// next queue
int upShift = 0;
for (int i = 0; i < std::min((int) this->game.getNextPieces().size(), 5); i++) {
sf::FloatRect nextBox = this->nextQueuePosition[i];
nextBox.position.y -= upShift;
sf::Vector2f nextCellSize(this->nextCellSizeZoom, this->nextCellSizeZoom);
sf::Color color = this->getColorOfBlock(this->game.getNextPieces().at(i).getBlockType(), 0);
sf::Color boxColor = sf::Color(150, 150, 150);
sf::Color boxColor = sf::Color(180, 180, 180);
int lowestRank = 0;
for (int y = 0; y < this->game.getNextPieces().at(i).getLength(); y++) {
for (int x = 0; x < this->game.getNextPieces().at(i).getLength(); x++) {
sf::RectangleShape cell(nextCellSize);
if (this->game.getNextPieces().at(i).getPositions().contains(Position{x, y})) {
cell.setFillColor(color);
lowestRank = y;
}
else {
cell.setFillColor(boxColor);
}
cell.setPosition(sf::Vector2f(this->nextQueuePosition[i].position.x + (x * this->nextCellSizeZoom),
this->nextQueuePosition[i].position.y + ((this->game.getNextPieces().at(i).getLength() - y - 1) * this->nextCellSizeZoom)));
cell.setPosition(sf::Vector2f(nextBox.position.x + (x * this->nextCellSizeZoom),
nextBox.position.y + ((this->game.getNextPieces().at(i).getLength() - y - 1) * this->nextCellSizeZoom)));
this->renderWindow->draw(cell);
}
}
upShift += nextBox.size.y - (this->game.getNextPieces().at(i).getLength() * this->nextCellSizeZoom);
}
// hold box
if (this->game.getHeldPiece() != nullptr) {
sf::Vector2f holdCellSize(this->holdCellSizeZoom, this->holdCellSizeZoom);
sf::Color color = this->getColorOfBlock(this->game.getHeldPiece()->getBlockType(), 0);
sf::Color boxColor = sf::Color(150, 150, 150);
sf::Color boxColor = sf::Color(180, 180, 180);
for (int y = 0; y < this->game.getHeldPiece()->getLength(); y++) {
for (int x = 0; x < this->game.getHeldPiece()->getLength(); x++) {
@@ -196,6 +214,51 @@ void GamePlayingAppMenu::drawFrame() const {
}
}
// stats
int windowSizeMultiplier = this->settings->getWindowSizeMultiplier();
int fontSize = (this->boardPosition.size.x > (windowSizeMultiplier * 30.f)) ? (windowSizeMultiplier) : (windowSizeMultiplier * 2);
sf::Text text(this->pressStartFont, "", fontSize);
text.setFillColor(sf::Color(0, 0, 0));
int millisecondes = this->game.getFramesPassed() * (1000.f / FRAMES_PER_SECOND);
std::string showedMillisecondes = std::to_string(millisecondes % 1000);
while (showedMillisecondes.size() < 3) {
showedMillisecondes = "0" + showedMillisecondes;
}
std::string showedSecondes = std::to_string((millisecondes / 1000) % 60);
while (showedSecondes.size() < 2) {
showedSecondes = "0" + showedSecondes;
}
std::string showedMinutes = std::to_string((millisecondes / (60 * 1000)));
std::string showedTime = showedMinutes + ":" + showedSecondes + "." + showedMillisecondes;
this->placeText(text, {}, getGamemodeName(this->settings->getGamemode()), 1.f, 3.f, {});
this->placeText(text, {}, getGamemodeGoal(this->settings->getGamemode()), 1.f, 6.f, {});
this->placeText(text, {}, "LINES:" + std::to_string(this->game.getClearedLines()), 1.f, 25.f, {});
this->placeText(text, {}, "LEVEL:" + std::to_string(this->game.getLevel()), 1.f, 30.f, {});
this->placeText(text, {}, "SCORE:" + std::to_string(this->game.getScore()), 1.f, 35.f, {});
this->placeText(text, {}, "GRADE:" + std::to_string(this->game.getGrade()), 1.f, 40.f, {});
this->placeText(text, {}, showedTime, 1.f, 45.f, {});
// game state
text.setOutlineColor(sf::Color(255, 255, 255));
text.setOutlineThickness(windowSizeMultiplier / 2.f);
if (this->game.hasWon()) {
this->placeTitle(text, {}, "WIN", 25.f, {});
}
else if (this->game.hasLost()) {
this->placeTitle(text, {}, "LOSE", 25.f, {});
}
else if (this->paused) {
this->placeTitle(text, {}, "PAUSE", 25.f, {});
}
else if (this->startTimer > 0) {
text.setCharacterSize(windowSizeMultiplier * 4);
this->placeTitle(text, {}, std::to_string(((this->startTimer - 1) / (TIME_BEFORE_STARTING / 4))), 25.f, {});
}
this->renderWindow->display();
}

View File

@@ -10,14 +10,15 @@
class GamePlayingAppMenu : public AppMenu {
private:
Game game;
int startTimer;
bool paused;
bool pausePressed;
bool retryPressed;
sf::Rect<float> boardPosition;
sf::FloatRect boardPosition;
float cellSizeZoom;
sf::Rect<float> holdBoxPosition;
sf::FloatRect holdBoxPosition;
float holdCellSizeZoom;
sf::Rect<float> nextQueuePosition[5];
sf::FloatRect nextQueuePosition[5];
float nextCellSizeZoom;
public:

View File

@@ -33,7 +33,7 @@ InfoAppMenu::InfoAppMenu(std::shared_ptr<MenuStack> menuStack, std::shared_ptr<S
"The rotation center is always the\n"
"center of the piece by default.\n"
"When kicking the piece, it will\n"
"compute and try every position that\n"
"compute and try (most) position that\n"
"touches the original piece,\n"
"prioritizing sides over depth and\n"
"firstly going down before going up.",

View File

@@ -112,6 +112,7 @@ void SettingsKeybindsAppMenu::drawFrame() const {
}
this->placeText(text, this->playerCursor, setStringToUpperCase(ACTION_NAMES[action]), 15.f, ((i - firstElem) * 10) + 25.f, sf::Vector2u{0, (unsigned int) i + 1});
text.setOutlineThickness(0);
this->placeText(text, {}, string, 40.f, ((i - firstElem) * 10) + 25.f, {});
sf::Sprite sprite(this->iconTextures[action]);