Merge branch 'master' of github.com:matteodelabre/projet-cmi

This commit is contained in:
Mattéo Delabre 2016-03-25 20:13:47 +01:00
commit 701673253a
11 changed files with 111 additions and 76 deletions

View File

@ -11,9 +11,9 @@ namespace Collision {
typedef std::map<std::pair<unsigned int, unsigned int>, 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);
}

View File

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

View File

@ -7,7 +7,7 @@
#include "resource_manager.hpp"
class Block;
class Ball;
class Player;
class Object {
private:

View File

@ -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
@ -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

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,53 +0,0 @@
#include "ball.hpp"
#include "block.hpp"
#include "constants.hpp"
#include <array>
#include <iostream>
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<sf::FloatRect> Ball::getAABB() {
return std::unique_ptr<sf::FloatRect>(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();
}

View File

@ -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) {

View File

@ -1,5 +1,5 @@
#include "collision.hpp"
#include "ball.hpp"
#include "player.hpp"
#include "block.hpp"
#include "object.hpp"
#include <utility>
@ -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<Ball&>(objA);
Player player = dynamic_cast<Player&>(objA);
Block block = dynamic_cast<Block&>(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<sf::FloatRect> 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

View File

@ -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;
}

73
src/player.cpp Normal file
View File

@ -0,0 +1,73 @@
#include "player.hpp"
#include "block.hpp"
#include "constants.hpp"
#include <array>
#include <iostream>
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<sf::FloatRect> Player::getAABB() {
return std::unique_ptr<sf::FloatRect>(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;
}