So having trouble setting up this sliding puzzle game where I have one square that matches the background and trying to make the squares move. I know about the keyboard input and tried to implement it but I am unsure how to connect that to my squares? Should I make a new class to move these square or how should I approach it? How can I that connection for it?
this is my classes for the rest of the game I have made except for the buttons as they work fine and the text object as well
class States
{
private:
std::vector<bool> objectStates;
public:
States();
enum states {HOVERED, NORMAL, HIDDEN, LAST};
bool checkState(states state) const;
void enableState(states state);
void disableState(states state);
};
States::States()
{
for(int i=0; i<LAST; i++)
objectStates.push_back(false);
}
bool States::checkState(states state) const
{
return objectStates[state];
}
void States::enableState(states state)
{
objectStates[state] = true;
}
void States::disableState(states state)
{
objectStates[state] = false;
}
#include <SFML/Graphics.hpp>
#include "States.h"
#include "LinkedListPiece.h"
class Rectangle : public sf::Drawable, public States
{
private:
sf::RectangleShape background;
sf::RectangleShape rectangle;
public:
Rectangle();
void center();
void draw(sf::RenderTarget& window, sf::RenderStates states) const;
void addEventHandler(sf::RenderWindow &window, sf::Event event);
void update();
void setSize(sf::Vector2f size);
void setPosition(sf::Vector2f position);
sf::FloatRect getGlobalBounds();
};
#include "Rectangle.h"
Rectangle::Rectangle() { enableState(HIDDEN);}
void Rectangle::update()
{
}
void Rectangle::draw(sf::RenderTarget& window, sf::RenderStates states) const
{
if(!checkState(HIDDEN))
window.draw(rectangle);
}
void Rectangle::center()
{
}
void Rectangle::addEventHandler(sf::RenderWindow &window, sf::Event event)
{
}
void Rectangle::setSize(sf::Vector2f size)
{
rectangle.setSize(size);
}
void Rectangle::setPosition(sf::Vector2f position)
{
rectangle.setPosition(position);
}
sf::FloatRect Rectangle::getGlobalBounds()
{
return background.getGlobalBounds();
}
#include <SFML/Graphics.hpp>
#include "Button.h"
#include "ButtonTwo.h"
#include "Fonts.h"
#include "Rectangle.h"
class MainGame
{
private:
sf::RenderWindow window;
Button button;
ButtonTwo exitButton;
Rectangle r;
Rectangle rTwo;
Rectangle rThree;
Rectangle rFour;
sf::Text title;
WindowDialog dialog;
public:
MainGame();
void run();
};
MainGame::MainGame() : window({1920, 1080, 32}, "SFML Final Project")
{
button.setOrigin({button.getGlobalBounds().width/2,
button.getGlobalBounds().height/2});
button.setPosition({window.getSize().x/2.f,
window.getSize().y/2.f});
exitButton.setOrigin({button.getGlobalBounds().width/2,
button.getGlobalBounds().height/2});
exitButton.setPosition({window.getSize().x/2.f,
window.getSize().y/1.5f});
title.setFont(Fonts::getFont());
title.setOrigin(title.getGlobalBounds().width/2,
title.getGlobalBounds().height/2);
title.setPosition(window.getSize().x/5.5f,
window.getSize().y/9.5f);
title.setFillColor(sf::Color::Blue);
title.setCharacterSize(150);
title.setString("Final Siding Puzzle Game");
r.setSize({100, 100});
r.setPosition({window.getSize().x / 2.f,
window.getSize().y / 2.f});
rTwo.setSize({100,100});
rTwo.setPosition({window.getSize().x/2.25f,
window.getSize().y/2.f});
rThree.setSize({100, 100});
rThree.setPosition({window.getSize().x / 2.f,
window.getSize().y / 2.48f});
rFour.setSize({100,100});
rFour.setPosition({window.getSize().x/2.25f,
window.getSize().y/2.48f});
}
void MainGame::run()
{
while(window.isOpen())
{
sf::Event event;
while(window.pollEvent(event))
{
if(event.type == sf::Event::Closed)
window.close();
sf::FloatRect bounds = button.getGlobalBounds();
sf::FloatRect boundsTwo = exitButton.getGlobalBounds();
sf::Vector2f mPos = (sf::Vector2f) sf::Mouse::getPosition(window);
if(sf::Mouse::isButtonPressed(sf::Mouse::Left) && bounds.contains(mPos))
{
//this will set the rectangle to a fade state
button.enableState(States::HIDDEN);
exitButton.enableState(States::HIDDEN);
r.disableState(States::HIDDEN);
rTwo.disableState(States::HIDDEN);
rThree.disableState(States::HIDDEN);
rFour.disableState(States::HIDDEN);
}
else if (sf::Mouse::isButtonPressed(sf::Mouse::Left) && boundsTwo.contains(mPos))
{
button.enableState(States::HIDDEN);
exitButton.enableState(States::HIDDEN);
}
//other events
button.addEventHandler(window, event);
exitButton.addEventHandler(window, event);
}
button.update();
window.clear(sf::Color(53, 189, 164));
window.draw(button);
window.draw(exitButton);
window.draw(title);
window.draw(r);
window.draw(rTwo);
window.draw(rThree);
window.draw(rFour);
window.display();
}
}
and my main
#include <iostream>
#include <SFML/Graphics.hpp>
#include "MainGame.h"
int main()
{
MainGame game;
game.run();
return 0;
}