Héritage de Ball depuis Object et nettoyage de ball.cpp

This commit is contained in:
Mattéo Delabre 2016-03-08 18:07:21 +01:00
parent fdc38d411d
commit f39bbd8bc8
2 changed files with 15 additions and 64 deletions

View File

@ -1,52 +1,13 @@
#include "ball.hpp" #include "ball.hpp"
#include "engine.hpp"
// TODO réécrire cette classe
Ball::Ball() { Ball::Ball() {
if (!texture.loadFromFile("res/balle.png")) { // TODO: implémenter cette fonction
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) { void Ball::update(sf::Vector2f forces, float delta) {
int verticalPos = std::ceil(position.y); // TODO: implémenter cette fonction
// 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) { void Ball::draw(sf::RenderWindow& window) {
sprite.setPosition(position + sf::Vector2f(0, -12)); // TODO: implémenter cette fonction
sprite.rotate(velocity.x / 10);
window.draw(sprite);
} }

View File

@ -1,38 +1,28 @@
#ifndef PTF_BALL_HPP #ifndef PTF_BALL_HPP
#define PTF_BALL_HPP #define PTF_BALL_HPP
#include "object.hpp"
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include <iostream> #include <iostream>
class Ball { class Ball : public Object {
protected: protected:
sf::Vector2f position;
sf::Vector2f velocity; sf::Vector2f velocity;
float mass; float mass;
int charge;
public: public:
Ball(); 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); 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 #endif