Héritage de Ball depuis Object et nettoyage de ball.cpp
This commit is contained in:
		
							parent
							
								
									fdc38d411d
								
							
						
					
					
						commit
						f39bbd8bc8
					
				
							
								
								
									
										47
									
								
								ball.cpp
								
								
								
								
							
							
						
						
									
										47
									
								
								ball.cpp
								
								
								
								
							|  | @ -1,52 +1,13 @@ | |||
| #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); | ||||
|     // TODO: implémenter cette fonction
 | ||||
| } | ||||
| 
 | ||||
| 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::update(sf::Vector2f forces, float delta) { | ||||
|     // TODO: implémenter cette fonction
 | ||||
| } | ||||
| 
 | ||||
| void Ball::draw(sf::RenderWindow& window) { | ||||
|     sprite.setPosition(position + sf::Vector2f(0, -12)); | ||||
|     sprite.rotate(velocity.x / 10); | ||||
| 
 | ||||
|     window.draw(sprite); | ||||
|     // TODO: implémenter cette fonction
 | ||||
| } | ||||
|  |  | |||
							
								
								
									
										32
									
								
								ball.hpp
								
								
								
								
							
							
						
						
									
										32
									
								
								ball.hpp
								
								
								
								
							|  | @ -1,38 +1,28 @@ | |||
| #ifndef PTF_BALL_HPP | ||||
| #define PTF_BALL_HPP | ||||
| 
 | ||||
| #include "object.hpp" | ||||
| #include <SFML/Graphics.hpp> | ||||
| #include <iostream> | ||||
| 
 | ||||
| class Ball { | ||||
| class Ball : public Object { | ||||
| protected: | ||||
|     sf::Vector2f position; | ||||
|     sf::Vector2f velocity; | ||||
|     float mass; | ||||
|     int charge; | ||||
| 
 | ||||
| public: | ||||
|     Ball(); | ||||
| 
 | ||||
|     void update(float delta); | ||||
|     /**
 | ||||
|      * Mise à jour de la position de la balle en fonction des forces | ||||
|      * qui lui sont appliquées | ||||
|      */ | ||||
|     void update(sf::Vector2f forces, float delta); | ||||
| 
 | ||||
|     /**
 | ||||
|      * Dessine la balle dans la fenêtre donnée | ||||
|      */ | ||||
|     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 | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue