diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..20570eb --- /dev/null +++ b/Makefile @@ -0,0 +1,18 @@ +CC=g++ +CFLAGS=-Wall -std=c++11 +TARGET=ptf + +$(TARGET): main.o ball.o engine.o + $(CC) $(CFLAGS) -o build/$(TARGET) build/engine.o build/main.o build/ball.o -lsfml-graphics -lsfml-window -lsfml-system -lpthread + +ball.o: ball.cpp + $(CC) $(CFLAGS) -o build/ball.o -c ball.cpp + +engine.o: engine.cpp + $(CC) $(CFLAGS) -o build/engine.o -c engine.cpp + +main.o: main.cpp + $(CC) $(CFLAGS) -o build/main.o -c main.cpp + +clean: + rm build/* diff --git a/ball.cpp b/ball.cpp new file mode 100644 index 0000000..fe4143e --- /dev/null +++ b/ball.cpp @@ -0,0 +1,52 @@ +#include "ball.hpp" +#include "engine.hpp" + +// TODO réécrire cette classe + +Ball::Ball() { + if (!texture.loadFromFile("res/balle.png")) { + std::cerr << "Failed to load texture" << std::endl; + } + + velocity.x = velocity.y = 0; + position.x = 150; position.y = 150; + + sprite.setOrigin(12, 12); + sprite.setTexture(texture); +} + +void Ball::update(float delta) { + int verticalPos = std::ceil(position.y); + + // gravity + velocity.y += Engine::GRAVITY * delta; + + // jump + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) && verticalPos >= 150) { + velocity.y = -Engine::JUMP; + } + + // go left/right + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { + velocity.x -= Engine::MOVE; + } + + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { + velocity.x += Engine::MOVE; + } + + if (verticalPos > 150) { + position.y = 150; + velocity.y = -.3f * std::abs(velocity.y); + } + + velocity.x *= .95f; + position += velocity * delta; +} + +void Ball::draw(sf::RenderWindow& window) { + sprite.setPosition(position + sf::Vector2f(0, -12)); + sprite.rotate(velocity.x / 10); + + window.draw(sprite); +} diff --git a/ball.hpp b/ball.hpp new file mode 100644 index 0000000..25e6d46 --- /dev/null +++ b/ball.hpp @@ -0,0 +1,38 @@ +#ifndef PTF_BALL_HPP +#define PTF_BALL_HPP + +#include +#include + +class Ball { +protected: + sf::Vector2f position; + sf::Vector2f velocity; + float mass; + int charge; + +public: + Ball(); + + void update(float delta); + void draw(sf::RenderWindow& window); + + // getters et setters + sf::Vector2f getPosition() { + return position; + } + + sf::Vector2f getVelocity() { + return velocity; + } + + float getMass() { + return mass; + } + + int getCharge() { + return charge; + } +}; + +#endif diff --git a/build/ball.o b/build/ball.o new file mode 100644 index 0000000..911488e Binary files /dev/null and b/build/ball.o differ diff --git a/build/engine.o b/build/engine.o new file mode 100644 index 0000000..d1577c9 Binary files /dev/null and b/build/engine.o differ diff --git a/build/main.o b/build/main.o new file mode 100644 index 0000000..2645193 Binary files /dev/null and b/build/main.o differ diff --git a/build/ptf b/build/ptf new file mode 100755 index 0000000..25e5184 Binary files /dev/null and b/build/ptf differ diff --git a/engine.cpp b/engine.cpp new file mode 100644 index 0000000..7191e13 --- /dev/null +++ b/engine.cpp @@ -0,0 +1,117 @@ +#include "engine.hpp" +#include + +/** + * Fonction d'initialisation de la classe Engine + */ +Engine::Engine() { + // initialise et ouvre la fenêtre + window.create( + sf::VideoMode(300, 150), "La cage de Jacob", + sf::Style::Default & ~sf::Style::Resize, + sf::ContextSettings(0, 0, 2) + ); + + window.setVerticalSyncEnabled(true); + + // au démarrage, aucune touche n'est enfoncée + goLeftKey = false; + goRightKey = false; + + // boucle d'événements sur la fenêtre + while (window.isOpen()) { + sf::Event event; + + // traitement des évènements reçus + while (window.pollEvent(event)) { + // fermeture de la fenêtre + if (event.type == sf::Event::Closed) { + window.close(); + } + + // une touche a été enfoncée + if (event.type == sf::Event::KeyPressed) { + if (event.key.code == sf::Keyboard::Left) { + goLeftKey = true; + } + + if (event.key.code == sf::Keyboard::Right) { + goRightKey = true; + } + } + + // une touche a été relâchée + if (event.type == sf::Event::KeyReleased) { + if (event.key.code == sf::Keyboard::Left) { + goLeftKey = false; + } + + if (event.key.code == sf::Keyboard::Right) { + goRightKey = false; + } + } + } + + // réinitialise l'horloge à zéro + // et stocke la différence de temps dans delta + float delta = clock.restart().asSeconds(); + update(delta); + draw(); + } +} + +/** + * Mise à jour de la physique du jeu + */ +void Engine::update(float delta) { + for (int i = 0; i < balls.size(); i++) { + sf::Vector2f forces(0, 0); + + // ajout de la force de gravité + forces += sf::Vector2f(0, Engine::GRAVITY); + + // déplacement de la balle + if (goLeftKey) { + forces += sf::Vector2f(-Engine::MOVE, 0); + } + + if (goRightKey) { + forces += sf::Vector2f(Engine::MOVE, 0); + } + + // force d'attraction + for (int j = 0; j < balls.size(); j++) { + if (i != j) { + sf::Vector2f attraction(balls[i].getPosition() - balls[j].getPosition()); + float distanceSquared = attraction.x * attraction.x + attraction.y * attraction.y; + + attraction /= std::sqrt(distanceSquared); + attraction *= Engine::ATTRACTION * ((balls[i].getCharge() * balls[j].getCharge()) / distanceSquared); + + forces += attraction; + } + } + + // TODO: COLLISIONS + + balls[i].update(forces, delta); + } +} + +/** + * Dessine la scène du jeu couche par couche + */ +void Engine::draw() { + // couche de fond + window.clear(sf::Color(66, 165, 245)); + + // grille de blocs + for (int i = 0; i < blocks.size(); i++) { + blocks[i].draw(); + } + + // dessin des balles + for (int i = 0; i < balls.size(); i++) { + balls[i].draw(); + } +} diff --git a/engine.hpp b/engine.hpp new file mode 100644 index 0000000..e2cbb0c --- /dev/null +++ b/engine.hpp @@ -0,0 +1,33 @@ +#ifndef PTF_ENGINE_HPP +#define PTF_ENGINE_HPP + +#include +#include +#include "ball.hpp" + +/** + * La classe principale Engine coordonne les éléments + * du jeu et organise le dessin des frames + */ +class Engine { +private: + sf::Clock clock; + sf::RenderWindow window; + + bool goLeftKey; + bool goRightKey; + +public: + static constexpr float GRAVITY = 10; + static constexpr float ATTRACTION = 10; + static constexpr float MOVE = 10; + + std::vector balls; + std::vector> blocks; + + Engine(); + void draw(); + void update(float delta); +}; + +#endif diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..c2e5e7f --- /dev/null +++ b/main.cpp @@ -0,0 +1,12 @@ +#include "ball.hpp" +#include "engine.hpp" + +int main() { + Engine engine; + Ball ball; + + engine.balls.push_back(ball); + engine.start(); + + return 0; +} diff --git a/res/balle.png b/res/balle.png new file mode 100644 index 0000000..887adf7 Binary files /dev/null and b/res/balle.png differ diff --git a/res/balle.svg b/res/balle.svg new file mode 100644 index 0000000..7dad9ff --- /dev/null +++ b/res/balle.svg @@ -0,0 +1,84733 @@ + + + + + + + + + + + + + + + + + + + + + + image/svg+xml + + + + + + + + + + +