-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathraylib_game.cpp
More file actions
161 lines (134 loc) · 3.73 KB
/
raylib_game.cpp
File metadata and controls
161 lines (134 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "raylib.h"
#include "graphic_gallows.h"
#include "letter_grid.h"
#include "raylib_game.h"
#include "word.h"
const std::unordered_map<GameState, void (Game::*)()> Game::updates_ = {
{GameState::STARTING, &Game::updateStarting},
{GameState::PLAYING, &Game::updatePlaying},
{GameState::SUCCESS, &Game::updateEnding},
{GameState::FAILURE, &Game::updateEnding},
{GameState::COMPLETE, &Game::updateNothing}};
const std::unordered_map<GameState, void (Game::*)() const> Game::draws_ = {
{GameState::STARTING, &Game::drawStarting},
{GameState::PLAYING, &Game::drawPlaying},
{GameState::SUCCESS, &Game::drawEnding},
{GameState::FAILURE, &Game::drawEnding},
{GameState::COMPLETE, &Game::drawNothing}};
namespace
{
void centre(
const Window &window,
const ::Font &font,
const std::string &text,
float y,
float size,
float spacing,
::Color colour)
{
auto textsize = ::MeasureTextEx(font, text.c_str(), size, spacing);
::DrawTextEx(font, text.c_str(), {window.width / 2.0f - textsize.x / 2.0f, y}, size, spacing, colour);
}
}
Game::Game(const Window &window, size_t min_length)
: window_{window}
, words_{"../assets/words-2025-5-16.txt"}
, state_{GameState::STARTING}
, word_{words_.random(min_length)}
, gallows_{GraphicGallows{}}
, font_{LoadFontEx("../assets/bloodcrow.ttf", 36, nullptr, 0)}
, letter_grid_{LetterGrid{font_, 40, 780, 36, 40.0f}}
, images_{ImageLoader{"../assets"}}
{
::SetTargetFPS(60);
::SetExitKey(0); // Disable Esc to exit
}
void Game::updateStarting()
{
if (::IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
{
state_ = GameState::PLAYING;
}
}
void Game::updatePlaying()
{
if (auto ch = letter_grid_.update(word_); ch != ' ')
{
if (!word_.guess(ch))
{
gallows_.next();
}
}
if (word_.done())
{
state_ = GameState::SUCCESS;
}
else if (word_.bad_letters.size() == gallows_.stages() - 1)
{
state_ = GameState::FAILURE;
}
}
void Game::updateEnding()
{
if (::IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
{
state_ = GameState::COMPLETE;
}
}
void Game::drawStarting() const
{
::DrawTexture(images_.at("lets-go-600"), 0, 0, WHITE);
say_click_to_continue();
}
void Game::drawPlaying() const
{
gallows_.draw();
show_guessed();
letter_grid_.draw(word_);
::DrawTextEx(font_, std::format("{}", word_).c_str(), {40, 620}, 36, 0, SKYBLUE);
}
void Game::drawEnding() const
{
auto info_text = std::format("The word was {}", word_.display());
centre(
window_,
font_,
info_text,
700,
36,
3,
state_ == GameState::SUCCESS ? ::Color{120, 255, 120, 255} : ::Color{255, 50, 50, 255});
::DrawTexture(state_ == GameState::SUCCESS ? images_.at("success-600") : images_.at("failure-600"), 0, 0, WHITE);
say_click_to_continue();
}
void Game::run()
{
int idx = 0;
auto hanged = [&]() { return; };
while (!::WindowShouldClose() && state_ != GameState::COMPLETE)
{
(this->*(updates_.at(state_)))();
::BeginDrawing();
::ClearBackground(BLACK);
(this->*(draws_.at(state_)))();
::EndDrawing();
}
}
void Game::say_click_to_continue() const
{
centre(window_, font_, "Click to Continue", 800, 36, 0, SKYBLUE);
}
void Game::show_guessed() const
{
const auto bad = word_.bad_letters;
if (bad.size() > 0)
{
::DrawTextEx(font_, "Guesses:", {40, 700}, 36, 0, SKYBLUE);
std::string out{};
for (char ch : bad)
{
out += std::format("{} ", static_cast<char>(toupper(ch)));
}
::DrawTextEx(font_, out.c_str(), {200, 700}, 36, 3, {255, 50, 50, 255});
}
}