Added bot controls, bot still has poor pathing
This commit is contained in:
parent
197311831f
commit
d2e1424e7d
@ -6,6 +6,7 @@ add_executable(snakeplusplus
|
|||||||
./snake.cpp
|
./snake.cpp
|
||||||
./playerinterface.cpp
|
./playerinterface.cpp
|
||||||
./common.cpp
|
./common.cpp
|
||||||
|
./botinterface.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
target_include_directories(snakeplusplus PUBLIC ${CMAKE_CURRENT_LIST_DIR})
|
target_include_directories(snakeplusplus PUBLIC ${CMAKE_CURRENT_LIST_DIR})
|
||||||
|
26
src/botinterface.cpp
Normal file
26
src/botinterface.cpp
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
#include "botinterface.hpp"
|
||||||
|
#include "common.hpp"
|
||||||
|
#include <SFML/System/Vector2.hpp>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
12
src/botinterface.hpp
Normal file
12
src/botinterface.hpp
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
#ifndef BOTINTERFACE_HPP
|
||||||
|
#define BOTINTERFACE_HPP
|
||||||
|
|
||||||
|
#include "common.hpp"
|
||||||
|
#include <SFML/System/Vector2.hpp>
|
||||||
|
|
||||||
|
namespace snakeplusplus
|
||||||
|
{
|
||||||
|
PlayerDirection GetBotInput(sf::Vector2f snakeHeadLocation, sf::Vector2f foodLocation);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
@ -1,6 +1,7 @@
|
|||||||
// GameState.cpp
|
// GameState.cpp
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <SFML/Graphics.hpp>
|
#include <SFML/Graphics.hpp>
|
||||||
|
#include "botinterface.hpp"
|
||||||
#include "common.hpp"
|
#include "common.hpp"
|
||||||
#include "playerinterface.hpp"
|
#include "playerinterface.hpp"
|
||||||
#include "gamestate.hpp"
|
#include "gamestate.hpp"
|
||||||
@ -113,7 +114,10 @@ namespace snakeplusplus
|
|||||||
|
|
||||||
void GameEngine::UpdatePlayerSpeed(void)
|
void GameEngine::UpdatePlayerSpeed(void)
|
||||||
{
|
{
|
||||||
switch (GetPlayerInput()) {
|
PlayerDirection controller;
|
||||||
|
if (isBotControlled) { controller = GetBotInput(player.headLocation, playerFood.location); }
|
||||||
|
else { controller = GetPlayerInput(); }
|
||||||
|
switch (controller) {
|
||||||
case kUp:
|
case kUp:
|
||||||
if (player.speed.y == 1) { break; }
|
if (player.speed.y == 1) { break; }
|
||||||
player.speed.x = 0;
|
player.speed.x = 0;
|
||||||
|
@ -21,6 +21,7 @@ namespace snakeplusplus
|
|||||||
Snake player;
|
Snake player;
|
||||||
Food playerFood;
|
Food playerFood;
|
||||||
bool isGameOver = 0;
|
bool isGameOver = 0;
|
||||||
|
bool isBotControlled = 1;
|
||||||
void DisplayEndScreen(void);
|
void DisplayEndScreen(void);
|
||||||
void Loop(void);
|
void Loop(void);
|
||||||
sf::Vector2f MovePlayer(void);
|
sf::Vector2f MovePlayer(void);
|
||||||
|
@ -29,7 +29,7 @@ namespace snakeplusplus
|
|||||||
sf::VideoMode gameVideoSettings;
|
sf::VideoMode gameVideoSettings;
|
||||||
sf::RectangleShape drawObject;
|
sf::RectangleShape drawObject;
|
||||||
bool isWindowAlive;
|
bool isWindowAlive;
|
||||||
sf::Time delay = sf::milliseconds(60);
|
sf::Time delay = sf::milliseconds(20);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user