Différenciation des deux joueurs

This commit is contained in:
maelle 2016-03-25 18:40:39 +01:00
parent e72f6eacbe
commit 1cc107ddd3
10 changed files with 86 additions and 37 deletions

View File

@ -28,8 +28,9 @@ public:
* cet objet avec un autre : la normale et la profondeur * cet objet avec un autre : la normale et la profondeur
*/ */
virtual bool getCollisionInfo(Object& obj, sf::Vector2f& normal, float& depth); 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); virtual bool getCollisionInfo(Block& obj, sf::Vector2f& normal, float& depth);
}; };
#endif #endif

View File

@ -45,3 +45,4 @@ public:
}; };
#endif #endif

View File

@ -7,7 +7,7 @@
#include "resource_manager.hpp" #include "resource_manager.hpp"
class Block; class Block;
class Ball; class Player;
class Object { class Object {
private: private:
@ -56,7 +56,7 @@ public:
* cet objet avec un autre : la normale et la profondeur * cet objet avec un autre : la normale et la profondeur
*/ */
virtual bool getCollisionInfo(Object& obj, sf::Vector2f& normal, float& depth); 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; virtual bool getCollisionInfo(Block& obj, sf::Vector2f& normal, float& depth) = 0;
/** /**
@ -169,3 +169,4 @@ struct ObjectCompare {
}; };
#endif #endif

View File

@ -1,14 +1,15 @@
#ifndef __PTF_BALL_HPP__ #ifndef __PTF_PLAYER_HPP__
#define __PTF_BALL_HPP__ #define __PTF_PLAYER_HPP__
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include <iostream> #include <iostream>
#include "object.hpp" #include "object.hpp"
#include "engine_state.hpp" #include "engine_state.hpp"
class Ball : public Object { class Player : public Object {
private: private:
sf::Sprite sprite; sf::Sprite sprite;
unsigned int player_number;
protected: protected:
/** /**
@ -17,7 +18,7 @@ protected:
virtual sf::Vector2f getForces(EngineState& state); virtual sf::Vector2f getForces(EngineState& state);
public: public:
Ball(float x, float y); Player(float x, float y);
/** /**
* Dessine la balle dans la fenêtre donnée * Dessine la balle dans la fenêtre donnée
@ -34,13 +35,24 @@ public:
* cet objet avec un autre : la normale et la profondeur * cet objet avec un autre : la normale et la profondeur
*/ */
virtual bool getCollisionInfo(Object& obj, sf::Vector2f& normal, float& depth); 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); virtual bool getCollisionInfo(Block& obj, sf::Vector2f& normal, float& depth);
/** /**
* Renvoie le rayon de la balle * Renvoie le rayon de la balle
*/ */
float getRadius(); float getRadius();
/**
* Renvoie le numéro du joueur
*/
unsigned int getPlayerNumber();
/**
* Modifie le numéro du joueur
*
*/
void setPlayerNumber(unsigned int set_number);
}; };
#endif #endif

BIN
res/ball.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

BIN
res/block.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

View File

