Différenciation des deux joueurs
This commit is contained in:
parent
e72f6eacbe
commit
1cc107ddd3
|
@ -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
|
||||
|
||||
|
|
|
@ -45,3 +45,4 @@ public:
|
|||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -1,14 +1,15 @@
|
|||
#ifndef __PTF_BALL_HPP__
|
||||
#define __PTF_BALL_HPP__
|
||||
#ifndef __PTF_PLAYER_HPP__
|
||||
#define __PTF_PLAYER_HPP__
|
||||
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include <iostream>
|
||||
#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
|
Binary file not shown.
After Width: | Height: | Size: 1.4 KiB |
Binary file not shown.
After Width: | Height: | Size: 2.1 KiB |
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
30
src/main.cpp
30
src/main.cpp
|
@ -1,14 +1,17 @@
|
|||
#include "ball.hpp"
|
||||
#include "player.hpp"
|
||||
#include "block.hpp"
|
||||
#include "engine.hpp"
|
||||
#include "constants.hpp"
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
|
||||
#include "object.hpp"
|
||||
#include "constants.hpp"
|
||||
#include <iostream>
|
||||
|
|
|
@ -1,34 +1,54 @@
|
|||
#include "ball.hpp"
|
||||
#include "player.hpp"
|
||||
#include "block.hpp"
|
||||
#include "constants.hpp"
|
||||
#include <array>
|
||||
#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
|
||||
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<sf::FloatRect> Ball::getAABB() {
|
||||
std::unique_ptr<sf::FloatRect> Player::getAABB() {
|
||||
return std::unique_ptr<sf::FloatRect>(new sf::FloatRect(
|
||||
getPosition().x - 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);
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
Loading…
Reference in New Issue