2022-08-15 23:51:18 -05:00
|
|
|
// GameState.cpp
|
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()
|
|
|
|
{
|
2023-03-13 21:10:52 -05:00
|
|
|
if (useSFML)
|
2023-03-17 17:27:02 -05:00
|
|
|
graphics.reset(new SFML());
|
2023-03-13 21:10:52 -05:00
|
|
|
else
|
|
|
|
graphics.reset(new CommandLine());
|
2022-07-28 15:18:24 -05:00
|
|
|
return;
|
|
|
|
}
|
2022-08-02 21:17:23 -05:00
|
|
|
|
2023-03-12 08:50:50 -05:00
|
|
|
void GameState::StartGame()
|
2022-08-02 21:17:23 -05:00
|
|
|
{
|
2023-03-17 17:27:02 -05:00
|
|
|
ResetGameBoard();
|
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 17:27:02 -05:00
|
|
|
// TODO: Reimplement for DisplayInterface
|
2023-03-13 08:24:56 -05:00
|
|
|
void GameState::DisplayEndScreen(void)
|
|
|
|
{
|
2023-03-17 17:27:02 -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-17 17:27:02 -05:00
|
|
|
// TODO: Reimplement for DisplayInterface
|
2023-03-12 21:57:46 -05:00
|
|
|
void GameState::GetKeyboardInput(void)
|
|
|
|
{
|
2023-03-17 17:27:02 -05:00
|
|
|
// sf::Event event;
|
|
|
|
// while (gameWindow.pollEvent(event))
|
|
|
|
// {
|
|
|
|
// if ((event.type == SFML:Event::Closed) ||
|
|
|
|
// (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)))
|
|
|
|
// gameWindow.close();
|
|
|
|
// }
|
|
|
|
// 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 17:27:02 -05:00
|
|
|
// TODO: Reimplement for DisplayInterface
|
2023-03-13 08:24:56 -05:00
|
|
|
bool GameState::PlayerWantsToContinue(void)
|
|
|
|
{
|
2023-03-17 17:27:02 -05:00
|
|
|
// sf::Event event;
|
|
|
|
// while (true)
|
|
|
|
// {
|
|
|
|
// while (gameWindow.pollEvent(event))
|
|
|
|
// {
|
|
|
|
// if ((event.type == sf::Event::Closed) ||
|
|
|
|
// (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)))
|
|
|
|
// gameWindow.close();
|
|
|
|
// return false;
|
|
|
|
// }
|
|
|
|
// if (sf::Keyboard::isKeyPressed(sf::Keyboard::Enter))
|
|
|
|
// return true;
|
|
|
|
// sf::sleep(delay);
|
|
|
|
// }
|
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)
|
|
|
|
{
|
|
|
|
// Keep making new food until generating a valid spot
|
|
|
|
while (player.IsTouchingObject(playerFood.GetFoodObject()))
|
|
|
|
playerFood.GenerateNewFood(GetGameBoundaries());
|
|
|
|
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();
|
|
|
|
gameBoard.resize(boardDimensions.y);
|
|
|
|
for (int i = 0; i < boardDimensions.y; i++)
|
|
|
|
for (int j = 0; j < boardDimensions.x; j++)
|
|
|
|
gameBoard[i].push_back(' ');
|
|
|
|
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();
|
|
|
|
player.MoveSnake(&playerFood);
|
2023-03-12 08:50:50 -05:00
|
|
|
RegenerateFood();
|
|
|
|
RenderWindow();
|
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
|
|
|
|
2023-03-17 17:27:02 -05:00
|
|
|
// TODO: Reimplement for DisplayInterface
|
2023-03-12 08:50:50 -05:00
|
|
|
void GameState::RenderWindow(void)
|
|
|
|
{
|
2023-03-17 17:27:02 -05:00
|
|
|
// gameWindow.clear();
|
|
|
|
// player.DisplaySnake(gameWindow);
|
|
|
|
// gameWindow.draw(playerFood.GetFoodObject());
|
|
|
|
// gameWindow.display();
|
|
|
|
// if (player.gameFinished)
|
|
|
|
// DisplayEndScreen();
|
2023-03-12 21:57:46 -05:00
|
|
|
return;
|
2023-03-12 08:50:50 -05:00
|
|
|
}
|