@ -1,5 +1,5 @@
#include "block.hpp" #include "block.hpp"
#include "ball.hpp" #include "player.hpp"
#include "constants.hpp" #include "constants.hpp"
#include "resource_manager.hpp" #include "resource_manager.hpp"
@ -16,7 +16,7 @@ void Block::draw(sf::RenderWindow& window, ResourceManager& resources) {
Object::draw(window, resources); Object::draw(window, resources);
// utilisation de la texture // utilisation de la texture
sprite.setTexture(resources.getTexture("block.png")); sprite.setTexture(resources.getTexture("block.bmp"));
// coloration du bloc en fonction de sa charge // coloration du bloc en fonction de sa charge
if (getCharge() > 0) { if (getCharge() > 0) {
@ -43,8 +43,8 @@ bool Block::getCollisionInfo(Object& obj, sf::Vector2f& normal, float& depth) {
return obj.getCollisionInfo(*this, normal, depth); return obj.getCollisionInfo(*this, normal, depth);
} }
bool Block::getCollisionInfo(Ball& obj, sf::Vector2f& normal, float& depth) { bool Block::getCollisionInfo(Player& obj, sf::Vector2f& normal, float& depth) {
// la collision Block -> Ball est la collision Ball -> Block // la collision Block -> Player est la collision Player -> Block
// avec une normale de collision retournée // avec une normale de collision retournée
bool result = obj.getCollisionInfo(*this, normal, depth); bool result = obj.getCollisionInfo(*this, normal, depth);
normal *= -1.f; normal *= -1.f;
@ -87,3 +87,4 @@ bool Block::getCollisionInfo(Block& obj, sf::Vector2f& normal, float& depth) {
return true; return true;
} }

View File

@ -1,14 +1,17 @@
#include "ball.hpp" #include "player.hpp"
#include "block.hpp" #include "block.hpp"
#include "engine.hpp" #include "engine.hpp"
#include "constants.hpp" #include "constants.hpp"
#include <cstdlib>
#include <iostream> #include <iostream>
int main() { int main() {
Engine engine; Engine engine;
Ball ball1(3.5f * Constants::GRID, 10 * Constants::GRID); Player player1(3.5f * Constants::GRID, 10 * Constants::GRID);
Ball ball2(18.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 block01(2 * Constants::GRID, 10 * Constants::GRID);
Block block02(2 * Constants::GRID, 11 * Constants::GRID); Block block02(2 * Constants::GRID, 11 * Constants::GRID);
Block block03(3 * 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 block21(20 * Constants::GRID, 11 * Constants::GRID);
Block block22(11 * Constants::GRID, 10 * Constants::GRID); Block block22(11 * Constants::GRID, 10 * Constants::GRID);
ball1.setCharge(-.01f); player1.setCharge(-.01f);
ball2.setCharge(-.01f); player2.setCharge(-.01f);
block22.setCharge(1.f); block22.setCharge(1.f);
block22.setMass(2); block22.setMass(2);
engine.addObject(ball1); engine.addObject(player1);
engine.addObject(ball2); engine.addObject(player2);
engine.addObject(block01); engine.addObject(block01);
engine.addObject(block02); engine.addObject(block02);
engine.addObject(block03); engine.addObject(block03);
@ -62,6 +65,15 @@ int main() {
engine.addObject(block21); engine.addObject(block21);
engine.addObject(block22); engine.addObject(block22);
try {
engine.start(); engine.start();
return 0; } 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;
}

View File

@ -1,3 +1,4 @@
#include "object.hpp" #include "object.hpp"
#include "constants.hpp" #include "constants.hpp"
#include <iostream> #include <iostream>

View File

@ -1,17 +1,27 @@
#include "ball.hpp" #include "player.hpp"
#include "block.hpp" #include "block.hpp"
#include "constants.hpp" #include "constants.hpp"
#include <array> #include <array>
#include <iostream> #include <iostream>
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 // déplacement de l'origine au centre de la balle
sprite.setOrigin(sf::Vector2f(getRadius(), getRadius())); 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); 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 // déplacement de la balle après appui sur les touches de direction
if (state.keys[sf::Keyboard::Left]) { if (state.keys[sf::Keyboard::Left]) {
forces += sf::Vector2f(-Constants::MOVE, 0); forces += sf::Vector2f(-Constants::MOVE, 0);
@ -20,15 +30,25 @@ sf::Vector2f Ball::getForces(EngineState& state) {
if (state.keys[sf::Keyboard::Right]) { if (state.keys[sf::Keyboard::Right]) {
forces += sf::Vector2f(Constants::MOVE, 0); 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; return forces;
} }
void Ball::draw(sf::RenderWindow& window, ResourceManager& resources) { void Player::draw(sf::RenderWindow& window, ResourceManager& resources) {
Object::draw(window, resources); Object::draw(window, resources);
// utilisation de la texture // 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 // déplacement du sprite à la position de la balle
sprite.rotate(getVelocity().x * .1f); sprite.rotate(getVelocity().x * .1f);
@ -36,7 +56,7 @@ void Ball::draw(sf::RenderWindow& window, ResourceManager& resources) {
window.draw(sprite); window.draw(sprite);
} }
std::unique_ptr<sf::FloatRect> Ball::getAABB() { std::unique_ptr<sf::FloatRect> Player::getAABB() {
return std::unique_ptr<sf::FloatRect>(new sf::FloatRect( return std::unique_ptr<sf::FloatRect>(new sf::FloatRect(
getPosition().x - getRadius(), getPosition().x - getRadius(),
getPosition().y - getRadius(), getPosition().y - getRadius(),
@ -44,11 +64,11 @@ std::unique_ptr<sf::FloatRect> 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); 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(); sf::Vector2f dir = getPosition() - obj.getPosition();
float squaredLength = dir.x * dir.x + dir.y * dir.y; float squaredLength = dir.x * dir.x + dir.y * dir.y;
float totalRadius = getRadius() + obj.getRadius(); float totalRadius = getRadius() + obj.getRadius();
@ -76,7 +96,7 @@ bool Ball::getCollisionInfo(Ball& obj, sf::Vector2f& normal, float& depth) {
return true; 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 // recherche du point le plus proche du centre de la
// balle sur le bloc. On regarde la position relative // balle sur le bloc. On regarde la position relative
// du cercle par rapport au bloc // du cercle par rapport au bloc
@ -152,6 +172,6 @@ bool Ball::getCollisionInfo(Block& obj, sf::Vector2f& normal, float& depth) {
return true; return true;
} }
float Ball::getRadius() { float Player::getRadius() {
return 10 * getMass(); return 10 * getMass();
} }