2022-08-15 23:51:18 -05:00
|
|
|
// Snake.cpp
|
2022-07-25 15:40:45 -05:00
|
|
|
#include <iostream>
|
2022-08-21 02:51:33 -05:00
|
|
|
#include <queue>
|
2022-07-25 15:40:45 -05:00
|
|
|
#include <SFML\Graphics.hpp>
|
2023-03-12 21:57:46 -05:00
|
|
|
#include "Common.h"
|
2022-07-25 15:40:45 -05:00
|
|
|
#include "Snake.h"
|
2022-08-15 23:51:18 -05:00
|
|
|
#include "SnakeFood.h"
|
2022-07-25 15:40:45 -05:00
|
|
|
|
2023-03-12 08:50:50 -05:00
|
|
|
// General constructor for snake class
|
2023-03-12 21:57:46 -05:00
|
|
|
Snake::Snake(void)
|
2023-03-12 08:50:50 -05:00
|
|
|
{
|
|
|
|
sf::RectangleShape newBodyPart(sf::Vector2f(kGridSize, kGridSize));
|
|
|
|
newBodyPart.setFillColor(sf::Color::Green);
|
|
|
|
snakeBody.push_back(newBodyPart);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Constructor for snake with position
|
|
|
|
Snake::Snake(sf::Vector2f headSize)
|
|
|
|
{
|
|
|
|
sf::RectangleShape newBodyPart(headSize);
|
|
|
|
newBodyPart.setFillColor(sf::Color::Green);
|
|
|
|
snakeBody.push_back(newBodyPart);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-03-12 21:57:46 -05:00
|
|
|
// Iterate through snake deque and draw to window
|
|
|
|
void Snake::DisplaySnake(sf::RenderWindow& window)
|
2022-08-21 02:51:33 -05:00
|
|
|
{
|
2023-03-12 21:57:46 -05:00
|
|
|
for (auto snakeBodyPart = snakeBody.cbegin(); snakeBodyPart != snakeBody.cend(); ++snakeBodyPart)
|
|
|
|
window.draw(*snakeBodyPart);
|
|
|
|
return;
|
2022-08-21 02:51:33 -05:00
|
|
|
}
|
|
|
|
|
2023-03-12 21:57:46 -05:00
|
|
|
// Return the RectangleShape head of snake
|
|
|
|
sf::RectangleShape Snake::GetSnakeHead(void)
|
2022-08-21 02:51:33 -05:00
|
|
|
{
|
2023-03-12 21:57:46 -05:00
|
|
|
sf::RectangleShape head;
|
|
|
|
head = snakeBody.front();
|
|
|
|
return head;
|
2022-08-21 02:51:33 -05:00
|
|
|
}
|
|
|
|
|
2023-03-12 21:57:46 -05:00
|
|
|
// Return the Vector2f head of snake
|
|
|
|
sf::Vector2f Snake::GetSnakeHeadPosition(void)
|
2022-07-25 15:40:45 -05:00
|
|
|
{
|
2023-03-12 21:57:46 -05:00
|
|
|
sf::Vector2f position;
|
|
|
|
position = snakeBody.front().getPosition();
|
|
|
|
return position;
|
2022-07-26 19:47:08 -05:00
|
|
|
}
|
|
|
|
|
2023-03-12 21:57:46 -05:00
|
|
|
// Checks if snake position matches object position
|
|
|
|
bool Snake::IsTouchingObject(sf::RectangleShape object)
|
2022-07-26 19:47:08 -05:00
|
|
|
{
|
2023-03-12 21:57:46 -05:00
|
|
|
for (auto snakeBodyPart = snakeBody.cbegin(); snakeBodyPart != snakeBody.cend(); ++snakeBodyPart)
|
|
|
|
{
|
|
|
|
if ((*snakeBodyPart).getPosition().x != object.getPosition().x)
|
|
|
|
continue;
|
|
|
|
if ((*snakeBodyPart).getPosition().y != object.getPosition().y)
|
|
|
|
continue;
|
2022-07-28 14:56:54 -05:00
|
|
|
return true;
|
2023-03-12 21:57:46 -05:00
|
|
|
}
|
2022-07-28 14:56:54 -05:00
|
|
|
return false;
|
2022-07-26 19:03:55 -05:00
|
|
|
}
|
|
|
|
|
2023-03-12 21:57:46 -05:00
|
|
|
// Move snake based on direction and check for collision
|
|
|
|
void Snake::MoveSnake(SnakeFood* snakeFood)
|
2022-07-25 15:40:45 -05:00
|
|
|
{
|
2023-03-12 21:57:46 -05:00
|
|
|
// TODO: Add losing on wall collision
|
|
|
|
if (CheckBoundaries()) // Wall collision
|
2022-08-10 20:05:28 -05:00
|
|
|
return;
|
2023-03-12 21:57:46 -05:00
|
|
|
sf::Vector2f newHeadPosition;
|
|
|
|
newHeadPosition = CalculateNewPosition(GetSnakeHeadPosition());
|
2023-03-12 08:50:50 -05:00
|
|
|
sf::RectangleShape newBodyPart(sf::Vector2f(kGridSize, kGridSize));
|
2022-07-26 19:24:46 -05:00
|
|
|
newBodyPart.setPosition(newHeadPosition);
|
2023-03-12 21:57:46 -05:00
|
|
|
// TODO: Add losing on self collision
|
|
|
|
if (IsSelfCollision(newBodyPart)) // Snake collision
|
2022-07-28 14:56:54 -05:00
|
|
|
return;
|
|
|
|
newBodyPart.setFillColor(sf::Color::Green);
|
2022-07-27 15:07:28 -05:00
|
|
|
snakeBody.push_front(newBodyPart);
|
2023-03-12 21:57:46 -05:00
|
|
|
if (!GlobalCollision(GetSnakeHeadPosition(), snakeFood->GetFoodObjectPosition()))
|
2022-07-27 15:07:28 -05:00
|
|
|
snakeBody.pop_back();
|
2022-07-25 16:12:11 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-03-12 21:57:46 -05:00
|
|
|
void Snake::UpdateDirection(int newDirection)
|
2022-07-26 16:56:26 -05:00
|
|
|
{
|
2023-03-12 21:57:46 -05:00
|
|
|
snakeDirection = newDirection;
|
|
|
|
return;
|
2022-07-26 16:56:26 -05:00
|
|
|
}
|
|
|
|
|
2023-03-12 21:57:46 -05:00
|
|
|
// Get a new coordinate position based on snake direction
|
|
|
|
sf::Vector2f Snake::CalculateNewPosition(sf::Vector2f position)
|
2022-07-26 19:03:55 -05:00
|
|
|
{
|
2023-03-12 21:57:46 -05:00
|
|
|
if (snakeDirection == 0)
|
|
|
|
return position;
|
|
|
|
if (snakeDirection == kLeft)
|
|
|
|
position.x -= kGridSize;
|
|
|
|
if (snakeDirection == kUp)
|
|
|
|
position.y -= kGridSize;
|
|
|
|
if (snakeDirection == kDown)
|
|
|
|
position.y += kGridSize;
|
|
|
|
if (snakeDirection == kRight)
|
|
|
|
position.x += kGridSize;
|
|
|
|
return position;
|
2022-07-26 19:03:55 -05:00
|
|
|
}
|
|
|
|
|
2023-03-12 21:57:46 -05:00
|
|
|
// Check snake head for running into boundaries
|
|
|
|
bool Snake::CheckBoundaries(void)
|
2022-07-26 19:03:55 -05:00
|
|
|
{
|
2023-03-12 21:57:46 -05:00
|
|
|
if (snakeBody.front().getPosition().x == 0 && snakeDirection == kLeft)
|
|
|
|
return true;
|
|
|
|
if (snakeBody.front().getPosition().y == 0 && snakeDirection == kUp)
|
|
|
|
return true;
|
|
|
|
// TODO: Change boundaries to not be hard-coded
|
|
|
|
if (snakeBody.front().getPosition().y > 675 && snakeDirection == kDown)
|
|
|
|
return true;
|
|
|
|
if (snakeBody.front().getPosition().x > 975 && snakeDirection == kRight)
|
|
|
|
return true;
|
|
|
|
return false;
|
2022-07-26 19:03:55 -05:00
|
|
|
}
|
|
|
|
|
2022-07-28 14:56:54 -05:00
|
|
|
// Test for snake self collision
|
|
|
|
bool Snake::IsSelfCollision(sf::RectangleShape testRectangle)
|
2022-08-15 23:51:18 -05:00
|
|
|
{
|
2023-03-12 08:50:50 -05:00
|
|
|
for (auto snakeBodyPart = snakeBody.cbegin(); snakeBodyPart != snakeBody.cend(); ++snakeBodyPart)
|
2023-03-12 21:57:46 -05:00
|
|
|
if (GlobalCollision(testRectangle.getPosition(), (*snakeBodyPart).getPosition()))
|
|
|
|
return true;
|
2023-03-12 08:50:50 -05:00
|
|
|
return false;
|
2022-07-26 16:56:26 -05:00
|
|
|
}
|