diff --git a/include/block.hpp b/include/block.hpp index 0f83240..cc9b39d 100644 --- a/include/block.hpp +++ b/include/block.hpp @@ -28,8 +28,9 @@ public: * cet objet avec un autre : la normale et la profondeur */ virtual bool getCollisionInfo(Object& obj, sf::Vector2f& normal, float& depth); - virtual bool getCollisionInfo(Ball& obj, sf::Vector2f& normal, float& depth); + virtual bool getCollisionInfo(Player& obj, sf::Vector2f& normal, float& depth); virtual bool getCollisionInfo(Block& obj, sf::Vector2f& normal, float& depth); }; #endif + diff --git a/include/engine.hpp b/include/engine.hpp index 08e28ba..b883ca8 100644 --- a/include/engine.hpp +++ b/include/engine.hpp @@ -45,3 +45,4 @@ public: }; #endif + diff --git a/include/object.hpp b/include/object.hpp index fb41581..2adf11a 100644 --- a/include/object.hpp +++ b/include/object.hpp @@ -7,7 +7,7 @@ #include "resource_manager.hpp" class Block; -class Ball; +class Player; class Object { private: @@ -56,7 +56,7 @@ public: * cet objet avec un autre : la normale et la profondeur */ virtual bool getCollisionInfo(Object& obj, sf::Vector2f& normal, float& depth); - virtual bool getCollisionInfo(Ball& obj, sf::Vector2f& normal, float& depth) = 0; + virtual bool getCollisionInfo(Player& obj, sf::Vector2f& normal, float& depth) = 0; virtual bool getCollisionInfo(Block& obj, sf::Vector2f& normal, float& depth) = 0; /** @@ -169,3 +169,4 @@ struct ObjectCompare { }; #endif + diff --git a/include/ball.hpp b/include/player.hpp similarity index 68% rename from include/ball.hpp rename to include/player.hpp index d47332c..f1ad8d1 100644 --- a/include/ball.hpp +++ b/include/player.hpp @@ -1,14 +1,15 @@ -#ifndef __PTF_BALL_HPP__ -#define __PTF_BALL_HPP__ +#ifndef __PTF_PLAYER_HPP__ +#define __PTF_PLAYER_HPP__ #include #include #include "object.hpp" #include "engine_state.hpp" -class Ball : public Object { +class Player : public Object { private: sf::Sprite sprite; + unsigned int player_number; protected: /** @@ -17,7 +18,7 @@ protected: virtual sf::Vector2f getForces(EngineState& state); public: - Ball(float x, float y); + Player(float x, float y); /** * Dessine la balle dans la fenêtre donnée @@ -34,13 +35,24 @@ public: * cet objet avec un autre : la normale et la profondeur */ virtual bool getCollisionInfo(Object& obj, sf::Vector2f& normal, float& depth); - virtual bool getCollisionInfo(Ball& obj, sf::Vector2f& normal, float& depth); + virtual bool getCollisionInfo(Player& obj, sf::Vector2f& normal, float& depth); virtual bool getCollisionInfo(Block& obj, sf::Vector2f& normal, float& depth); /** * Renvoie le rayon de la balle */ float getRadius(); + + /** + * Renvoie le numéro du joueur + */ + unsigned int getPlayerNumber(); + + /** + * Modifie le numéro du joueur + * + */ + void setPlayerNumber(unsigned int set_number); }; #endif diff --git a/res/ball.bmp b/res/ball.bmp new file mode 100644 index 0000000..f0ce621 Binary files /dev/null and b/res/ball.bmp differ diff --git a/res/block.bmp b/res/block.bmp new file mode 100644 index 0000000..e496717 Binary files /dev/null and b/res/block.bmp differ diff --git a/src/block.cpp b/src/block.cpp index c146ab9..736fc1d 100644 --- a/src/block.cpp +++ b/src/block.cpp @@ -1,5 +1,5 @@ #include "block.hpp" -#include "ball.hpp" +#include "player.hpp" #include "constants.hpp" #include "resource_manager.hpp" @@ -16,7 +16,7 @@ void Block::draw(sf::RenderWindow& window, ResourceManager& resources) { Object::draw(window, resources); // utilisation de la texture - sprite.setTexture(resources.getTexture("block.png")); + sprite.setTexture(resources.getTexture("block.bmp")); // coloration du bloc en fonction de sa charge if (getCharge() > 0) { @@ -43,8 +43,8 @@ bool Block::getCollisionInfo(Object& obj, sf::Vector2f& normal, float& depth) { return obj.getCollisionInfo(*this, normal, depth); } -bool Block::getCollisionInfo(Ball& obj, sf::Vector2f& normal, float& depth) { - // la collision Block -> Ball est la collision Ball -> Block +bool Block::getCollisionInfo(Player& obj, sf::Vector2f& normal, float& depth) { + // la collision Block -> Player est la collision Player -> Block // avec une normale de collision retournée bool result = obj.getCollisionInfo(*this, normal, depth); normal *= -1.f; @@ -87,3 +87,4 @@ bool Block::getCollisionInfo(Block& obj, sf::Vector2f& normal, float& depth) { return true; } + diff --git a/src/main.cpp b/src/main.cpp index 1c0f6b1..aa9a588 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,14 +1,17 @@ -#include "ball.hpp" +#include "player.hpp" #include "block.hpp" #include "engine.hpp" #include "constants.hpp" +#include #include int main() { Engine engine; - Ball ball1(3.5f * Constants::GRID, 10 * Constants::GRID); - Ball ball2(18.5f * Constants::GRID, 10 * Constants::GRID); + Player player1(3.5f * Constants::GRID, 10 * Constants::GRID); + player1.setPlayerNumber(1); + Player player2(18.5f * Constants::GRID, 10 * Constants::GRID); + player2.setPlayerNumber(2); Block block01(2 * Constants::GRID, 10 * Constants::GRID); Block block02(2 * Constants::GRID, 11 * Constants::GRID); Block block03(3 * Constants::GRID, 11 * Constants::GRID); @@ -32,13 +35,13 @@ int main() { Block block21(20 * Constants::GRID, 11 * Constants::GRID); Block block22(11 * Constants::GRID, 10 * Constants::GRID); - ball1.setCharge(-.01f); - ball2.setCharge(-.01f); + player1.setCharge(-.01f); + player2.setCharge(-.01f); block22.setCharge(1.f); block22.setMass(2); - engine.addObject(ball1); - engine.addObject(ball2); + engine.addObject(player1); + engine.addObject(player2); engine.addObject(block01); engine.addObject(block02); engine.addObject(block03); @@ -62,6 +65,15 @@ int main() { engine.addObject(block21); engine.addObject(block22); - engine.start(); - return 0; + try { + engine.start(); + } catch (const std::exception& exception) { + std::cerr << std::endl; + std::cerr << "Le programme a quitté après une erreur d'exécution." << std::endl; + std::cerr << exception.what() << std::endl; + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; } + diff --git a/src/object.cpp b/src/object.cpp index 7943fec..c9d1481 100644 --- a/src/object.cpp +++ b/src/object.cpp @@ -1,3 +1,4 @@ + #include "object.hpp" #include "constants.hpp" #include diff --git a/src/ball.cpp b/src/player.cpp similarity index 73% rename from src/ball.cpp rename to src/player.cpp index 6f6cd2b..f5933ad 100644 --- a/src/ball.cpp +++ b/src/player.cpp @@ -1,34 +1,54 @@ -#include "ball.hpp" +#include "player.hpp" #include "block.hpp" #include "constants.hpp" #include #include -Ball::Ball(float x, float y) : Object(x, y) { +Player::Player(float x, float y) : Object(x, y) { // déplacement de l'origine au centre de la balle sprite.setOrigin(sf::Vector2f(getRadius(), getRadius())); } -sf::Vector2f Ball::getForces(EngineState& state) { +unsigned int Player::getPlayerNumber(){ + return player_number; +} + +void Player::setPlayerNumber(unsigned int set_number){ + player_number = set_number; +} + +sf::Vector2f Player::getForces(EngineState& state) { sf::Vector2f forces = Object::getForces(state); + + //commandes du joueur 1 + if(player_number==1){ + // déplacement de la balle après appui sur les touches de direction + if (state.keys[sf::Keyboard::Left]) { + forces += sf::Vector2f(-Constants::MOVE, 0); + } - // déplacement de la balle après appui sur les touches de direction - if (state.keys[sf::Keyboard::Left]) { - forces += sf::Vector2f(-Constants::MOVE, 0); + if (state.keys[sf::Keyboard::Right]) { + forces += sf::Vector2f(Constants::MOVE, 0); + } } - - if (state.keys[sf::Keyboard::Right]) { - forces += sf::Vector2f(Constants::MOVE, 0); + else{ + if(state.keys[sf::Keyboard::Q]){ + forces += sf::Vector2f(-Constants::MOVE, 0); + } + + if(state.keys[sf::Keyboard::D]){ + forces += sf::Vector2f(Constants::MOVE, 0); + } } return forces; } -void Ball::draw(sf::RenderWindow& window, ResourceManager& resources) { +void Player::draw(sf::RenderWindow& window, ResourceManager& resources) { Object::draw(window, resources); // utilisation de la texture - sprite.setTexture(resources.getTexture("ball.png")); + sprite.setTexture(resources.getTexture("ball.bmp")); // déplacement du sprite à la position de la balle sprite.rotate(getVelocity().x * .1f); @@ -36,7 +56,7 @@ void Ball::draw(sf::RenderWindow& window, ResourceManager& resources) { window.draw(sprite); } -std::unique_ptr Ball::getAABB() { +std::unique_ptr Player::getAABB() { return std::unique_ptr(new sf::FloatRect( getPosition().x - getRadius(), getPosition().y - getRadius(), @@ -44,11 +64,11 @@ std::unique_ptr Ball::getAABB() { )); } -bool Ball::getCollisionInfo(Object& obj, sf::Vector2f& normal, float& depth) { +bool Player::getCollisionInfo(Object& obj, sf::Vector2f& normal, float& depth) { return obj.getCollisionInfo(*this, normal, depth); } -bool Ball::getCollisionInfo(Ball& obj, sf::Vector2f& normal, float& depth) { +bool Player::getCollisionInfo(Player& obj, sf::Vector2f& normal, float& depth) { sf::Vector2f dir = getPosition() - obj.getPosition(); float squaredLength = dir.x * dir.x + dir.y * dir.y; float totalRadius = getRadius() + obj.getRadius(); @@ -76,7 +96,7 @@ bool Ball::getCollisionInfo(Ball& obj, sf::Vector2f& normal, float& depth) { return true; } -bool Ball::getCollisionInfo(Block& obj, sf::Vector2f& normal, float& depth) { +bool Player::getCollisionInfo(Block& obj, sf::Vector2f& normal, float& depth) { // recherche du point le plus proche du centre de la // balle sur le bloc. On regarde la position relative // du cercle par rapport au bloc @@ -152,6 +172,6 @@ bool Ball::getCollisionInfo(Block& obj, sf::Vector2f& normal, float& depth) { return true; } -float Ball::getRadius() { +float Player::getRadius() { return 10 * getMass(); }