Initial commit
This commit is contained in:
		
							parent
							
								
									bbee35624e
								
							
						
					
					
						commit
						0a2943c6bf
					
				|  | @ -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/* | ||||
|  | @ -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); | ||||
| } | ||||
|  | @ -0,0 +1,38 @@ | |||
| #ifndef PTF_BALL_HPP | ||||
| #define PTF_BALL_HPP | ||||
| 
 | ||||
| #include <SFML/Graphics.hpp> | ||||
| #include <iostream> | ||||
| 
 | ||||
| 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 | ||||
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							|  | @ -0,0 +1,117 @@ | |||
| #include "engine.hpp" | ||||
| #include <cmath> | ||||
| 
 | ||||
| /**
 | ||||
|  * 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(); | ||||
|     } | ||||
| } | ||||
|  | @ -0,0 +1,33 @@ | |||
| #ifndef PTF_ENGINE_HPP | ||||
| #define PTF_ENGINE_HPP | ||||
| 
 | ||||
| #include <vector> | ||||
| #include <SFML/Graphics.hpp> | ||||
| #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<Ball> balls; | ||||
|     std::vector<std::vector<Block>> blocks; | ||||
| 
 | ||||
|     Engine(); | ||||
|     void draw(); | ||||
|     void update(float delta); | ||||
| }; | ||||
| 
 | ||||
| #endif | ||||
|  | @ -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; | ||||
| } | ||||
										
											Binary file not shown.
										
									
								
							| After Width: | Height: | Size: 1.6 KiB | 
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							| After Width: | Height: | Size: 6.2 MiB | 
		Loading…
	
		Reference in New Issue