2016-03-25 17:40:39 +00:00
|
|
|
#include "player.hpp"
|
2016-03-15 21:00:03 +00:00
|
|
|
#include "block.hpp"
|
2016-03-14 20:20:40 +00:00
|
|
|
#include "constants.hpp"
|
2016-03-15 21:00:03 +00:00
|
|
|
#include <array>
|
2016-03-19 14:33:31 +00:00
|
|
|
#include <iostream>
|
2016-03-04 15:29:31 +00:00
|
|
|
|
2016-03-25 17:40:39 +00:00
|
|
|
Player::Player(float x, float y) : Object(x, y) {
|
2016-03-22 19:04:18 +00:00
|
|
|
// déplacement de l'origine au centre de la balle
|
|
|
|
sprite.setOrigin(sf::Vector2f(getRadius(), getRadius()));
|
2016-03-14 20:11:09 +00:00
|
|
|
}
|
|
|
|
|
2016-03-25 17:40:39 +00:00
|
|
|
sf::Vector2f Player::getForces(EngineState& state) {
|
2016-03-15 16:08:21 +00:00
|
|
|
sf::Vector2f forces = Object::getForces(state);
|
2016-03-25 19:13:47 +00:00
|
|
|
|
2016-03-25 19:56:04 +00:00
|
|
|
// déplacement de la balle après appui sur les touches de direction
|
|
|
|
if (getPlayerNumber() == 1) {
|
2016-03-25 17:40:39 +00:00
|
|
|
if (state.keys[sf::Keyboard::Left]) {
|
|
|
|
forces += sf::Vector2f(-Constants::MOVE, 0);
|
|
|
|
}
|
2016-03-08 18:52:55 +00:00
|
|
|
|
2016-03-25 17:40:39 +00:00
|
|
|
if (state.keys[sf::Keyboard::Right]) {
|
|
|
|
forces += sf::Vector2f(Constants::MOVE, 0);
|
|
|
|
}
|
2016-03-08 18:52:55 +00:00
|
|
|
}
|
2016-03-25 19:56:04 +00:00
|
|
|
|
|
|
|
if (getPlayerNumber() == 2) {
|
|
|
|
if (state.keys[sf::Keyboard::Q]) {
|
2016-03-25 17:40:39 +00:00
|
|
|
forces += sf::Vector2f(-Constants::MOVE, 0);
|
|
|
|
}
|
2016-03-25 19:13:47 +00:00
|
|
|
|
2016-03-25 19:56:04 +00:00
|
|
|
if (state.keys[sf::Keyboard::D]) {
|
2016-03-25 17:40:39 +00:00
|
|
|
forces += sf::Vector2f(Constants::MOVE, 0);
|
|
|
|
}
|
2016-03-08 18:52:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return forces;
|
2016-03-04 15:29:31 +00:00
|
|
|
}
|
2016-03-15 21:00:03 +00:00
|
|
|
|
2016-03-25 17:40:39 +00:00
|
|
|
void Player::draw(sf::RenderWindow& window, ResourceManager& resources) {
|
2016-03-22 19:04:18 +00:00
|
|
|
Object::draw(window, resources);
|
2016-03-15 21:00:03 +00:00
|
|
|
|
2016-03-22 19:04:18 +00:00
|
|
|
// utilisation de la texture
|
2016-03-25 20:31:43 +00:00
|
|
|
sprite.setTexture(resources.getTexture("ball.png"));
|
2016-03-15 21:00:03 +00:00
|
|
|
|
2016-03-22 19:04:18 +00:00
|
|
|
// déplacement du sprite à la position de la balle
|
|
|
|
sprite.rotate(getVelocity().x * .1f);
|
|
|
|
sprite.setPosition(getPosition());
|
|
|
|
window.draw(sprite);
|
2016-03-15 21:00:03 +00:00
|
|
|
}
|
|
|
|
|
2016-03-25 17:40:39 +00:00
|
|
|
std::unique_ptr<sf::FloatRect> Player::getAABB() {
|
2016-03-15 21:00:03 +00:00
|
|
|
return std::unique_ptr<sf::FloatRect>(new sf::FloatRect(
|
2016-03-19 21:59:44 +00:00
|
|
|
getPosition().x - getRadius(),
|
|
|
|
getPosition().y - getRadius(),
|
|
|
|
2 * getRadius(), 2 * getRadius()
|
2016-03-15 21:00:03 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2016-03-25 19:13:47 +00:00
|
|
|
unsigned int Player::getTypeId() {
|
|
|
|
return Player::TYPE_ID;
|
2016-03-15 21:00:03 +00:00
|
|
|
}
|
|
|
|
|
2016-03-25 19:13:47 +00:00
|
|
|
float Player::getRadius() {
|
|
|
|
return 10 * getMass();
|
2016-03-15 21:00:03 +00:00
|
|
|
}
|
|
|
|
|
2016-03-25 19:13:47 +00:00
|
|
|
unsigned int Player::getPlayerNumber() {
|
|
|
|
return player_number;
|
2016-03-15 21:00:03 +00:00
|
|
|
}
|
2016-03-19 21:59:44 +00:00
|
|
|
|
2016-03-25 19:13:47 +00:00
|
|
|
void Player::setPlayerNumber(unsigned int set_number) {
|
|
|
|
player_number = set_number;
|
2016-03-19 21:59:44 +00:00
|
|
|
}
|