From a25a637a35488bdbabb939c601f27c0eb2835689 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matt=C3=A9o=20Delabre?= Date: Mon, 14 Mar 2016 21:20:40 +0100 Subject: [PATCH] =?UTF-8?q?Centralisation=20des=20constantes=20dans=20un?= =?UTF-8?q?=20en-t=C3=AAte=20constants?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/ball.hpp | 3 --- include/block.hpp | 2 -- include/constants.hpp | 33 +++++++++++++++++++++++++++++++++ include/physics_object.hpp | 2 -- src/ball.cpp | 7 ++++--- src/block.cpp | 11 ++++++----- src/main.cpp | 23 ++++++++++++----------- src/physics_object.cpp | 3 ++- 8 files changed, 57 insertions(+), 27 deletions(-) create mode 100644 include/constants.hpp diff --git a/include/ball.hpp b/include/ball.hpp index 7d73e60..be52241 100644 --- a/include/ball.hpp +++ b/include/ball.hpp @@ -16,9 +16,6 @@ protected: */ virtual sf::Vector2f getForces(EngineState& state); - static constexpr float ATTRACTION = 25000; - static constexpr float MOVE = 100; - public: Ball(float x, float y); diff --git a/include/block.hpp b/include/block.hpp index 35d417e..11a433c 100644 --- a/include/block.hpp +++ b/include/block.hpp @@ -12,8 +12,6 @@ protected: sf::RectangleShape shape; public: - static constexpr float GRID = 32; - Block(float x, float y); /** diff --git a/include/constants.hpp b/include/constants.hpp new file mode 100644 index 0000000..6b550cd --- /dev/null +++ b/include/constants.hpp @@ -0,0 +1,33 @@ +#ifndef __PTF_CONSTANTS_HPP__ +#define __PTF_CONSTANTS_HPP__ + +namespace Constants { + /** + * Constante d'attraction. Utilisée dans la formule + * pour le calcul de l'attraction coulombienne entre + * deux objets + */ + static constexpr float ATTRACTION = 25000; + + /** + * Constante de déplacement. Définit la quantité de + * mouvement qui est donnée à un objet lorsqu'il + * est manipulé manuellement par le joueur + */ + static constexpr float MOVE = 100; + + /** + * Constante de gravité. Utilisée dans la formule + * pour calculer la force de gravité appliquée + * uniformément vers le bas de la fenêtre sur tous + * les objets + */ + static constexpr float GRAVITY = 20; + + /** + * Taille de la grille des blocs en pixels + */ + static constexpr float GRID = 32; +} + +#endif diff --git a/include/physics_object.hpp b/include/physics_object.hpp index 34e778a..0488965 100644 --- a/include/physics_object.hpp +++ b/include/physics_object.hpp @@ -19,8 +19,6 @@ protected: */ virtual sf::Vector2f getForces(EngineState& state); - static constexpr float GRAVITY = 20; - public: PhysicsObject(float x, float y); diff --git a/src/ball.cpp b/src/ball.cpp index 5217195..9f2a379 100644 --- a/src/ball.cpp +++ b/src/ball.cpp @@ -1,4 +1,5 @@ #include "ball.hpp" +#include "constants.hpp" Ball::Ball(float x, float y) : PhysicsObject(x, y), shape(10 * mass) { shape.setOrigin(sf::Vector2f(10 * mass, 10 * mass)); @@ -35,11 +36,11 @@ sf::Vector2f Ball::getForces(EngineState& state) { // déplacement de la balle après appui sur les touches de direction if (state.keys[sf::Keyboard::Left]) { - forces += sf::Vector2f(-Ball::MOVE, 0); + forces += sf::Vector2f(-Constants::MOVE, 0); } if (state.keys[sf::Keyboard::Right]) { - forces += sf::Vector2f(Ball::MOVE, 0); + forces += sf::Vector2f(Constants::MOVE, 0); } // force d'attraction entre les balles et les blocs chargés @@ -66,7 +67,7 @@ sf::Vector2f Ball::getForces(EngineState& state) { // normalisation du vecteur direction qui porte // la force d'attraction, puis application de la norme attraction /= std::sqrt(distanceSquared); - attraction *= Ball::ATTRACTION * ( + attraction *= Constants::ATTRACTION * ( (charge * attractive->getCharge()) / distanceSquared ); diff --git a/src/block.cpp b/src/block.cpp index 2fbc918..2da0fde 100644 --- a/src/block.cpp +++ b/src/block.cpp @@ -1,7 +1,8 @@ #include "block.hpp" +#include "constants.hpp" -Block::Block(float x, float y) : Object(x, y), shape(sf::Vector2f(Block::GRID, Block::GRID)) { - shape.setOrigin(sf::Vector2f(Block::GRID / 2, Block::GRID / 2)); +Block::Block(float x, float y) : Object(x, y), shape(sf::Vector2f(Constants::GRID, Constants::GRID)) { + shape.setOrigin(sf::Vector2f(Constants::GRID / 2, Constants::GRID / 2)); } void Block::draw(sf::RenderWindow& window) { @@ -30,9 +31,9 @@ void Block::update(EngineState& state) { std::unique_ptr Block::getAABB() { return std::unique_ptr(new sf::FloatRect( - position.x - Block::GRID / 2, - position.y - Block::GRID / 2, - Block::GRID, Block::GRID + position.x - Constants::GRID / 2, + position.y - Constants::GRID / 2, + Constants::GRID, Constants::GRID )); } diff --git a/src/main.cpp b/src/main.cpp index f90efbc..19bf608 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,23 +1,24 @@ #include "ball.hpp" #include "block.hpp" #include "engine.hpp" +#include "constants.hpp" #include int main() { Engine engine; - Ball ball1(5 * Block::GRID, 1 * Block::GRID); - Ball ball2(7 * Block::GRID, 1 * Block::GRID); + Ball ball1(5 * Constants::GRID, 1 * Constants::GRID); + Ball ball2(7 * Constants::GRID, 1 * Constants::GRID); - Block block1(2 * Block::GRID, 7 * Block::GRID); - Block block2(3 * Block::GRID, 7 * Block::GRID); - Block block3(4 * Block::GRID, 7 * Block::GRID); - Block block4(5 * Block::GRID, 7 * Block::GRID); - Block block5(6 * Block::GRID, 7 * Block::GRID); - Block block6(7 * Block::GRID, 7 * Block::GRID); - Block block7(8 * Block::GRID, 7 * Block::GRID); - Block block8(9 * Block::GRID, 7 * Block::GRID); - Block block9(10 * Block::GRID, 7 * Block::GRID); + Block block1(2 * Constants::GRID, 7 * Constants::GRID); + Block block2(3 * Constants::GRID, 7 * Constants::GRID); + Block block3(4 * Constants::GRID, 7 * Constants::GRID); + Block block4(5 * Constants::GRID, 7 * Constants::GRID); + Block block5(6 * Constants::GRID, 7 * Constants::GRID); + Block block6(7 * Constants::GRID, 7 * Constants::GRID); + Block block7(8 * Constants::GRID, 7 * Constants::GRID); + Block block8(9 * Constants::GRID, 7 * Constants::GRID); + Block block9(10 * Constants::GRID, 7 * Constants::GRID); ball1.setCharge(-2); ball2.setCharge(-2); diff --git a/src/physics_object.cpp b/src/physics_object.cpp index 7a43c0c..63a1586 100644 --- a/src/physics_object.cpp +++ b/src/physics_object.cpp @@ -1,4 +1,5 @@ #include "physics_object.hpp" +#include "constants.hpp" PhysicsObject::PhysicsObject(float x, float y) : Object(x, y), mass(1), accelLine(sf::LinesStrip, 2), @@ -32,7 +33,7 @@ sf::Vector2f PhysicsObject::getForces(EngineState& state) { sf::Vector2f forces(0, 0); // force de gravité - forces += sf::Vector2f(0, PhysicsObject::GRAVITY); + forces += sf::Vector2f(0, Constants::GRAVITY); return forces; }