core: fix not closing when window exits and add bot gameplay to be an argument

This commit is contained in:
Trianta 2024-08-03 18:34:14 -05:00
parent 722970eb12
commit 6e7ae19e8b
5 changed files with 21 additions and 14 deletions

View File

@ -23,17 +23,19 @@ void GameEngine::Start()
void GameEngine::Reset()
{
graphics.CheckContinue(state.m_bIsBotControlled);
bot.AddIteration(player.body.size());
if (!state.m_bIsBotControlled)
graphics.CheckContinue();
player.Reset();
if (state.m_bIsBotControlled) { while (!bot.path.empty()) { bot.path.pop(); } }
PrepareGameBoard();
state.m_bIsGameOver = false;
if (state.m_bIsBotControlled) {
while (!bot.path.empty())
bot.path.pop();
if (state.m_bNoDisplay)
graphics.SetShowGame(false);
else
graphics.SetShowGame((bot.amountPlayed + 1) % 50 == 0);
return;
bot.AddIteration(player.body.size());
}
}
void GameEngine::Loop(void)

View File

@ -19,7 +19,7 @@ public:
sf::Vector2f GetGameBoundaries(void);
struct GameState {
unsigned char m_bIsGameOver : 1 = 0;
unsigned char m_bIsBotControlled : 1 = 1;
unsigned char m_bIsBotControlled : 1 = 0;
unsigned char m_bNoDisplay : 1 = 0;
unsigned char _3 : 1 = 0;
unsigned char _4 : 1 = 0;

View File

@ -11,6 +11,10 @@ int main(int argc, char* argv[])
if (it->compare("--no-gui") == 0) {
g_pEngine->state.m_bNoDisplay = true;
std::cout << "[LOG - Main] Disabling display" << std::endl;
} else if (it->compare("--bot") == 0) {
g_pEngine->state.m_bIsBotControlled = true;
std::cout << "[LOG - Main] Bot control enabled" << std::endl;
} else {
std::cerr << "[ERROR] Argument option not found, exiting..." << std::endl;
return 1;

View File

@ -37,9 +37,8 @@ PlayerOutput::PlayerOutput(void)
return;
}
void PlayerOutput::CheckContinue(bool isBotControlled)
void PlayerOutput::CheckContinue()
{
if (isBotControlled) { return; }
DisplayEndScreen();
while (true)
{
@ -115,7 +114,7 @@ void PlayerOutput::StartGameWindow(void)
}
void PlayerOutput::SetShowGame(bool isShowing) {
if (isShowing) { delay = sf::milliseconds(2); }
if (isShowing) { delay = sf::milliseconds(5); }
else { delay = sf::milliseconds(0); }
return;
}
@ -125,8 +124,10 @@ void PlayerOutput::CheckWindowEvents(void)
while (gameWindow.pollEvent(event))
{
if ((event.type == sf::Event::Closed)
|| (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)))
|| (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))) {
gameWindow.close();
isWindowAlive = false;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Equal)) {
if (delay > sf::milliseconds(16)) { continue; }
delay += sf::milliseconds(1);

View File

@ -14,7 +14,7 @@ public:
sf::Vector2f gameBoundaries;
PlayerOutput(void);
bool IsOpen(void);
void CheckContinue(bool isBotControlled);
void CheckContinue();
void DisplayGameState(std::vector< std::vector<GameSpace> >& gameBoard, int score);
void DisplayScore(int score);
void StartGameWindow(void);
@ -30,7 +30,7 @@ private:
sf::RectangleShape drawObject;
sf::Event event;
bool isWindowAlive = false;
sf::Time delay = sf::milliseconds(1);
sf::Time delay = sf::milliseconds(12);
};
#endif