diff --git a/include/collision.hpp b/include/collision.hpp index e671903..7f17c37 100644 --- a/include/collision.hpp +++ b/include/collision.hpp @@ -11,9 +11,9 @@ namespace Collision { typedef std::map, collision_data> collision_dispatcher; extern collision_dispatcher dispatch; - bool ballToBlock(Object& objA, Object& objB, sf::Vector2f& normal, float& depth); - bool blockToBall(Object& objA, Object& objB, sf::Vector2f& normal, float& depth); - bool ballToBall(Object& objA, Object& objB, sf::Vector2f& normal, float& depth); + bool playerToBlock(Object& objA, Object& objB, sf::Vector2f& normal, float& depth); + bool blockToPlayer(Object& objA, Object& objB, sf::Vector2f& normal, float& depth); + bool playerToPlayer(Object& objA, Object& objB, sf::Vector2f& normal, float& depth); bool blockToBlock(Object& objA, Object& objB, sf::Vector2f& normal, float& depth); } 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 0d14471..42ba13e 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: diff --git a/include/ball.hpp b/include/player.hpp similarity index 69% rename from include/ball.hpp rename to include/player.hpp index e1aba7b..ac3a94e 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 @@ -39,6 +40,16 @@ public: * 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/ball.cpp b/src/ball.cpp deleted file mode 100644 index f96efbb..0000000 --- a/src/ball.cpp +++ /dev/null @@ -1,53 +0,0 @@ -#include "ball.hpp" -#include "block.hpp" -#include "constants.hpp" -#include -#include - -Ball::Ball(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) { - sf::Vector2f forces = Object::getForces(state); - - // 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); - } - - return forces; -} - -void Ball::draw(sf::RenderWindow& window, ResourceManager& resources) { - Object::draw(window, resources); - - // utilisation de la texture - sprite.setTexture(resources.getTexture("ball.png")); - - // déplacement du sprite à la position de la balle - sprite.rotate(getVelocity().x * .1f); - sprite.setPosition(getPosition()); - window.draw(sprite); -} - -std::unique_ptr Ball::getAABB() { - return std::unique_ptr(new sf::FloatRect( - getPosition().x - getRadius(), - getPosition().y - getRadius(), - 2 * getRadius(), 2 * getRadius() - )); -} - -unsigned int Ball::getTypeId() { - return Ball::TYPE_ID; -} - -float Ball::getRadius() { - return 10 * getMass(); -} diff --git a/src/block.cpp b/src/block.cpp index 98b98f0..77d240d 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) { diff --git a/src/collision.cpp b/src/collision.cpp index 839101b..3f9ad6c 100644 --- a/src/collision.cpp +++ b/src/collision.cpp @@ -1,5 +1,5 @@ #include "collision.hpp" -#include "ball.hpp" +#include "player.hpp" #include "block.hpp" #include "object.hpp" #include @@ -9,21 +9,21 @@ namespace Collision { // initialisation du dictionnaire associant les types // impliqués dans une collision à leur fonction de résolution collision_dispatcher dispatch = { - {std::make_pair(Ball::TYPE_ID, Block::TYPE_ID), &ballToBlock}, - {std::make_pair(Block::TYPE_ID, Ball::TYPE_ID), &blockToBall}, - {std::make_pair(Ball::TYPE_ID, Ball::TYPE_ID), &ballToBall}, + {std::make_pair(Player::TYPE_ID, Block::TYPE_ID), &ballToBlock}, + {std::make_pair(Block::TYPE_ID, Player::TYPE_ID), &blockToBall}, + {std::make_pair(Player::TYPE_ID, Player::TYPE_ID), &ballToBall}, {std::make_pair(Block::TYPE_ID, Block::TYPE_ID), &blockToBlock} }; bool ballToBlock(Object& objA, Object& objB, sf::Vector2f& normal, float& depth) { - Ball ball = dynamic_cast(objA); + Player player = dynamic_cast(objA); Block block = dynamic_cast(objB); // 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 std::unique_ptr aabb = block.getAABB(); - sf::Vector2f relpos = block.getPosition() - ball.getPosition(); + sf::Vector2f relpos = block.getPosition() - player.getPosition(); sf::Vector2f closest = relpos; // on restreint la position relative pour rester diff --git a/src/main.cpp b/src/main.cpp index f3dc232..aa9a588 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,4 @@ -#include "ball.hpp" +#include "player.hpp" #include "block.hpp" #include "engine.hpp" #include "constants.hpp" @@ -8,8 +8,10 @@ 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); @@ -33,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); @@ -74,3 +76,4 @@ int main() { return EXIT_SUCCESS; } + diff --git a/src/player.cpp b/src/player.cpp new file mode 100644 index 0000000..5b33a04 --- /dev/null +++ b/src/player.cpp @@ -0,0 +1,73 @@ +#include "player.hpp" +#include "block.hpp" +#include "constants.hpp" +#include +#include + +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 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); + } + + 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 Player::draw(sf::RenderWindow& window, ResourceManager& resources) { + Object::draw(window, resources); + + // utilisation de la texture + sprite.setTexture(resources.getTexture("ball.bmp")); + + // déplacement du sprite à la position de la balle + sprite.rotate(getVelocity().x * .1f); + sprite.setPosition(getPosition()); + window.draw(sprite); +} + +std::unique_ptr Player::getAABB() { + return std::unique_ptr(new sf::FloatRect( + getPosition().x - getRadius(), + getPosition().y - getRadius(), + 2 * getRadius(), 2 * getRadius() + )); +} + +unsigned int Player::getTypeId() { + return Player::TYPE_ID; +} + +float Player::getRadius() { + return 10 * getMass(); +} + +unsigned int Player::getPlayerNumber() { + return player_number; +} + +void Player::setPlayerNumber(unsigned int set_number) { + player_number = set_number; +}