Skip to content

Commit 91837f1

Browse files
committed
Add WIN and GAME_OVER states that display a faded rectangle mesh
Add capability to reset game after winning or losing
1 parent 13c705f commit 91837f1

File tree

5 files changed

+80
-13
lines changed

5 files changed

+80
-13
lines changed

src/Cell.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ void Cell::UpdateActor(float deltaTime)
3838
mesh->SetColor(PURPLE);
3939
lineMesh->SetIsShow(true);
4040
break;
41+
case MINE_EXPOSE:
42+
mesh->SetColor(RED);
43+
lineMesh->SetIsShow(true);
44+
break;
4145
default:
4246
mesh->SetColor(DARKGRAY);
4347
}

src/Cell.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ enum CellType
1919
ADJACENT,
2020
MINE,
2121
MINE_SEALED,
22+
MINE_EXPOSE,
2223
SEALED
2324
};
2425

src/Game.cpp

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ void Game::ProcessInput()
4545
{
4646
mGrid->ProcessInput(GetMouseX(), GetMouseY());
4747
}
48+
else if (mGameState == WIN || mGameState == GAME_OVER)
49+
{
50+
if (IsKeyReleased(KEY_SPACE))
51+
{
52+
mGrid->Reset();
53+
}
54+
}
4855
}
4956

5057
void Game::UpdateGame()
@@ -69,32 +76,53 @@ void Game::GenerateOutput()
6976
GuiSetStyle(DEFAULT, TEXT_SIZE, 40);
7077
GuiSetStyle(DEFAULT, TEXT_SPACING, 10);
7178
GuiSetStyle(BUTTON, BASE_COLOR_NORMAL, 0x000000FF);
72-
if (GuiButton({ (mScreenWidth / 2.f) - (buttonWidth / 2.f),
73-
(mScreenHeight / 2.f) - (buttonHeight / 2.f) - padding,
74-
buttonWidth,
75-
buttonHeight },
79+
if (GuiButton({(mScreenWidth / 2.f) - (buttonWidth / 2.f),
80+
(mScreenHeight / 2.f) - (buttonHeight / 2.f) - padding,
81+
buttonWidth,
82+
buttonHeight},
7683
"START"))
7784
{
7885
printf("START GAME!\n");
7986
mGameState = PLAYING;
8087
}
81-
if (GuiButton({ (mScreenWidth / 2.f) - (buttonWidth / 2.f),
82-
(mScreenHeight / 2.f) - (buttonHeight / 2.f) + buttonHeight,
83-
buttonWidth,
84-
buttonHeight },
88+
if (GuiButton({(mScreenWidth / 2.f) - (buttonWidth / 2.f),
89+
(mScreenHeight / 2.f) - (buttonHeight / 2.f) + buttonHeight,
90+
buttonWidth,
91+
buttonHeight},
8592
"EXIT"))
8693
{
8794
printf("EXIT GAME!\n");
8895
mIsRunning = false;
8996
}
9097
}
9198
break;
92-
case PLAYING:
99+
default:
100+
{
101+
// This is our default PLAYING rendering output
102+
// This way we can render our GAME_OVER rectangular box correctly
93103
ClearBackground(LIGHTGRAY);
94-
for (auto drawCom : mDraws)
104+
for (auto drawCom: mDraws)
95105
{
96106
drawCom->Draw();
97107
}
108+
if (mGameState == GAME_OVER)
109+
{
110+
int fontSize = 50;
111+
int width = 1200, height = 500;
112+
DrawRectangle((mScreenWidth / 2) - (width / 2), (mScreenHeight / 2) - (height / 2), width, height, {0, 0, 0, 220});
113+
DrawText("YOU CLICKED A MINE CELL! GAME OVER!", (mScreenWidth / 2) - 500, (mScreenHeight / 2) - 40, fontSize, RED);
114+
DrawText("PRESS SPACE BAR TO TRY AGAIN!", (mScreenWidth / 2) - 500, (mScreenHeight / 2) + 20, fontSize, RED);
115+
}
116+
else if (mGameState == WIN)
117+
{
118+
int width = 1200, height = 500;
119+
int fontSize = 50;
120+
DrawRectangle((mScreenWidth / 2) - (width / 2), (mScreenHeight / 2) - (height / 2), width, height, {0, 0, 0, 220});
121+
DrawText("CONGRATULATIONS, YOU HAVE WON!", (mScreenWidth / 2) - 500, (mScreenHeight / 2) - 40, fontSize, GREEN);
122+
DrawText("PRESS SPACE BAR TO PLAY AGAIN!", (mScreenWidth / 2) - 500, (mScreenHeight / 2) + 20, fontSize, GREEN);
123+
}
124+
}
125+
break;
98126
}
99127
EndDrawing();
100128
}

src/Grid.cpp

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,25 @@ void Grid::Initialize()
6464
SetMines();
6565
}
6666

