Skip to content

Commit 14bd250

Browse files
committed
Fixed paint mode erase
1 parent bbe87c2 commit 14bd250

File tree

5 files changed

+48
-24
lines changed

5 files changed

+48
-24
lines changed

include/cellMatrix.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class cellMatrix
3434
std::unique_ptr<std::vector<completetion>> checkCompletetion();
3535
void executeCompletetions(std::unique_ptr<std::vector<completetion>>& completetions, cell withCell);
3636

37+
cell getCellState(const sf::Vector2u& tableCellCoords);
3738
void applyBlock(Block& theBlock, const sf::Vector2u& tableCellCoords, cell cellType = cell::occupied);
3839
bool canBlockBePlaced(Block& theBlock);
3940

include/table.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ class Table
3131

3232
cellMatrix theMatrix;
3333

34-
sf::Vector2i mousePositionToCellPosition(const sf::Vector2f& mousePosition);
35-
3634
void calculateVertexes();
3735

3836
public:
@@ -42,10 +40,11 @@ class Table
4240

4341
void updateColors();
4442

45-
void applyBlock(Block& theBlock, const sf::Vector2u& tableCellCoords);
43+
cell getCellState(const sf::Vector2u& tableCellCoords);
44+
void applyBlock(Block& theBlock, const sf::Vector2u& tableCellCoords, cell cellType = cell::occupied);
4645
bool canBlockBePlaced(Block& theBlock);
47-
4846
sf::Vector2i previewBlock(Block& theHoldingBlock, const sf::Vector2f& mousePosition);
47+
sf::Vector2i mousePositionToCellPosition(const sf::Vector2f& mousePosition);
4948

5049
const cellMatrix& getMatrix();
5150
void reset();

src/cellMatrix.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ unsigned cellMatrix::getEmptyCellsAmount() const {
5353
return amount;
5454
}
5555

