2022-08-15 23:51:18 -05:00
|
|
|
// GameState.cpp
|
2023-04-06 19:27:10 -05:00
|
|
|
#include <stdexcept>
|
2023-03-17 17:27:02 -05:00
|
|
|
#include <SFML/Graphics.hpp>
|
2023-08-19 12:56:40 -05:00
|
|
|
#include "botinterface.hpp"
|
2023-04-06 19:27:10 -05:00
|
|
|
#include "common.hpp"
|
|
|
|
#include "playerinterface.hpp"
|
|
|
|
#include "gamestate.hpp"
|
2022-07-28 15:18:24 -05:00
|
|
|
|
2023-04-06 19:27:10 -05:00
|
|
|
namespace snakeplusplus
|
2022-07-28 15:18:24 -05:00
|
|
|
{
|
2023-04-06 19:27:10 -05:00
|
|
|
GameEngine::GameEngine()
|
2023-03-17 20:13:50 -05:00
|
|
|
{
|
2023-08-19 23:32:53 -05:00
|
|
|
InitializeGenerator();
|
2023-04-06 19:27:10 -05:00
|
|
|
return;
|
2023-03-17 20:13:50 -05:00
|
|
|
}
|
|
|
|
|
2023-08-18 17:51:58 -05:00
|
|
|
void GameEngine::Start()
|
2023-04-06 19:27:10 -05:00
|
|
|
{
|
2023-04-06 22:22:06 -05:00
|
|
|
PrepareGameBoard();
|
|
|
|
graphics.StartGameWindow();
|
2023-08-18 17:51:58 -05:00
|
|
|
Loop();
|
2023-04-06 19:27:10 -05:00
|
|
|
return;
|
|
|
|
}
|
2023-03-12 08:50:50 -05:00
|
|
|
|
2023-08-18 17:51:58 -05:00
|
|
|
void GameEngine::Reset()
|
|
|
|
{
|
2023-08-19 23:32:53 -05:00
|
|
|
graphics.CheckContinue(isBotControlled);
|
2023-08-18 17:51:58 -05:00
|
|
|
player.Reset();
|
2023-10-13 19:07:08 -05:00
|
|
|
if (isBotControlled) {
|
|
|
|
while (!bot.path.empty()) { bot.path.pop(); }
|
|
|
|
}
|
2023-08-18 17:51:58 -05:00
|
|
|
PrepareGameBoard();
|
2023-08-19 23:32:53 -05:00
|
|
|
isGameOver = false;
|
2023-08-18 18:09:09 -05:00
|
|
|
return;
|
2023-08-18 17:51:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void GameEngine::Loop(void)
|
2023-04-06 19:27:10 -05:00
|
|
|
{
|
2023-04-06 22:22:06 -05:00
|
|
|
while (graphics.IsOpen())
|
2023-04-06 19:27:10 -05:00
|
|
|
{
|
2023-08-19 12:56:40 -05:00
|
|
|
if (isGameOver) { Reset(); }
|
2023-04-06 22:22:06 -05:00
|
|
|
UpdatePlayerSpeed();
|
|
|
|
PlaceNewSnakePart(MovePlayer());
|
2023-04-15 05:15:11 -05:00
|
|
|
RegenerateFood();
|
|
|
|
graphics.DisplayGameState(gameBoard);
|
2023-04-06 19:27:10 -05:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2023-05-17 20:20:01 -05:00
|
|
|
|
2023-04-06 22:22:06 -05:00
|
|
|
sf::Vector2f GameEngine::MovePlayer(void)
|
|
|
|
{
|
|
|
|
sf::Vector2f newHeadPosition;
|
|
|
|
newHeadPosition.x = player.headLocation.x + player.speed.x;
|
|
|
|
newHeadPosition.y = player.headLocation.y + player.speed.y;
|
|
|
|
return newHeadPosition;
|
|
|
|
}
|
2023-03-17 21:07:58 -05:00
|
|
|
|
2023-04-06 19:27:10 -05:00
|
|
|
sf::Vector2f GameEngine::GetGameBoundaries(void)
|
|
|
|
{
|
2023-04-06 22:22:06 -05:00
|
|
|
return graphics.gameBoundaries;
|
2023-04-06 19:27:10 -05:00
|
|
|
}
|
2023-03-13 08:24:56 -05:00
|
|
|
|
2023-04-06 19:27:10 -05:00
|
|
|
void GameEngine::PlaceNewSnakePart(sf::Vector2f location)
|
2023-03-17 21:40:49 -05:00
|
|
|
{
|
2023-08-18 22:36:47 -05:00
|
|
|
if (!player.speed.x && !player.speed.y) { return; }
|
2023-04-06 19:27:10 -05:00
|
|
|
try
|
|
|
|
{
|
2023-04-15 05:15:11 -05:00
|
|
|
char* locationState;
|
|
|
|
locationState = &gameBoard.at(location.y).at(location.x);
|
2023-08-18 18:09:09 -05:00
|
|
|
if (*locationState == 'O' && (player.body.size() > 1))
|
2023-04-06 19:27:10 -05:00
|
|
|
isGameOver = true; // Game should end (Snake touching snake)
|
2023-04-15 05:15:11 -05:00
|
|
|
*locationState = 'O';
|
|
|
|
player.body.push(locationState);
|
|
|
|
player.headLocation = location;
|
2023-04-06 22:22:06 -05:00
|
|
|
if (playerFood.location != location)
|
|
|
|
player.Pop();
|
2023-04-06 19:27:10 -05:00
|
|
|
} catch (const std::out_of_range& error) {
|
|
|
|
isGameOver = true; // Snake ran into edge
|
|
|
|
}
|
2023-08-18 18:09:09 -05:00
|
|
|
return;
|
2023-03-17 21:40:49 -05:00
|
|
|
}
|
2022-08-02 21:17:23 -05:00
|
|
|
|
2023-04-06 19:27:10 -05:00
|
|
|
// Generates new food until not colliding with player
|
|
|
|
void GameEngine::RegenerateFood(void)
|
|
|
|
{
|
2023-04-06 22:22:06 -05:00
|
|
|
sf::Vector2f newLocation = playerFood.location;
|
2023-04-06 19:27:10 -05:00
|
|
|
bool isUpdated = false;
|
|
|
|
while (gameBoard.at(newLocation.y).at(newLocation.x) == 'O')
|
|
|
|
{
|
|
|
|
isUpdated = true;
|
|
|
|
playerFood.GenerateNewFood(GetGameBoundaries());
|
2023-04-06 22:22:06 -05:00
|
|
|
newLocation = playerFood.location;
|
2023-04-06 19:27:10 -05:00
|
|
|
}
|
2023-10-13 19:07:08 -05:00
|
|
|
if (isUpdated) {
|
|
|
|
gameBoard.at(newLocation.y).at(newLocation.x) = 'X';
|
|
|
|
}
|
2023-04-06 19:27:10 -05:00
|
|
|
return;
|
|
|
|
}
|
2023-03-17 17:27:02 -05:00
|
|
|
|
2023-04-06 22:22:06 -05:00
|
|
|
void GameEngine::PrepareGameBoard(void)
|
2022-08-02 21:17:23 -05:00
|
|
|
{
|
2023-04-06 19:27:10 -05:00
|
|
|
gameBoard.clear();
|
|
|
|
sf::Vector2f boardDimensions = GetGameBoundaries();
|
2023-04-15 05:15:11 -05:00
|
|
|
gameBoard.resize(boardDimensions.y, std::vector<char> (boardDimensions.x, ' '));
|
2023-08-18 19:51:15 -05:00
|
|
|
// Snake setup
|
2023-08-19 00:19:04 -05:00
|
|
|
player.headLocation.x = GenerateRandomNumber(boardDimensions.x);
|
|
|
|
player.headLocation.y = GenerateRandomNumber(boardDimensions.y);
|
2023-08-18 19:51:15 -05:00
|
|
|
{
|
|
|
|
char* locationState = &gameBoard.at(player.headLocation.y).at(player.headLocation.x);
|
|
|
|
player.body.push(locationState);
|
|
|
|
*locationState = 'O';
|
|
|
|
}
|
|
|
|
// Food setup
|
2023-08-19 00:19:04 -05:00
|
|
|
playerFood.GenerateNewFood(boardDimensions);
|
|
|
|
gameBoard.at(playerFood.location.y).at(playerFood.location.x) = 'X';
|
2023-04-06 22:22:06 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GameEngine::UpdatePlayerSpeed(void)
|
|
|
|
{
|
2023-08-19 12:56:40 -05:00
|
|
|
PlayerDirection controller;
|
2023-10-13 19:07:08 -05:00
|
|
|
if (isBotControlled) {
|
|
|
|
if (bot.path.empty()) {
|
2023-10-13 19:22:11 -05:00
|
|
|
bot.GetNewPath(gameBoard, player.headLocation, GetGameBoundaries(), player.body.size());
|
2023-10-13 19:07:08 -05:00
|
|
|
}
|
|
|
|
controller = bot.GetInput(&player.headLocation);
|
|
|
|
}
|
2023-08-19 12:56:40 -05:00
|
|
|
else { controller = GetPlayerInput(); }
|
|
|
|
switch (controller) {
|
2023-04-06 22:22:06 -05:00
|
|
|
case kUp:
|
2023-08-19 23:32:53 -05:00
|
|
|
if (player.speed.y == kUnitSpeed) { break; }
|
2023-04-06 22:22:06 -05:00
|
|
|
player.speed.x = 0;
|
2023-08-19 23:32:53 -05:00
|
|
|
player.speed.y = -kUnitSpeed;
|
2023-04-15 05:15:11 -05:00
|
|
|
break;
|
2023-04-06 22:22:06 -05:00
|
|
|
case kLeft:
|
2023-08-19 23:32:53 -05:00
|
|
|
if (player.speed.x == kUnitSpeed) { break; }
|
|
|
|
player.speed.x = -kUnitSpeed;
|
2023-04-06 22:22:06 -05:00
|
|
|
player.speed.y = 0;
|
2023-04-15 05:15:11 -05:00
|
|
|
break;
|
2023-04-06 22:22:06 -05:00
|
|
|
case kRight:
|
2023-08-19 23:32:53 -05:00
|
|
|
if (player.speed.x == -kUnitSpeed) { break; }
|
|
|
|
player.speed.x = kUnitSpeed;
|
2023-04-06 22:22:06 -05:00
|
|
|
player.speed.y = 0;
|
2023-04-15 05:15:11 -05:00
|
|
|
break;
|
2023-04-06 22:22:06 -05:00
|
|
|
case kDown:
|
2023-08-19 23:32:53 -05:00
|
|
|
if (player.speed.y == -kUnitSpeed) { break; }
|
2023-04-06 22:22:06 -05:00
|
|
|
player.speed.x = 0;
|
2023-08-19 23:32:53 -05:00
|
|
|
player.speed.y = kUnitSpeed;
|
2023-04-15 05:15:11 -05:00
|
|
|
break;
|
2023-04-06 22:22:06 -05:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2023-08-18 18:09:09 -05:00
|
|
|
return;
|
2022-08-02 21:17:23 -05:00
|
|
|
}
|
|
|
|
}
|