lap times

This commit is contained in:
thatscringebro 2023-10-11 09:35:05 -04:00
parent e397e49b58
commit 11e7558feb
3 changed files with 35 additions and 15 deletions

View File

@ -27,7 +27,7 @@ Game::Game()
racecar.setRotation(180); racecar.setRotation(180);
// Initialize the times and quadrants data structure // Initialize the times and quadrants data structure
timesAndQuadrants.push_back(std::make_pair("Quadrant 1", "00:00:00.000")); timesAndQuadrants.push_back(std::make_pair(1, "00:00:00.000"));
} }
void Game::run() { void Game::run() {
@ -59,10 +59,12 @@ void Game::run() {
timerText.setString(timerString); timerText.setString(timerString);
// Detect when the racecar changes quadrants and save the time // Detect when the racecar changes quadrants and save the time
std::string currentQuadrant = determineQuadrant(racecar.getX(), racecar.getY()); int currentQuadrant = determineQuadrant(racecar.getX(), racecar.getY());
if (currentQuadrant != timesAndQuadrants.back().first) { if (currentQuadrant != timesAndQuadrants.back().first) {
timesAndQuadrants.push_back(std::make_pair(currentQuadrant, timerString)); timesAndQuadrants.push_back(std::make_pair(currentQuadrant, timerString));
} }
determineLap(timerString);
sf::Event event; sf::Event event;
while (window.pollEvent(event)) { while (window.pollEvent(event)) {
@ -163,20 +165,37 @@ bool Game::isTrackCollision(float x, float y) {
return false; // No collision with track or out of bounds return false; // No collision with track or out of bounds
} }
std::string Game::determineQuadrant(float x, float y) { void Game::determineLap(std::string timerString) {
for (int i = 0; i < timesAndQuadrants.size(); i++) { lapFinished = true;
if (timesAndQuadrants[i] != requiredOrder[i]) int requiredOrder[] = {1,2,1,2,3,4,1};
if(timesAndQuadrants.size() >= 7) {
for (int i = 0; i < 7; i++) {
if (std::get<0>(timesAndQuadrants[i]) != requiredOrder[i])
lapFinished = false;
}
} }
else {
lapFinished = false;
}
if (lapFinished) {
lapTimes.push_back(timerString);
timesAndQuadrants.clear();
timesAndQuadrants.push_back(std::make_pair(1, timerString));
}
}
int Game::determineQuadrant(float x, float y) {
if (y <= 460 && x <= 1150) if (y <= 460 && x <= 1150)
return "Quadrant 1"; return 1;
if (y > 460 && x <= 1150) if (y > 460 && x <= 1150)
return "Quadrant 2"; return 2;
if (y > 460 && x > 1150) if (y > 460 && x > 1150)
return "Quadrant 3"; return 3;
if (y <= 460 && x > 1150) if (y <= 460 && x > 1150)
return "Quadrant 4"; return 4;
return 0;
} }
void Game::displaySavedTimes() { void Game::displaySavedTimes() {
@ -194,7 +213,7 @@ void Game::displaySavedTimes() {
textY += 30; // Adjust the spacing as needed textY += 30; // Adjust the spacing as needed
} }
for (const auto& timeAndQuadrant : timesAndQuadrants) { for (const auto& timeAndQuadrant : timesAndQuadrants) {
std::string timeString = timeAndQuadrant.first + ": " + timeAndQuadrant.second; std::string timeString = "Quadrant " + std::to_string(timeAndQuadrant.first) + ": " + timeAndQuadrant.second;
sf::Text text; sf::Text text;
text.setFont(font); text.setFont(font);
text.setCharacterSize(18); text.setCharacterSize(18);

7
game.h
View File

@ -11,8 +11,9 @@ public:
Game(); Game();
void run(); void run();
bool isTrackCollision(float x, float y); bool isTrackCollision(float x, float y);
std::string determineQuadrant(float x, float y); int determineQuadrant(float x, float y);
void displaySavedTimes(); void displaySavedTimes();
void determineLap(std::string timerString);
private: private:
int max_x; int max_x;
@ -26,8 +27,8 @@ private:
sf::Clock gameTimeClock; sf::Clock gameTimeClock;
sf::View view; sf::View view;
Racecar racecar; Racecar racecar;
std::vector<std::pair<std::string, std::string>> timesAndQuadrants; // Store times and quadrants std::vector<std::pair<int, std::string>> timesAndQuadrants; // Store times and quadrants
bool lapStarted; // Indicates if a lap has started bool lapFinished;
std::vector<std::string> lapTimes; // Store lap times std::vector<std::string> lapTimes; // Store lap times
std::string asciiMap = " /------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\\ \n" std::string asciiMap = " /------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\\ \n"
" / \\ \n" " / \\ \n"

Binary file not shown.