snakeplusplus/src/gamestate.cpp

141 lines
3.9 KiB
C++
Raw Normal View History

// GameState.cpp
#include <stdexcept>
#include <SFML/Graphics.hpp>
#include "common.hpp"
#include "playerinterface.hpp"
#include "gamestate.hpp"
namespace snakeplusplus
{
GameEngine::GameEngine()
2023-03-17 20:13:50 -05:00
{
2023-08-18 19:09:57 -05:00
snakeplusplus::InitializeGenerator();
return;
2023-03-17 20:13:50 -05:00
}
void GameEngine::Start()
{
PrepareGameBoard();
graphics.StartGameWindow();
Loop();
return;
}
2023-03-12 08:50:50 -05:00
void GameEngine::Reset()
{
graphics.CheckContinue();
player.Reset();
PrepareGameBoard();
isGameOver = 0;
2023-08-18 18:09:09 -05:00
return;
}
void GameEngine::Loop(void)
{
while (graphics.IsOpen())
{
if (isGameOver) {Reset();}
UpdatePlayerSpeed();
PlaceNewSnakePart(MovePlayer());
2023-04-15 05:15:11 -05:00
RegenerateFood();
graphics.DisplayGameState(gameBoard);
}
return;
}
2023-05-17 20:20:01 -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;
}
sf::Vector2f GameEngine::GetGameBoundaries(void)
{
return graphics.gameBoundaries;
}
void GameEngine::PlaceNewSnakePart(sf::Vector2f location)
{
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))
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;
if (playerFood.location != location)
player.Pop();
} catch (const std::out_of_range& error) {
isGameOver = true; // Snake ran into edge
}
2023-08-18 18:09:09 -05:00
return;
}
// Generates new food until not colliding with player
void GameEngine::RegenerateFood(void)
{
sf::Vector2f newLocation = playerFood.location;
bool isUpdated = false;
while (gameBoard.at(newLocation.y).at(newLocation.x) == 'O')
{
isUpdated = true;
playerFood.GenerateNewFood(GetGameBoundaries());
newLocation = playerFood.location;
}
if (isUpdated)
gameBoard.at(newLocation.y).at(newLocation.x) = 'X';
return;
}
void GameEngine::PrepareGameBoard(void)
{
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
{
player.headLocation.x = GenerateRandomNumber(boardDimensions.x);
player.headLocation.y = GenerateRandomNumber(boardDimensions.y);
char* locationState = &gameBoard.at(player.headLocation.y).at(player.headLocation.x);
player.body.push(locationState);
*locationState = 'O';
}
// Food setup
{
playerFood.GenerateNewFood(boardDimensions);
sf::Vector2f newLocation = playerFood.location;
gameBoard.at(newLocation.y).at(newLocation.x) = 'X';
}
return;
}
void GameEngine::UpdatePlayerSpeed(void)
{
2023-08-14 17:39:04 -05:00
switch (GetPlayerInput()) {
case kUp:
player.speed.x = 0;
2023-04-15 05:15:11 -05:00
player.speed.y = -1;
break;
case kLeft:
player.speed.x = -1;
player.speed.y = 0;
2023-04-15 05:15:11 -05:00
break;
case kRight:
player.speed.x = 1;
player.speed.y = 0;
2023-04-15 05:15:11 -05:00
break;
case kDown:
player.speed.x = 0;
2023-04-15 05:15:11 -05:00
player.speed.y = 1;
break;
default:
break;
}
2023-08-18 18:09:09 -05:00
return;
}
}