67+
void Grid::Reset()
68+
{
69+
for (auto& row : mCellList)
70+
{
71+
for (auto cell : row)
72+
{
73+
cell->SetCellType(UNEXPOSE);
74+
// Reset TextComponent
75+
auto textComp = reinterpret_cast<TextComponent*>(cell->GetComponent("TextComponent"));
76+
if (textComp)
77+
{
78+
textComp->SetIsShow(false);
79+
}
80+
}
81+
}
82+
SetMines();
83+
GetGame()->SetGameState(Game::PLAYING);
84+
}
85+
6786
void Grid::UpdateActor(float deltaTime)
6887
{
6988
Actor::UpdateActor(deltaTime);
@@ -202,7 +221,14 @@ void Grid::Expose(Cell *cell)
202221
{
203222
// End game
204223
printf("YOU CLICKED A MINE! GAME OVER!\n");
205-
// GetGame()->SetGameState(Game::GAME_OVER);
224+
GetGame()->SetGameState(Game::GAME_OVER);
225+
for (auto mine : mMineList)
226+
{
227+
if (mine->GetCellType() != MINE_SEALED)
228+
{
229+
mine->SetCellType(MINE_EXPOSE);
230+
}
231+
}
206232
}
207233
else if (cell->GetCellType() == UNEXPOSE)
208234
{
@@ -215,12 +241,18 @@ void Grid::Expose(Cell *cell)
215241
// We change its cell type and add a text component
216242
cell->SetCellType(ADJACENT);
217243
cell->SetNumOfMines(numOfMines);
218-
auto textComp = new TextComponent("TextComponent", cell, 2);
244+
// Check if this cell already has a TextComponent
245+
auto textComp = reinterpret_cast<TextComponent*>(cell->GetComponent("TextComponent"));
246+
if (!textComp)
247+
{
248+
textComp = new TextComponent("TextComponent", cell, 2);
249+
}
219250
textComp->SetColor(RAYWHITE);
220251
textComp->SetFont(GetFontDefault());
221252
textComp->SetFontSize(40.f);
222253
textComp->SetSpacing(0.f);
223254
textComp->SetText(std::to_string(cell->GetNumOfMines()));
255+
textComp->SetIsShow(true);
224256
}
225257
else
226258
{
@@ -250,5 +282,5 @@ void Grid::CheckForWin()
250282
}
251283
// Once we get to this point, we know all mines are set to MINE_SEALED state.
252284
// Hence, set game condition to WIN
253-
// GetGame()->SetGameState(Game::WIN);
285+
GetGame()->SetGameState(Game::WIN);
254286
}

src/Grid.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class Grid : public Actor
1414
~Grid() = default;
1515

1616
void Initialize();
17+
void Reset();
18+
1719
void UpdateActor(float deltaTime) override;
1820
void ProcessInput(int mouseX, int mouseY);
1921

0 commit comments

Comments
 (0)