56+
cell cellMatrix::getCellState(const sf::Vector2u& tableCellCoords) {
57+
return cellTable[tableCellCoords.x][tableCellCoords.y];
58+
}
59+
5660
sf::Vector2i cellMatrix::previewBlock(Block& theHoldingBlock, const sf::Vector2i& matrixPosition) {
5761
const auto& blockStructure = theHoldingBlock.getStructure();
5862
sf::Vector2i cellCoords = matrixPosition;
@@ -224,7 +228,7 @@ void cellMatrix::applyBlock(Block& theBlock, const sf::Vector2u& tableCellCoords
224228
for (unsigned x = 0; x < theBlock.getStructureSize().x; x++) {
225229
for (unsigned y = 0; y < theBlock.getStructureSize().y; y++) {
226230
if (structure[x][y] == 1)
227-
cellTable[tableCellCoords.x + x][tableCellCoords.y + y] = cell::occupied;
231+
cellTable[tableCellCoords.x + x][tableCellCoords.y + y] = cellType;
228232
}
229233
}
230234
}

src/pickupBoard.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,6 @@ void PickupBoard::pollEvent(sf::RenderWindow& window, sf::Event& theEvent) {
220220
}
221221
}
222222
else {
223-
if (Settings::Gameplay::paintMode)
224-
paintModePreviewCoords = Game::theTable->previewBlock(paintModeBlock, mousePosition);
225-
226223
if (theEvent.type == sf::Event::MouseButtonPressed && theEvent.mouseButton.button == sf::Mouse::Left) {
227224
for (unsigned i = 0; i < 3; i++) { //check for every 3 blocks from the pickup area
228225
if (pickupableBlocks[i] == nullptr) //if they are picked up, if a block is nullptr, it means it was used
@@ -236,13 +233,28 @@ void PickupBoard::pollEvent(sf::RenderWindow& window, sf::Event& theEvent) {
236233
pickupableBlocks[i]->setScale(PICKUP_SCALE);
237234
}
238235
}
236+
}
239237

240-
//if no pickupable blocks were picked up, process paintMode
241-
if (pickedUpIndex == -1 && Settings::Gameplay::paintMode && (paintModePreviewCoords.x != -1 && paintModePreviewCoords.y != -1)) {
242-
Game::theTable->applyBlock(paintModeBlock, { static_cast<unsigned>(paintModePreviewCoords.x), static_cast<unsigned>(paintModePreviewCoords.y) });
243-
244-
if (isBoardLost())
245-
Game::theScore->setGameLost();
238+
//if no pickupable blocks were picked up, process paintMode
239+
if (Settings::Gameplay::paintMode && pickedUpIndex == -1) {
240+
if (theEvent.type == sf::Event::MouseButtonPressed) {
241+
if (theEvent.mouseButton.button == sf::Mouse::Left) {
242+
paintModePreviewCoords = Game::theTable->previewBlock(paintModeBlock, mousePosition);
243+
if (paintModePreviewCoords.x != -1 && paintModePreviewCoords.y != -1) {
244+
Game::theTable->applyBlock(paintModeBlock, { static_cast<unsigned>(paintModePreviewCoords.x), static_cast<unsigned>(paintModePreviewCoords.y) });
245+
246+
if (isBoardLost())
247+
Game::theScore->setGameLost();
248+
}
249+
}
250+
else if (theEvent.mouseButton.button == sf::Mouse::Right) {
251+
paintModePreviewCoords = Game::theTable->mousePositionToCellPosition(mousePosition);
252+
if (paintModePreviewCoords.x != -1 && paintModePreviewCoords.y != -1) {
253+
sf::Vector2u unsignedPreviewCoords = { static_cast<unsigned>(paintModePreviewCoords.x), static_cast<unsigned>(paintModePreviewCoords.y) };
254+
if(Game::theTable->getCellState(unsignedPreviewCoords) == cell::occupied)
255+
Game::theTable->applyBlock(paintModeBlock, unsignedPreviewCoords, cell::empty);
256+
}
257+
}
246258
}
247259
}
248260
}

src/table.cpp

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,23 +76,31 @@ sf::Vector2i Table::mousePositionToCellPosition(const sf::Vector2f& mousePositio
7676
return { -1, -1 };
7777
}
7878

79-
void Table::applyBlock(Block& theBlock, const sf::Vector2u& tableCellCoords) {
79+
cell Table::getCellState(const sf::Vector2u& tableCellCoords) {
80+
return theMatrix.getCellState(tableCellCoords);
81+
}
82+
83+
void Table::applyBlock(Block& theBlock, const sf::Vector2u& tableCellCoords, cell cellType) {
8084
//wont do any verifications for the tableCellCords or the block compatibility
8185
//because this function is SUPPOSED to be used ONLY with the RIGHT stuff.
8286

83-
Game::theScore->addPiecePlaced(theBlock.getStructureIndex());
84-
85-
theMatrix.applyBlock(theBlock, tableCellCoords);
87+
theMatrix.applyBlock(theBlock, tableCellCoords, cellType);
8688

87-
auto completedMarks = theMatrix.checkCompletetion();
89+
Game::theScore->addPiecePlaced(theBlock.getStructureIndex());
8890

89-
Game::theScore->processMarks(completedMarks);
90-
if (completedMarks->empty())
91+
if (cellType != cell::occupied)
9192
Audio::play(Audio::effect::GoodPlacement);
92-
else
93-
Audio::play(Audio::effect::Completetion);
93+
else {
94+
auto completedMarks = theMatrix.checkCompletetion();
95+
96+
Game::theScore->processMarks(completedMarks);
97+
if (completedMarks->empty())
98+
Audio::play(Audio::effect::GoodPlacement);
99+
else
100+
Audio::play(Audio::effect::Completetion);
94101

95-
theMatrix.executeCompletetions(completedMarks, cell::empty);
102+
theMatrix.executeCompletetions(completedMarks, cell::empty);
103+
}
96104
}
97105

98106
sf::Vector2i Table::previewBlock(Block& theHoldingBlock, const sf::Vector2f& mousePosition) {

0 commit comments

Comments
 (0)