2022-07-25 15:40:45 -05:00
|
|
|
#include <iostream>
|
|
|
|
#include <SFML\Graphics.hpp>
|
|
|
|
#include <SFML\System.hpp>
|
|
|
|
#include "Snake.h"
|
|
|
|
|
2022-07-25 16:06:06 -05:00
|
|
|
// Test for collision between two objects
|
2022-07-25 15:40:45 -05:00
|
|
|
bool SnakeCollision(sf::RectangleShape object1, sf::RectangleShape object2)
|
|
|
|
{
|
2022-07-25 16:06:06 -05:00
|
|
|
// Hack for getting a temporary collision
|
|
|
|
// Collision only tested for origin corrordinate
|
2022-07-25 15:40:45 -05:00
|
|
|
sf::Vector2f object1Position = object1.getPosition();
|
|
|
|
sf::Vector2f object2Position = object2.getPosition();
|
|
|
|
if (object1Position.x != object2Position.x)
|
|
|
|
return 0;
|
|
|
|
if (object1Position.y != object2Position.y)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-07-25 16:06:06 -05:00
|
|
|
// Move snake head piece
|
2022-07-26 19:03:55 -05:00
|
|
|
int SnakeMovement()
|
2022-07-25 15:40:45 -05:00
|
|
|
{
|
2022-07-26 19:03:55 -05:00
|
|
|
std::cout << "Test text \n";
|
|
|
|
int snakeDirection = 0;
|
|
|
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
|
|
|
|
snakeDirection = 1;
|
|
|
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
|
|
|
|
snakeDirection = 2;
|
|
|
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
|
|
|
|
snakeDirection = 3;
|
|
|
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
|
|
|
|
snakeDirection = 4;
|
|
|
|
return snakeDirection;
|
|
|
|
}
|
|
|
|
|
|
|
|
sf::Vector2f CalculateNewPosition(int direction, sf::Vector2f position)
|
|
|
|
{
|
|
|
|
if (direction == 0)
|
|
|
|
return position;
|
|
|
|
if (direction == 1)
|
|
|
|
position.x -= 25;
|
|
|
|
if (direction == 2)
|
|
|
|
position.y -= 25;
|
|
|
|
if (direction == 3)
|
|
|
|
position.y += 25;
|
|
|
|
if (direction == 4)
|
|
|
|
position.x += 25;
|
|
|
|
return position;
|
2022-07-25 15:40:45 -05:00
|
|
|
}
|
|
|
|
|
2022-07-25 16:06:06 -05:00
|
|
|
void Snake::ExtendSnake()
|
2022-07-25 15:40:45 -05:00
|
|
|
{
|
2022-07-25 16:12:11 -05:00
|
|
|
// Create a new deque RectangleShape without popping old
|
2022-07-25 15:40:45 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-26 19:03:55 -05:00
|
|
|
void Snake::MoveSnake(int snakeDirection)
|
2022-07-25 15:40:45 -05:00
|
|
|
{
|
2022-07-25 16:12:11 -05:00
|
|
|
// Create a new deque RectangleShape and pop old
|
|
|
|
// Todo: Depreciate ExtendSnake and just add a collision test
|
2022-07-26 19:03:55 -05:00
|
|
|
sf::Vector2f newHeadPosition;
|
|
|
|
newHeadPosition = GetSnakeHeadPosition();
|
|
|
|
newHeadPosition = CalculateNewPosition(snakeDirection, newHeadPosition);
|
|
|
|
sf::RectangleShape newBodyPart(newHeadPosition);
|
|
|
|
snakeBody.push_back(newBodyPart);
|
|
|
|
// snakeBody.pop_front();
|
2022-07-25 16:12:11 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-26 16:56:26 -05:00
|
|
|
// Get x and y position of snake head
|
2022-07-26 19:03:55 -05:00
|
|
|
sf::Vector2f Snake::GetSnakeHeadPosition()
|
2022-07-26 16:56:26 -05:00
|
|
|
{
|
|
|
|
sf::Vector2f position;
|
|
|
|
position = snakeBody.front().getPosition();
|
|
|
|
return position;
|
|
|
|
}
|
|
|
|
|
2022-07-26 19:03:55 -05:00
|
|
|
sf::RectangleShape Snake::GetSnakeHead()
|
|
|
|
{
|
|
|
|
sf::RectangleShape head;
|
|
|
|
head = snakeBody.front();
|
|
|
|
return head;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Snake::DisplaySnake(sf::RenderWindow& window)
|
|
|
|
{
|
|
|
|
for (auto it = snakeBody.cbegin(); it != snakeBody.cend(); ++it)
|
|
|
|
{
|
|
|
|
window.draw(*it);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-25 16:12:11 -05:00
|
|
|
Snake::Snake()
|
|
|
|
{
|
|
|
|
// Possibly unnecessary
|
|
|
|
// The big 3 could be used to create a fresh game state
|
2022-07-25 15:40:45 -05:00
|
|
|
return;
|
|
|
|
}
|
2022-07-26 16:56:26 -05:00
|
|
|
Snake::Snake(sf::Vector2f head)
|
|
|
|
{
|
|
|
|
sf::RectangleShape newBodyPart(head);
|
|
|
|
snakeBody.push_back(newBodyPart);
|
|
|
|
return;
|
|
|
|
}
|
2022-07-25 15:43:42 -05:00
|
|
|
// SnakeNode::SnakeNode();
|
|
|
|
// SnakeNode::SnakeNode(sf::Vector2f addBodyPiece);
|