sliding-puzzle/board.h

84 lines
2.1 KiB
C
Raw Normal View History

2020-12-13 13:45:52 -05:00
#pragma once
#include <SFML/Graphics/RenderWindow.hpp>
2021-01-08 16:14:28 -05:00
#include <SFML/Graphics/RectangleShape.hpp>
2020-12-13 13:45:52 -05:00
#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
};
2020-12-14 14:22:52 -05:00
//////////////////////////////////
2020-12-13 13:45:52 -05:00
class Board
{
public:
explicit Board();
2020-12-13 13:45:52 -05:00
~Board();
2021-01-08 16:14:28 -05:00
bool init(const std::string& path, int splitting, const sf::RenderWindow& window);
2020-12-13 13:45:52 -05:00
void draw(sf::RenderWindow& window);
2021-03-24 10:19:55 -04:00
bool isWinCondition() const;
2020-12-14 14:22:52 -05:00
2021-03-24 10:19:55 -04:00
void setCursorVisibility(bool visible);
bool tryProcessDirection(const DIRECTION& direction);
2020-12-13 13:45:52 -05:00
bool moveSelection(const DIRECTION& direction);
2021-03-24 10:19:55 -04:00
bool swapOnSelection(const DIRECTION& direction);
2020-12-14 14:22:52 -05:00
// Go to or leave from selection mode
2020-12-13 13:45:52 -05:00
void onSelectionMode();
2020-12-14 14:22:52 -05:00
2020-12-13 13:45:52 -05:00
private:
struct Cell
{
std::vector<Cell*>::size_type inital_index;
std::vector<Cell*>::size_type current_index;
2020-12-13 13:45:52 -05:00
sf::Sprite *sprite;
2020-12-14 14:22:52 -05:00
static int side_length;
2020-12-13 13:45:52 -05:00
};
2020-12-14 14:22:52 -05:00
using Cells = std::vector<Cell*>;
2020-12-13 13:45:52 -05:00
2021-01-08 16:14:28 -05:00
sf::RectangleShape rect_filling;
2020-12-13 13:45:52 -05:00
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
2020-12-13 13:45:52 -05:00
Cells vec_field;
2021-01-08 16:14:28 -05:00
Cells::size_type solved_tiles; // amount of tiles placed on initial positions
2020-12-13 13:45:52 -05:00
sf::Texture global_texture;
bool on_selection;
2021-01-08 16:14:28 -05:00
bool is_cursor_visible;
2020-12-13 13:45:52 -05:00
2020-12-14 14:22:52 -05:00
void swapCells(Cells::size_type curr_index, Cells::size_type swap_index);
// Draw selection cursor on given tile
2020-12-13 13:45:52 -05:00
void setSelectionVertex(Cells::size_type index);
2021-03-24 10:19:55 -04:00
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();
2020-12-13 13:45:52 -05:00
};