2022-08-15 23:51:18 -05:00
|
|
|
// GameState.cpp
|
2023-03-17 20:13:50 -05:00
|
|
|
#include <string>
|
2023-03-17 17:27:02 -05:00
|
|
|
#include <SFML/Graphics.hpp>
|
2023-03-13 21:10:52 -05:00
|
|
|
#include "common.h"
|
2023-03-17 17:27:02 -05:00
|
|
|
#include "display.h"
|
2023-03-13 21:10:52 -05:00
|
|
|
#include "gamestate.h"
|
2022-07-28 15:18:24 -05:00
|
|
|
|
|
|
|
GameState::GameState()
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2022-08-02 21:17:23 -05:00
|
|
|
|
2023-03-17 20:13:50 -05:00
|
|
|
void GameState::SetGameSettings(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
std::string convertedString;
|
|
|
|
for (int i = 0; i < argc; i++)
|
|
|
|
{
|
|
|
|
convertedString = argv[i];
|
|
|
|
if (convertedString == "--no-sfml")
|
|
|
|
useSFML = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-12 08:50:50 -05:00
|
|
|
void GameState::StartGame()
|
2022-08-02 21:17:23 -05:00
|
|
|
{
|
2023-03-17 20:13:50 -05:00
|
|
|
ApplySettings();
|
2023-03-17 17:27:02 -05:00
|
|
|
ResetGameBoard();
|
2023-03-17 21:07:58 -05:00
|
|
|
graphics->StartGameWindow();
|
2023-03-12 08:50:50 -05:00
|
|
|
RunGameLoop();
|
2023-03-12 21:57:46 -05:00
|
|
|
return;
|
2023-03-12 08:50:50 -05:00
|
|
|
}
|
|
|
|
|
2023-03-17 20:13:50 -05:00
|
|
|
void GameState::ApplySettings(void)
|
|
|
|
{
|
|
|
|
if (useSFML)
|
|
|
|
graphics.reset(new SFML());
|
|
|
|
else
|
|
|
|
graphics.reset(new CommandLine());
|
2023-03-17 21:07:58 -05:00
|
|
|
return;
|
2023-03-17 20:13:50 -05:00
|
|
|
}
|
|
|
|
|
2023-03-17 17:27:02 -05:00
|
|
|
// TODO: Reimplement for DisplayInterface
|
2023-03-13 08:24:56 -05:00
|
|
|
void GameState::DisplayEndScreen(void)
|
|
|
|
{
|
2023-03-17 21:07:58 -05:00
|
|
|
graphics->DisplayEndScreen();
|
2023-03-13 08:24:56 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-03-12 08:50:50 -05:00
|
|
|
sf::Vector2f GameState::GetGameBoundaries(void)
|
|
|
|
{
|
2023-03-17 17:27:02 -05:00
|
|
|
return graphics->gameBoundaries;
|
2023-03-12 08:50:50 -05:00
|
|
|
}
|
|
|
|
|
2023-03-12 21:57:46 -05:00
|
|
|
void GameState::GetKeyboardInput(void)
|
|
|
|
{
|
2023-03-17 20:13:50 -05:00
|
|
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
|
|
|
|
player.UpdateDirection(kLeft);
|
|
|
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
|
|
|
|
player.UpdateDirection(kUp);
|
|
|
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
|
|
|
|
player.UpdateDirection(kDown);
|
|
|
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
|
|
|
|
player.UpdateDirection(kRight);
|
2023-03-12 21:57:46 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-03-17 21:07:58 -05:00
|
|
|
void GameState::PlaceNewSnakePart(sf::Vector2f location)
|
|
|
|
{
|
|
|
|
gameBoard.at(location.y).at(location.x) = 'o';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GameState::PlayerWantsToContinue(void)
|
2023-03-13 08:24:56 -05:00
|
|
|
{
|
2023-03-17 21:07:58 -05:00
|
|
|
graphics->CheckContinue();
|
|
|
|
return;
|
2023-03-13 08:24:56 -05:00
|
|
|
}
|
|
|
|
|
2023-03-12 08:50:50 -05:00
|
|
|
// Generates new food until not colliding with player
|
|
|
|
void GameState::RegenerateFood(void)
|
|
|
|
{
|
2023-03-17 21:40:49 -05:00
|
|
|
sf::Vector2f newLocation = playerFood.GetFoodLocation();
|
|
|
|
bool isUpdated = false;
|
2023-03-12 08:50:50 -05:00
|
|
|
// Keep making new food until generating a valid spot
|
2023-03-17 20:13:50 -05:00
|
|
|
while (gameBoard.at(newLocation.y).at(newLocation.x) == 'o')
|
2023-03-17 21:40:49 -05:00
|
|
|
{
|
|
|
|
isUpdated = true;
|
2023-03-12 08:50:50 -05:00
|
|
|
playerFood.GenerateNewFood(GetGameBoundaries());
|
2023-03-17 21:40:49 -05:00
|
|
|
newLocation = playerFood.GetFoodLocation();
|
|
|
|
}
|
|
|
|
if (isUpdated)
|
|
|
|
gameBoard.at(newLocation.y).at(newLocation.x) = 'x';
|
2023-03-12 08:50:50 -05:00
|
|
|
return;
|
|
|
|
}
|
2022-08-02 21:17:23 -05:00
|
|
|
|
2023-03-17 17:27:02 -05:00
|
|
|
void GameState::ResetGameBoard(void)
|
|
|
|
{
|
|
|
|
gameBoard.clear();
|
|
|
|
sf::Vector2f boardDimensions = GetGameBoundaries();
|
2023-03-17 21:07:58 -05:00
|
|
|
std::vector<char> tempBoard;
|
|
|
|
tempBoard.resize(boardDimensions.x, ' ');
|
|
|
|
gameBoard.resize(boardDimensions.y, tempBoard);
|
2023-03-17 21:40:49 -05:00
|
|
|
playerFood.GenerateNewFood(boardDimensions);
|
|
|
|
sf::Vector2f foodStartLocation = playerFood.GetFoodLocation();
|
|
|
|
gameBoard.at(foodStartLocation.y).at(foodStartLocation.x) = 'x';
|
|
|
|
|
2023-03-17 17:27:02 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-03-12 08:50:50 -05:00
|
|
|
void GameState::RunGameLoop(void)
|
|
|
|
{
|
2023-03-17 17:27:02 -05:00
|
|
|
while (graphics->IsOpen())
|
2022-08-02 21:17:23 -05:00
|
|
|
{
|
2023-03-12 21:57:46 -05:00
|
|
|
GetKeyboardInput();
|
2023-03-17 21:07:58 -05:00
|
|
|
PlaceNewSnakePart(player.MoveSnake());
|
2023-03-12 08:50:50 -05:00
|
|
|
RegenerateFood();
|
2023-03-17 21:07:58 -05:00
|
|
|
graphics->DisplayGameState(&gameBoard);
|
|
|
|
if (isGameOver)
|
|
|
|
PlayerWantsToContinue();
|
2022-08-02 21:17:23 -05:00
|
|
|
}
|
2023-03-12 21:57:46 -05:00
|
|
|
return;
|
2022-08-02 21:17:23 -05:00
|
|
|
}
|
2023-03-12 08:50:50 -05:00
|
|
|
|