From d2e1424e7db231d3ac479763c0120213e543e16c Mon Sep 17 00:00:00 2001 From: Trimutex Date: Sat, 19 Aug 2023 12:56:40 -0500 Subject: [PATCH] Added bot controls, bot still has poor pathing --- src/CMakeLists.txt | 1 + src/botinterface.cpp | 26 ++++++++++++++++++++++++++ src/botinterface.hpp | 12 ++++++++++++ src/gamestate.cpp | 8 ++++++-- src/gamestate.hpp | 1 + src/playerinterface.hpp | 2 +- 6 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 src/botinterface.cpp create mode 100644 src/botinterface.hpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 04900db..147c92d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -6,6 +6,7 @@ add_executable(snakeplusplus ./snake.cpp ./playerinterface.cpp ./common.cpp + ./botinterface.cpp ) target_include_directories(snakeplusplus PUBLIC ${CMAKE_CURRENT_LIST_DIR}) diff --git a/src/botinterface.cpp b/src/botinterface.cpp new file mode 100644 index 0000000..fe9fc28 --- /dev/null +++ b/src/botinterface.cpp @@ -0,0 +1,26 @@ +#include "botinterface.hpp" +#include "common.hpp" +#include + +namespace snakeplusplus +{ + PlayerDirection lastKnownDirection = kNone; + PlayerDirection GetBotInput(sf::Vector2f snakeHeadLocation, sf::Vector2f foodLocation) + { + sf::Vector2f directionDelta; + directionDelta = snakeHeadLocation - foodLocation; + if ((directionDelta.y > 0) + && (lastKnownDirection != kDown)) + { lastKnownDirection = kUp; } + else if ((directionDelta.y < 0) + && (lastKnownDirection != kUp)) + { lastKnownDirection = kDown; } + else if ((directionDelta.x > 0) + && (lastKnownDirection != kRight)) + { lastKnownDirection = kLeft; } + else if ((directionDelta.x < 0) + && (lastKnownDirection != kLeft)) + { lastKnownDirection = kRight; } + return lastKnownDirection; + } +} diff --git a/src/botinterface.hpp b/src/botinterface.hpp new file mode 100644 index 0000000..cd2f14e --- /dev/null +++ b/src/botinterface.hpp @@ -0,0 +1,12 @@ +#ifndef BOTINTERFACE_HPP +#define BOTINTERFACE_HPP + +#include "common.hpp" +#include + +namespace snakeplusplus +{ + PlayerDirection GetBotInput(sf::Vector2f snakeHeadLocation, sf::Vector2f foodLocation); +} + +#endif diff --git a/src/gamestate.cpp b/src/gamestate.cpp index 329c5c0..035cf65 100755 --- a/src/gamestate.cpp +++ b/src/gamestate.cpp @@ -1,6 +1,7 @@ // GameState.cpp #include #include +#include "botinterface.hpp" #include "common.hpp" #include "playerinterface.hpp" #include "gamestate.hpp" @@ -34,7 +35,7 @@ namespace snakeplusplus { while (graphics.IsOpen()) { - if (isGameOver) {Reset();} + if (isGameOver) { Reset(); } UpdatePlayerSpeed(); PlaceNewSnakePart(MovePlayer()); RegenerateFood(); @@ -113,7 +114,10 @@ namespace snakeplusplus void GameEngine::UpdatePlayerSpeed(void) { - switch (GetPlayerInput()) { + PlayerDirection controller; + if (isBotControlled) { controller = GetBotInput(player.headLocation, playerFood.location); } + else { controller = GetPlayerInput(); } + switch (controller) { case kUp: if (player.speed.y == 1) { break; } player.speed.x = 0; diff --git a/src/gamestate.hpp b/src/gamestate.hpp index 29763a4..baf8b80 100755 --- a/src/gamestate.hpp +++ b/src/gamestate.hpp @@ -21,6 +21,7 @@ namespace snakeplusplus Snake player; Food playerFood; bool isGameOver = 0; + bool isBotControlled = 1; void DisplayEndScreen(void); void Loop(void); sf::Vector2f MovePlayer(void); diff --git a/src/playerinterface.hpp b/src/playerinterface.hpp index 26e12f5..a4de760 100755 --- a/src/playerinterface.hpp +++ b/src/playerinterface.hpp @@ -29,7 +29,7 @@ namespace snakeplusplus sf::VideoMode gameVideoSettings; sf::RectangleShape drawObject; bool isWindowAlive; - sf::Time delay = sf::milliseconds(60); + sf::Time delay = sf::milliseconds(20); }; }