You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.1 KiB
C++

#pragma once
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/Graphics/VertexArray.hpp>
#include <SFML/Graphics/Texture.hpp>
#include <SFML/Graphics/Sprite.hpp>
#include <array>
using pos = std::pair<int, int>;
enum class DIRECTION
{
UP,
DOWN,
RIGHT,
LEFT,
NONE
};
//////////////////////////////////
class Board
{
public:
explicit Board();
~Board();
bool init(const std::string& path, int splitting, const sf::RenderWindow& window);
void draw(sf::RenderWindow& window);
bool isWinCondition() const;
void setCursorVisibility(bool visible);
bool tryProcessDirection(const DIRECTION& direction);
bool moveSelection(const DIRECTION& direction);
bool swapOnSelection(const DIRECTION& direction);
// Go to or leave from selection mode
void onSelectionMode();
private:
struct Cell
{
std::vector<Cell*>::size_type inital_index;
std::vector<Cell*>::size_type current_index;
sf::Sprite *sprite;
static int side_length;
};
using Cells = std::vector<Cell*>;
sf::RectangleShape rect_filling;
sf::VertexArray rect_selection;
Cells::size_type selection_index;
Cells::size_type cells_on_width; // amount of cells on horizontal side of board
Cells::size_type cells_on_height; // amount of cells on vertical side of board
Cells vec_field;
Cells::size_type solved_tiles; // amount of tiles placed on initial positions
sf::Texture global_texture;
bool on_selection;
bool is_cursor_visible;
void swapCells(Cells::size_type curr_index, Cells::size_type swap_index);
// Draw selection cursor on given tile
void setSelectionVertex(Cells::size_type index);
void calculateBoardProperties(int splitting);
void splitImageIntoTiles(int tile_length);
void scaleImageToWindow(const sf::RenderWindow &window);
float calculateScalingToWindow(const sf::RenderWindow &window) const;
void scaleTiles(float scaling);
std::pair<float, float> calculateTileShiftVector(Cells::size_type tile_index, int shift) const;
void shuffleTiles();
};