fixed holding spwaning the piece 1 row lower

This commit is contained in:
2025-04-01 18:45:01 +02:00
parent 46ebb88ef2
commit a62b3c018d
5 changed files with 39 additions and 32 deletions

View File

@@ -67,27 +67,29 @@ void Game::nextFrame(const std::set<Action>& playerActions) {
if (AREJustEnded) { if (AREJustEnded) {
this->lost = this->board.spawnNextPiece(); this->lost = this->board.spawnNextPiece();
this->resetPiece(true); this->resetPiece(true);
}
/* IRS and IHS */ /* IRS and IHS */
bool initialRotated = (this->initialActions.contains(ROTATE_0) || this->initialActions.contains(ROTATE_CW)
|| this->initialActions.contains(ROTATE_180) || this->initialActions.contains(ROTATE_CCW));
Rotation initialRotation = NONE Rotation initialRotation = NONE
+ ((this->initialActions.contains(ROTATE_CW)) ? CLOCKWISE : NONE) + ((this->initialActions.contains(ROTATE_CW)) ? CLOCKWISE : NONE)
+ ((this->initialActions.contains(ROTATE_180)) ? DOUBLE : NONE) + ((this->initialActions.contains(ROTATE_180)) ? DOUBLE : NONE)
+ ((this->initialActions.contains(ROTATE_CCW)) ? COUNTERCLOCKWISE : NONE); + ((this->initialActions.contains(ROTATE_CCW)) ? COUNTERCLOCKWISE : NONE);
if (this->initialActions.contains(HOLD)) { if (this->initialActions.contains(HOLD)) {
this->lost = (!this->board.hold(initialRotation)); this->board.hold(initialRotation);
} }
else { else {
if ((initialRotation != NONE) || this->initialActions.contains(ROTATE_0)) { if (initialRotated) {
this->lost = (!this->board.rotate(initialRotation)); this->board.rotate(initialRotation);
} }
} }
this->lost = this->board.activePieceInWall();
if (this->lost) { if (this->lost) {
if (initialRotation == NONE) {
this->board.rotate(NONE); this->board.rotate(NONE);
this->lost = this->board.activePieceInWall(); this->lost = this->board.activePieceInWall();
if (this->lost) { if (this->lost) {
this->framesPassed++; this->framesPassed++;
return; return;
@@ -97,7 +99,7 @@ void Game::nextFrame(const std::set<Action>& playerActions) {
/* HOLD */ /* HOLD */
if (playerActions.contains(HOLD) && (!this->heldActions.contains(HOLD))) { if (playerActions.contains(HOLD) && (!this->heldActions.contains(HOLD))) {
if (this->board.hold()) { if (this->board.hold({})) {
this->resetPiece(false); this->resetPiece(false);
} }
} }

View File

@@ -11,6 +11,7 @@
#include <utility> #include <utility>
#include <cstdlib> #include <cstdlib>
#include <iostream> #include <iostream>
#include <optional>
GameBoard::GameBoard(int boardWidth, int boardHeight, const std::shared_ptr<PiecesList>& piecesList, int nextQueueLength) : GameBoard::GameBoard(int boardWidth, int boardHeight, const std::shared_ptr<PiecesList>& piecesList, int nextQueueLength) :
@@ -194,12 +195,12 @@ bool GameBoard::activePieceOverlaps(const std::set<Position>& safePositions, con
return false; return false;
} }
bool GameBoard::hold(Rotation initialRotation) { bool GameBoard::hold(std::optional<Rotation> initialRotation) {
Position storedPosition = this->activePiecePosition;
std::swap(this->activePiece, this->heldPiece); std::swap(this->activePiece, this->heldPiece);
bool isFirstTimeHolding = (this->activePiece == nullptr); bool isFirstTimeHolding = (this->activePiece == nullptr);
if (isFirstTimeHolding) { if (isFirstTimeHolding) {
// try with the next piece in queue since there is no piece in the hold box yet
if (this->nextQueueLength == 0) { if (this->nextQueueLength == 0) {
this->activePiece = std::make_shared<Piece>(this->generator.lookNext()); this->activePiece = std::make_shared<Piece>(this->generator.lookNext());
} }
@@ -208,21 +209,25 @@ bool GameBoard::hold(Rotation initialRotation) {
} }
} }
Piece stored = *this->activePiece; // try initial rotation
Position storedPosition = this->activePiecePosition;
this->goToSpawnPosition(); this->goToSpawnPosition();
this->rotate(initialRotation); if (initialRotation.has_value()) {
std::shared_ptr<Piece> storedPiece(this->activePiece);
this->rotate(initialRotation.value());
// if the piece can't spawn, abort initial rotation
if (this->activePieceInWall()) { if (this->activePieceInWall()) {
this->activePiece = std::make_shared<Piece>(stored); this->activePiece = storedPiece;
this->goToSpawnPosition(); }
}
// if the piece can't spawn, try 0° rotation
if (this->activePieceInWall()) {
std::shared_ptr<Piece> storedPiece(this->activePiece);
this->rotate(NONE);
// if the piece still can't spawn, abort holding // if the piece still can't spawn, abort holding
if (this->activePieceInWall()) { if (this->activePieceInWall()) {
if (isFirstTimeHolding) { this->activePiece = (isFirstTimeHolding) ? nullptr : storedPiece;
this->activePiece = nullptr;
}
std::swap(this->activePiece, this->heldPiece); std::swap(this->activePiece, this->heldPiece);
this->activePiecePosition = storedPosition; this->activePiecePosition = storedPosition;
return false; return false;
@@ -230,7 +235,6 @@ bool GameBoard::hold(Rotation initialRotation) {
} }
if (isFirstTimeHolding) { if (isFirstTimeHolding) {
// confirm we keep the piece we tried with
this->nextQueue.push_back(this->generator.getNext()); this->nextQueue.push_back(this->generator.getNext());
this->nextQueue.erase(this->nextQueue.begin()); this->nextQueue.erase(this->nextQueue.begin());
} }

View File

@@ -7,6 +7,7 @@
#include <vector> #include <vector>
#include <memory> #include <memory>
#include <optional>
/** /**
@@ -90,7 +91,7 @@ class GameBoard {
* Tries holding the active piece or swapping it if one was already stocked, while trying to apply an initial rotation to the newly spawned piece * Tries holding the active piece or swapping it if one was already stocked, while trying to apply an initial rotation to the newly spawned piece
* @return If it suceeded * @return If it suceeded
*/ */
bool hold(Rotation initialRotation = NONE); bool hold(std::optional<Rotation> initialRotation);
/** /**
* Spawns the next piece from the queue * Spawns the next piece from the queue

View File

@@ -39,7 +39,7 @@ inline std::string getGamemodeGoal(Gamemode gamemode) {
"200 lines", "200 lines",
"2 minutes", "2 minutes",
"200 lines", "200 lines",
"Chill" "Infinite"
}; };
return GAMEMODE_DESCRIPTIONS[gamemode]; return GAMEMODE_DESCRIPTIONS[gamemode];

View File

@@ -138,7 +138,7 @@ void GamePiecesAppMenu::drawSelectedPiecesRow(float yPos) const {
xProgress += (1.f + 8.f); xProgress += (1.f + 8.f);
} }
else { else {
this->placeText(text, {}, "ERROR_UNSUPPORTED", xProgress, yPos, {}); this->placeText(text, {}, "UNLOADED_" + std::to_string(pieceSize), xProgress, yPos, {});
xProgress += (1.f + (text.getGlobalBounds().size.x / this->settings->getWindowSizeMultiplier())); xProgress += (1.f + (text.getGlobalBounds().size.x / this->settings->getWindowSizeMultiplier()));
} }
} }
@@ -148,7 +148,7 @@ void GamePiecesAppMenu::drawSelectedPiecesRow(float yPos) const {
xProgress += (1.f + (text.getGlobalBounds().size.x / this->settings->getWindowSizeMultiplier())); xProgress += (1.f + (text.getGlobalBounds().size.x / this->settings->getWindowSizeMultiplier()));
} }
else { else {
this->placeText(text, {}, "ERROR_UNSUPPORTED", xProgress, yPos, {}); this->placeText(text, {}, "UNLOADED_" + std::to_string(pieceSize), xProgress, yPos, {});
xProgress += (1.f + (text.getGlobalBounds().size.x / this->settings->getWindowSizeMultiplier())); xProgress += (1.f + (text.getGlobalBounds().size.x / this->settings->getWindowSizeMultiplier()));
} }
} }