#include <SFML/Graphics.hpp>
#include <cmath>
#include <iostream>
#define window_size_x 1200
#define window_size_y 800
#define max_speed 500
using namespace std;
void BoxCollision(sf::Sprite & sprite_1, sf::Sprite & sprite_2, float x, float y)
{
float sprite_1_top = sprite_1.getPosition().y - sprite_1.getOrigin().y;
float sprite_1_bottom = sprite_1.getPosition().y - sprite_1.getOrigin().y + sprite_1.getTexture()->getSize().y;
float sprite_1_left = sprite_1.getPosition().x - sprite_1.getOrigin().x;
float sprite_1_right = sprite_1.getPosition().x - sprite_1.getOrigin().x + sprite_1.getTexture()->getSize().x;
float sprite_1_previous_top = y - sprite_1.getOrigin().y;
float sprite_1_previous_bottom = y - sprite_1.getOrigin().y + sprite_1.getTexture()->getSize().y;
float sprite_1_previous_left = x - sprite_1.getOrigin().x;
float sprite_1_previous_right = x - sprite_1.getOrigin().x + sprite_1.getTexture()->getSize().x;
float sprite_2_top = sprite_2.getPosition().y - sprite_2.getOrigin().y;
float sprite_2_bottom = sprite_2.getPosition().y - sprite_2.getOrigin().y + sprite_2.getTexture()->getSize().y;
float sprite_2_left = sprite_2.getPosition().x - sprite_2.getOrigin().x;
float sprite_2_right = sprite_2.getPosition().x - sprite_2.getOrigin().x + sprite_2.getTexture()->getSize().x;
if (sprite_1_top < sprite_2_bottom &&
sprite_1_bottom > sprite_2_top &&
sprite_1_left < sprite_2_right &&
sprite_1_right > sprite_2_left)
{
if (sprite_1_previous_top >= sprite_2_bottom)
sprite_1.setPosition(sprite_1.getPosition().x, sprite_2_bottom + sprite_1.getOrigin().y);
if ((sprite_1_previous_left >= sprite_2_right && sprite_1_previous_bottom <= sprite_2_top) || (sprite_1_previous_right <= sprite_2_left && sprite_1_previous_bottom <= sprite_2_top))
{
cout << "yep" << endl;
sprite_1.setPosition(sprite_1.getPosition().x, sprite_2_top - sprite_1.getTexture()->getSize().y + sprite_1.getOrigin().y);
}
else
{
if (sprite_1_previous_bottom <= sprite_2_top)
sprite_1.setPosition(sprite_1.getPosition().x, sprite_2_top - sprite_1.getTexture()->getSize().y + sprite_1.getOrigin().y);
if (sprite_1_previous_left >= sprite_2_right)
sprite_1.setPosition(sprite_2_right + sprite_1.getOrigin().x, sprite_1.getPosition().y);
if (sprite_1_previous_right <= sprite_2_left)
sprite_1.setPosition(sprite_2_left - sprite_1.getTexture()->getSize().x + sprite_1.getOrigin().x, sprite_1.getPosition().y);
}
}
}
int main()
{
sf::RenderWindow window(sf::VideoMode(window_size_x, window_size_y), "Ambient");
//window.setVerticalSyncEnabled(true);
//window.setFramerateLimit(10);
sf::Texture background_texture;
background_texture.loadFromFile("Scene.png");
sf::Texture box_texture;
box_texture.loadFromFile("Box_1.png");
sf::Texture ramp_texture;
ramp_texture.loadFromFile("LB.png");
sf::Texture triangle_texture;
triangle_texture.loadFromFile("Triangle.png");
sf::Sprite background;
background.setTexture(background_texture);
background.setPosition(window_size_x / 2.0f, window_size_y / 2.0f);
background.setOrigin(background_texture.getSize().x / 2.0f, background_texture.getSize().y / 2.0f);
sf::Sprite box_1;
box_1.setTexture(box_texture);
box_1.setPosition(100, 50);
sf::Sprite box_2;
box_2.setTexture(box_texture);
box_2.setPosition(250, 50);
sf::Sprite box_3;
box_3.setTexture(box_texture);
box_3.setPosition(400, 50);
sf::Sprite box_4;
box_4.setTexture(box_texture);
box_4.setPosition(550, 50);
sf::Sprite box_5;
box_5.setTexture(box_texture);
box_5.setPosition(700, 50);
sf::Sprite box_6;
box_6.setTexture(box_texture);
box_6.setPosition(850, 50);
sf::Sprite box_7;
box_7.setTexture(box_texture);
box_7.setPosition(1000, 50);
sf::Sprite box_8;
box_8.setTexture(box_texture);
box_8.setPosition(1000, 200);
sf::Sprite box_9;
box_9.setTexture(box_texture);
box_9.setPosition(1000, 350);
sf::Sprite box_10;
box_10.setTexture(box_texture);
box_10.setPosition(1000, 500);
sf::Sprite box_11;
box_11.setTexture(box_texture);
box_11.setPosition(1000, 650);
sf::Sprite box_12;
box_12.setTexture(box_texture);
box_12.setPosition(850, 650);
sf::Sprite box_13;
box_13.setTexture(box_texture);
box_13.setPosition(700, 650);
sf::Sprite box_14;
box_14.setTexture(box_texture);
box_14.setPosition(550, 650);
sf::Sprite box_15;
box_15.setTexture(box_texture);
box_15.setPosition(400, 650);
sf::Sprite box_16;
box_16.setTexture(box_texture);
box_16.setPosition(250, 650);
sf::Sprite box_17;
box_17.setTexture(box_texture);
box_17.setPosition(100, 650);
sf::Sprite box_18;
box_18.setTexture(box_texture);
box_18.setPosition(100, 500);
sf::Sprite box_19;
box_19.setTexture(box_texture);
box_19.setPosition(100, 350);
sf::Sprite box_20;
box_20.setTexture(box_texture);
box_20.setPosition(100, 200);
sf::Sprite ramp;
ramp.setTexture(ramp_texture);
ramp.setPosition(550, 400);
sf::Sprite triangle;
triangle.setTexture(triangle_texture);
triangle.setPosition(600, 350);
triangle.setOrigin(triangle_texture.getSize().x / 2.0f, triangle_texture.getSize().y / 2.0f);
sf::Clock clock;
float delta = 0;
float previous_x = 0;
float previous_y = 0;
while (window.isOpen())
{
delta = clock.restart().asSeconds();
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed || event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)
window.close();
}
previous_x = triangle.getPosition().x;
previous_y = triangle.getPosition().y;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) triangle.move(0, -max_speed * delta);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) triangle.move(0, max_speed * delta);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) triangle.move(-max_speed * delta, 0);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) triangle.move(max_speed * delta, 0);
BoxCollision(triangle, box_1, previous_x, previous_y);
BoxCollision(triangle, box_2, previous_x, previous_y);
BoxCollision(triangle, box_3, previous_x, previous_y);
BoxCollision(triangle, box_4, previous_x, previous_y);
BoxCollision(triangle, box_5, previous_x, previous_y);
BoxCollision(triangle, box_6, previous_x, previous_y);
BoxCollision(triangle, box_7, previous_x, previous_y);
BoxCollision(triangle, box_8, previous_x, previous_y);
BoxCollision(triangle, box_9, previous_x, previous_y);
BoxCollision(triangle, box_10, previous_x, previous_y);
BoxCollision(triangle, box_11, previous_x, previous_y);
BoxCollision(triangle, box_12, previous_x, previous_y);
BoxCollision(triangle, box_13, previous_x, previous_y);
BoxCollision(triangle, box_14, previous_x, previous_y);
BoxCollision(triangle, box_15, previous_x, previous_y);
BoxCollision(triangle, box_16, previous_x, previous_y);
BoxCollision(triangle, box_17, previous_x, previous_y);
BoxCollision(triangle, box_18, previous_x, previous_y);
BoxCollision(triangle, box_19, previous_x, previous_y);
BoxCollision(triangle, box_20, previous_x, previous_y);
window.clear();
window.draw(background);
window.draw(box_1);
window.draw(box_2);
window.draw(box_3);
window.draw(box_4);
window.draw(box_5);
window.draw(box_6);
window.draw(box_7);
window.draw(box_8);
window.draw(box_9);
window.draw(box_10);
window.draw(box_11);
window.draw(box_12);
window.draw(box_13);
window.draw(box_14);
window.draw(box_15);
window.draw(box_16);
window.draw(box_17);
window.draw(box_18);
window.draw(box_19);
window.draw(box_20);
window.draw(triangle);
window.display();
}
return 0;
}